Port Forwarding
Port Forwarding and Pivoting
SSH
Locally
Example: we want to connect to the port 1025 on the remote machine "delivery" on our port 1337.
Example: we want to access the port 1025 on the remote machine "pivotable" through the machine "delivery" on our machine on port 1337.
Remotely
Example: SSH to my kali, opens up port 1025 on the victim's localhost, to be accessible at port 1337 on kali.
Dynamically
This creates a SOCKS proxy that allows you to reach IP addresses that are in the same network as a box you can SSH to.
Run the following commands on your machine to create a SOCKS proxy:
Then, on your machine, make sure /etc/proxychains.conf
has:
Then you can use the proxychains
command in front of your other commands to proxy through the machine. For example, this command curls the IP address 192.168.122.4 through the proxied machine:
To use that with burp, go to "User Options", select "use socks proxy", and configure it to go through localhost on port 1080.
Note: SOCKS proxies proxy TCP traffic (and UDP with SOCKS5), but not ICMP, for example. So you can't ping through a proxy!
Note: Firefox and Chromium didn't seem to work with proxychains. It might be possible to specify a SOCKS proxy in the browser settings, though.
Two Hops
This section explains how to forward a reverse shell gained in a second hacked box through a first hacked box to kali.
Run the following commands on the first hacked machine:
Question: Why can't we just specify the private IP address in the above command instead of 127.0.0.1? In that case, we wouldn't have to redirect with socat later on.
You can confirm whether it worked by running:
This will send everything from the hacked machine's localhost:9001 to kali:9001. However, it will not send anything from the hacked machine's private IP address to kali.
You can use socat to redirect traffic from the second hacked machine's private IP address to its localhost, so that the traffic could be forwarded to kali.
In this example, the first hacked box's IP address is 192.168.122.1:
After you have set that up, you can run a reverse shell on the second hacked machine and have it connect to port 9001 on the first hacked machine. That connection will be forwarded to port 9001 on your kali. Don't forget to run a listener on port 9001 in kali to catch the connection.
Chisel
This will allow you to access port 4505 on the target's localhost from port 4505 on your own machine.
Socat
Same Machine Redirection
You can use socat for redirecting a port to another port on the same machine.
For example, this command redirects port 9001 on the IP address 192.168.122.1 to localhost:9001 on that same machine:
Access Remote Port Through Local Port
Warning: For some strange reason, when I used this to forward a web port, then I was able to do curl requests to this, but not gobuster or wfuzz.
After following the above example, your port 2222 will be forwarded to a victim's port 22. So you can SSH to the victim like this:
Meterpreter Autorouting
Use autoroute to automatically route traffic to a specific host
Set up a socks proxy
Then set up proxychains by editing /etc/proxychains.conf
. Add the socks5 proxy line under [ProxyList]
(if there is already a ProxyList, add the line under the existing ProxyList):
In metasploit, set up autorouting
After that is done, you can run metasploit tools against the selected host. For example, scanner/portscan/tcp
(the socks proxy part wasn't actually needed for this).
But you want to reach the host using non-metasploit tools, too. To run nmap and other tools against hosts in the chosen subnet, use proxychains. For example, let's say you chose the subnet 172.17.0.0, and you want to scan the host at 172.17.0.1:
Warning: I didn't actually get the proxychains part to work on the OpenSource HTB machine, unfortunately.
Meterpreter Port Forwarding
Portfwd is a command you can use for port forwarding on meterpreter
From meterpreter shell:
-l
: local port to listen on-p
: remote port to connect to-r
: remote IP address to connect to
Pivoting Cheat Sheet
In-depth guide on different scenarios (ssh, reverse, tunnelling, etc):
Last updated