Domain 5Pivoting & MetasploitCORE

Why Tunneling in Pentesting

Foundational concepts explaining why tunneling and pivoting are essential in penetration testing, covering network segmentation, firewall bypass, NAT traversal, and the tunneling taxonomy.

Domain: Domain 5: Pivoting, Tunneling & Metasploit•Updated: 2026-09-01
#tunneling#pivoting#network-segmentation#firewall-bypass#oscp

Why Tunneling in Pentesting

The Core Problem

During an OSCP engagement, you will almost always encounter the following scenario:

  • You gain initial foothold on Machine A (e.g., 10.10.10.5) through an exploitable web application or service.
  • Machine B (10.10.10.10) — the real target — sits on an internal subnet and is only reachable from Machine A.
  • Your attacker machine (10.10.14.3) cannot directly reach Machine B due to firewall rules, network segmentation, or NAT.

Tunneling bridges this gap. It creates a communication channel through the compromised pivot machine so you can reach otherwise inaccessible targets.

graph LR
    subgraph "Your Network"
        A[Attacker<br/>10.10.14.3]
    end
    subgraph "Target Network - DMZ"
        B[Pivot / Machine A<br/>10.10.10.5<br/>Compromised]
    end
    subgraph "Target Network - Internal"
        C[Target / Machine B<br/>10.10.10.10<br/>Not directly reachable]
    end
    A -. "DIRECT: BLOCKED" .-> C
    A -->|Step 1: Compromise| B
    B -->|Step 2: Tunnel through| C

Key Reasons for Tunneling

1. Network Segmentation

Modern networks are divided into segments (DMZ, Internal, Restricted, Database) with firewalls controlling traffic between them. When you compromise a host in one segment, you cannot directly access hosts in other segments.

Segment Typical Access Example
DMZ Internet-facing Web servers, mail servers
Internal From DMZ only Workstations, file servers
Restricted From Internal only Domain controllers, databases

OSCP Reality: Most OSCP machines have at least one internal-only host that requires pivoting.

2. Firewall Bypass

Outbound firewall rules often restrict which ports and protocols can leave the network. Common restrictions:

Allowed Outbound Blocked Outbound
HTTP (80) SSH (22)
HTTPS (443) RDP (3389)
DNS (53) SMB (445)
Custom ports

Strategy: Wrap your tunnel traffic inside an allowed protocol (e.g., tunnel SSH traffic over HTTPS port 443).

3. NAT Traversal

Internal hosts behind NAT gateways have private IP addresses that are not routable from the outside. Even if you know the internal IP, your packets cannot reach it directly.

graph LR
    A[Attacker<br/>Public IP] -->|Cannot reach| NAT[NAT Gateway]
    NAT --> B[Internal Host<br/>192.168.x.x]
    A -->|Compromise| C[Host with NAT access]
    C -->|Tunnel through NAT| B

4. Multi-Hop Access

Some networks require traversing multiple compromised hosts to reach the final target:

graph LR
    A[Attacker] -->|Hop 1| B[DMZ Server]
    B -->|Hop 2| C[Internal Workstation]
    C -->|Hop 3| D[Domain Controller]

Each hop requires its own tunnel, and tunnels can be chained together.

5. Stealth

Encrypted tunnels hide your traffic from network monitoring tools:

  • IDS/IPS cannot inspect encrypted tunnel payloads
  • Network sniffers see only encrypted data
  • Proxy logs show legitimate-looking connections

6. Protocol Translation

Wrap blocked protocols inside allowed ones:

Blocked Protocol Wrapped Inside Tunnel Tool
SMB (445) HTTPS (443) Chisel + TLS
RDP (3389) SSH (22) SSH Local Forward
MySQL (3306) DNS (53) DNS Tunnel
Any TCP HTTP (80) ReGeorg

Tunneling Taxonomy

All tunneling techniques can be categorized into four families:

