In the undergraduate completion of the time to contact the TCP agent this thing, then need to use a proxy to send and receive the data to make changes, while using the proxy also let me have a deeper understanding of the HTTP protocol.
One of the main things that TCP proxies use is the socket. The proxy connects localhost and remotehost through the socket, and then the traffic and data through proxy can be analyzed.
1 __author__='Seven'2 ImportSYS3 ImportSocket4 ImportThreading5 6 7 defHexdump (SRC, length=16):8result = []9digits = 4ifIsinstance (SRC, Unicode)Else2Ten One forIinchxrange (0, Len (src), length): As = src[i:i +Length] -Hexa = b' '. Join (["%0*x"% (digits, ord (x)) forXinchS]) -Text = b"'. join ([xif0x20 <= Ord (x) < 0x7FElseB'.' forXinchS]) theResult.append (b"%04x%-*s%s"% (i, length * (digits + 1)), hexa, text)) - - PrintB'\ n'. Join (Result) - + - defReceive_from (connection): +Buffer ="" A at #We Set a 2 second time out depending in your target this may need to be adjusted -Connection.settimeout (2) - - Try: - whileTrue: -data = CONNECTION.RECV (4096) in if notData: - Break toBuffer + =Data + except: - Pass the * returnBuffer $ Panax Notoginseng - defrequest_handler (buffer): the #Perform packet mofifications + returnBuffer A the + defresponse_handler (buffer): - #Perform pakect modifications $ returnBuffer $ - - defProxy_handler (Client_socket, Remote_host, Remote_port, Receive_first): theRemote_socket =Socket.socket (socket.af_inet, socket. SOCK_STREAM) - Remote_socket.connect ((Remote_host, remote_port))Wuyi the ifReceive_first: -Remote_buffer =Receive_from (Remote_socket) Wu hexdump (Remote_buffer) -Remote_buffer =Response_handler (Remote_buffer) About ifLen (remote_buffer): $ Print "[<==] sending%d bytes to localhost."%Len (remote_buffer) - client_socket.send (Remote_buffer) - - whileTrue: ALocal_buffer =Receive_from (Client_socket) + ifLen (local_buffer): the Print "[==>] Received%d bytes from localhost."%Len (local_buffer) - hexdump (Local_buffer) $Local_buffer =request_handler (Local_buffer) the remote_socket.send (Local_buffer) the Print "[==>] Sent to remote." theRemote_buffer =Receive_from (Remote_socket) the ifLen (remote_buffer): - Print "[<==] Received%d bytes from remote."%Len (remote_buffer) in hexdump (Remote_buffer) theRemote_buffer =Response_handler (Remote_buffer) the client_socket.send (Remote_buffer) About Print "[<==] Sent to localhost." the if notLen (Local_buffer)or notLen (remote_buffer): the client_socket.close () the remote_socket.close () + Print "[*] No more data. Closing connections." - the BreakBayi the the defServer_loop (Local_host, Local_port, Remote_host, Remote_port, Receive_first): -Server =Socket.socket (socket.af_inet, socket. SOCK_STREAM) - Try: the Server.bind ((Local_host, local_port)) the except: the Print "[!!] Failed to listen on%s:%d"%(Local_host, Local_port) the Print "[!!] Check for other listening sockets or correct permissions." - sys.exit (0) the Print "[*] Listening on%s:%d"%(Local_host, Local_port) the theServer.listen (5)94 the whileTrue: theClient_socket, addr =server.accept () the Print "[==>] Received incoming connection from%s:%d"% (Addr[0], addr[1])98Proxy_thread = Threading. Thread (target=Proxy_handler, Aboutargs=(Client_socket, Remote_host, Remote_port, Receive_first)) - Proxy_thread.start ()101 102 103 defMain ():104 ifLen (sys.argv[1:])! = 5: the Print "Usage:./tcp proxy.py [localhost] [localport] [remotehost] [remoteport] [Receive_first]"106 Print "Example:./tcp proxy.py 127.0.0.1 9000 10.12.132.1 9000 True"107 sys.exit (0)108 109Local_host = sys.argv[1] thelocal_port = Int (sys.argv[2])111 theRemote_host = sys.argv[3]113remote_port = Int (sys.argv[4]) the theReceive_first = sys.argv[5] the 117 if "True" inchReceive_first:118Receive_first =True119 Else: -Receive_first =False121 122 Server_loop (Local_host, Local_port, Remote_host, Remote_port, Receive_first)123 124 theMain ()
The data received can be modified in the request_handler and Response_handler functions.
The Hexdump function on the tall is from http://code.activestate.com/recipes/142812-hex-dumper/, which means that you can not understand how to read. If you have a friend that you can read, please talk to me.
Black Hat Python's #2:tcp agent