SSH Tunnelling
SSH Tunneling
Local Port Forwarding
Local port forwarding allows you to forward a port from your local machine to a remote server. This is useful for accessing services on a remote network.
1
ssh -L [local_port]:[remote_address]:[remote_port] user@remote_host -p [ssh_port]
Example:
1
ssh -L 8080:192.168.1.20:3389 user@remote_host -p 2222
Access the service via localhost:8080.
Dynamic Port Forwarding
Dynamic port forwarding creates a SOCKS proxy on your local machine, which can be used to route traffic through the SSH server.
1
ssh -D [local_port] user@remote_host -p [ssh_port]
Example:
1
ssh -D 8181 user@remote_host -p 2222
Configure your applications to use localhost:8181
as a SOCKS proxy.
Reverse Tunneling
Reverse tunneling allows a remote server to access services on your local machine.
1
ssh -R [remote_port]:localhost:[local_port] user@remote_host -p [ssh_port]
Examples:
- Forward RDP:
1
ssh -R 8181:localhost:3389 user@remote_host
- Forward SSH:
1
ssh -R 43022:localhost:22 user@remote_host -p 45
Connect using:
1
ssh localhost -p 43022
Note: Ensure GatewayPorts
and AllowTcpForwarding
are enabled on the server.
Serve Local Service as Remote Service
This setup allows you to expose a local service to a remote server, making it accessible from the remote network.
You are essentially binding a port from your local system to the remote port. You could host your website locally and share that port via the SSH service, making the port available on your remote system without actually port-forwarding.
1
ssh -N -R [remote_port]:127.0.0.1:[local_port] user@remote_host -p [ssh_port]
Example:
Forward a local service on port 8096 to a remote server:
1
ssh -N -R 8096:127.0.0.1:8096 user@$KALI -p 4444
Configuration: Ensure GatewayPorts yes
is set in /etc/ssh/sshd_config
on the remote server to allow remote hosts to connect to the forwarded port.
Minecraft Server Example
Here’s an example if you wanted to share your local Minecraft server to a remote host:
1
ssh -vvv -N -R 25565:127.0.0.1:25565 [email protected]
-vvv
: Enables verbose mode for detailed debugging information.-N
: Tells SSH not to execute a remote command, useful for port forwarding only.-R
: Specifies reverse port forwarding.
Now on the remote system/hosting provider, you can check the port 25565
and you’ll see that it’s bound to your local machine via a SSH Tunnel.
VNC Forwarding
Forward VNC traffic through SSH:
1
ssh -L [local_port]:localhost:[vnc_port] user@remote_host
Example:
1
ssh -L 5903:localhost:5900 [email protected]
X11 Forwarding
X11 forwarding allows you to run graphical applications over SSH.
1
ssh -X user@remote_host
For a fully trusted connection, use:
1
ssh -Y user@remote_host
Set up the display:
1
2
xhost +
export DISPLAY="127.0.0.1:10.0"