Tunneling traffic over SSH

How to securely publish applications on the Internet with SSH

Introduction

In today’s interconnected world, ensuring the security and privacy of data transmission is paramount. Enter Secure Shell (SSH) tunneling, a robust technique that guarantees encrypted communication and offers many practical applications. In this article, we will dive deep into the world of SSH tunneling, unraveling its mechanics and exploring its diverse range of applications.

Understanding SSH Tunneling

SSH tunneling, also known as SSH port forwarding, securely transmits data between a local machine and a remote server through an encrypted SSH connection. This technique leverages the security features of SSH, a protocol originally designed for secure remote shell access, to create a secure “tunnel” through which data can flow seamlessly.

There are three primary types of SSH tunneling: local port forwarding, remote port forwarding, and dynamic port forwarding. Each type serves a specific purpose and can be a powerful tool in various scenarios.

1. Local Port Forwarding

Local port forwarding enables you to access a service on a remote server as if it were running locally on your machine. This is particularly useful when a service is not directly accessible due to network restrictions or firewalls. By creating a tunnel from a local port to the remote server’s port, you can bypass these barriers and securely interact with the service.

Consider a scenario where you need to restrict access to a certain admin page. With local port forwarding, you can create a tunnel to a remote server and route your web traffic through it, effectively securing and accessing the access to the admin console.

The syntax is ssh -L localhost:localport:remoteserver:remoteport ssh_user@ssh_address, you can combine with the -N switch to not execute a remote command and -i to specify a private key.

# Example for a service running on a linux machine only accessible on 127.0.0.1:5001
# We ssh to 192.168.1.73, the IP address of that machine, and create a tunnel from our local port 5001 to the remote port 5001

ssh -N -i .\id_ed25519 -L 127.0.0.1:5001:127.0.0.1:5001 [email protected]

# We can now access the service on our local machine on 127.0.0.1:5001

2. Remote Port Forwarding

Remote port forwarding operates in the opposite direction. It allows you to expose a service running on your local machine to a remote server’s network. This can be immensely useful for scenarios where you need to share resources with a remote system or provide access to a service that resides on your machine. This requires GatewayPorts yes in sshd_config, which is by default set to no.

Imagine you’re a developer working on a local web server, and you want a colleague to test the application remotely. By utilizing remote port forwarding, you can create a secure tunnel to your colleague’s machine, granting them access to your locally hosted application as if it were on their own system.

The syntax is ssh -R sourceport:forwardToHost:OnPort ssh_user@ssh_address. You can combine with the -N switch to not execute a remote command.

# Example for a newly developped website on running on the local machine on port 8080
# We ssh to 192.168.1.73, the IP address of the machine we want to publish the website on, and create a tunnel from the remote port 80 to our local port 8080
ssh -R 8080:localhost:80 [email protected]

# We can now access the website on the remote machine on port 80

3. Dynamic Port Forwarding (SSH SOCKS Proxy)

Dynamic port forwarding involves creating a SOCKS proxy on your local machine via the remote server. This proxy can route various types of traffic through the secure tunnel, making it ideal for secure browsing and accessing services over an encrypted connection.

In a coffee shop or airport with an unsecured Wi-Fi network, dynamic port forwarding shines. By setting up a dynamic tunnel, you can route all your internet traffic through the SSH connection, ensuring that your data remains encrypted and secure even on a potentially risky network.

The syntax is ssh -D port ssh_user@ssh_address. You also need to configure your browser to use the SOCKS proxy on ssh_host:port

# For example, we need to access a site that's only accessible from within the corporate network. 
# We can create a dynamic tunnel to a server in the corporate network and route our traffic through it.

ssh -D 1080 bastion.coporate.net

# After configuring our browser to use the SOCKS proxy on bastion.coporate.net:1080, we can now access the sites as we would from the bastion host.

Note on securing SSH daemons

Now that we discussed the different types of SSH tunneling, let’s take a moment to discuss securing SSH daemons.

Securing SSH is crucial to prevent unauthorized access and protect sensitive data. To enhance SSH security, follow these essential practices. First, disable SSH protocol versions 1 as they are vulnerable. Implement strong authentication methods such as public key authentication and two-factor authentication, avoiding password-only authentication. Regularly update your SSH server software and the underlying operating system to patch vulnerabilities. Employ firewall rules to restrict SSH access to trusted IP addresses only. Set up Fail2ban or similar tools to automatically block repeated failed login attempts. Enforce strong password policies, and consider using passphrase-protected private keys for added security. Finally, monitor SSH logs for suspicious activity, and adhere to security best practices to ensure a robust and fortified SSH environment. One could also configure SSH to listen on a non-default port to deter automated attacks, although this is security through obscurity.

Bonus: Configure fail2ban

You might want to read my previous article on Fail2ban. In short, Fail2ban is a tool that scans log files for failed login attempts and blocks the IP address after a certain threshold. This is useful to prevent brute-force attacks on SSH, but also on other services like web servers.

Avatar
Sven de Windt
Systems Administrator

Systems engineer with an intrest in automation

Related

comments powered by Disqus