SSH jumping over socks(4/5) proxy chain. Host -> socks proxy -> socks proxy -> destination

netcatsockssshssh-tunneling

I got a great answer for my previous question about connecting from Machine A to Machine C via Socks proxy located on Machine B.

Say Machine B Ip is 218.62.97.105 and it is listening on port 1080

The command for that:

ssh -o ProxyCommand='socat - socks:218.62.97.105:HOST_C:21,socksport=1080'

I wonder if it is possible to make a chain from the socks proxies.

Consider scenario: Machine A -> Machine B (Socks proxy 1) -> Machine C (Socks proxy 2) -> Machine D (destination)

Machine B IP: 218.62.97.105 PORT 1080

Machine C IP: 11.11.11.11 PORT 3128

Machine D IP: 55.55.55.55 PORT 8080

I hope there is someone out there experienced with socat or any other tool as it seems pretty complicated for me at this point.

+100 for anyone who could give me the working answer.


debug of socat 1 command:

2012/10/02 20:45:00 socat[15641] D read -> 8
2012/10/02 20:45:00 socat[15641] D received socks4 reply data (offset 0): 00 5c 00 50 c1 6b 90 17
2012/10/02 20:45:00 socat[15641] D received all 8 bytes
2012/10/02 20:45:00 socat[15641] I received socks reply VN=0 CD=92 DSTPORT=80 DSTIP=193.107.144.23
2012/10/02 20:45:00 socat[15641] E socks: ident refused by client
2012/10/02 20:45:00 socat[15641] N exit(1)
2012/10/02 20:45:00 socat[15641] I shutdown(4, 2)
2012/10/02 20:45:00 socat[15641] D shutdown()  -> 0
2012/10/02 20:45:00 socat[15641] I shutdown(3, 2)
2012/10/02 20:45:00 socat[15641] D shutdown()  -> 0
2012/10/02 20:45:00 socat[15638] I childdied(signum=17)
2012/10/02 20:45:00 socat[15638] D waitpid(-1, 0xbfbea3fc, 1)
2012/10/02 20:45:00 socat[15638] D waitpid(, {256}, ) -> 15641
2012/10/02 20:45:00 socat[15638] I childdied(17): cannot identify child 15641
2012/10/02 20:45:00 socat[15638] D saving pid in diedunknown1
2012/10/02 20:45:00 socat[15638] W waitpid(): child 15641 exited with status 1
2012/10/02 20:45:00 socat[15638] D waitpid(-1, 0xbfbea3fc, 1)
2012/10/02 20:45:00 socat[15638] D waitpid(, {256}, ) -> -1
2012/10/02 20:45:00 socat[15638] I waitpid(-1, {}, WNOHANG): No child processes
2012/10/02 20:45:00 socat[15638] I childdied() finished

socat second command:

2012/10/02 20:44:38 socat[15640] D socket(2, 1, 6)
2012/10/02 20:44:38 socat[15640] I socket(2, 1, 6) -> 3
2012/10/02 20:44:38 socat[15640] D fcntl(3, 2, 1)
2012/10/02 20:44:38 socat[15640] D fcntl() -> 0
2012/10/02 20:44:38 socat[15640] D connect(3, {2,AF=2 127.0.0.1:22222}, 16)
2012/10/02 20:44:38 socat[15640] D connect() -> 0
2012/10/02 20:44:38 socat[15640] D getsockname(3, 0xbf8111cc, 0xbf811058{112})
2012/10/02 20:44:38 socat[15640] D getsockname(, {AF=2 127.0.0.1:40843}, {16}) -> 0
2012/10/02 20:44:38 socat[15640] N successfully connected from local address AF=2 127.0.0.1:40843
2012/10/02 20:44:38 socat[15640] I sending socks4 request VN=4 DC=1 DSTPORT=21 DSTIP=xx.xxx.xxx.xxx USERID=mnmnc
2012/10/02 20:44:38 socat[15640] D malloc(42)
2012/10/02 20:44:38 socat[15640] D malloc() -> 0x8f1ec80
2012/10/02 20:44:38 socat[15640] D sending socks4(a) request data 04 01 00 15 3e f4 9f 9a 6d 6e 6d 6e 63 00
2012/10/02 20:44:38 socat[15640] D write(3, 0xbf811304, 14)
2012/10/02 20:44:38 socat[15640] D write -> 14
2012/10/02 20:44:38 socat[15640] I waiting for socks reply
2012/10/02 20:44:38 socat[15640] D read(3, 0xbf811234, 8)
2012/10/02 20:45:00 socat[15640] D read -> 0
2012/10/02 20:45:00 socat[15640] E read(): EOF during read of socks reply, peer might not be a socks4 server
2012/10/02 20:45:00 socat[15640] N exit(1)
2012/10/02 20:45:00 socat[15640] I shutdown(3, 2)
2012/10/02 20:45:00 socat[15640] D shutdown()  -> 0
ssh_exchange_identification: Connection closed by remote host

Best Answer

With:

socat tcp-listen:12345,reuseaddr,fork,bind=127.1 socks:218.62.97.105:11.11.11.11:3128,socksport=1080

you will have a socat waiting for TCP connections on port 12345 on the loopback interface, and forward them to 11.11.11.11:3128 by way of the socks server on 218.62.97.105:1080

You can then use that to connect to D:

ssh -o ProxyCommand='socat - socks:127.1:%h:%p,socksport=12345' -p 8080 55.55.55.55

(untested)

Related Question