Domain 5Pivoting & MetasploitCORE

SSH Tunneling

Complete guide to SSH tunneling including local forwarding, remote forwarding, dynamic SOCKS proxy, and bastion host jumping for OSCP pivoting scenarios.

Domain: Domain 5: Pivoting, Tunneling & Metasploit•Updated: 2026-09-01
#ssh#tunneling#port-forwarding#socks-proxy#oscp

SSH Tunneling

SSH is the most reliable and widely available tunneling method for OSCP. It requires an SSH server running on the pivot machine (port 22). Nearly every Linux machine has SSH available, and Windows 10/11 includes OpenSSH server by default.

SSH Tunnel Types Overview

Type Flag Direction Best For
Local Forward -L Attacker → Internal Access one internal service
Remote Forward -R Internal → Attacker Receive reverse shells from deep internal hosts
Dynamic (SOCKS) -D Bidirectional proxy Scan/access multiple internal hosts
Jump -J Multi-hop chain Deep network traversal through bastions

SSH Tunnel Flags Reference

Flag Description Example
-L Local port forwarding ssh -L 8080:target:80 user@pivot
-R Remote port forwarding ssh -R 9999:localhost:4444 user@pivot
-D Dynamic SOCKS proxy ssh -D 1080 -N user@pivot
-J ProxyJump (bastion) ssh -J user@bastion user@target
-N No remote command (tunnel only) Always use with -L, -R, -D
-f Go to background ssh -f -N -D 1080 user@pivot
-v Verbose output Debug tunnel issues
-p Alternate SSH port ssh -p 2222 user@pivot
-i Private key file ssh -i id_rsa user@pivot
-o Set SSH option -o StrictHostKeyChecking=no

1. Local Port Forwarding (-L)

Purpose: Access a service on the internal network from your attacker machine.

This is the most commonly used SSH tunnel type in OSCP.

Architecture

graph LR
    A[Attacker<br/>10.10.14.3] -->|SSH encrypted| B[Pivot<br/>10.10.10.5]
    B -->|Local network| C[Target<br/>10.10.10.10:80]
    A -.->|via localhost:8080| C

Syntax

ssh -L [local_port]:[target_ip]:[target_port] [user]@[pivot_ip]

Step-by-Step Example

# Access internal web server on 10.10.10.10:80 through pivot 10.10.10.5
ssh -L 8080:10.10.10.10:80 user@10.10.10.5

# After connection, browse:
# http://localhost:8080 → reaches 10.10.10.10:80

How It Works

  1. SSH binds port 8080 on your local (attacker) machine.
  2. Traffic to localhost:8080 is encrypted through the SSH tunnel.
  3. The SSH server on the pivot forwards it to 10.10.10.10:80.
  4. Response returns through the same encrypted channel.

Background the Tunnel

# Run in background (recommended for OSCP)
ssh -f -N -L 8080:10.10.10.10:80 user@10.10.10.5

# -f : Fork to background after authentication
# -N : Do not execute a remote command (tunnel only)

OSCP Use Cases

  • Internal web application not reachable from your attacker machine
  • Database service (MySQL 3306, PostgreSQL 5432) bound to internal interface
  • RDP (3389) on an internal Windows machine
  • Any service on the internal network you need to interact with

Forwarding Multiple Ports

# Forward multiple ports in one SSH session
ssh -f -N -L 8080:10.10.10.10:80 \
         -L 3306:10.10.10.10:3306 \
         -L 445:10.10.10.10:445 \
         user@10.10.10.5

2. Remote Port Forwarding (-R)

Purpose: Expose a service on your attacker machine to the internal network.

Architecture

graph LR
    A[Attacker<br/>10.10.14.3:4444] -->|SSH| B[Pivot<br/>10.10.10.5]
    B -.->|via localhost:9999| A
    C[Internal Host<br/>10.10.10.20] -->|Connects to| B

Syntax

ssh -R [remote_port]:[local_ip]:[local_port] [user]@[pivot_ip]

Example

# Expose your listener on port 4444 to the internal network
ssh -R 9999:127.0.0.1:4444 user@10.10.10.5

# Internal hosts can now connect to pivot:9999 → reaches your attacker:4444

How It Works

  1. SSH binds port 9999 on the pivot (remote) machine.
  2. Any connection to pivot:9999 is tunneled back to your localhost:4444.
  3. Useful for receiving reverse shells from deeper internal hosts that cannot reach your attacker IP.

OSCP Use Cases

  • You need an internal host to connect back to you, but your IP is not routable from the internal network.
  • Setting up a listener that internal services can reach.
  • Receiving reverse shells from hosts behind additional firewalls.

Gateway Ports (Allow External Access)

By default, -R binds only to 127.0.0.1 on the pivot. To bind to all interfaces:

# On pivot, edit /etc/ssh/sshd_config:
GatewayPorts yes

# Then restart SSH:
sudo systemctl restart sshd

# Now use:
ssh -R 0.0.0.0:9999:127.0.0.1:4444 user@10.10.10.5

3. Dynamic Port Forwarding (SOCKS Proxy) (-D)

Purpose: Create a full SOCKS proxy through the pivot for arbitrary traffic to any internal host.

