I always asked myself how proxychains can build a chain of proxies like this:
my pc -> proxy1 -> proxy2 -> proxy3 -> proxy4 -> proxy5 -> site
How can this program make possible that all this chain is linked and the request goes to the site through all these proxies and that the answer goes to my pc through all these proxies? Is this possibile to make a chain of proxies using socket library:
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
or some other library like this?
I'd like to know how it works, cause I know proxychains is written in C and since I only know Python, I can't analyse the source.
The main idea behind proxychains is that proxies allow you to build a tunnel to another system. For example if you want to reach system T via HTTP proxy A and SOCKS4 proxy B you do the following:
- Create a TCP connection (i.e. a socket) to proxy A.
- Do a HTTP CONNECT request to establish a tunnel to proxy B - see RFC 2817 for details about CONNECT. After this tunnel is established your socket is still connected to proxy A but proxy A will sent all your data to proxy B and vice versa.
- Send the SOCKS4 header on the socket to establish another tunnel via proxy B. This SOCKS4 header will be sent from your system via the socket to proxy A which will then forward it to proxy B. B will then make a connection to the final target T based on the information in this header and then forward any data it receives (via proxy A which got it from your program) to T and vice versa. For more information on the SOCKS protocol see Wikipedia.
From then on any data you send from your socket to proxy A will be forwarded to proxy B and then forwarded to target T. Similar T will sent its response back to B which will sent it back to A which will sent it to your application.
Is this possibile to make a chain of proxies using socket library:
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
or some other library like this?
As you can see from the description above the steps are needed
- create a socket
- connect the socket to proxy A
- create a tunnel via A to proxy B - either with HTTP or SOCKS protocol
- similar create a tunnel via [A,B] to proxy C
- similar create a tunnel via [A,B,C] to D
- ... until your last proxy is instructed to built the tunnel to the final target T
This can be easily implemented in Python as long you have the appropriate knowledge of the HTTP and SOCKS protocols. Or you could simply wrap your python program into proxychains similar to any other program. This will magically hook into the connect and build the tunnels for you.