01. Dynamic Port Forwarding with SSH and SOCKS Tunneling
SSH port forwarding lets me tunnel traffic through a pivot host I have SSH access to, reaching services and networks the attack box cannot touch directly.
Executing Local Port Forward
Forward a local port on the attack box to a service running on the pivot host.
ssh -L 1234:localhost:3306 Ubuntu@10.129.202.64
-L 1234: local port on the attack box3306: port on the pivoting server to redirect to
This opens port 1234 on the SSH client, then forwards that traffic to the SQL server on port 3306. I can now interact with it on my localhost using tools like nmap: nmap -sC -sV -p1234 localhost. That enumerates the SQL server, since traffic is being routed from 3306 to 1234.
Confirm Port Forward Is Active
Check that the attack box is now listening on 1234.
netstat -antp | grep 1234
Confirm Port Forward with Nmap
nmap -sC -sV -p1234 localhost
Forward Multiple Ports to Localhost
A single SSH command can set up several local forwards at once.
ssh -L 1234:localhost:3306 8080:localhost:80 Ubuntu@10.129.202.64
-L 1234: attack box local port to receive SQL3306: pivoting server port to redirect SQL8080: attack box local port to receive HTTP80: pivoting server port to redirect HTTP
Dynamic Port Forwarding
Instead of forwarding one port at a time, dynamic forwarding turns the SSH connection into a SOCKS proxy, so any tool can route through the pivot into the target network.
ssh -D 9050 ubuntu@10.129.202.64
-D 9050: requests SSH to enable dynamic port forwarding
Then redirect TCP connections with ProxyChains. Edit the config file at /etc/proxychains.conf and add this at the end:
socks4 127.0.0.1 9050
Using Nmap with ProxyChains Port 9050 To Victim Network
proxychains nmap -v -sn 172.16.5.1-200
NOTE: We can only perform full TCP connect scans over proxychains. Reason why is because proxychains cannot understand half-connect scans.
Enumerating Windows Target Through ProxyChains
For full TCP connect scans (-sT), only one host can be scanned at a time.
proxychains -v -Pn -sT 172.16.5.19
Exploit Windows RDP via ProxyChains with MSFconsole
Launch msfconsole through ProxyChains so the RDP scanner reaches the target across the tunnel.
proxychains msfconsole
use auxiliary/scanner/rdp/rdp_scanner
set RHOSTS 172.16.5.19
exploit
Xfreerdp with ProxyChains
proxychains xfreerdp /v:172.16.5.19 /u:victor /p:pass@123