SSH tunneling explained

Source: Internet
Author: User
Tags ssh access ssh server

SSH tunneling explained

Recently I wanted to set up a Remote Desktop Sharing Session from home PC to my laptop. while going through the set up guide I Came authentication SS SSH tunneling. even though there are except articles on the subject still it took me a considerable amount of Googling, some experimenting and couple of Wireshark sessions to grasp what's going under the hood. most of the guides were incomplete in terms of explain Ing the concept which left me desiring for a good article on the subject with some explanatory mappings strations. So I decided to write it my self. So here goes...Introduction

A ssh tunnel consists of an encrypted tunnel created through a SSH protocol
Connection. a ssh tunnel can be used to transfer unencrypted traffic over
Network through an encrypted channel. For example we can use a SSH tunnel
Securely transfer files between a FTP server and a client even though the ftp
Protocol itself is not encrypted. SSH tunnels also provide a means to bypass firewils that prohibits or filter certain Internet services. for example an organization will block certain sites using their proxy filter. but users may not wish to have their web traffic
Monitored or blocked by the organization proxy filter. If users can connect
An external SSH server, they can create a SSH tunnel to forward a given port on
Their local machine to port 80 on remote web-server via the external SSH
Server. I will describe this scenario in detail in a little while.

To set up a SSH tunnel a given port of one machine needs to be forwarded (
Which I am going to talk about in a little while) to a port in the other
Machine which will be the other end of the tunnel. Once the SSH tunnel has been
Established, the user can connect to earlier specified port at first machine
Access the network service.Port forwarding

SSH tunnels can be created in several ways using different kinds of port forwarding
Machisms. ports can be forwarded in three ways.

1. Local port forwarding

2. Remote port forwarding

3. Dynamic port forwarding

I didn't explain what port forwarding is. I found Wikipedia's definition more explanatory.

Port forwarding or port mapping is a name given to the Combined Technique

1. Translating the address and/or port number of a packet to a new destination

2. Possibly accepting such packet (s) in a packet filter (firewall)

3. forwarding the packet according to the routing table.

Here the first technique will be used in creating an SSH tunnel. when a client application connects to the local port (local endpoint) of the SSH tunnel and transfer data these data will be forwarded to the remote end by translating the host and port values to that of the remote end of the channel.

So with that let's see how SSH tunnels can be created using forwarded ports with an examples.Tunnelling with local port forwarding

Let's say that Yahoo.com is being blocked using a proxy filter in the university.
(For the sake of this example. :). cannot think any valid reason why Yahoo wocould be blocked ). a ssh tunnel can be used to bypass this restriction. let's name my machine at the university as 'work' and my home machine as 'home '. 'home' needs to have a public IP for this to work. and I am running a SSH server on my home machine. following dimo-strates the scenario.

To create the SSH tunnel execute following from 'work' machine.

1

ssh -L 9001:yahoo.com:80 home

The 'l' switch indicates that a local port forward is need to be created. The switch syntax is as follows.

1

-L <local-port-to-listen>:<remote-host>:<remote-port>

Now the SSH client at 'work' will connect to SSH server running at 'home' (usually running at port 22) binding port 9001 of 'work' to listen for local requests thus creating a SSH tunnel between 'Home' and 'work '. at the 'Home' end it will create a connection to 'yahoo. com 'at port 80. so 'work' doesn' t need to know how to connect to Yahoo.com. only 'home' needs to worry about that. the channel between 'work' and 'home' will be encrypted while the connection between 'Home' and 'yahoo. com 'will be unencrypted.

Now it is possible to browse Yahoo.com by visiting http: // localhost: 9001 in the Web browser at 'work' computer. the 'home' computer will act as a gateway which wowould accept requests from 'work' machine and fetch data and tunnelling it back. so the syntax of the full command wocould be as follows.

1

ssh -L <local-port-to-listen>:<remote-host>:<remote-port> <gateway>

The image below describes the scenario.

Here the 'host' to 'yahoo. com' connection is only made when browser makes
Request not at the tunnel setup time.

It is also possible to specify a port in the 'Home' computer itself instead
Connecting to an external host. This is useful if I were to set up a VNC session
Between 'work' and 'home'. Then the command line wocould be as follows.

1

ssh -L 5900:localhost:5900 home (Executed from ‘work‘)

So here what does localhost refer? Is it the 'work' since the command line is executed from 'work '? Turns out that it is not. as explained earlier is relative to the Gateway ('home' in this case), not the machine from where the tunnel is initiated. so this will make a connection to port 5900 of the 'Home' computer where the VNC client wocould be listening in.

The created tunnel can be used to transfer all kinds of data not limited to Web browsing sessions. we can also tunnel SSH sessions from this as well. let's assume there is another computer ('banned') to which we need to SSH from within university but the SSH access is being blocked. it is possible to tunnel a SSH session to this host using a local port forward. the setup wocould look like this.

As can be seen now the transferred data between 'work' and 'banne' are encrypted end to end. For this we need to create a local port forward as follows.

1

ssh -L 9001:banned:22 home

Now we need to create a SSH session to local port 9001 from where the session
Will get tunneled to 'banned' via 'home' computer.

1

ssh -p 9001 localhost

With that let's move on to next type of SSH tunnelling method, reverse tunnelling.Reverse tunnelling with remote port forwarding

Let's say it is required to connect to an internal university website from home.
The university firewall is blocking all incoming traffic. How can we connect from 'home' to internal network so that we can browse the internal site? A VPN setup is a good candidate here. However for this example let's assume we don't have this facility. Enter SSH reverse tunnelling ..

As in the earlier case we will initiate the tunnel from 'work' computer behind the firewall. this is possible since only incoming traffic is blocking and outgoing traffic is allowed. however instead of the earlier case the client will now be at the 'Home' computer. instead of-L option we now define-R which specifies
A reverse tunnel need to be created.

1

ssh -R 9001:intra-site.com:80 home (Executed from ‘work‘)

Once executed the SSH client at 'work' will connect to SSH server running at home creating a SSH channel. then the server will bind port 9001 on 'home' machine to listen for incoming requests which wowould subsequently be routed through the created SSH channel between 'Home' and 'work '. now it's possible to browse the internal site
By visiting http: // localhost: 9001 in 'home' web browser. the 'work' will then create a connection to intra-site and relay back the response to 'home' via the created SSH channel.

As nice all of these wowould be still you need to create another tunnel if you need to connect to another site in both cases. wouldn't it be nice if it is possible to proxy traffic to any site using the SSH channel created? That's what dynamic port forwarding is all about.Dynamic port forwarding

Dynamic port forwarding allows to configure one local port for tunnelling data to all remote destinations. however to utilize this client application connecting to local port shocould send their traffic using the SOCKS protocol. at the client side of the tunnel a SOCKS proxy wocould be created and the application (eg. browser) uses the SOCKS Protocol to specify where the traffic shoshould be sent when it leaves the other end of the SSH tunnel.

1

ssh -D 9001 home (Executed from ‘work‘)

Here SSH will create a SOCKS proxy listening in for connections at local port
9001 and upon authentication ing a request wocould route the traffic via SSH Channel
Created between 'work' and 'home'. For this it is required to configure
Browser to point to the socks proxy at port 9001 at localhost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.