Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted
+100

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)

share|improve this answer
    
Firstly I used the command socat tcp-listen:22222,reuseaddr,fork,bind=127.1 socks:218.62.97.105:193.107.144.23:80,socksport=1080 and i checked that both those proxies are working with the command you have given me in my previous question. However when i supply the second command : ssh -o ProxyCommand='socat - socks:127.1:%h:%p,socksport=22222' -p 21 xxx.dyndns.org nothing is really happening for a while and then the error message: EOF during read of socks reply, peer might not be a socks4 server ssh_exchange_identification: Connection closed by remote host –  mnmnc Oct 2 '12 at 16:52
    
the xxx.dyndns.org is my computer - Machine A but its external IP address and listening on port 21. I'm sure it works fine as i used it with ssh -o ProxyCommand='socat - socks:218.62.97.105:xxx.dyndns.org:21,socksport=1080' and it gave me the sshd prompt. –  mnmnc Oct 2 '12 at 16:59
1  
Try and add a few -d options to the socats. –  Stephane Chazelas Oct 2 '12 at 18:17
    
I added the debug in my question. I feel like the fault is in the line 2012/10/02 20:52:21 socat[17091] E socks: ident refused by client which might be caused by my username being passed in the line 2012/10/02 20:52:10 socat[17091] I sending socks4 request VN=4 DC=1 DSTPORT=1080 DSTIP=89.223.109.20 USERID=mnmnc –  mnmnc Oct 2 '12 at 18:55
    
Yes, I suspect those SOCKS proxies have limitations on what you can connect to through them. As when I try the solution above with two socks proxies of mine (ssh -D ones) it works fine. –  Stephane Chazelas Oct 2 '12 at 19:30
add comment

I use tsocks for this purpose. It's a wrapper that catches all connects and forwards them according to your tsocks.conf file. For instance:

server = 127.0.0.1
server_type = 5
server_port = 1338

Where as you already have a ssh-proxy set on localhost port 1338. Using this method, you can nest connections since the next ssh session running in the tsocks wrapper will behave as if on the other end of the ssh connection.

Not necessarily better than using socat, but I find it easier to keep control of.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.