Friday, January 14, 2011

ssh dynamic port forward (socks4) test with netcat (nc)

I wrote a test case for ssh, and there was a need to test the ssh's -D switch - the dynamic port forward with socks4, where the only tool what I found was the nc aka. netcat, I needed to solve it w/o socat, tsocks or any other fency tool, where nc had no socks support yet.
ssh hapci -N -D 1080 & sleep 3; printf '\x04\x01\x00\x50\x7f\x00\x00\x01\x00GET / HTTP/1.0\n\n' | nc -q 3 localhost 1080; kill %1
this scriptlet launches ssh and listen on the localhost:1080 TCP port with socks4/socks5 protocol. So I used printf to build up the socks4 header where 0x00 0x50 is port 80 (HTTP), and 0x7f 0x00 0x00 0x01 is 127.0.0.1 - localhost, and then I can emit the given HTTP GET command to the localhost's 80 port, then I kill the background job ssh.