{"id":5185,"date":"2020-03-10T22:22:05","date_gmt":"2020-03-10T19:22:05","guid":{"rendered":"https:\/\/kifarunix.com\/?p=5185"},"modified":"2021-08-19T21:09:59","modified_gmt":"2021-08-19T18:09:59","slug":"configure-libmodsecurity-with-apache-on-centos-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-libmodsecurity-with-apache-on-centos-8\/","title":{"rendered":"Configure LibModsecurity with Apache on CentOS 8"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to configure LibModsecurity with Apache on CentOS 8. <a rel=\"noreferrer noopener\" aria-label=\"LibMosecurity (opens in a new tab)\" href=\"https:\/\/www.modsecurity.org\/about.html\" target=\"_blank\">LibMosecurity<\/a> also known as ModSecurity version 3, is an open source, cross platform web application firewall (WAF) engine&nbsp;which provides protection against a wide range of web application attacks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure LibModsecurity with Apache on CentOS 8<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Run System Update<\/h3>\n\n\n\n<p>Begin by updating your system packages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install Required Build Tools and Dependencies<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf config-manager --set-enabled powertools<\/code><\/pre>\n\n\n\n<p>Install additional repositories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install https:\/\/dl.fedoraproject.org\/pub\/epel\/epel-release-latest-8.noarch.rpm<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install https:\/\/rpms.remirepo.net\/enterprise\/remi-release-8.rpm<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf config-manager --set-enabled remi<\/code><\/pre>\n\n\n\n<p>Install the required dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>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<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Download LibModsecurity Source Code<\/h3>\n\n\n\n<p>Create a temporary directory to store the source tarballs.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir ~\/modsec<\/code><\/pre>\n\n\n\n<p>You can choose to use&nbsp;<code>\/opt<\/code>&nbsp;instead.<\/p>\n\n\n\n<p>Navigate to <a rel=\"noreferrer noopener\" aria-label=\"ModSecurity releases page (opens in a new tab)\" href=\"https:\/\/github.com\/SpiderLabs\/ModSecurity\/releases\/\" target=\"_blank\">ModSecurity releases page<\/a> and download ModSecurity source code. You can simply use wget to pull it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd ~\/modsec<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget -P ~\/modsec https:\/\/github.com\/SpiderLabs\/ModSecurity\/releases\/download\/v3.0.4\/modsecurity-v3.0.4.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the ModSecurity source code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd ~\/modsec<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf modsecurity-v3.0.4.tar.gz<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile and Install LibModsecurity<\/h3>\n\n\n\n<p>Navigate to the LibModsecurity source directory, configure, compile and install it<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd modsecurity-v3.0.4<\/code><\/pre>\n\n\n\n<p>Configure LibModsecurity to adapt it to your system and check if any required dependency is missing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/build.sh<\/code><\/pre>\n\n\n\n<p>You can safely ignore the <strong>fatal: No names found, cannot describe anything<\/strong>&nbsp;messages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure<\/code><\/pre>\n\n\n\n<p>Fix any dependency issue just in case there is any before you can proceed to compile and install LibModsecurity with Apache on CentOS<\/p>\n\n\n\n<p>Compile and install LibModSecurity.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make install<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install ModSecurity-Apache Connector on CentOS 8<\/h3>\n\n\n\n<p>Once the installation of LibModsecurity is done, proceed to install the ModSecurity-apache connector which provides a communication channel between Apache and libModsecurity.&nbsp;<\/p>\n\n\n\n<p>Clone the git repository for the ModSecurity Apache connector.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd ~\ngit clone https:\/\/github.com\/SpiderLabs\/ModSecurity-apache<\/code><\/pre>\n\n\n\n<p>Navigate to ModSecurity-apache directory and run the following commands to compile and install it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd ModSecurity-apache<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/autogen.sh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure --with-libmodsecurity=\/usr\/local\/modsecurity\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make install<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Apache with LibModsecurity on CentOS 8<\/h2>\n\n\n\n<p>Next, configure Apache to load Modsecurity Apache connector module by adding the line below to the main Apache configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"<code>LoadModule security3_module <\/code>\/usr\/lib64\/httpd\/modules\/mod_security3.so\" | sudo tee -a \/etc\/httpd\/conf\/httpd.conf<\/code><\/pre>\n\n\n\n<p>Create ModSecurity configuration directory under&nbsp;<code>\/etc\/httpd\/conf.d<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir \/etc\/httpd\/conf.d\/modsecurity.d<\/code><\/pre>\n\n\n\n<p>Copy the sample ModSecurity configuration file from the source code directory to the ModSec configuration directory created above renaming it as follows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp ~\/modsec\/modsecurity-v3.0.4\/modsecurity.conf-recommended \/etc\/httpd\/conf.d\/modsecurity.d\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Also copy the&nbsp;<code>unicode.mapping<\/code>&nbsp;file from ModSecurity source directory to Apache Modsecurity configuration directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo cp ~\/modsec\/modsecurity-v3.0.4\/unicode.mapping \/etc\/httpd\/conf.d\/modsecurity.d\/<\/code><\/pre>\n\n\n\n<p>Activate ModSecurity by changing the value of&nbsp;<code>SecRuleEngine<\/code>&nbsp;to&nbsp;<code>On<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed -i 's\/SecRuleEngine DetectionOnly\/SecRuleEngine On\/' \/etc\/httpd\/conf.d\/modsecurity.d\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Change the default log directory for Modsecurity<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed -i 's#\/var\/log\/modsec_audit.log#\/var\/log\/httpd\/modsec_audit.log#' \/etc\/httpd\/conf.d\/modsecurity.d\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Configure ModSecurity rules by creating a file where you can define the rules to include.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Include \"\/etc\/httpd\/conf.d\/modsecurity.d\/modsecurity.conf\"\nInclude \"\/etc\/httpd\/conf.d\/modsecurity.d\/owasp-crs\/crs-setup.conf\"\nInclude \"\/etc\/httpd\/conf.d\/modsecurity.d\/owasp-crs\/rules\/*.conf\"<\/code><\/pre>\n\n\n\n<p>Since we have included the OWASP Rules, proceed to install them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install OWASP ModSecurity Core Rule Set (CRS)<\/h4>\n\n\n\n<p>The&nbsp;<strong>OWASP ModSecurity Core Rule Set (CRS)<\/strong>&nbsp;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.<\/p>\n\n\n\n<p>Clone the CRS from&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/SpiderLabs\/owasp-modsecurity-crs\/\" target=\"_blank\">GitHub repository<\/a>&nbsp;to&nbsp;<code>\/etc\/apache2\/modsecurity.d\/<\/code>&nbsp;as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>git clone https:\/\/github.com\/SpiderLabs\/owasp-modsecurity-crs.git \/etc\/httpd\/conf.d\/modsecurity.d\/owasp-crs<\/code><\/pre>\n\n\n\n<p>Next, rename&nbsp;<code>crs-setup.conf.example<\/code>&nbsp;to&nbsp;<code>crs-setup.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/etc\/httpd\/conf.d\/modsecurity.d\/owasp-crs\/crs-setup.conf{.example,}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Activate ModSecurity 3 on CentOS 8<\/h3>\n\n\n\n<p>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\u2019s default site configuration file.<\/p>\n\n\n\n<p>Note that you have to enable ModSecurity per directory context.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/httpd\/conf\/httpd.conf<\/code><\/pre>\n\n\n\n<p>See our below the changes made on the default web root directory on the default Apache configuration;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\n&lt;Directory \"\/var\/www\/html\"&gt;\n    modsecurity on\n    modsecurity_rules_file \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf\n    Options Indexes FollowSymLinks\n    AllowOverride None\n    Require all granted\n&lt;\/Directory&gt;\n...<\/code><\/pre>\n\n\n\n<p>The lines;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code> modsecurity on\n modsecurity_rules_file \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf<\/code><\/pre>\n\n\n\n<p>Turns on Modsecurity and specifies the location of the Modsecurity rules respectively.<\/p>\n\n\n\n<p>Check Apache for configuration errors and restart it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>httpd -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Syntax OK<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart httpd<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing Modsecurity<\/h3>\n\n\n\n<p>Next, test the effectiveness of Modsecurity with OWASP rules, for example, using the command injection. Run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>curl localhost\/index.html?exec=\/bin\/bash<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML 2.0\/\/EN\"&gt;\n&lt;html&gt;&lt;head&gt;\n&lt;title&gt;403 Forbidden&lt;\/title&gt;\n&lt;\/head&gt;&lt;body&gt;\n&lt;h1&gt;Forbidden&lt;\/h1&gt;\n&lt;p&gt;You don't have permission to access \/index.html\non this server.&lt;\/p&gt;\n&lt;\/body&gt;&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>If you see,&nbsp;<strong><code>403 Forbidden<\/code><\/strong>&nbsp;then it means you have nailed it.<\/p>\n\n\n\n<p>You can as well check Modsecurity logs;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail \/var\/log\/httpd\/modsec_audit.log<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>...\nModSecurity: 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 \"\"] [<strong>msg \"Remote Command Execution: Unix Shell Code Found<\/strong>\"] [data \"<strong>Matched Data: bin\/bash found within ARGS:exec: \/bin\/bash<\/strong>\"] [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\"]\n<strong>ModSecurity: Access denied with code 403 (phase 2).<\/strong> 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\"] <strong>[id \"949110\"]<\/strong> [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 \"\"]<\/code><\/pre>\n\n\n\n<p>Well, there you go. ModSecurity 3 or LibModSeceurity is now installed, activated and protecting your site against web attacks.<\/p>\n\n\n\n<p>Feel free to set up more rules as you wish and protect your web application.<\/p>\n\n\n\n<p>That marks the end of our our guide on how to install and configure LibModsecurity with Apache on CentOS 8. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/configure-libmodsecurity-with-nginx-on-centos-8\/\" target=\"_blank\">Configure LibModsecurity with Nginx on CentOS 8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-libmodsecurity-with-apache-on-ubuntu-18-04\/\" target=\"_blank\">Install LibModsecurity with Apache on Ubuntu 18.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-libmodsecurity-with-apache-on-fedora-30-29-centos-7\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install LibModsecurity with Apache on Fedora 30\/29\/CentOS 7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":10113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[34,121,1207],"tags":[202,1337,1140,1336,1141,1335,1334,307],"class_list":["post-5185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security","category-howtos","category-modsecurity","tag-apache","tag-centos-8-modsecurity","tag-libmodsecurity","tag-libmodsecurity-on-centos-8","tag-modsecurity-3","tag-modsecurity-3-on-centos-8","tag-modsecurity-apache-connector","tag-waf","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5185"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=5185"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5185\/revisions"}],"predecessor-version":[{"id":10110,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/5185\/revisions\/10110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/10113"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=5185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=5185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=5185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}