Protecting your web server against different kinds of attacks is a crucial responsibility for any system administrator. ModEvasive is an Apache web server module that helps you to protect your web server in the event of DoS, DDoS, and brute-force attacks. These types of attacks cause the server to run out of memory, crashing your website.
The mod_evasive module works by creating a hash table of IP Addresses and URIs and monitoring for suspicious incoming server requests, such as:
- Making more than 100 concurrent connections per second.
- Requesting the same page several times per second.
If such a suspicious request occurs, the mod_evasive module sends a 403 error and blocks the IP address.
In this tutorial, we will show you how to install and configure mod_evasive with Apache on an Ubuntu 18.04 server.
- A fresh Ubuntu 18.04 VPS on the Atlantic.Net Cloud Platform.
- A static IP address configured on your server.
Step 1 – Create Atlantic.Net Cloud Server
First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 18.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.
Once you are logged into your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.
apt-get update -y
Step 2 – Install mod_evasive
Before starting, Apache webserver needs to be installed on your server. If not installed, you can install it with the following command:
apt-get install apache2 apache2-utils -y
Once Apache web server is installed, you can install mod_evasive with the following command:
apt-get install libapache2-mod-evasive -y
During the installation, you will be asked to configure a Postfix mail server for email notification. You can choose your desired option to complete the installation. If you are unsure, just choose local only or no configuration.
After installing mod_evasive, you can verify whether the mod_evasive module is enabled by running the following command:
apachectl -M | grep evasive
You should get the following output:
[Mon Jan 27 13:55:35.707317 2020] [so:warn] [pid 29031] AH01574: module dav_module is already loaded, skipping evasive20_module (shared)
At this point, the mod_evasive module is installed and enabled. You can now proceed to the next step.
Step 3 – Configure mod_evasive
The default configuration file of mod_evasive is located at /etc/apache2/mods-enabled/evasive.conf. You will need to configure this file per your requirements.
You can open this file using the nano editor as shown below:
Change the file as shown below. We recommend amending DOSEmailNotify to the address you want the email sent to (if configured) and DOSSystemCommand – for example “su – richard -c ‘/sbin… %s …’”
<IfModule mod_evasive20.c> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 100 DOSEmailNotify [email protected] DOSSystemCommand "su - someuser -c '/sbin/... %s ...'" DOSLogDir "/var/log/mod_evasive" </IfModule>
Save and close the file when you are finished.
A brief explanation of each option is shown below:
- DOSHashTableSize: mod_evasive uses this option to control the hash table size. It is recommended to increase this if you have a busy web server.
- DOSPageCount: This option specifies the threshold limit for the number of requests allowed to the same URI per second. Once the threshold limit has been exceeded, the client’s IP address will be blacklisted.
- DOSSiteCount: This option specifies the limit on the total number of requests allowed to the same IP address.
- DOSPageInterval: This option specifies the page count interval.
- DOSSiteInterval: This option specifies the site count interval.
- DOSBlockingPeriod : This option defines the amount of time in seconds that a client will be blocked.
- DOSEmailNotify: This option sends an email to the specified address when an IP address has been blacklisted.
- DOSSystemCommand: Whenever an IP address has been blacklisted, the specified system command will be executed.
- DOSLogDir: This option defines the mod_evasive log directory.
Next, create a directory to store the mod_evasive log and change its ownership to www-data with the following command:
mkdir /var/log/mod_evasive chown -R www-data:www-data /var/log/mod_evasive
Finally, restart the Apache service to implement the changes:
systemctl restart apache2
Step 4 – Test mod_evasive
At this point, the mod_evasive module is installed and configured. It’s time to test whether the module is working correctly.
Go to the remote system and send a bulk page request to the server using the ab command:
ab -n 1000 -c 20 http://your-server-ip/
This command will cause the equivalent of a DoS attack by sending 1000 page requests in 10 concurrent connections.
On the server, check the mail log by running the following command:
tail -15 /var/mail/root
You should see that the client IP address has been blacklisted by mod_evasive:
Received: by ubuntu1804 (Postfix, from userid 33) id B0C3EC1753; Mon, 27 Jan 2020 14:15:09 +0000 (UTC) To: [email protected] MIME-Version: 1.0 Content-Type: text/plain; charset="ANSI_X3.4-1968" Content-Transfer-Encoding: 8bit Message-Id: <[email protected]> Date: Mon, 27 Jan 2020 14:15:09 +0000 (UTC) From: www-data <[email protected]> To: [email protected] Subject: HTTP BLACKLIST 22.214.171.124 mod_evasive HTTP Blacklisted 126.96.36.199
You can also test mod_evasive using the test.pl built-in script. You will need to modify this script to make it works.
You can edit the script as shown below:
Find the following line:
print $SOCKET "GET /?$_ HTTP/1.0\n\n";
Replace it with the following:
print $SOCKET "GET /?$_ HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n";
Save and close the file when you are finished. Then, run the script using the perl command:
If everything works correctly, you should get the following output:
HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden HTTP/1.1 403 Forbidden
Congratulations! The mod_evasive module is now configured to protect your server against DDoS and Brute force attacks.