Reverse Shells
Commonly used reverse shells
Netcat Example
Setup a listener on the attacking machine:
On the target:
Socat Example
Setup a listener on the attacking machine:
Connect from a Windows Machine:
- The "pipes" option is used to force powershell (or cmd.exe) to use Unix style standard input and output.
Connect from a Linux Machine:
Bash
Normal Bash Reverse Shell
This reverse shell initiates an interactive bash shell that redirects input and output through a TCP connection to the attacker's IP (ATTACKER_IP) on port 443. The >& operator combines both standard output and standard error.
Bash Read Line Reverse Shell
This reverse shell creates a new file descriptor (5 in this case) and connects to a TCP socket. It will read and execute commands from the socket, sending the output back through the same socket.
Bash With File Descriptor 196 Reverse Shell
This reverse shell uses a file descriptor (196 in this case) to establish a TCP connection. It allows the shell to read commands from the network and send output back through the same connection.
Bash With File Descriptor 5 Reverse Shell
Similar to the first example, this command opens a shell (bash -i), but it uses file descriptor 5 for input and output, enabling an interactive session over the TCP connection.
PHP
Basic PHP reverse shell
PHP Reverse Shell Using the exec Function
- This reverse shell creates a socket connection to the attacker's IP on port 443 and uses the exec function to execute a shell, redirecting standard input and output.
PHP Reverse Shell Using the shell_exec Function
- Similar to the previous command, but uses the shell_exec function.
PHP Reverse Shell Using the system Function
- This reverse shell employs the system function, which executes the command and outputs the result to the browser.
PHP Reverse Shell Using the passthru Function
- The passthru function executes a command and sends raw output back to the browser. This is useful when working with binary data.
PHP Reverse Shell Using the popen Function
- This reverse shell uses popen to open a process file pointer, allowing the shell to be executed.
PHP SOCK
Add to the top of a PHP file!
Python
Python Reverse Shell by Exporting Environment Variables
- This reverse shell sets the remote host and port as environment variables, creates a socket connection, and duplicates the socket file descriptor for standard input/output.
Python Reverse Shell Using the subprocess Module
- This reverse shell uses the subprocess module to spawn a shell and set up a similar environment as the Python Reverse Shell by Exporting Environment Variables command.
- The following script works as well.
Short Python Reverse Shell
- This reverse shell creates a socket (s), connects to the attacker, and redirects standard input, output, and error to the socket using os.dup2().
Powershell
- Payload can be found at https://github.com/samratashok/nishang/blob/master/Shells/Invoke-PowerShellTcp.ps1
Others
Telnet
- This reverse shell creates a named pipe using mkfifo and connects to the attacker via Telnet on IP ATTACKER_IP and port 443.
AWK
BusyBox
- This BusyBox reverse shell uses Netcat (nc) to connect to the attacker at ATTACKER_IP:443. Once connected, it executes /bin/sh, exposing the command line to the attacker.