graph TD
    A[Tunneling Techniques] --> B[SSH-Based]
    A --> C[Tool-Based]
    A --> D[Protocol-Based]
    A --> E[Framework-Based]

    B --> B1[Local Forward -L]
    B --> B2[Remote Forward -R]
    B --> B3[Dynamic SOCKS -D]
    B --> B4[Bastion Jump -J]

    C --> C1[Chisel]
    C --> C2[Socat]
    C --> C3[Netcat / Ncat]
    C --> C4[Proxychains]

    D --> D1[HTTP/HTTPS ReGeorg]
    D --> D2[DNS Tunneling]
    D --> D3[ICMP Tunneling]
    D --> D4[WebSocket]

    E --> E1[Meterpreter portfwd]
    E --> E2[Metasploit autoroute]
    E --> E3[OpenVPN / Ligolo-ng]

SSH-Based Tunneling

Uses the SSH protocol itself to create tunnels. Requires SSH server on the pivot. Most reliable and widely available.

Tool-Based Tunneling

Dedicated tunneling tools (Chisel, Socat, Netcat) that create TCP/UDP tunnels without requiring SSH. More flexible when SSH is unavailable.

Protocol-Based Tunneling

Encapsulates traffic inside other protocols (HTTP, DNS, ICMP, WebSocket). Used when firewalls only allow specific protocols outbound.

Framework-Based Tunneling

Built into penetration testing frameworks (Metasploit Meterpreter, OpenVPN). Integrated with exploitation workflows.

Decision Matrix: Which Tunnel to Use?

Scenario Best Tool Why
SSH port (22) open on pivot SSH Tunnel Native, encrypted, reliable, always available
Only HTTP/HTTPS outbound allowed ReGeorg / Ligolo Wraps traffic in web protocols
Need fast TCP tunnel, no SSH Chisel Low latency, Go-based, single binary, simple
Meterpreter session available Meterpreter portfwd Built-in, no extra tools needed
Full MSF exploit routing needed autoroute Integrates with MSF exploits natively
Only DNS (53) allowed outbound DNS Tunnel (iodine/dnscat2) DNS is rarely blocked
Only ICMP allowed outbound ICMP Tunnel (ptunnel2) Ping is rarely blocked
Multi-hop chain needed Chisel + Proxychains Chainable, lightweight, reliable
Full VPN-like access needed Ligolo-ng / OpenVPN IP-level tunnel, all tools work directly
WebSocket available wstunnel / Ligolo (WS) Full-duplex, passes WAFs

Real-World OSCP Exam Scenarios

Scenario 1: Basic Pivot (Most Common)

You exploit a web app on the DMZ machine. The internal machine runs a service only accessible from the DMZ. You need to SSH tunnel or use Chisel to reach the internal service.

Scenario 2: Database Access

The internal machine runs MySQL/PostgreSQL on port 3306/5432, only bound to 127.0.0.1 or the internal interface. Forward the port through your pivot to access it locally.

Scenario 3: Domain Controller Access

You compromise a workstation and need to reach the Domain Controller for credential dumping or lateral movement. Set up a SOCKS proxy and route Impacket tools through it.

Scenario 4: Multi-Hop to Flag

The final flag is on a machine three hops away. Chain tunnels through each compromised host to reach the target.

Common Pitfalls and Mistakes

Mistake Consequence Fix
Forgetting -N flag in SSH Opens unnecessary shell Always use -N for tunnel-only connections
Using -sS with proxychains + nmap SYN scan doesn’t work through SOCKS Use -sT (TCP connect scan) instead
Setting LHOST to attacker IP in autoroute Reverse shell fails Set LHOST to the pivot’s internal IP
Not backgrounding tunnel processes Blocks your terminal Use &, tmux, or nohup
Tunnel drops during exam Lose access to internal network Use autossh or tmux for persistence
Forgetting to close forwarded ports Port conflicts on reconnect Use portfwd flush or kill old tunnels

Key Takeaways

  1. Tunneling is mandatory for OSCP — almost every machine requires at least one pivot.
  2. SSH tunneling is your first choice when port 22 is available — it’s encrypted, reliable, and built-in.
  3. Chisel is your backup when SSH is not available — fast, simple, single binary.
  4. Meterpreter portfwd is convenient when you already have a Meterpreter session.
  5. Proxychains + SOCKS gives you flexible access to route any tool through a tunnel.
  6. Know when to use each technique — the decision matrix above is your guide during the exam.
  7. Practice tunneling setups before the exam — muscle memory matters under time pressure.

Further Reading