[Python Development simulation netcat tool] Bhpnet

Source: Internet
Author: User
#!/usr/bin/python #-*-coding:utf8-*-import sys import socket import getopt import threading Import Subproce  SS # defines some global variables listen = False Command = False upload = False Execute = "target =" upload_destination =  
    "" Port = 0 def run_command (command): # Delete the space at the end of the string command = Command.rstrip () # To run the command and put the output back try:output = subprocess.check_output (command, stderr=subprocess.  
  
  
STDOUT, shell=true) except:output = "Failed to execute command.\r\n" # Send output return  
    def client_handler (client_socket): Global upload Global Execute global command # Check upload file  
        If Len (upload_destination): # Read all the characters and write down the target file_buffer = "# continue to read data until no data is met  
            While true:data = CLIENT_SOCKET.RECV (1024) if not data:break Else:file_buffer + = Data try: 
            File_descriptor = open (Upload_destination, "WB") File_descriptor.write (File_buffer)  
        File_descriptor.close () Client_socket.send ("Successfully saved file to%s\r\n"% upload_destination)   
    Except:client_socket.send ("Failed to save file to%s\r\n"% upload_destination) # Check command execution  
  
  
    If Len (execute): # Run command output = Run_command (execute) client_socket.send (output) # If you need a command line shell, then we go to another loop if Command:while True: # Jump out of a window client_s  
                Ocket.send ("<BHP:#>") Cmd_buffer = "" While "\ n" Not in Cmd_buffer:  
            Cmd_buffer + = CLIENT_SOCKET.RECV (1024) # return command Output response = Run_command (Cmd_buffer) # Return response Data Client_socket.send (response) def server_loop (): Global target # If you don't define a target, that We monitor all interfaces if not leN (target): target = "0.0.0.0" server = Socket.socket (socket.af_inet, socket. Sock_stream) Server.bind ((target, Port) Server.listen (5) while True:client_socket, add R = server.accept () # splits a thread to handle the new client Client_thread = Threading.  
    Thread (Target=client_handler, args= (Client_socket,)) Client_thread.start () def client_sender (buffer): Client = Socket.socket (socket.af_inet, socket.  
            Sock_stream Try: # Connect to target host Client.connect ((target, Port) if Len (buffer): Client.send (buffer) while True: # now waits for data to return Recv_len = 1 RE Sponse = "" While Recv_len:data = Client.recv (4096) Recv_len = Len (d  
  
            ATA) Response + = data if Recv_len < 4096:break Print Response # WaitingMore Input buffer = Raw_input ("") buffer + = "\ \" # Sent out Client.send ( Buffer) Except:print "[*] exception!  
  
    Exiting. " #关闭连接 client.close () def usage (): print "BHP Net Tool" Print print usage:bhpnet.py-t targe  T_host-p Port "Print"-l--listen-listen on [Host]:[port] for incoming connections "print"-E --execute=file_to_run-execute the given file upon receiving a connection "print"-C--command-initia Lize a Commandshell "print"-u--upload=destination-upon receiving connection upload a file and write to [Destina tion] "Print print print" Examples: "print" Bhpnet.py-t 192.168.0.1-p 5555-l-C "print" B Hpnet.py-t 192.168.0.1-p 5555-l-u=c:\\target.exe "print" Bhpnet.py-t 192.168.0.1-p "5555-l-e=\" CAT/ETC/PASSW D\ "" Print "echo ' Abcdefghi ' | Python./bhpnet.py-t 192.168.11.12-p 135 "Sys.exit (0) def main (): Global Listen Global Port Global Execute Global Comm  
  
  
    and global upload_destination Global target if not Len (sys.argv[1:]): Usage () # Read command-line options, and if not, display usage try:opts, args = Getopt.getopt (sys.argv[1:], "HLE:T:P:CU:", ["Help", "Listen", "exec Ute "," target "," Port "," command "," upload "]) except getopt. Getopterror as Err:print str (ERR) usage () for o,a in Opts:if o in ("H", "--hel P "): Usage () elif O in (" L ","--listen "): Listen = True elif o in (" E ",  
        "--execute"): Execute = a elif o in ("-C", "--commandshell"): Command = True  
            Elif O In ("-U", "--upload"): Upload_destination = a elif o in ("T", "--target"):  target = a elif o in ("-P", "--port"): port = Int (a)
        Else:assert False, "Unhandled Option" #我们是进行监听还是仅从标准输入读取数据并发送数据.  
        If not listen and Len (target) and port > 0: # Read memory data from the command line # This will block, so send the data when the standard input is not sent ctrl-d Buffer = Sys.stdin.read () # Send data client_sender (buffer) # We start listening and ready to upload files, execute command # Place a  
 Bounce Shell # depends on the command line option above if Listen:server_loop () #调用main函数 main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.