- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
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.
Lab 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/hostsfile 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.comBefore 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
8050on our public IP/DNS. -
Get the
statsinformation for our HAProxy installation. -
Use ApacheBench (
ab) to send a total of100000requests, with100concurrent requests to bothwww.site1.comandwww.site2.comusinghttpson each of our server instances.Checking our
statsweb 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.cfgfile:- Add a new
backendto put ourstick-tablein.- Create a
sitck-tableto track connections:- type ip
- size 1m
- expire 10m
- http_req_rate(10s)
- Create a
- Add a line to our
frontendblock to feed the connection data to theper_ip_ratesbackend. - After the line you just added to your
frontend, add a line to start denying requests with an HTTP429 Too Many Requestsresponse when the counters in thestick-tableare over 10 requests per second. - Add a line to deny requests with an HTTP
500 Internal Server Errorresponse for agents reporting ascurl. - Add a line to deny requests with an HTTP
503 Service Unavailableresponse for agents in the/etc/haproxy/blocked.aclfile.
- Add a new
- Modify the
/etc/haproxy/blocked.aclfile.- 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
statsand restart our ApacheBench instances.- Kill ApacheBench on all 3 hosts.
- Restart the
haproxyservice.
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 of100000requests, with100concurrent requests to bothwww.site1.comandwww.site2.comusinghttps. - Check the
statsweb interface again and see the effects of our changes.
Test Blocking Requests Using ACLs
We had blocked the
curlagent and all requests from the Client 2 instance. Let's check our work!- Try using
curlon the HAProxy host to load our sites. - Try using
wgeton the HAProxy host to load our sites. - Try using
wgeton the Client 1 host to load our sites. - Try using
wgeton the Client 2 host to load our sites.
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.