Port Forwarding with SSH
SSH tunneling, also known as port forwarding, is a method of creating a tunnel between two endpoints through which traffic is forwarded.
Local Port Forwarding
We can forward a port on the local machine to a port on the remote machine by adding the flag "-L" with SSH. Also, it is required the remote SSH username/password.
Examples
Below are some examples.
Stop Local Port Forwarding
To stop the local port forwarding if it is running background, find the process ID and specify it to kill
command.
Remote Port Forwarding
We can forward a port on the remote machine to a port on the local machine by adding the flag "-R" with SSH.
Examples
Below are some examples.
Dynamic Port Forwarding
If we cannot determine the remote ports opened internally, we can find them using dynamic port forwarding. First off, execute the dynamic port forwarding using ssh.
And update the configuration for Proxychains. In “/etc/proxychains.conf”, comment out “socks4 127.0.0.1 9050" and add “socks5 127.0.0.1 1337” on the bottom of the file.
After that, try port scanning to find open ports of the remote machine over 127.0.0.1.
When we found the open ports, we can execute the Local Port Forwarding using the ports we found. *By the way, we can close the previous dynamic port forwarding if not necessary.
For instance, assume that the remote machine opens port 80 internally. Now access http://127.0.0.1/. We can access the remote webserver.
Reverse Connection
Reverse connections are often used in situations where the server needs to be accessible from the client's network, but the server's network is restricted. By initiating a reverse connection, the client can establish a connection to the server without the need for the server to be accessible on the public internet.
1. Generate SSH Keys in Remote Machine
Then save them (public key and private key) to arbitrary folder.
Copy the content of public key (id_rsa.pug).
2. Add Content of Public Key to authorized_key in Your Local Machine
To clarify that the key only for reverse connection, add the following line to this content in authorized_key.
Check if SSH server is running. If the server is not running, start SSH server.
3. Run Reverse Proxy in Remote Machine
Reverse port forwarding using the private key (id_rsa)
4. Confirmation in Your Local Machine
You can access to <remote-ip>:<remote-port>
5. Close Connection in Remote Machine
After that, stop reverse connection.
Last updated