Benefits and Limitations of Using SSH Tunneling. How to Use SSH Tunneling

Benefits and Limitations of Using SSH Tunneling. How to Use SSH Tunneling

Daily short news for you
  • For a long time, I have been thinking about how to increase brand presence, as well as users for the blog. After much contemplation, it seems the only way is to share on social media or hope they seek it out, until...

    Wearing this shirt means no more worries about traffic jams, the more crowded it gets, the more fun it is because hundreds of eyes are watching 🤓

    (It really works, you know 🤭)

    » Read more
  • A cycle of developing many projects is quite interesting. Summarized in 3 steps: See something complex -> Simplify it -> Add features until it becomes complex again... -> Back to a new loop.

    Why is that? Let me give you 2 examples to illustrate.

    Markdown was created with the aim of producing a plain text format that is "easy to write, easy to read, and easy to convert into something like HTML." At that time, no one had the patience to sit and write while also adding formatting for how the text displayed on the web. Yet now, people are "stuffing" or creating variations based on markdown to add so many new formats that… they can’t even remember all the syntax.

    React is also an example. Since the time of PHP, there has been a desire to create something that clearly separates the user interface from the core logic processing of applications into two distinct parts for better readability and writing. The result is that UI/UX libraries have developed very robustly, providing excellent user interaction, while the application logic resides on a separate server. The duo of Front-end and Back-end emerged from this, with the indispensable REST API waiter. Yet now, React doesn’t look much different from PHP, leading to Vue, Svelte... all converging back to a single point.

    However, the loop is not bad; on the contrary, this loop is more about evolution than "regression." Sometimes, it creates something good from something old, and people rely on that goodness to continue the loop. In other words, it’s about distilling the essence little by little 😁

    » Read more
  • Alongside the official projects, I occasionally see "side" projects aimed at optimizing or improving the language in some aspects. For example, nature-lang/nature is a project focused on enhancing Go, introducing some changes to make using Go more user-friendly.

    Looking back, it resembles JavaScript quite a bit 😆

    » Read more

Introduction

Sometimes, we may hesitate about whether it is convenient to expose the port of a database or any other service to the Internet for remote connection and management. Exposing a port like this is similar to letting thieves see the door and lock of a house; all they need is a skilled "locksmith" to unlock it sooner or later.

We all know that using just a username and password is not enough for a secure system. There are many ways for users to unknowingly lose their credentials. Nowadays, login systems have added features such as two-factor authentication, one-time password (OTP) login, or even login using secret keys created by complex algorithms to provide better security for users.

SSH (Secure Shell) is a protocol used for remote login and administration of devices, as well as for transferring files over unreliable networks. It is safe to say that SSH is the most widely used protocol for logging in and controlling another computer over the Internet. SSH becomes powerful because, in addition to using usernames and passwords, it also supports the use of SSH keys, making it extremely difficult to crack.

When we have an SSH session, we have the privilege to operate the server with the same permissions as the user set up in the system. This includes mapping any TCP/IP port of the server to the personal computer. This feature is called SSH Tunneling, and in this article, we will learn more about SSH Tunneling.

What is SSH Tunneling?

The term "tunnel" in English refers to an underground passage. True to its name, SSH Tunneling diggs a "tunnel" through SSH.

Using SSH Tunneling, we can forward any TCP/IP port from the server to the client and secure that connection.

For example, suppose we have a server A at IP address x.x.x.x that has MySQL Server installed using port 3306. A has configured not to expose port 3306 to the outside world. This means that only A can connect to the MySQL server.

However, using SSH Tunneling to dig a "tunnel" to forward port 3306 of A to some port on machine B is entirely feasible. In other words, we can indirectly connect to A through B.

To do this, of course, we must have an SSH session to A in order to establish the Tunneling.

Benefits of Using SSH Tunneling

SSH Tunneling has certain benefits for different user groups.

For individual users, SSH Tunneling can be a solution to connect to remote server applications quickly and easily while still ensuring security over untrusted networks. No need to open ports on the Internet, no need to set up a virtual private network...

For enterprise environments, SSH Tunneling is widely used in computer systems through software built on top of it. But overall, its purpose is still to connect applications within a computer system [reference].

Limitations

While the network benefits are significant, SSH Tunnels come with risks.

SSH connections are heavily encrypted, which unintentionally makes it difficult for network monitoring tools to observe the data inside the "tunnel". If an attacker takes advantage of this to steal data, it can be very dangerous.

Using SSH Tunneling, it is easy to set up port forwarding to bypass blocked ports from the firewall to another machine. Most firewalls provide little or no protection against this.

Because SSH tunnel setup is simply done with a single command, malware can silently install code to execute on the server for malicious purposes.

SSH Tunnel attacks also aim to be anonymous. Attackers who somehow gain access to the victim's servers will create a Tunneling and control them remotely. These servers will be used for large-scale attacks, such as DDOS. In fact, there have been reports of millions of IoT devices being attacked and exploited using this method [reference].

Overcoming Limitations

To prevent the risks caused by SSH Tunneling, operators need to have the ability to monitor, control, and inspect encrypted SSH connections. Proper configuration and enhanced security of the operating system on IoT devices are also necessary.

Setting up SSH Tunnels

Local Forwarding

Suppose you want to forward port 3306 of server A with IP address x.x.x.x to port 3307 of machine B with IP address z.z.z.z:

$ ssh -L 3307:z.z.z.z:3306 [email protected]

With [email protected] being the command to log into the server using SSH.
Now, any connection to z.z.z.z:3307 will be equivalent to x.x.x.x:3306.

Similarly, if you want to forward port 3306 of server A to your local computer:

$ ssh -L 3307:127.0.0.1:3306 [email protected]

By default, Tunneling commands will keep the session, and you will see the terminal holding a connection to the server. To run Tunneling in the background, add the -N -f options:

$ ssh -L 3307:z.z.z.z:3306 -N -f [email protected]

Remote Forwarding

Suppose you want to forward port 80 of server A with IP address x.x.x.x to port 8080 on your local machine:

$ ssh -R 80:localhost:8080 [email protected]

With [email protected] being the information to log into the server using SSH.

By default, Remote Forwarding is turned off on the server. To use this feature, you need to change the GatewayPorts no configuration to GatewayPorts yes in the regular SSH configuration file sshd_config located at /etc/ssh/sshd_config.

Alternatively, set GatewayPorts clientspecified to specify a new IP address with forwarding rights.

$ ssh -R z.z.z.z:80:localhost:8080 [email protected]

This only allows IP z.z.z.z to forward port 80 of A to port 8080 on the local machine.

Conclusion

SSH Tunneling, or port-forwarding, is a solution to forward the port of one machine to another machine through an SSH-dug "tunnel", ensuring high reliability.

SSH Tunneling brings many benefits but also comes with risks. Preventing these risks requires system operators with the ability to monitor the system.

Setting up SSH Tunnels is straightforward, with just a single command.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...