- Lab
- A Cloud Guru
Protecting an HTTP Service Using HAProxy
One common attack vector is our websites or web-based applications. HAProxy can help us fend off HTTP floods, Slowloris attacks, and more. In this lab, we’re going to get hands-on with HAProxy, using it to protect our HTTP connections. We’re going to secure our HTTP connections against HTTP floods, and block `curl` agents and clients by IP address. Upon completion of the lab, you will be able to configure an HAProxy installation to protect HTTP-based services.
Path Info
Table of Contents
-
Challenge
Protect an HTTP Service with HAProxy
Running Basic HAProxy Server Tests
First, we need to add 2 entries to the
/etc/hosts
file on each of our client machines. We need these to point to the private IP address of the HAProxy server:<HAPROXY SERVER PRIVATE IP> www.site1.com <HAPROXY SERVER PRIVATE IP> www.site2.com
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.
-
Open the web browser and connect to port
8050
on our public IP/DNS. -
Get the
stats
information for our HAProxy installation. -
Use ApacheBench (
ab
) to send a total of100000
requests, with100
concurrent requests to bothwww.site1.com
andwww.site2.com
usinghttps
on each of our server instances.Checking our
stats
web interface, we can see the traffic coming in from both local instances of ApacheBench.
Defending Against HTTP Flood Attacks
We can set request rate limits to block a single agent by tracking the number or rate of requests from a single client using a
stick-table
.- Edit the
/etc/haproxy/haproxy.cfg
file:- Add a new
backend
to put ourstick-table
in.- Create a
sitck-table
to track connections:- type ip
- size 1m
- expire 10m
- http_req_rate(10s)
- Create a
- Add a line to our
frontend
block to feed the connection data to theper_ip_rates
backend
. - After the line you just added to your
frontend
, add a line to start denying requests with an HTTP429 Too Many Requests
response when the counters in thestick-table
are over 10 requests per second. - Add a line to deny requests with an HTTP
500 Internal Server Error
response for agents reporting ascurl
. - Add a line to deny requests with an HTTP
503 Service Unavailable
response for agents in the/etc/haproxy/blocked.acl
file.
- Add a new
- Modify the
/etc/haproxy/blocked.acl
file.- Add a line with the private IP address of the Client 2 host.
-
-
Challenge
Test HTTP Attack Protection
Before we see what the effects of our changes are, let's reset our
stats
and restart our ApacheBench instances.- Kill ApacheBench on all 3 hosts.
- Restart the
haproxy
service.
Test Blocking Requests Based On Rate Limits
- To test our blocking requests, generate a new batch of traffic on the Client 1 instance only. Use ApacheBench (
ab
) to send a total of100000
requests, with100
concurrent requests to bothwww.site1.com
andwww.site2.com
usinghttps
. - Check the
stats
web interface again and see the effects of our changes.
Test Blocking Requests Using ACLs
We had blocked the
curl
agent and all requests from the Client 2 instance. Let's check our work!- Try using
curl
on the HAProxy host to load our sites. - Try using
wget
on the HAProxy host to load our sites. - Try using
wget
on the Client 1 host to load our sites. - Try using
wget
on the Client 2 host to load our sites.
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.