black electronics

SSH Tunneling / Port Forwarding

Common SSH tunneling commands for local, remote and dynamic port forwarding during pentests.

SSH tunneling lets you reach hosts and services that aren’t directly routable from your machine, by relaying traffic through a host you already have access to.

Local port forwarding

Forwards a port on your machine to a port on a remote network, via a gateway host.

Terminal window
ssh gateway -p 53 -L 8080:10.11.1.50:80

This creates a tunnel to gateway on port 53, which relays traffic to 10.11.1.50:80. Connecting to 127.0.0.1:8080 on your machine reaches that remote service.

Remote port forwarding

Exposes a port on your attacking machine, from the compromised host’s perspective — useful for exposing an internal service back out to you.

Terminal window
ssh gateway -p 53 -R 3390:127.0.0.1:3389

Run from the compromised internal machine, this tunnels its local port 3389 (RDP) back to port 3390 on your attacking machine (gateway).

Dynamic port forwarding

Turns your local machine into a SOCKS proxy for reaching an entire target network through a single pivot host.

Terminal window
ssh -D 8080 -p 2222 pivot_host

Traffic sent to local port 8080 (via a SOCKS-aware client or proxychains) is tunneled through pivot_host on port 2222 into its network.

Checking what’s listening before you tunnel

Terminal window
netstat -tulpn
-t # TCP sockets
-u # UDP sockets
-l # Listening sockets only
-p # Process using the socket
-n # Don't resolve service names
Useful LinksPentest PayloadsCheat Sheets