Sunday, September 11, 2022

Port forwarding from VM , Docker or WSL2 to host Windows 10 or 11 to allow remote access.

 

If you need to bypass NAT restriction on WSL or if you have a Virtual Machine that is NAT only in VirtualBox or VMware you can use these command on the host OS to enable port forwarding from the Host to the nested virtual machines.

Scenario,

HOST OS: windows 10 IP address 192.168.69.129

VM Ubuntu: on WSL2 IP address: 172.25.187.188

We have a Ubuntu box running on windows 10 WSL2, we have started a simple http server using python3 on port 9000, to allow this port access from the Host lan, we can use some of the netsh to expose that port to the wider network.

Now we need to proxy the port via the host to connect it to the VM running on WSL.

Below commands must be Run as administrator in Command Prompt

netsh interface portproxy add v4tov4 listenport=9000 listenaddress=192.168.69.129 connectport=9000 connectaddress=172.25.187.188


Now we need to enable this port via windows advance firewall:

netsh advfirewall firewall add rule name= "Open Port 9000" dir=in action=allow protocol=TCP localport=9000


Note 
This command will live after a reboot, so it is important to make sure you reset the port proxy sittings 

To see what ports you have enabled 
netsh interface portproxy show all 

To reset all ports 
netsh interface portproxy reset



Conclusion

This above trick can also be used during security assessment, In some situation you might land on a machine that is multi homed networked, meaning it has access to 2 networks, so this is a good way to proxy the port so you can remote access to it.

It can also be used in the cloud so you can redirect the traffic via another public IP. Example would be is you are in need of whitelisting, then you can use a windows VM running on a cloud provider that is then whitelisted at the client end, from home now you can connect to that port on the public IP of the VM you have in the cloud, that will then redirect you to the destination network.


No comments:

Post a Comment