{"id":1433,"date":"2017-05-23T16:12:08","date_gmt":"2017-05-23T20:12:08","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=1433"},"modified":"2017-08-21T10:25:20","modified_gmt":"2017-08-21T14:25:20","slug":"protection-for-apache-using-mod_dosevasive","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2017\/05\/23\/protection-for-apache-using-mod_dosevasive\/","title":{"rendered":"Protection for APACHE using mod_dosevasive"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1434 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/mod_evasive.png\" alt=\"\" width=\"785\" height=\"328\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/mod_evasive.png 785w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/mod_evasive-300x125.png 300w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/mod_evasive-768x321.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/p>\n<p>The <strong>mod_evasive<\/strong> Apache module, formerly known as <strong>mod_dosevasive<\/strong>, helps protect against DoS, DDoS (Distributed Denial of Service), and brute force attacks on the Apache web server. It can provide evasive action during attacks and report abuses via email and syslog facilities. The module works by creating an internal dynamic table of IP addresses and URIs as well as denying any single IP address from any of the following:<\/p>\n<ul>\n<li>Requesting the same page more than a few times per second<\/li>\n<li>Making more than 50 concurrent requests on the same child per second<\/li>\n<li>Making any requests while temporarily blacklisted<\/li>\n<\/ul>\n<p>If any of the above conditions are met, a 403 response is sent and the IP address is logged. Optionally, an email notification can be sent to the server owner or a system command can be run to block the IP address.<\/p>\n<h2 id=\"step-1-\u2014-installing-mod_evasive\">Step 1 \u2014 Installing mod_evasive<\/h2>\n<p>In this section, we will be installing the packages required for mod_evasive to function and finally install mod_evasive.<\/p>\n<p>First, we need to install the EPEL (Extra Packages for Enterprise Linux) yum repository on the server. EPEL is a Fedora Special Interest Group that creates, maintains, and manages a high quality set of open source add-on software packages for Enterprise Linux. Run the following command to install and enable the EPEL repository on your server:<\/p>\n<p>On CentOS 7:<\/p>\n<pre class=\"code-pre command\"><code>sudo rpm -ivh http:\/\/dl.fedoraproject.org\/pub\/epel\/7\/x86_64\/e\/epel-release-7-5.noarch.rpm<\/code><\/pre>\n<p>Let us verify that the EPEL repo is enabled using:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum repolist<\/code><\/pre>\n<p>If enabled, you will see the following repo listed in the output:<\/p>\n<pre class=\"code-pre \"><code>epel\/x86_64                                                            Extra Packages for Enterprise Linux 7 - x86_64\r\n<\/code><\/pre>\n<p>Now, let us protect the base packages from EPEL using the yum plugin <strong>protectbase<\/strong>.<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install yum-plugin-protectbase.noarch -y<\/code><\/pre>\n<p>The purpose of the <strong>protectbase<\/strong> plugin is to protect certain yum repositories from updates from other repositories. Packages in the protected repositories will not be updated or overridden by packages in non-protected repositories even if the non-protected repo has a later version.<\/p>\n<p>Now we are ready to install mod_evasive module. Run the following command to install it:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install mod_evasive -y<\/code><\/pre>\n<h2 id=\"step-2-\u2014-verifying-the-installation\">Step 2 \u2014 Verifying the Installation<\/h2>\n<p>Now that mod_evasive is installed, let&#8217;s verify that configuration file has been installed and that the module is being loaded.<\/p>\n<p>During installation, the mod_evasive configuration file <code>\/etc\/httpd\/conf.d\/mod_evasive.conf<\/code> was added. To verify this run:<\/p>\n<pre class=\"code-pre command\"><code>sudo ls -al \/etc\/httpd\/conf.d\/mod_evasive.conf<\/code><\/pre>\n<p>Output should look similar to:<\/p>\n<pre class=\"code-pre \"><code>-rw-r--r-- 1 root root 3473 Jul 21 01:41 \/etc\/httpd\/conf.d\/mod_evasive.conf<\/code><\/pre>\n<p>By default, the following <code>LoadModule<\/code> line will be added to the top of configuration file <code>mod_evasive.conf<\/code>. Open the file and add the line if it is not already present. This line tells the Apache web server to load and use the mod_evasive module.<\/p>\n<p>On CentOS 7, the line should read as follows:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>LoadModule evasive20_module modules\/mod_evasive24.so\r\n<\/code><\/pre>\n<p>Let us list the modules loaded for the Apache web server and look for mod_evasive:<\/p>\n<pre class=\"code-pre command\"><code>sudo  httpd -M | grep evasive<\/code><\/pre>\n<p>The output should show:<\/p>\n<pre class=\"code-pre \"><code>evasive20_module (shared)<\/code><\/pre>\n<h2 id=\"step-3-\u2014-configuring-mod_evasive\">Step 3 \u2014 Configuring mod_evasive<\/h2>\n<p>Now that the installation is complete and verified, let us look into the configuration of the module. mod_evasive can be easily customized through the <code>mod_evasive.conf<\/code> configuration file. We will discuss some of the configuration parameters in this tutorial. Please refer to the configuration file for information on all the parameters \u2014 it contains a description of each parameter.<\/p>\n<p>One of the configuration options you need to change is <code>DOSEmailNotify<\/code>. This is a very useful directive. If this value is set, an email will be sent to the email address specified whenever an IP address is blacklisted. The email body will show <code>mod_evasive HTTP Blacklisted <span class=\"highlight\">222.222.222.251<\/span><\/code><\/p>\n<p>For example, if you want to send mod_evasive alerts to say, <span class=\"highlight\">johndoh@example.com<\/span>, edit the file:<\/p>\n<pre class=\"code-pre command\"><code>sudo nano \/etc\/httpd\/conf.d\/mod_evasive.conf<\/code><\/pre>\n<p>Uncomment the <code>DOSEmailNotify<\/code> line by removing the <code>#<\/code> in front of the line, and change the email address to yours:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSEmailNotify   <span class=\"highlight\">youremail@yourdomain.com<\/span>\r\n<\/code><\/pre>\n<p><span class=\"note\"><strong>Note:<\/strong> mod_evasive uses <code>\/bin\/mail<\/code> for sending email alerts. You need to have a mail server installed and working<\/span>.<\/p>\n<p>Another parameter you might want to set is <code>DOSWhitelist<\/code>. Using this option, IP addresses of trusted clients can be added to the whitelist to ensure they are never denied. The purpose of whitelisting is to protect software, scripts, local search bots, or other automated tools from being denied for requesting large amounts of data from the server.<\/p>\n<p>To whitelist an IP address, for example 222.222.222.252, add an entry to the configuration file like this:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSWhitelist    222.222.222.252\r\n<\/code><\/pre>\n<p>Wildcards can be used on up to the last 3 octets of the IP address if necessary.<\/p>\n<p>To whitelist multiple IP addresses from different IP ranges, you can add separate DOSWhitelist lines in the configuration file like this:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSWhitelist    <span class=\"highlight\">222.222.222.251<\/span>\r\nDOSWhitelist    <span class=\"highlight\">222.222.222.222<\/span>\r\n<\/code><\/pre>\n<p><code>DOSPageCount<\/code> and <code>DOSSiteCount<\/code> are two other parameters recommended to be changed to less aggressive values to avoid clients getting blocked unnecessarily.<\/p>\n<p><code>DOSPageCount<\/code> is the limit for the number of requests for the same page per page interval (usually set to one second) by an IP address. Once the threshold for that interval has been exceeded, the IP address of the client will be added to the blocked list. The default value is set quite low at 2. You can change it to a higher value, say 20, by editing the following in <code>\/etc\/httpd\/conf.d\/mod_evasive.conf<\/code>:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSPageCount 20\r\n<\/code><\/pre>\n<p><code>DOSSiteCount<\/code> is the limit for the total number of requests for the same website by an IP address per site interval (defaults to 1 second). To change it to a larger value such as 100 seconds:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSSiteCount 100\r\n<\/code><\/pre>\n<p>There are a few other parameters you can change to achieve better performance.<\/p>\n<p>One is <code>DOSBlockingPeriod<\/code>, which is the amount of time (in seconds) that a client (IP address) will be blocked for if they are added to the blocked list. During this time, all subsequent requests from the client will result in a 403 (Forbidden) error and the timer being reset (defaults to 10 seconds).<\/p>\n<p>For example, if you want to increase the blocking period to 300 seconds:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSBlockingPeriod    300\r\n<\/code><\/pre>\n<p>Another is <code>DOSLogDir<\/code> which refers to the temporary directory used by mod_evasive. By default <code>\/tmp<\/code> will be used for a locking mechanism, which opens some security issues if your system is open to shell users. In the event you have non-privileged shell users, you will want to create a directory writeable only to the user Apache is running as (usually <strong>apache<\/strong>) then set this parameter in your mod_evasive.conf file.<\/p>\n<p>For example, to set the directory used by mod_evasive to <code>\/var\/log\/mod_evasive<\/code>, create the directory using:<\/p>\n<pre class=\"code-pre command\"><code>sudo mkdir \/var\/log\/mod_evasive<\/code><\/pre>\n<p>Then set the ownership to <code>apache<\/code> user:<\/p>\n<pre class=\"code-pre command\"><code>sudo chown -R apache:apache \/var\/log\/mod_evasive<\/code><\/pre>\n<p>Now edit the mod_evasive configuration and change the directory as follows:<\/p>\n<div class=\"code-label \" title=\"\/etc\/httpd\/conf.d\/mod_evasive.conf\">\/etc\/httpd\/conf.d\/mod_evasive.conf<\/div>\n<pre class=\"code-pre \"><code>DOSLogDir           \"\/var\/log\/mod_evasive\"\r\n<\/code><\/pre>\n<p>Another parameter is <code>DOSSystemCommand<\/code>. If a value is set, the command specified will be executed whenever an IP address is blacklisted. Using this parameter, you can integrate mod_evasive with the firewall installed on your server or a shell script and block the IP addresses blacklisted by mod_evasive in the firewall.<\/p>\n<h2 id=\"step-4-\u2014-loading-the-mod_evasive-module\">Step 4 \u2014 Loading the mod_evasive Module<\/h2>\n<p>Once we have made the changes in the configuration file, we need to restart the Apache web server for them to take effect. Run the following command to restart Apache.<\/p>\n<p>On CentOS 7:<\/p>\n<pre class=\"code-pre command\"><code>sudo systemctl restart httpd.service<\/code><\/pre>\n<p><span class=\"note\"><strong>Note:<\/strong> Please note that mod_evasive appears to conflict with the FrontPage Server Extensions. You might also want to check your Apache web server settings to make sure mod_evasive is able to function well. Suggested Apache tweaks are to have a very high value for <code>MaxRequestsPerChild<\/code> but not unlimited (A value of zero implies unlimited) and to have <code>KeepAlive<\/code> enabled with <code>KeepAliveTimeout<\/code> set reasonably long.<\/span><\/p>\n<h2 id=\"step-5-\u2014-testing-mod_evasive\">Step 5 \u2014 Testing mod_evasive<\/h2>\n<p>Let us do a short test to see if the module is working correctly. We will be using a perl script <strong>test.pl<\/strong> written by mod_evasive developers. To execute the script, we need to first install <code>perl<\/code> package on the server using:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install -y perl<\/code><\/pre>\n<p>The test script is installed with mod_evasive at the following location:<\/p>\n<pre class=\"code-pre \"><code>\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/code><\/pre>\n<p>By default, the test script requests the same page from your Apache web server 100 times in a row to trigger mod_evasive. In the last section, we modified mod_evasive to be more tolerant of requests per second to the same page. We need to change the script to 200 requests in a row instead of 100 to make sure we trigger all of mod_evasive&#8217;s notification methods.<\/p>\n<p>Edit <code>\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/code>:<\/p>\n<pre class=\"code-pre command\"><code>sudo nano \/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/code><\/pre>\n<p>Find the following line:<\/p>\n<div class=\"code-label \" title=\"\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl\">\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/div>\n<pre class=\"code-pre \"><code>for(0..<span class=\"highlight\">100<\/span>) {\r\n<\/code><\/pre>\n<p>Replace <span class=\"highlight\">100<\/span> with <span class=\"highlight\">200<\/span>:<\/p>\n<div class=\"code-label \" title=\"\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl\">\/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/div>\n<pre class=\"code-pre \"><code>for(0..<span class=\"highlight\">200<\/span>) {\r\n<\/code><\/pre>\n<p>Save and exit.<\/p>\n<p>To execute the script, run:<\/p>\n<pre class=\"code-pre command\"><code>sudo perl \/usr\/share\/doc\/mod_evasive-1.10.1\/test.pl<\/code><\/pre>\n<p>You should see output similar to:<\/p>\n<pre class=\"code-pre \"><code>HTTP\/1.1 403 Forbidden\r\nHTTP\/1.1 403 Forbidden\r\nHTTP\/1.1 403 Forbidden\r\nHTTP\/1.1 403 Forbidden\r\nHTTP\/1.1 403 Forbidden\r\n...<\/code><\/pre>\n<p>The script makes 100 requests to your web server. the 403 response code indicates access is denied by the web server. mod_evasive also logs to syslog when the IP address is blocked. Check the log file using:<\/p>\n<pre class=\"code-pre command\"><code>sudo tailf \/var\/log\/messages<\/code><\/pre>\n<p>It should show a line similar to:<\/p>\n<pre class=\"code-pre \"><code>May 21 00:11:18 servername mod_evasive[18290]: Blacklisting address 127.0.0.1: possible DoS attack.<\/code><\/pre>\n<p>indicating the IP address is blocked by mod_evasive.<\/p>\n<p>If you have configured mod_evasive to send email alerts when an IP is blocked, you will have an email in your inbox with the following content:<\/p>\n<pre class=\"code-pre \"><code>mod_evasive HTTP Blacklisted 127.0.0.1<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>mod_evasive is great at fending off single server, scripted attacks as well as distributed attacks. However, it is only useful to the point of your server&#8217;s total bandwidth and processor capacity for processing and responding to invalid requests. For this reason, it is a good idea to integrate this module with your server firewall for maximum protection. Without a really good infrastructure and a firewall in place, a heavy DDoS might still take you offline. If an attack is very heavy and persistent, you might need to move to a hardware-based DDoS mitigation solution.<\/p>\n<p>Source : www.digitalocean.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The mod_evasive Apache module, formerly known as mod_dosevasive, helps protect against DoS, DDoS (Distributed Denial of Service), and brute force attacks on the Apache web server. It can provide evasive action during attacks and report abuses via email and syslog facilities. The module works by creating an internal dynamic table of IP addresses and URIs &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2017\/05\/23\/protection-for-apache-using-mod_dosevasive\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Protection for APACHE using mod_dosevasive<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1433","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=1433"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1433\/revisions"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}