Last updated on August 19th, 2021 at 09:09 pm
In this guide, we are going to learn how to configure LibModsecurity with Apache on CentOS 8. LibMosecurity also known as ModSecurity version 3, is an open source, cross platform web application firewall (WAF) engine which provides protection against a wide range of web application attacks.
Configure LibModsecurity with Apache on CentOS 8
Run System Update
Begin by updating your system packages.
Install Required Build Tools and Dependencies
LibModsecurity are going to be compiled from the source and thus a number of build tools and dependencies are required. Run the command below to install them.
dnf config-manager --set-enabled powertools
Install additional repositories.
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf config-manager --set-enabled remi
Install the required dependencies.
dnf install gcc-c++ flex bison yajl curl-devel curl zlib-devel pcre-devel autoconf automake git curl make libxml2-devel pkgconfig libtool httpd-devel redhat-rpm-config git wget openssl openssl-devel vim GeoIP-devel doxygen yajl-devel libmaxminddb libmaxminddb-devel GeoIP-devel lmdb lmdb-devel ssdeep-devel lua-devel
Download LibModsecurity Source Code
Create a temporary directory to store the source tarballs.
You can choose to use
Navigate to ModSecurity releases page and download ModSecurity source code. You can simply use wget to pull it.
wget -P ~/modsec https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.4/modsecurity-v3.0.4.tar.gz
Extract the ModSecurity source code.
tar xzf modsecurity-v3.0.4.tar.gz
Compile and Install LibModsecurity
Navigate to the LibModsecurity source directory, configure, compile and install it
Configure LibModsecurity to adapt it to your system and check if any required dependency is missing.
You can safely ignore the fatal: No names found, cannot describe anything messages.
Fix any dependency issue just in case there is any before you can proceed to compile and install LibModsecurity with Apache on CentOS
Compile and install LibModSecurity.
Install ModSecurity-Apache Connector on CentOS 8
Once the installation of LibModsecurity is done, proceed to install the ModSecurity-apache connector which provides a communication channel between Apache and libModsecurity.
Clone the git repository for the ModSecurity Apache connector.
cd ~ git clone https://github.com/SpiderLabs/ModSecurity-apache
Navigate to ModSecurity-apache directory and run the following commands to compile and install it.
Configure Apache with LibModsecurity on CentOS 8
Next, configure Apache to load Modsecurity Apache connector module by adding the line below to the main Apache configuration file.
LoadModule security3_module/usr/lib64/httpd/modules/mod_security3.so" | sudo tee -a /etc/httpd/conf/httpd.conf
Create ModSecurity configuration directory under
Copy the sample ModSecurity configuration file from the source code directory to the ModSec configuration directory created above renaming it as follows.
cp ~/modsec/modsecurity-v3.0.4/modsecurity.conf-recommended /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
Also copy the
unicode.mapping file from ModSecurity source directory to Apache Modsecurity configuration directory.
sudo cp ~/modsec/modsecurity-v3.0.4/unicode.mapping /etc/httpd/conf.d/modsecurity.d/
Activate ModSecurity by changing the value of
sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
Change the default log directory for Modsecurity
sed -i 's#/var/log/modsec_audit.log#/var/log/httpd/modsec_audit.log#' /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
Configure ModSecurity rules by creating a file where you can define the rules to include.
Include "/etc/httpd/conf.d/modsecurity.d/modsecurity.conf" Include "/etc/httpd/conf.d/modsecurity.d/owasp-crs/crs-setup.conf" Include "/etc/httpd/conf.d/modsecurity.d/owasp-crs/rules/*.conf"
Since we have included the OWASP Rules, proceed to install them.
Install OWASP ModSecurity Core Rule Set (CRS)
The OWASP ModSecurity Core Rule Set (CRS) is a set of generic attack detection rules for use with ModSecurity. It aims at protecting the web applications from a wide range of attacks, including the OWASP Top Ten, minimum of false alerts.
Clone the CRS from GitHub repository to
/etc/apache2/modsecurity.d/ as shown below;
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /etc/httpd/conf.d/modsecurity.d/owasp-crs
Activate ModSecurity 3 on CentOS 8
After all that, activate the modsecurity on the default site configuration file or on any virtual host configuration file. In this guide, we are using Apache’s default site configuration file.
Note that you have to enable ModSecurity per directory context.
See our below the changes made on the default web root directory on the default Apache configuration;
... <Directory "/var/www/html"> modsecurity on modsecurity_rules_file /etc/httpd/conf.d/modsecurity.d/rules.conf Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ...
modsecurity on modsecurity_rules_file /etc/httpd/conf.d/modsecurity.d/rules.conf
Turns on Modsecurity and specifies the location of the Modsecurity rules respectively.
Check Apache for configuration errors and restart it.
systemctl restart httpd
Next, test the effectiveness of Modsecurity with OWASP rules, for example, using the command injection. Run the command below;
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access /index.html on this server.</p> </body></html>
If you see,
403 Forbidden then it means you have nailed it.
You can as well check Modsecurity logs;
... ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:exec' (Value: `/bin/bash' ) [file "/etc/httpd/conf.d/modsecurity.d/owasp-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"] [line "496"] [id "932160"] [rev ""] [msg "Remote Command Execution: Unix Shell Code Found"] [data "Matched Data: bin/bash found within ARGS:exec: /bin/bash"] [severity "2"] [ver "OWASP_CRS/3.2.0"] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-shell"] [tag "platform-unix"] [tag "attack-rce"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"] [hostname "centos8.kifarunix-demo.com"] [uri "/index.html"] [unique_id "158386776469.002836"] [ref "o1,8v21,9t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase"] ModSecurity: Access denied with code 403 (phase 2). Matched "Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `5' ) [file "/etc/httpd/conf.d/modsecurity.d/owasp-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "79"] [id "949110"] [rev ""] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [data ""] [severity "2"] [ver ""] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "centos8.kifarunix-demo.com"] [uri "/index.html"] [unique_id "158386776469.002836"] [ref ""]
Well, there you go. ModSecurity 3 or LibModSeceurity is now installed, activated and protecting your site against web attacks.
Feel free to set up more rules as you wish and protect your web application.
That marks the end of our our guide on how to install and configure LibModsecurity with Apache on CentOS 8.
Configure LibModsecurity with Nginx on CentOS 8
Install LibModsecurity with Apache on Ubuntu 18.04
Install LibModsecurity with Apache on Fedora 30/29/CentOS 7
Thanks. I was able to install Modsecurity, but if SELinux is enabled, apache wont start. Disabling SeLinux, it will start.
I’ve chagned context for these files, but still it will not start:
restorecon -vFR /etc/httpd/conf.d/modsecurity.d/
restorecon -vF /usr/lib64/httpd/modules/mod_security3.so