Python implementation Netcat

Source: Internet
Author: User

import sysimport socketimport getoptimport threadingimport subprocesslisten =  Falsecommand = Falseupload = Falseexecute =  "target = " "Upload_ destination =  "" Port = 0def usage ():    print  "BHP Net  tool "    print    print " usage: bhpnet.py -t  target_host -p port "    print "-l --listen -listen on  [host]:[port] for incoming connections "    print "-e -- Execute=file_to_run -execute the given file uponreceiving a connection "     print  "-c --command - initialize a command shell"     print  "-u --upload=destination - upon receiveing  Connection upload a fiLe and write to [destination] "    print    print     print  "Examples:"     print  "bhpnet.py -t  192.168.0.1 -p 5555 -l -c "    print " bhpnet.py -t  192.168.0.1 -p 5555 -l -u=c:\\target.exe "    print " echo  " Abcdefghi '  | ./bhpnet.py -t 192.168.11.12 -p 135 '      Sys.exit (0) Def main ():     global listen    global port     global execute    global command     Global upload_destination    global target    if not  len (sys.argv[1:]):         usage ()     try:       &nBsp; opts,args = getopt.getopt (sys.argv[1:], "HLE:T:P:CU:", ["Help", "Listen", "execute", "Port", " Command "," Upload "])     except getopt. GETOPTERROR&NBSP;AS&NBSP;ERR:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINT&NBSP;STR (ERR)          usage ()     for o,a in opts:         if o in  ("-H", "--help"):             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, "Unhanded option"     if  Not listen and len (target)  and port > 0:         buffer&nbSp;= sys.stdin.read ()         client_sender (buffer)      if listen:        server_loop () Main () Def client_ Sender (Buffer):     client = socket.socket (Socket.af_inet,socket. Sock_stream)     try:        client.connect ((target , port))         if len (buffer):             client.send (buffer)          While true:            recv_len = 1             response =  ""              while recv_len:            &nBSP;&NBSP;&NBSP;&NBSP;&NBSP;DATA&NBSP;=&NBSP;CLIENT.RECV (4096)                  recv_len = len (data)                  response += data                 if recv_len <  4096:                     break            print  Response,            buffer = raw_input ( "")             buffer +=  "\ n"              client.send (buffer)      except:        print  "[*] exception! exiting.]         client.close () Def server_loop ():     Global target    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,addr =  server.accept ()         client_thread = threading. Thread (target=client_handler,args= (Client_socket,))         client_ Thread.Start () Def run_command (command):     command = command.rstrip ()      try:     &Nbsp;  output = subprocess.check_output (command,stderr=subprocess. Stdout,shell=true)     except:        output =   "failed to execute command.\r\n"     return outputdef client _handler (client_soket):     global upload    global execute     global command    if len (upload_destination):         file_buffer =  ""          while true:            data = &NBSP;CLIENT_SOKET.RECV (4096)             if  not data:                 break            else:                 file_buffer += data        try:             file_description = open ( Upload_destination, "WB")             file_ Description.write (File_buffer)             file_ Description.close ()             client_soket.send (" successfully saved file to %s\r\n " % upload_destination)          except:            client_ Soket.send ("failed to save file to %s\r\n"  % upload_destination)      if len (Execute):        output = run_command (Execute)          client_soket.send (output)     if command:         while True:             client_soket ("<BHP:#>")              cmd_buffer =  ""              while  "\ n"  not in cmd_buffer:           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CMD_BUFFER&NBSP;+=&NBSP;CLIENT_SOKET.RECV (1024x768)              response = run_command (Cmd_buffer)              client_soket.send (response)


This article is from the "Focus on PHP" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1763214

Python implementation Netcat

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.