Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Protecting an SSH Service Using HAProxy

HAProxy isn't just for securing HTTP; it can also be used to protect TCP-based services as well. One service that, when exposed to the Internet, is always under constant attack is SSH. In this lab, we're going to get hands-on with HAProxy, using it to protect our SSH service. We'll proxy our SSH service. Then we'll set some connection boundaries in HAProxy, which should cut down on malicious traffic to our SSH service. Upon completion of the lab, you will be able to configure an HAProxy installation to protect a TCP-based service.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Jul 01, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Protect an SSH Service with HAProxy

    Before we get started, let's confirm that our SSH service is working.

    • Check our containers using podman
    • Try pulling a file from our SSH container, directly, on the HAProxy server, using scp:
      • StrictHostKeyChecking=no
      • UserKnownHostsFile=/dev/null
      • port=2223
      • [email protected]
      • file: /sshfiles/ssh-test.txt

    Securing Our SSH Service Using HAProxy

    In order to secure our SSH service, we need to add a frontend and a backend to /etc/haproxy/haproxy.cfg.

    Let's make some changes to our /etc/haproxy/haproxy.cfg file

    • Add a frontend for SSH named ssh-in:

      • bind port 2222 to all addresses
      • tcp mode
      • use the sshd1 backend
    • Add a backend for SSH named sshd1:

      • tcp mode
      • one server, sshd1-server1
        • localhost, port 2223
        • add a check for it
    • Restart the haproxy service

    • Try pulling a file from our SSH container, via HAProxy, on the HAProxy server, using scp

      • StrictHostKeyChecking=no
      • UserKnownHostsFile=/dev/null
      • port=2222
      • [email protected]
      • file: /sshfiles/ssh-test.txt

    Running Some Basic Tests

    Before we get started with protecting our sites with HAProxy, let's take a look at what a stock HAProxy configuration looks like when presented with a large number of requests.

    We'll open our web browser, connect to port 8050 on our public IP/DNS, and go get the stats information for our HAProxy installation.

    Let's generate some SSH traffic on our local HAProxy host.

    • Use a for loop to launch 1000 scp operations
      • Put each in the background
      • Try pulling a file from our SSH container, via HAProxy, on the HAProxy server, using scp:
        • StrictHostKeyChecking=no
        • UserKnownHostsFile=/dev/null
        • port=2222
        • [email protected]
        • file: /sshfiles/ssh-test.txt

    We can see traffic moving through our ssh-in frontend and sshd1 backend. We're ready to start setting some boundaries on SSH connections.

    Setting Some SSH Restrictions

    We just dropped 1,000 SSH connections on our SSH service in short order. Even though our service has no problem handling that from a single client, it's not a normal use case. We'd like to set some connection restrictions on our SSH service, so it can't be overwhelmed by a single client.

    We're going to create a new backend to hold a stick-table to track SSH connections by client, named ssh_per_ip_connections:

    • type ip
    • size 1m
    • expire 1m
    • store
      • conn_cur
      • conn_rate(1m)

    We'll add the following to our SSH frontend: Use the TCP log format, set a 1 minute client timeout, and track our SSH connections in the ssh_per_ip_connections stick-table we created. We'll reject connections over 2 per client or if there are more than 10 connections in the span of 1 minute.

  2. Challenge

    Test Your SSH Attack Protection

    Testing Our SSH Restrictions

    Now that we've set some boundaries on SSH connections, let's test our work!

    Before we proceed, let's restart the haproxy service to pick up our configuration changes and reset our statistics.

    Let's generate some SSH traffic on our local HAProxy host

    • Use a for loop to launch 1000 scp operations
      • Put each in the background
      • Try pulling a file from our SSH container, via HAProxy, on the HAProxy server, using scp
        • StrictHostKeyChecking=no
        • UserKnownHostsFile=/dev/null
        • port=2222
        • [email protected]
        • file: /sshfiles/ssh-test.txt

    Looking at the HAProxy stats web interface using a web browser, we can see that 2 connections succeeded, and 998 failed. Things are working!

    Let's give our HAProxy server a minute to recover, then try some serial SSH connections.

    • Try pulling a single file from our SSH container, via HAProxy, on the HAProxy server, using scp
      • StrictHostKeyChecking=no
      • UserKnownHostsFile=/dev/null
      • port=2222
      • [email protected]
      • file: /sshfiles/ssh-test.txt

    Repeating this, one scp at a time, until we are blocked, we can go until we hit our rate limit of 10 sessions per minute. Our SSH service is protected!

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans