I recently bought the book Black Hat Python by Justin Seitz because I've been interested in getting into Python and playing around with network security.
I'm working in Kali Linux and one of the examples in the book is a simple TCP Proxy. I've already written a few scripts that connect are connected through ports, but now I'm having trouble trying to create a connection to a remote server, such as Google.
Now, I admit that I'm just about a complete novice with Python. I mostly program in C++. As a disclosure, I did move the main() function to the top of the code as to be organized in order of execution. In the actual source, it's located at the very bottom.
Everything from here up is just the main() function getting all of the parameters and variables parsed and setup.
The server_loop() function is the source function for my issue. It contains all of the code for connecting clients to the servers.
The first section is just setting up my server socket as well as creating a few execution shortcuts for myself.
Binding the server to my local address works just fine. It's during the while loop that the problem starts, I would think.
Here starts the while loop to keep the program open to handle incoming connections. The tout thread contains the code for timing out and closing the server socket.
This seems to be where my program hangs. You see, I'm not 100% sure how this is supposed to be handled. In the book, the code works just fine for him and will successfully return from server.accept() and connect the client_socket to the correct address.
In my execution, however, the program stops at the accept() function and never returns any socket or address data. Unless my understanding of the accept() function is wrong and the code wasn't designed with remote servers in mind.
I'm not entirely sure how the accept() function even works. Does it send a SYN packet to the host to initiate a connection request or is the function just sitting there waiting, thinking that the host is going to send a SYN-ACK return when no SYN was even sent in the first place?
If the server.accept() were to return normally, the client_socket would be connected to the host and initialize the rest of the proxy as it sends and receives data normally. The rest of the code is for completion in case I missed something crucial that's actually the cause of the failure.
When I run the program, I use: sudo python ./nproxy.py @ @ www.google.com 21 True
In order, it calls my program, the first two parameters are shortcuts to automatically connect to the localhost address as well as the port of the host that we're trying to connect to. In this case, it's 21, but I've tried on port 80 as well with the same results. I've also tried about a dozen different websites as well and none seem to be returning my connection requests.
Thank you so much in advance for any advice or help you can give.