How to Do SSH tunneling (port forwarding)
In this post we will see how SSH works ?, What is SSH tunneling? What is important of SSH tunnels and how to setup the SSH tunnel.
When SSH server is installed in machine then by default its allowed SSH tunneling.
SSH tunneling (port forwarding)
Tunneling is the concept to encapsulate the network protocol to another protocol. here we put into ssh. so all network communication are encrypted. it also called port forwarding. because in SSH tunneling we are going to bind one local port. so what are the packet we are going to send that particle port, all packets are transparently encrypt and delivered to remote system.
What is the need for SSH tunneling?
SSH is enough to administrate the remote system. Its not enough to access the all services which are available in remote system.
Let I describe in the following scenario,
Now lets consider the scenario, system (192.168.56.101) is my system its connected to Internet and server its called privateserver (192.168.56.102) machine have two Ethernet interfaces. one is connected to Internet. and another is connected to 10.10.1.0/24 local network. intranet (10.10.1.11) machine is part of 10.10.1.0/24 sub network.
Note: Here I mentioned local system (192.168.56.101) and remote system (192.168.56.102) are looking like local sub-net IP address. for VM purpose I used these IP address. but reality is it shoshould be any public IP address.
Important Note: SSH server daemon service is running in port 22 in all these three system and firewall is allowed to connect these System Using SSH client.
Now System (192.168.56.101) can communicate to privateserver (192.168.56.102), because both system are connected to Internet directly. but System (192.168.56.101) can't communicate to Intranet (10.10.1.11) machine, because from System (192.168.56.101) perspective 10.10.1.11 is non route able IP address. 10.10.1.11 is not public IP address, its local IP address. so we can't route the packets directly.
Now the situation like that from System (192.168.56.101) I want to access the Intranet (10.10.1.11) machine.Without modifying IP Tables, redirect, like tools/services in intermediate (OR) target machine. Because I m not sys-admin, so I don't have root privilege.
One of the possible solution is first use SSH to log in into privateserver (192.168.56.102) and then again (nested) log in to Intranet (10.10.1.11 ). yes its good idea, its works, now I can remotely administrate the Intranet (10.10.1.11 ). but if Intranet (10.10.1.11) offer some services like VNC, Apache httpd, SMTP, POP3, Squid proxy thenHow we can access these services from System (192.168.56.101 )?
Here its not strictly Intranet (10.10.1.11) offer these service, it may be 10.10.1.0/24 sub net any one of the machine can offer these services. even in 10.10.1.0/24 sub-net have one intranet-website. how we can access these site from System (192.168.56.101) machine Firefox?
The solution is SSH tunneling or ssh port forwarding
How SSH works
In terminal when we typeSSH [email protected]Then Terminal application invoke the SSH client, SSH client connect to Private-Server.org's SSH server in port 22. then both client and server exchange the identity, public keys, cipher suite information and create one shell process in server side. then secure channel is established between client and server. then future all commands from 192.168.101 are go through this secure channel to server (192.168.56.102) and execute the command and the response are come back in same secure channel.
For example after establish SSH, I will issue the LS command. the LS command goes to SSH client then that command encapsulate and encrypted then send to server. server decrypt and extract the command and execute the command in Shell (which is created when SSH is established), the output of LS command is not printed in shell. its redirect the output to SSH client in same secure channel and follow the same procedure (encapsulate and encrypt ). and finally Terminal application shows the output of LS command.
I think here all command is encoded to base64 encoding before send to server and before encrypt.(But I am not sure)
Here very important note that SSH is not disturb the port other than 22 (or which port SSH server is running ).
SSH Tunneling
SSH can work within channel's simultaneously. in normal case we use shell channel. but now we are going to use data channel. so base concept is, in 192.168.56.101 machine SSH client bind one port and establish secure connection with server (192.168.56.102 ). and create one data channel and shell channel. (we can omit shell channel using-N option in SSH command ).
Now in system machine (192.168.56.101) Any application send data to that port (which one SSH client is bind), then SSH client transparently receive the data and encrypt and sent to server machine. in server (192.168.56.102) receive and decrypt it and make the local call. (We discuss later)
SSH tunneling types
SSH offers three types of Tunneling
Dynamic tunneling (socks proxy)
Local port forwarding
Remote port forwarding
Dynamic Tunneling
Command: Ssh-D 8080 [email protected]
Here-D 8080 is mention socks V5 proxy bind 8080 port in Client Side
Now SSH client in 192.168.56.101 machine create one socks proxy server in client side and bind one local port. Then connect to 192.168.56.102 remote machine and establish secure channel.
Now all client application like Firefox, chrome,... need to configure the proxy setting to socks Proxy Server localhost and port number. (check video for reference below the post)
Note: Consider server (192.168.56.102) and Intranet (10.10.1.11) both machines run Apache httpd server and bind port 80
After configuration is completes,
In Firefox now type http: // localhost/, we know that in client machine (192.168.56.101) there is no service is running in port 80. but when we hit enter in browser, we can see the website of server machine (192.168.56.102 ).
How we can access server website using http: // localhost/in client machine?
We configure the proxy in browser. so browser send all HTTP Request (even localhost anf 127.0.0.1) to port 8080, here Socks proxy is running in that port. then socks Proxy Server packs our HTTP request to SSH client and encrypt and send to server. server decrypt and extract the HTTP request. now in server side the HTTP requestHttp: // localhost/Is point to server machine itself. so server invoke the request. if any service is running in port 80, is called and response is send back to client using same secure channel. so browser shows server webpage in client side.
We know that from client machine (192.168.56.101), the IP address 10.10.1.11 is non routable. But now if we issueHttp: // 10.10.1.11/In Firefox browser, its shows the 10.10.1.11 Apache server webpage.
Advantage:
One proxy server is enough to access the all services of remote machine and its sub-Net Services
Disadvantage:
We need to configure the proxy settings in client application, if application doesn't support the proxy then we can't access the service.
Local port forwarding
Command: SSH-l 8000: localhost: 80 [email protected]
Here syntax is-L <local port >:< remote hostname >:< remote service port>
So when we execute the command, its bind port 8000 in client side. now what are request send to Port 6000 its redirect to SSH client, then encrypt and send to server machine. now server directly delivered the data to port 80.
In dynamic tunnel, server checks the packet and decide where we need to send the packet, for example http: // localhost/then its send to 80, SMTP then send to 25. but in local port it always send to single port which one we mention During setup the local port forwarding<Remote hostname >:< remote service port>
This time in Firefox no need to configure the proxy, clear the proxy (if its present)
And type in Address BarHttp: // localhost: 8000/
Then HTTP request is going to local port 8000, then its redirect and send to server. server directly send to port 80. (ITS Based on<Remote hostname >:< remote service port>, Here nowlocalhost: 80)
If u want to access the 10.10.1.11 machine HTTP service then we need to setup new local port forwarding.
We can't use prior port forwarding. Because old one always pointing to localhost: 80.
Command: Ssh-l 8000: 10.10.1.11: 80 [email protected]
Now client side local port 8000 is redirected to 10.10.1.11: 80 machine via 192.168.56.102.
In Firefox using http: // localhost: 8080/we can access the 10.10.1.11 machine HTTP service.
(For further reference check the video below. In video I show the how to access remote desktop using VNC)
Advantage:
No need to configure the proxy setting
Disadvantage:
Each service we need to setup different local port forwarding, (I. e for example, 2 port forwarding is needed to get 192.168.56.102 and 10.10.1.11 HTTP Services)
Remote port forwarding
Remote port forwarding is same like local port forwarding. But this time we need to set the port forwarding in server side (192.168.56.102), not Client Side
Command: SSH-r 8000: localhost: 80 [email protected]
Here very important changes is from server we going to connect client, so[Email protected]Here its 192.168.56.101 not 102.
When we execute this command its connect to client and create 8000 port inClient Side (not server side).
As usual client use its local port 8000 to connect server. Like local port forwarding.
Why remote port forwarding is important?
Its rarely used, when we worked the machine. that machine is inside the nat. so from outside no one can access it. that kind of situation physically access the machine and connect back to our client system (192.168.56.101) Using remote port forward. then from client machine we can access the services. (its seems to be little bit confusing, but its simple)
In Windows machine
In Windows machine if want to run SSH server then there is lots of SSH servers are available winsshd FreeSSHD, OpenSSH. If we want only SSH client then we can use putty.
Suppose in system (192.168.56.101) I am using Windows then how we access the SSH tunneling.
Open the putty and type the host [email protected] and select connection-> ssh-> tunnels (see the image), then type8080In source port and selectDynamic, If u want dynamic tunnel (its equal to-D 8080 in Command Option)
If we want local port forwarding then put any unused Port6000In source port and destination isLocalhost: 80And selectLocal.(Its equal to-l 6000: localhost: 80)
If u want to access 10.10.1.11 machine the Remote Desktop then make local port forward (check above image) and destination is10.10.1.11: 5900Here 5900 is the port used by VNC server. Then using any VNC client (here I used TightVNC client) to connect ur local port 6000
So mentionLocalhost: 6000,Here we need to mention double colon::, Because in VNC client port no is specified in this manner. (check the video)
Bypass firewall
This SSH tunnel concept can be used to bypass firewall. lets consider the scenario. in server side its Enable Firewall and some services are running and these services are can access through localhost or same machine. but u can't access through remote machine. because Firewall Block the all the port except T ssh port (22 ).
This kind of situation we use tunnel and almost all services can be accessed from outside. because firewall is rule based. in Firewall perspective all traffic is goes through port 22. so its allowed. but internally we made tunnel and access all services.
How to Prevent SSH Tunnel
Open the SSH server Config File/Etc/ssh/sshd_configThen set the Parameter
Allowtcpforwarding No
Then restart SSH service. Then they not allow the SSH tunnel service, but still we can access SSH Shell service.
But in SSH man page they clearly mentioned
"Note that disabling TCP forwarding does not improve security unless users are also denied shell access, as they can always install their own forwarders ."
So still we can use SSH tunnel even though we setAllowtcpforwarding No.(I will show in my next post)
I hope everything is clear. If any misunderstanding please let me know.