Pivoting means using a host you have already reached as a stepping stone to a network you cannot reach directly. OpenSSH does it with nothing but a login, which makes it a standard tool for assessors and intruders alike. Below are the three forwarding modes, the options that make them dependable, proxychains, and the defence. Test only networks and systems you own or are authorised to assess.
Scope the pivot before you use it
The scope document should name the pivot host and the networks behind it that you may test; otherwise a tunnel extends the test into address space nobody agreed. The Network and Active Directory Ultimate Course runs one internal test in your own lab, from scope and rules of engagement to the report.
The three forwarding modes
The ssh(1) manual page defines each mode. The lab below has an operator machine at 192.0.2.10, a pivot host at 192.0.2.50 that also has an inner address 10.10.10.1, and an inner web server at 10.10.10.20.
Local forwarding, -L, opens a listening port on the client and sends each connection through the SSH session to a fixed destination, which the pivot connects to. Connecting to 127.0.0.1:8080 on the operator machine reaches port 80 on 10.10.10.20, which sees the connection come from the pivot.
Remote forwarding, -R, is the reverse: a listening port on the server sends connections back to a destination on the client side. It exposes a service on your machine, such as a lab listener, to the pivot’s network. Given a port alone, -R makes the remote end a SOCKS proxy that connects onward from your machine.
Dynamic forwarding, -D, opens a local port that speaks SOCKS4 and SOCKS5. The application protocol tells the pivot where to connect, so one tunnel reaches any host and port the pivot can reach.
A local or dynamic forward binds to the loopback address unless GatewayPorts is set on the client or you give a bind address, so other machines cannot use your tunnel. On the server, GatewayPorts in sshd_config(5) defaults to no, keeping remote forwards on loopback too; clientspecified or yes widens that.
Options that make a tunnel dependable
-Nruns no remote command, so the session only forwards.-fsends ssh to the background after authentication.ExitOnForwardFailure yesmakes ssh exit if a port cannot be bound, and with-fit waits for remote forwards to be established first. Without it, a failed bind can leave a session that looks healthy and forwards nothing. It ignores failures to reach the final destination.-Jand theProxyJumpdirective reach a host through one or more jump hosts, separated by commas. The manual notes that options on the command line generally apply to the destination, not the jump hosts, so put jump host settings in~/.ssh/config.
The same settings live in ~/.ssh/config as LocalForward, RemoteForward, DynamicForward, ExitOnForwardFailure, SessionType none (equivalent to -N) and ProxyJump.
SOCKS and what crosses it
The SOCKS server in ssh accepts only the CONNECT command, so it carries TCP and nothing else, though SOCKS5 itself also defines UDP relaying. Tools that open ordinary connections work: a browser, curl, ssh, SMB and RDP clients. A SYN scan and ICMP echo are raw packets, and UDP probes are not TCP, so none enter the tunnel. Nmap’s -sT scan asks the operating system to make a normal connection, which is what lets it ride a proxy. Nmap’s --proxies option takes HTTP and SOCKS4 proxies, which -D also serves, but only version detection and NSE use it, not ping, port scanning or OS discovery, so the port scan needs a hooking tool.
Proxychains
proxychains-ng preloads a library that hooks the libc network calls of a dynamically linked program and redirects its TCP connections through its proxy list, so a statically linked binary is not hooked at all. Its README calls the method a hack that may fail with scripts, programs that start many processes or load modules with dlopen(); UDP, ICMP and pcap based scanning do not work.
strict_chain requires every proxy in the list to be up, whereas dynamic_chain skips dead ones. proxy_dns answers libc name lookups with a placeholder 224.x.x.x address and hands the name to the proxy, so names resolve on the pivot, not on your own resolver. Replace the shipped file’s closing socks4 127.0.0.1 9050 entry with your own socks5 line. At v4.18 the program looks for its file in this order: the -f option, the PROXYCHAINS_CONF_FILE environment variable, proxychains.conf in the current directory, $HOME/.proxychains/proxychains.conf, a Haiku settings path, the sysconfdir set at build time (/usr/local/etc by default, /etc in the README’s build line), then /etc/proxychains.conf. Debian’s package ships /etc/proxychains4.conf and searches it last.
The README says scanners that use standard libc calls may work, which Nmap’s connect scan does, and advises a numeric target for Nmap when proxy_dns is on. The v4.18 source reports any failed connection as refused, which Nmap records as closed, so nothing shows as filtered. Add -Pn, because privileged host discovery sends raw packets, and -n, because Nmap’s reverse DNS goes over UDP straight to your configured name servers.
Lab commands
Checked against OpenSSH 10.6p2 (portable release of 2026-11-05) manual pages, proxychains-ng v4.18 (the latest tag) and Nmap 7.993. Run only against your own lab.
# Local forward: operator 127.0.0.1:8080 to inner web server port 80, through the pivot
ssh -N -f -o ExitOnForwardFailure=yes -L 8080:10.10.10.20:80 [email protected]
# Dynamic forward: SOCKS proxy on 127.0.0.1:1080
ssh -N -f -o ExitOnForwardFailure=yes -D 1080 [email protected]
# Remote forward: pivot port 9001 sends back to operator port 4444
ssh -N -f -o ExitOnForwardFailure=yes -R 9001:127.0.0.1:4444 [email protected]
# Jump through the pivot to SSH on the inner host
ssh -J [email protected] [email protected]
# ~/.ssh/config
Host pivot
HostName 192.0.2.50
User labuser
DynamicForward 127.0.0.1:1080
ExitOnForwardFailure yes
SessionType none
# /etc/proxychains.conf (or a file passed with -f)
strict_chain
proxy_dns
[ProxyList]
socks5 127.0.0.1 1080
# Use the tunnel (TCP connect scan, numeric target, no host discovery, no reverse DNS)
proxychains4 -q nmap -sT -Pn -n -p 22,80,445 10.10.10.20
proxychains4 -q curl http://10.10.10.20/Detection and defence
In sshd_config(5), AllowTcpForwarding defaults to yes and takes no, local or remote, and AllowStreamLocalForwarding (Unix socket forwarding) has the same values and default. PermitOpen limits where forwards may connect and PermitListen where remote forwards may listen; both allow everything by default. PermitTunnel (tun device forwarding, a VPN style layer 2 or 3 tunnel) defaults to no. DisableForwarding turns off every forwarding feature and overrides the others. The manual warns that disabling TCP forwarding does not improve security unless users are also denied shell access, since they can install their own forwarders, so pair it with ForceCommand or a restricted shell for service accounts.
sshd logs a working forward and its target only at debug (10.6p2 source). At the default LogLevel INFO the pivot’s log names a destination in cases such as a forward refused by AllowTcpForwarding (“refused local port forward”), one refused by PermitOpen (“but the request was denied”) and a forwarded connection that fails (“connect_to … failed”), which a scan through the tunnel produces in bulk. “Administratively prohibited” appears only on the client. On OpenSSH 9.8 and later the onward connections and these log lines come from the per-session process sshd-session, so match it as well as sshd in auditd rules and log filters. MITRE’s Linux analytic AN1484 watches auditd connect calls and sshd sessions with unusual forwarding. Nothing expires by default: ChannelTimeout (for example direct-tcpip=10m) closes idle forwarded channels, and UnusedConnectionTimeout closes connections with none open, such as an idle -N tunnel, though neither stops a new forward.
Segmentation and egress filtering stop a compromised host reaching networks it should never talk to. MITRE describes the technique as T1572 Protocol Tunneling, which names SSH port forwarding, and T1090 Proxy for connection proxying and chained proxies.
Where to go next
The Network and Active Directory Ultimate Course covers scoping an internal test, discovery with Nmap and finding command and control channels in Zeek logs. Next, sshuttle, Meterpreter routes and Ligolo-ng cover tunnelling that carries more than TCP, and file transfers covers moving tools and data during an assessment. For the whole workflow, see Penetration Testing Field Notes.
This article replaces the KSEC ARK notes first published in 2019.
More from this author
- One capture, three questions2 December, 2026
- Payload channels versus timing channels2 December, 2026
