daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import the modules
  2. import sys
  3. import socket
  4. import threading
  5.  
  6. #define the server
  7. def server_loop(local_host,local_port,remote_host,remote_port,receive_first):
  8.     server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9.  
  10.     try:
  11.         server.bind((local_host, local_port))
  12.         server.listen(5)
  13.         print ("[*] Listening on %s:%sd" % (local_host, local_port))
  14.     except:
  15.         print("[!!] Failed to listen on %s:%sd" % (local_host,local_port))
  16.         print ("[!!] Check for others listening sockets or correct permissions")
  17.         sys.exit(0)
  18.  
  19.     while True:
  20.         client_socket, addr = server.accept()
  21.  
  22.         #print out the local connection information
  23.         print ("[==>] Received incoming connection from %s:%sd" % (addr[0],addr[1]))
  24.  
  25.         #start a thread to talk to the remote host
  26.         proxy_thread = threading.Thread(target=proxy_handler,args=(client_socket,remote_host,remote_port,receive_first))
  27.  
  28.         proxy_thread.start()
  29.     else:
  30.         print ("something went wrong")
  31.  
  32. def main():
  33.     #no fancy command-line parasing here
  34.     if len(sys.argv[1:]) !=5:
  35.         print ("Usage: ./TCP proxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]")
  36.         print("Example: ./TCP proxy.py 127.0.0.1 9000 10.12.132.1 9000 True")
  37.  
  38.     #set up local listening parameters
  39.     local_host = sys.argv[1]
  40.     local_port = int(sys.argv[2])
  41.  
  42.     #set up remote target
  43.     remote_host = sys.argv[3]
  44.     remote_port = int(sys.argv[4])
  45.  
  46.     #this tells proxy to connect and receive data before sending to remote host
  47.     receive_first = sys.argv[5]
  48.  
  49.     if "True" in receive_first:
  50.         receive_first = True
  51.     else:
  52.         receive_first = False
  53.  
  54.     #now spin up our listening socket
  55.     server_loop(local_host,local_port,remote_host,remote_port,receive_first)
  56.  
  57. def proxy_handler(client_socket, remote_host, remote_port, receive_first):
  58.     #connect to the remote host
  59.     remote_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  60.     remote_socket.connect((remote_host,remote_port))
  61.  
  62.     #receive data from the remote end if necessary
  63.     if receive_first:
  64.         remote_buffer = receive_from(remote_socket)
  65.         hexdump(remote_buffer)
  66.  
  67.         #send it to the repsonse handler
  68.         remote_buffer = response_handler(remote_buffer)
  69.  
  70.         #if data is able to be sent to local client, send it
  71.         if len(remote_buffer):
  72.             print ("[<==] Sending %d bytes to localhost." % len(remote_buffer))
  73.             client_socket.send(remote_buffer)
  74.     #now loop and read from local,sent to remote,send to local,rinse/wash/repeat
  75.     while True:
  76.         #read from local host
  77.         local_buffer = receive_from(client_socket)
  78.  
  79.         if len(local_buffer):
  80.             print ("[==>] Received %d bytes from localhost." % len(local_buffer))
  81.  
  82.             #send it to request handler
  83.             local_buffer = request_handler(local_buffer)
  84.  
  85.             #send data to remote host
  86.             remote_socket.send(local_buffer)
  87.             print ("[==>] Sent to remote.")
  88.  
  89.         #receive back response
  90.         remote_buffer = receive_from(remote_socket)
  91.         if len(remote_buffer):
  92.             print ("[<==] Received %d bytes from remote." % len(remote_buffer))
  93.             hexdump(remote_buffer)
  94.  
  95.             #send response to handler
  96.             remote_buffer = response_handler(remote_buffer)
  97.  
  98.             #send response to local socket
  99.             client_socket.send(remote_buffer)
  100.  
  101.             print ("[<==] Sent to localhost.")
  102.  
  103.         #if no data left on either side, close connection
  104.         if not len(local_buffer) or not len(remote_buffer):
  105.             client_socket.close()
  106.             remote_socket.close()
  107.             print ("[*] No more data, closing connections.")
  108.  
  109.             break
  110.  
  111. #this is a pretty hex dumping function taken from the comments of http://code.activestate.com/recipes/142812-hex-dumper/
  112. def hexdump(src, length=16):
  113.     result = []
  114.    
  115.     digits = 4 if isinstance(src, unicode) else 2
  116.    
  117.     for i in range(0,len(src), length):
  118.         s = src[i:i+length]
  119.         hexa = b' '.join(["%0*X" % (digits, ord(x)) for x in s])
  120.         text = b' '.join([x if 0x20 <= ord(x) <0x7F else b'.' for x in s])
  121.         result.append(b"%04X %-*s %s" % (i, length*(digits + 1), hexa, text) )
  122.  
  123.     print (b'\n'.join(result))
  124.  
  125. def receive_from(connection):
  126.     buffer = ""
  127.  
  128.     #set a 2 second timeout; depending on your target this may need to be adjusted
  129.     connection.settimeout(2)
  130.  
  131.     try:
  132.         #keep reading the buffer until no more data is there or it times out
  133.         while True:
  134.             data = connection.recv(4096)
  135.            
  136.             if not data:
  137.                 break
  138.             buffer += data
  139.     except:
  140.         pass
  141.     return buffer
  142.  
  143. #modify any requested destined for the remote host
  144. def request_handler(buffer):
  145.     #perform packet modifications
  146.     return buffer
  147.  
  148. #modify any responses destined for the local host
  149. def response_handler(buffer):
  150.     #perform packet modifications
  151.     return buffer
  152.  
  153. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top