DEV Community

Cover image for SSH Tunnels: The Secret Superpower of SSH
Arashad Dodhiya
Arashad Dodhiya

Posted on

SSH Tunnels: The Secret Superpower of SSH

When I first heard the term SSH Tunneling, I imagined some kind of secret underground network built by hackers.

The reality is much simpler.

SSH Tunneling is basically creating a secure path between two computers and then sending other traffic through that path.

Think of it as a private tunnel that nobody else can easily see inside.

Once you understand that idea, everything else starts making sense.


First, What Is SSH?

SSH stands for:

Secure Shell
Enter fullscreen mode Exit fullscreen mode

It allows you to remotely control another machine.

Example:

ssh user@server.com
Enter fullscreen mode Exit fullscreen mode

You connect to a remote server and get a terminal.

You can:

Run commands
Create files
Install software
Manage servers
Enter fullscreen mode Exit fullscreen mode

All securely encrypted.


The Normal SSH Connection

Suppose you have:

Laptop
   |
Internet
   |
Server
Enter fullscreen mode Exit fullscreen mode

You connect:

ssh user@server.com
Enter fullscreen mode Exit fullscreen mode

and get a terminal.

Simple.

Most people stop learning here.

But SSH can do much more.


The Secret Superpower

SSH can transport more than terminal commands.

It can transport:

Web traffic
Database traffic
Remote desktop traffic
Application traffic
Almost anything
Enter fullscreen mode Exit fullscreen mode

through the encrypted connection.

This is called:

SSH Tunneling
Enter fullscreen mode Exit fullscreen mode

Why Does SSH Tunneling Exist?

Imagine a company database.

The database runs on:

10.0.0.5
Port 3306
Enter fullscreen mode Exit fullscreen mode

The company does NOT want this database exposed to the internet.

So the firewall blocks everyone.

Good security.

But now a system administrator working remotely needs access.

What should they do?

Expose the database?

No.

Use SSH Tunneling.


Think Of A Tunnel Through A Mountain

Imagine two cities separated by a mountain.

Without a tunnel:

City A
   ^
 Mountain
   ^
City B
Enter fullscreen mode Exit fullscreen mode

Travel is difficult.

Now build a tunnel:

City A
   |
 Tunnel
   |
City B
Enter fullscreen mode Exit fullscreen mode

Everything passes through safely.

SSH Tunneling works exactly like that.


Local Port Forwarding (The Most Common Type)

This is the SSH tunnel most people encounter first.

Suppose:

Your Laptop
Enter fullscreen mode Exit fullscreen mode

connects to:

Server
Enter fullscreen mode Exit fullscreen mode

using SSH.

The server can access:

Database
10.0.0.5:3306
Enter fullscreen mode Exit fullscreen mode

that your laptop cannot reach directly.


Without SSH Tunnel

Laptop
   X
Database
Enter fullscreen mode Exit fullscreen mode

Connection blocked.


With SSH Tunnel

Laptop
   |
SSH Tunnel
   |
Server
   |
Database
Enter fullscreen mode Exit fullscreen mode

Now traffic can travel through the SSH connection.


Real Command

ssh -L 3306:10.0.0.5:3306 user@server.com
Enter fullscreen mode Exit fullscreen mode

Looks scary.

Let's decode it.


What Does This Mean?

-L
Enter fullscreen mode Exit fullscreen mode

means:

Local Port Forwarding
Enter fullscreen mode Exit fullscreen mode

This part:

3306
Enter fullscreen mode Exit fullscreen mode

means:

Open local port 3306 on my laptop.


This part:

10.0.0.5:3306
Enter fullscreen mode Exit fullscreen mode

means:

Forward traffic to the database server.


So the command really means:

"Anything I send to my laptop's port 3306 should travel through SSH and end up at 10.0.0.5:3306."


Visualizing It

Before:

Laptop
   X
Database
Enter fullscreen mode Exit fullscreen mode

After:

Laptop
   |
localhost:3306
   |
SSH Tunnel
   |
Server
   |
10.0.0.5:3306
Enter fullscreen mode Exit fullscreen mode

Now your laptop thinks the database is local.

Pretty cool.


A Web Server Example

Suppose a web application is running on:

127.0.0.1:8080
Enter fullscreen mode Exit fullscreen mode

inside a remote server.

Nobody else can access it.

Not even you.

Because it only listens locally.


SSH Tunnel:

ssh -L 9999:127.0.0.1:8080 user@server.com
Enter fullscreen mode Exit fullscreen mode

Now:

localhost:9999
Enter fullscreen mode Exit fullscreen mode

on your laptop becomes:

127.0.0.1:8080
Enter fullscreen mode Exit fullscreen mode

on the remote server.

Open browser:

http://localhost:9999
Enter fullscreen mode Exit fullscreen mode

and the website appears.

Magic?

No.

Tunnel.


Why Pentesters Love SSH Tunnels

Imagine you've compromised a machine inside a network.

That machine can access:

10.10.10.5
10.10.10.10
10.10.10.20
Enter fullscreen mode Exit fullscreen mode

Internal systems.

Your laptop cannot.


Without tunnel:

You
  X
Internal Network
Enter fullscreen mode Exit fullscreen mode

With tunnel:

You
 |
SSH Tunnel
 |
Compromised Host
 |
Internal Network
Enter fullscreen mode Exit fullscreen mode

Now you can interact with internal systems through the tunnel.

This concept becomes the foundation for:

Pivoting
Lateral Movement
Internal Enumeration
Post Exploitation
Enter fullscreen mode Exit fullscreen mode

in penetration testing.


Reverse SSH Tunnels

Sometimes the opposite problem exists.

Suppose a machine sits behind NAT.

Nobody can connect to it.

Example:

Home PC
Enter fullscreen mode Exit fullscreen mode

behind a router.

Direct SSH doesn't work.


Instead:

Home PC
      |
      | Outbound SSH
      |
VPS Server
Enter fullscreen mode Exit fullscreen mode

The home PC initiates the connection.

Then the VPS exposes a port.

This is called:

Reverse SSH Tunneling
Enter fullscreen mode Exit fullscreen mode

Very useful when direct connections are impossible.


The Hotel Analogy

Imagine a hotel.

The hotel represents the internet.

You need to secretly move packages between two rooms.

Instead of carrying them through public hallways, you build a private underground tunnel.

Nobody sees:

Where it came from
Where it went
What was inside
Enter fullscreen mode Exit fullscreen mode

The package simply appears on the other side.

That's SSH Tunneling.

A secure private pathway for traffic.


Common Beginner Mistake

Many beginners think:

SSH Tunneling gives you access to things you were never allowed to access.

Not exactly.

SSH Tunneling only allows you to use access that already exists.

If the remote server can reach something, you can potentially send traffic through that server.

The tunnel doesn't create access.

It transports traffic.


The One-Sentence Explanation

If you remember only one thing from this article, remember this:

SSH Tunneling allows you to securely send traffic through an SSH connection as if that traffic originated from the remote machine.

Once this idea clicks, concepts like VPNs, pivoting, jump hosts, remote administration, cloud infrastructure, and penetration testing become much easier to understand.

Because at its core, SSH Tunneling is just a secure traffic tunnel between two places.

Top comments (0)