24/7 ACTIVE THREAT INTELLIGENCE & NODE TELEMETRY MON - FRI: 10AM - 5PM
Home / Blog / Infrastructure & Linux

Securing Apache2 with ModSecurity

Securing Apache2 with ModSecurity

ModSecurity is a free and open source web application that started out as an Apache module and grew to a fully-fledged web application firewall. It works by inspecting requests sent to the web server in real time against a predefined rule set, preventing typical web application attacks like XSS and SQL Injection.

Install ModSecurity

ModSecurity can be installed by running the following command in your terminal:

command
sudo apt install libapache2-mod-security2 -y

After installing ModSecurity, enable the Apache 2 headers module by running the following command:

command
sudo a2enmod headers

Restart apache2 service.

Configure ModSecurity

ModSecurity is a firewall and therefore requires rules to function. This section shows you how to implement the OWASP Core Rule Set. First, you must prepare the ModSecurity configuration file.

Remove the .recommended extension from the ModSecurity configuration file name with the following command:

command
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

With a text editor such as vim, open /etc/modsecurity/modsecurity.conf and change the value for SecRuleEngine to On:

command
SecRuleEngine On

Also check if SecRequestBodyAccess is On.

Restart Apache to apply the changes.

Setting up the OWASP ModSecurity Core Rule Set

The OWASP ModSecurity Core Rule Set (CRS) is a set of generic attack detection rules for use with ModSecurity or compatible web application firewalls. The CRS aims to protect web applications from a wide range of attacks, including the OWASP Top Ten, with a minimum of false alerts. The CRS provides protection against many common attack categories, including SQL Injection, Cross Site Scripting, and Local File Inclusion.

First, delete the current rule set that comes prepackaged with ModSecurity by running the following command:

command
sudo rm -rf /usr/share/modsecurity-crs

Clone the OWASP-CRS GitHub repository into the /etc/apache2/modsecurity.d/ directory:

command
sudo git clone https://github.com/coreruleset/coreruleset /etc/apache2/modsecurity.d

Rename the crs-setup.conf.example to crs-setup.conf:

command
sudo mv /usr/share/modsecurity-crs/crs-setup.conf.example /usr/share/modsecurity-crs/crs-setup.conf

Change module configuration

To begin using ModSecurity, enable it in the Apache configuration file by following the steps outlined below:

Using a text editor such as vim, edit the /etc/apache2/mods-available/security2.conf file to include the OWASP-CRS files you have downloaded:

command
Include /etc/apache2/modsecurity.d/owasp-modsecurity-crs/crs-setup.conf

In /etc/apache2/sites-enabled/website.com.conf file VirtualHost block, include the SecRuleEngine directive set to On.

command
SecRuleEngine On

Restart the apache2 service to apply the configuration.

Test ModSecurity

Test ModSecurity by performing a simple local file inclusion attack by running the following command:

command
curl http://website.com/index.php?exec=../../../etc/passwd

If ModSecurity has been configured correctly and is actively blocking attacks, the following error is returned:

command
403 Forbidden

If you have any questions or need help to setup ModSecurity feel free to contact us.

← PREVIOUS ARTICLE
How to configure automated security updates on Linux
NEXT ARTICLE →
Ubuntu Pro - extended security