This is the most flexible SSH tunnel type and is heavily used in OSCP.

Architecture

graph LR
    A[Attacker<br/>10.10.14.3] -->|SSH SOCKS| B[Pivot<br/>10.10.10.5]
    B --> C[Target 1<br/>10.10.10.10]
    B --> D[Target 2<br/>10.10.10.20]
    B --> E[Target N<br/>10.10.10.x]

Syntax

ssh -D [local_port] -N [user]@[pivot_ip]

Example

# Create SOCKS5 proxy on localhost:1080
ssh -D 1080 -N user@10.10.10.5

# -D 1080 → Bind SOCKS proxy on localhost:1080
# -N       → No remote command (tunnel only)

Configure Proxychains

# Edit /etc/proxychains4.conf
# At the bottom, in [ProxyList] section:
[ProxyList]
socks5  127.0.0.1  1080

Use with Any Tool

# Scan internal network through the SOCKS proxy
proxychains4 nmap -sT -Pn -p 22,80,443,3306,8080 10.10.10.10

# Access internal web app
proxychains4 curl http://10.10.10.10:8080

# Launch browser through proxy
proxychains4 firefox http://10.10.10.10

# Use Impacket tools through proxy
proxychains4 python3 secretsdump.py DOMAIN/user:pass@10.10.10.10

OSCP Use Cases

  • You need to scan and access multiple internal hosts.
  • SOCKS proxy gives you flexible, on-demand access to any internal service.
  • Combine with Proxychains for seamless tool integration.

4. SSH Bastion Host Jumping (-J)

Purpose: Chain through multiple SSH hops to reach deep internal networks.

Architecture

graph LR
    A[Attacker] -->|SSH| B[Bastion 1<br/>10.10.10.5]
    B -->|SSH| C[Bastion 2<br/>10.10.20.5]
    C --> D[Target<br/>10.10.30.10:80]

Method 1: ProxyJump (OpenSSH 7.3+)

# Jump through bastion1 to reach bastion2
ssh -J user@10.10.10.5 user@10.10.20.5

# With port forwarding through the chain
ssh -J user@10.10.10.5 -L 8080:10.10.30.10:80 user@10.10.20.5

# Multiple jumps
ssh -J user@10.10.10.5,user@10.10.20.5 user@10.10.30.5

Method 2: ProxyCommand

# For older OpenSSH versions
ssh -o ProxyCommand="ssh -W %h:%p user@10.10.10.5" user@10.10.20.5

Method 3: Chained SSH Config

# ~/.ssh/config
Host bastion2
    HostName 10.10.20.5
    User user
    ProxyJump bastion1

Host bastion1
    HostName 10.10.10.5
    User user

# Then simply:
ssh bastion2

OSCP Use Cases

  • DMZ → Internal → Restricted network architecture.
  • Each zone requires a separate hop.
  • ProxyJump chains them seamlessly.

Combining SSH Tunnels with Proxychains4

Full Workflow

graph LR
    A[proxychains4 nmap] -->|SOCKS5| B[localhost:1080]
    B -->|SSH Tunnel| C[Pivot 10.10.10.5]
    C --> D[Target 10.10.10.10]
# Step 1: Create SSH SOCKS tunnel (background)
ssh -f -N -D 1080 user@10.10.10.5

# Step 2: Configure /etc/proxychains4.conf
# [ProxyList]
# socks5  127.0.0.1  1080

# Step 3: Use tools through the proxy
proxychains4 nmap -sT -Pn -p 22,80,443 10.10.10.10
proxychains4 curl http://10.10.10.10:8080
proxychains4 python3 psexec.py DOMAIN/admin:pass@10.10.10.10

Critical Nmap Note

Always use -sT (TCP connect scan), NOT -sS (SYN scan) when scanning through proxychains:

# CORRECT
proxychains4 nmap -sT -Pn -p 22,80,443,3306,8080 10.10.10.10

# WRONG - SYN scan does not work through SOCKS proxy
proxychains4 nmap -sS -Pn -p 22,80,443 10.10.10.10

Troubleshooting SSH Tunnels

Problem Cause Solution
Connection refused on local port SSH server not running on pivot Verify SSH is running: ss -tlnp | grep 22
Permission denied Wrong credentials or key Check username, password, or SSH key
Local port already in use Port conflict Use a different local port or kill existing process
Tunnel works but target unreachable Target not reachable from pivot Verify pivot can reach target: ping target from pivot
Slow tunnel performance Compression or network latency Add -o Compression=yes or check network
Host key verification failed First-time connection Add -o StrictHostKeyChecking=no (exam only)
Tunnel drops when terminal closes Not backgrounded Use -f flag or run inside tmux

OSCP Exam Tips

  1. Always use -f -N for background tunnels — you need your terminal free.
  2. Use tmux to keep tunnels alive even if your SSH session to Kali drops.
  3. Document your tunnels — keep a note of which local port maps to which target.
  4. SOCKS proxy (-D) is your most versatile option — set it up early.
  5. Test the tunnel immediately after creation: curl http://localhost:[port].
  6. Clean up old tunnels before creating new ones to avoid port conflicts.

Further Reading