🌐Dynamic Port Forwarding

Overview

Dynamic port forwarding with SSH creates a SOCKS proxy that allows us to pivot through compromised hosts to access internal networks. This technique is essential when we need to access multiple services on networks that are not directly reachable from our attack host.


Port Forwarding in Context

Port forwarding redirects communication requests from one port to another using TCP as the primary communication layer. Different application layer protocols (SSH, SOCKS) can encapsulate the forwarded traffic to:

  • Bypass firewalls

  • Use existing services on compromised hosts

  • Pivot to other networks


SSH Local Port Forwarding (-L)

Basic Concept

Forward a local port to a remote destination through an SSH server (pivot host).

Syntax:

ssh -L [local_port]:[destination_host]:[destination_port] [user]@[ssh_server]

Practical Example from HTB

Scenario: We have compromised Ubuntu server (10.129.202.64) with MySQL running locally on port 3306.

Initial Scan:

Setting up Local Port Forward:

Traffic Flow:

Verification:

Multiple Port Forwarding


Dynamic Port Forwarding (-D) - SOCKS Proxy

When to Use Dynamic Port Forwarding

Use dynamic port forwarding when:

  • You need to access multiple services on an internal network

  • You don't know which services are available beforehand

  • You want to tunnel various tools through the compromised host

Setting up SOCKS Proxy

Example Scenario: Ubuntu server has multiple network interfaces:

  • ens192: 10.129.202.64 (external, accessible from attack host)

  • ens224: 172.16.5.129 (internal network interface)

  • lo: 127.0.0.1 (loopback)

Discovery Process: How We Found 172.16.5.19

Step 1: Identify Internal Networks

Step 2: Network Range Calculation

Step 3: Live Host Discovery

Step 4: Service Identification

Checking Network Interfaces on Pivot:

Creating SOCKS Proxy:


Configuring Proxychains

Configuration File Setup

Complete Configuration Example:

Verify Configuration


Using Tools through SOCKS Proxy

Nmap through Proxychains

Important Notes:

  • Only TCP connect scans (-sT) work through proxychains

  • Use -Pn to skip ping probes (Windows Defender blocks ICMP)

  • Partial packets (SYN scans) return incorrect results

Network Discovery:

Port Scanning Specific Host:

Metasploit through Proxychains

Starting Metasploit:

Using Auxiliary Modules:

RDP Connection through Proxy


SOCKS Protocol Details

SOCKS vs Regular Proxies

SOCKS (Socket Secure) Protocol:

  • Works at Session Layer (Layer 5)

  • Can handle any type of traffic (TCP/UDP)

  • Client initiates connection to SOCKS server

  • Server forwards traffic on behalf of client

Types:

  • SOCKS4: No authentication, no UDP support

  • SOCKS5: Authentication support, UDP support, better security

Traffic Flow in SOCKS Tunneling


Advanced Techniques

Multiple Simultaneous Tunnels

Background Tunnels

Compressed Tunnels


Troubleshooting

Common Issues and Solutions

1. Proxychains Connection Timeouts

2. DNS Resolution Problems

3. Windows Firewall Blocking Scans

4. SSH Connection Issues

Debugging Commands


Best Practices

Security Considerations

  1. Use key-based authentication when possible

  2. Clean up tunnels after use

  3. Monitor tunnel stability for long operations

  4. Use compression (-C) for slow connections

Performance Optimization

  1. Use specific port ranges instead of full scans

  2. Target known live hosts when possible

  3. Use multiple parallel tunnels for different services

  4. Keep tunnel sessions active with ServerAliveInterval

Operational Security

  1. Mimic legitimate traffic patterns

  2. Use encrypted tunnels (SSH)

  3. Avoid suspicious port combinations

  4. Document tunnel configurations for team use


Lab Exercises (HTB Style)

Exercise 1: Basic Port Forward

Exercise 2: SOCKS Proxy Setup

Exercise 3: RDP Access


Quick Reference Commands

Task

Command

Local port forward

ssh -L 1234:target:3306 user@pivot

SOCKS proxy

ssh -D 9050 user@pivot

Background tunnel

ssh -fNT -D 9050 user@pivot

Proxychains scan

proxychains nmap -Pn -sT target

Metasploit via proxy

proxychains msfconsole

RDP via proxy

proxychains xfreerdp /v:target /u:user /p:pass

Check tunnel

netstat -antp | grep 9050


Network Diagrams

Local Port Forward Flow

SOCKS Proxy Flow


References

  • HTB Academy: Pivoting, Tunneling & Port Forwarding

  • SSH Manual: man ssh

  • Proxychains: /etc/proxychains.conf

  • SOCKS Protocol: RFC 1928 (SOCKS5)

Last updated