Register
 

SMF - Just Installed!

FreeNIX Security Labs Forum



Author Topic: TCP Proxy  (Read 84 times)

randomfoo

  • Newbie
  • *
  • Posts: 5
    • View Profile
TCP Proxy
« on: January 28, 2018, 09:32:08 AM »
I'm trying to develop a tcp proxy in python. The goal is to proxy the communication between a simple C# client/server application. That application is made for network analysis purpose. Here's the code: You are not allowed to view links. Register or Login

And that is my python code
Code: You are not allowed to view links. Register or Login
#!/usr/bin/python3

import socket
import threading


class Server:

def __init__(self, listen_ip, listen_port):

self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listen_socket.bind((listen_ip, listen_port))
self.listen_socket.listen(5)

def main_loop(self,remote_host, remote_port):

while True:
(client_socket, addr) = self.listen_socket.accept()

proxy_thread = threading.Thread(target=self.handle_client, args=(client_socket, remote_host, remote_port))
proxy_thread.start()

def handle_client(self, client_socket, remote_host, remote_port):
#client_socket.setblocking(0)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#server_socket.setblocking(0)
server_socket.connect((remote_host,remote_port))
print("DEBUG")


while True:
request = ""
request = client_socket.recv(4096)
print(request, client_socket.getsockname(), client_socket.getpeername(), '\n', server_socket.getsockname(), server_socket.getpeername())
 
server_socket.send(request)

response = ""
if len(response = server_socket.recv(4096)) != 0:
print(response)

client_socket.send(response)

if __name__ == '__main__':
    # create a "TheServer" instance passing host:port
        proxy = Server("192.168.1.9", 4444)
        try:
            # try to call method main_loop
            proxy.main_loop('192.168.1.6',12345)
        except KeyboardInterrupt:
            print "Ctrl C - Stopping server" 

When client connect to the listening proxy port send a message that is properly send to remote host. But i can't received any response from server and i get some error in client application.

Code: You are not allowed to view links. Register or Login
WIN_CLIENt_PC > dotnet exec ChatClient/bin/Release/netcoreapp1.1/ChatClient.dll bob 192.168.1.9 --port 4444
ChatClient (c) 2017 James Forshaw
WARNING: Don't use this for a real chat system!!!
Connecting to 192.168.1.9:4444
> ss
> Error: System.AggregateException: One or more errors occurred. (Unable to read
 data from the transport connection: Impossibile stabilire la connessione. Rispo
sta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata
risposta dall'host collegato.) ---> System.IO.IOException: Unable to read data f
rom the transport connection: Impossibile stabilire la connessione. Risposta non
 corretta della parte connessa dopo l'intervallo di tempo oppure mancata rispost
a dall'host collegato. ---> System.Net.Sockets.SocketException: Impossibile stab
ilire la connessione. Risposta non corretta della parte connessa dopo l'interval
lo di tempo oppure mancata risposta dall'host collegato
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size,
 SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   at ChatProtocol.XorStream.Read(Byte[] buffer, Int32 offset, Int32 count) in C
:\Users\Admin\Documents\ExampleChatApplication-master\ChatProtocol\XorStream.cs:
line 76
   at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
   at System.IO.BinaryReader.ReadInt32()
   at ChatProtocol.BinaryNetworkTransport.ReadPacket() in C:\Users\Admin\Documen
ts\ExampleChatApplication-master\ChatProtocol\BinaryNetworkTransport.cs:line 71
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.TcpChatConnection.<ReadPacket>d__15.MoveNext() in C:\Users\Admi
n\Documents\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 345
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.TcpChatConnection.<ReadPacket>d__16.MoveNext() in C:\Users\Admi
n\Documents\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 354
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.ChatConnection.<HandleHello>d__6.MoveNext() in C:\Users\Admin\D
ocuments\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 47
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.Program.<RunClient>d__14.MoveNext() in C:\Users\Admin\Documents
\ExampleChatApplication-master\ChatClient\Program.cs:line 224
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.IO.IOException: Unable to read data from the tr
ansport connection: Impossibile stabilire la connessione. Risposta non corretta
della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'hos
t collegato. ---> System.Net.Sockets.SocketException: Impossibile stabilire la c
onnessione. Risposta non corretta della parte connessa dopo l'intervallo di temp
o oppure mancata risposta dall'host collegato
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size,
 SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   at ChatProtocol.XorStream.Read(Byte[] buffer, Int32 offset, Int32 count) in C
:\Users\Admin\Documents\ExampleChatApplication-master\ChatProtocol\XorStream.cs:
line 76
   at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
   at System.IO.BinaryReader.ReadInt32()
   at ChatProtocol.BinaryNetworkTransport.ReadPacket() in C:\Users\Admin\Documen
ts\ExampleChatApplication-master\ChatProtocol\BinaryNetworkTransport.cs:line 71
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.TcpChatConnection.<ReadPacket>d__15.MoveNext() in C:\Users\Admi
n\Documents\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 345
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.TcpChatConnection.<ReadPacket>d__16.MoveNext() in C:\Users\Admi
n\Documents\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 354
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.ChatConnection.<HandleHello>d__6.MoveNext() in C:\Users\Admin\D
ocuments\ExampleChatApplication-master\ChatClient\ChatConnection.cs:line 47
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at ChatClient.Program.<RunClient>d__14.MoveNext() in C:\Users\Admin\Documents
\ExampleChatApplication-master\ChatClient\Program.cs:line 224<---