2. Dynamic Portfwd SSH and SOCKS tunneling

Port forwarding

SSH Local Port Forwarding example

![](/img/Pasted image 20240824132926.png|750)

PORT     STATE  SERVICE
22/tcp   open   ssh
3306/tcp closed mysql
ssh -L 1234:localhost:3306 ubuntu@10.129.202.64

-L - Tells ssh to request ssh server to forward all data we send via port 1234 to localhost:3306 on ubuntu server. We should be able to access mysql locally on port 1234. Use netstat or nmap to query our localhost and verify if the service was forwarded.

Testing succesful portfwd

netstat -antp | grep 1234
tcp        0      0 127.0.0.1:1234          0.0.0.0:*               LISTEN      4034/ssh            
tcp6       0      0 ::1:1234                :::*                    LISTEN      4034/ssh 

Forwarding multiple ports

ssh -L 1234:localhost:3306 -L 8080:localhost:80 ubuntu@10.129.202.64

Now, if you type ifconfig on the Ubuntu host, you will find that this server has multiple NICs:

Searching for pivot opportunities

Identify the internal nic with ifconfig:

[...]
ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.5.129  netmask 255.255.254.0  broadcast 172.16.5.255
        inet6 fe80::250:56ff:feb9:a9aa  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:b9:a9:aa  txqueuelen 1000  (Ethernet)
        RX packets 8251  bytes 1125190 (1.1 MB)
        RX errors 0  dropped 40  overruns 0  frame 0
        TX packets 1538  bytes 123584 (123.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[...]

Unlike the previous scenario where we knew which port to access, in our current scenario, we don't know which services lie on the other side of the network. So, we can scan smaller ranges of IPs on the network (172.16.5.1-200) network or the entire subnet (172.16.5.0/23). We cannot perform this scan directly from our attack host because it does not have routes to the 172.16.5.0/23 network. To do this, we will have to perform dynamic port forwarding and pivot our network packets via the Ubuntu server. We can do this by starting a SOCKS listener on our local host (personal attack host or Pwnbox) and then configure SSH to forward that traffic via SSH to the network (172.16.5.0/23) after connecting to the target host.

This process is called SSH Tunneling over SOCKS proxy SOCKS == Socket Secure

Technique is often used to circumvent restrictions put in place by firewalls. Another benefit of SOCKS proxy for pivoting / forwarding data is that SOCKS proxies can pivot via creating a route to an external server from NAT networks. Two types:

Enabling Dynamic Port Forwarding with SSH

Whatever data you send here will be broadcasted to the entire network (172.16.5.0/23) over SSH. We can use the below command to perform this dynamic port forwarding.

ssh -D 9050 ubuntu@10.129.202.64

-D requests SSH server to enable dynamic port forwarding. With this enabled, we require a tool that can route any tool's packets over the port 9050 We do this using proxychains, which is capable of redirecting TCP conns through TOR, SOCKS, and HTTPS/HTTP proxy servers and allows us to chain multiple proxy servers together. Proxychains also hides the IP addr of the requesting host - Receiving host will only see IP of pivot host.

To inform proxychains we must use 9050 modify proxychains configuration file at /etc/proxychains.conf

tail -4 /etc/proxychains.conf

# meanwile
# defaults set to "tor"
socks4 	127.0.0.1 9050

Prefix your commands with proxychains i.e. proxychains nmap [...]

This is called SOCKS tunneling.

Enumerating windows host through proxychains

proxychains nmap -v -Pn -sT 172.16.5.19

You can use MSFCONSOLE through proxychains aswell.

proxychains msfconsole
msf6 > search rdp_scanner
msf6 > use 0
msf6 auxiliary(scanner/rdp/rdp_scanner) > set rhosts 172.16.5.19
rhosts => 172.16.5.19
msf6 auxiliary(scanner/rdp/rdp_scanner) > run
|S-chain|-<>-127.0.0.1:9050-<><>-172.16.5.19:3389-<><>-OK
|S-chain|-<>-127.0.0.1:9050-<><>-172.16.5.19:3389-<><>-OK
|S-chain|-<>-127.0.0.1:9050-<><>-172.16.5.19:3389-<><>-OK

[*] 172.16.5.19:3389      - Detected RDP on 172.16.5.19:3389      (name:DC01) (domain:DC01) (domain_fqdn:DC01) (server_fqdn:DC01) (os_version:10.0.17763) (Requires NLA: No)
[*] 172.16.5.19:3389      - Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed