Wednesday 13 June 2012

bash based netcat

Recently, i've stumbled upon machine that didn't have a nc installed.

I've needed it so badly ;) that i've decided to install manually preconfigured version and then i realized that i can try it do implement simple replacement nc with bash.

Here is the result:

$ echo -e "GET / HTTP/1.0\r\n\r\n" |  ./bashnc google.pl 80
HTTP/1.0 302 Found
Location: http://www.google.fi/
Cache-Control: private
(...)

works like a charm.

The script bashnc is ridiculously simple:

#/bin/bash
(
    cat <&3 & # 3 is original stdin
    cat >&4 & # 4 is original stdout
    wait
) 3<&0 4>&1 > /dev/tcp/$1/$2 0<&1

Works like a charm.

As a proof of soundness, i've tried to run ssh on top of this bashnc proxy:

$ ssh -oProxyCommand='bash -c "( cat <&3 & cat >&4 & wait ) 3<&0 4>&1 > /dev/tcp/%h/%p 0<&1 "' some_host uname
Linux some_host 3.2.0-2-amd64 #1 SMP Tue Mar 13 16:54:04 UTC 2012 x86_64 GNU/Linux
$