apache:install_mod_security_and_mod_evasive
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
apache:install_mod_security_and_mod_evasive [2016/10/12 22:18] – peter | apache:install_mod_security_and_mod_evasive [2023/07/17 10:19] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Apache - Install mod_security and mod_evasive ====== | ||
- | |||
- | **ModSecurity** is a toolkit for real-time web application monitoring, logging, and access control. | ||
- | |||
- | **mod_evasive** is an evasive manoeuvres module for Apache to provide evasive action in the event of an HTTP DoS or DDoS attack or brute force attack. | ||
- | |||
- | |||
- | |||
- | ===== Install ModSecurity ===== | ||
- | |||
- | To install [[http:// | ||
- | |||
- | Install the dependencies. | ||
- | |||
- | <code bash> | ||
- | sudo apt-get install libxml2 libxml2-dev libxml2-utils | ||
- | sudo apt-get install libaprutil1 libaprutil1-dev | ||
- | </ | ||
- | |||
- | **NOTE**: | ||
- | |||
- | <code bash> | ||
- | ln -s / | ||
- | </ | ||
- | |||
- | |||
- | Install ModSecurity: | ||
- | |||
- | <code bash> | ||
- | sudo apt-get install libapache-mod-security | ||
- | </ | ||
- | |||
- | |||
- | ===== Configure ModSecurity rules. ===== | ||
- | |||
- | Activate the recommended default rules to get things going. | ||
- | |||
- | <code bash> | ||
- | sudo mv / | ||
- | </ | ||
- | |||
- | The default folder for ModSecurity rules is **/ | ||
- | |||
- | We need to activate all the **base rules** and make sure they also get loaded. | ||
- | |||
- | You might want to edit the **SecRequestBodyLimit** option in the modsecurity.conf file. | ||
- | |||
- | **SecRequestBodyLimit** limits the page request size and limits file uploads to **128 KB** by default. | ||
- | |||
- | This settings is very important as it limits the size of all files that can be uploaded to the server. | ||
- | |||
- | Execute the command: | ||
- | |||
- | <code bash> | ||
- | sudo vi / | ||
- | </ | ||
- | |||
- | First activate the rules by editing the **SecRuleEngine** option and set to On and modify your server signature. | ||
- | |||
- | <file bash / | ||
- | SecRuleEngine On | ||
- | SecServerSignature FreeOSHTTP | ||
- | </ | ||
- | |||
- | Edit the following to option to increase the request limit to 16 MB and save the file: | ||
- | |||
- | <file bash / | ||
- | SecRequestBodyLimit 16384000 | ||
- | SecRequestBodyInMemoryLimit 16384000 | ||
- | </ | ||
- | |||
- | |||
- | ===== Download and install the latest OWASP Core Rule Set ===== | ||
- | |||
- | The OWASP ModSecurity CRS Project' | ||
- | |||
- | Download and install the latest [[https:// | ||
- | |||
- | We will also activate the default CRS config file **modsecurity_crs_10_setup.conf.example**. | ||
- | |||
- | If you prefer not to use the latest rules, replace the wget for master below with the a specific version you would like to use e.g : **v2.2.5**. | ||
- | | ||
- | Execute the following commands: | ||
- | |||
- | <code bash> | ||
- | cd /tmp | ||
- | sudo wget -O SpiderLabs-owasp-modsecurity-crs.tar.gz https:// | ||
- | sudo tar -zxvf SpiderLabs-owasp-modsecurity-crs.tar.gz | ||
- | sudo cp -R SpiderLabs-owasp-modsecurity-crs-*/ | ||
- | sudo rm SpiderLabs-owasp-modsecurity-crs.tar.gz | ||
- | sudo rm -R SpiderLabs-owasp-modsecurity-crs-* | ||
- | sudo mv / | ||
- | </ | ||
- | |||
- | |||
- | Create symbolic links to all activated base rules. | ||
- | |||
- | <code bash> | ||
- | cd / | ||
- | for f in * ; do sudo ln -s / | ||
- | |||
- | cd / | ||
- | for f in * ; do sudo ln -s / | ||
- | </ | ||
- | |||
- | |||
- | Add these rules to Apache2. | ||
- | |||
- | <code bash> | ||
- | sudo vi / | ||
- | </ | ||
- | |||
- | |||
- | Add the following to towards the end of the file with other includes and save the file: | ||
- | |||
- | <file bash / | ||
- | Include "/ | ||
- | </ | ||
- | |||
- | |||
- | ===== Check if ModSecurity is enabled and restart Apache ===== | ||
- | |||
- | Before restarting Apache2 check if the modules has been loaded. | ||
- | |||
- | Execute the following commands: | ||
- | |||
- | <code bash> | ||
- | sudo a2enmod headers | ||
- | sudo a2enmod mod-security | ||
- | </ | ||
- | |||
- | |||
- | Restart the Apache2 webserver: | ||
- | |||
- | <code bash> | ||
- | sudo /etc/init.d apache2 restart | ||
- | </ | ||
- | |||
- | or | ||
- | |||
- | <code bash> | ||
- | service apache2 restart | ||
- | </ | ||
- | |||
- | |||
- | ===== Test ModSecurity ===== | ||
- | |||
- | To test mod_security you can use curl to send HTTP requests to the Apache server. | ||
- | |||
- | You can check mod_security by running the following command: | ||
- | |||
- | <code bash> | ||
- | sudo curl -i http:// | ||
- | </ | ||
- | |||
- | You should see a 403 Forbidden response, as shown below on this page. ModSecurity has blocked the request, because the User Agent identifies it as a Nessus scan. | ||
- | |||
- | < | ||
- | HTTP/1.1 403 Forbidden | ||
- | Date: Tue, 27 Oct 2015 11:08:39 GMT | ||
- | Server: Apache | ||
- | X-Frame-Options: | ||
- | Last-Modified: | ||
- | Accept-Ranges: | ||
- | Content-Length: | ||
- | X-XSS-Protection: | ||
- | Content-Type: | ||
- | </ | ||
- | |||
- | ===== Install ModEvasive ===== | ||
- | |||
- | Install [[http:// | ||
- | |||
- | Execute the following: | ||
- | |||
- | <code bash> | ||
- | sudo apt-get install libapache2-mod-evasive | ||
- | </ | ||
- | |||
- | |||
- | ===== Create log file directory for mod_evasive ===== | ||
- | |||
- | Execute the following: | ||
- | |||
- | <code bash> | ||
- | sudo mkdir / | ||
- | </ | ||
- | |||
- | |||
- | Change the log folder permissions: | ||
- | |||
- | <code bash> | ||
- | sudo chown www-data: | ||
- | </ | ||
- | |||
- | |||
- | ===== Create mod-evasive.conf file and configure ModEvasive ===== | ||
- | |||
- | Execute the following: | ||
- | |||
- | <code bash> | ||
- | sudo vi / | ||
- | </ | ||
- | |||
- | Add the following, changing the email value, and other options below as required: | ||
- | |||
- | <file bash / | ||
- | < | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | ===== Fix mod-evasive email bug ===== | ||
- | |||
- | Because of [[https:// | ||
- | |||
- | A temporary workaround is to create a symlink to the mail program. | ||
- | |||
- | Execute the following: | ||
- | |||
- | <code bash> | ||
- | sudo ln -s / | ||
- | </ | ||
- | |||
- | |||
- | ===== Check if ModEvasive is enabled and restart Apache ===== | ||
- | |||
- | Before restarting Apache2 check if the module has been loaded. | ||
- | |||
- | Execute the following: | ||
- | |||
- | <code bash> | ||
- | sudo a2enmod mod-evasive | ||
- | </ | ||
- | |||
- | |||
- | Restart the Apache2 webserver: | ||
- | |||
- | <code bash> | ||
- | sudo / | ||
- | </ | ||
- | |||
- | or | ||
- | |||
- | <code bash> | ||
- | service apache2 restart | ||
- | </ | ||
- | |||
- | |||
- | ===== Test ModEvasive ===== | ||
- | |||
- | You can test mod_evasive by using the Perl **scripttest.pl** written by the Mod_Evasive developers. | ||
- | |||
- | Before running this script, you need to make some changes: | ||
- | |||
- | <code bash> | ||
- | sudo vi / | ||
- | </ | ||
- | |||
- | Find the line **for(0..100) {** Replace **100** with **200**. | ||
- | |||
- | <file perl / | ||
- | # | ||
- | # test.pl: small script to test mod_dosevasive' | ||
- | |||
- | use IO::Socket; | ||
- | use strict; | ||
- | |||
- | for(0..200) { | ||
- | my($response); | ||
- | my($SOCKET) = new IO:: | ||
- | PeerAddr=> | ||
- | if (! defined $SOCKET) { die $!; } | ||
- | print $SOCKET "GET /?$_ HTTP/ | ||
- | $response = < | ||
- | print $response; | ||
- | close($SOCKET); | ||
- | }` | ||
- | </ | ||
- | |||
- | Save and exit. | ||
- | |||
- | Now, run the script: | ||
- | |||
- | <code bash> | ||
- | sudo / | ||
- | </ | ||
- | |||
- | You should see 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 | ||
- | </ | ||
- | |||
- | ModEvasive also logs to syslog when the IP address is blocked. | ||
- | |||
- | <code bash> | ||
- | sudo tailf / | ||
- | </ | ||
- | |||
- | You should see the following output: | ||
- | |||
- | < | ||
- | Oct 26 15:36:42 CentOS-7 mod_evasive[2732]: | ||
- | </ | ||
apache/install_mod_security_and_mod_evasive.1476310739.txt.gz · Last modified: 2020/07/15 09:30 (external edit)