{"id":10092,"date":"2021-08-19T19:27:53","date_gmt":"2021-08-19T16:27:53","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10092"},"modified":"2024-03-18T18:50:23","modified_gmt":"2024-03-18T15:50:23","slug":"easily-install-modsecurity-with-apache-on-rocky-linux-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/easily-install-modsecurity-with-apache-on-rocky-linux-8\/","title":{"rendered":"Easily Install ModSecurity with Apache on Rocky Linux 8"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to install ModSecurity with Apache on Rocky Linux 8. This guide focusses on installing the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.modsecurity.org\/about.html\" target=\"_blank\">LibMosecurity<\/a>,&nbsp;also known as ModSecurity version 3. ModSecurity 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\">Installing ModSecurity with Apache on Rocky Linux 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.<\/p>\n\n\n\n<p>Run the commands 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 epel-release -y<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install https:\/\/rpms.remirepo.net\/enterprise\/remi-release-8.rpm -y<\/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 perl-File-Path -y<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Download Modsecurity 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&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/SpiderLabs\/ModSecurity\/releases\/\" target=\"_blank\">ModSecurity releases page<\/a>&nbsp;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.5\/modsecurity-v3.0.5.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.5.tar.gz<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile and Install Modsecurity on Rocky Linux<\/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.5<\/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&nbsp;<strong>fatal: *<\/strong>&nbsp;messages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure --with-maxmind=no<\/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 ModSecurity on Rocky Linux 8.<\/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 Rocky Linux 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 Modsecurity on Rocky Linux 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.5\/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.5\/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-code\"><code>cat &gt; \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf &lt;&lt; 'EOL'\nInclude \"\/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\"\nEOL<\/code><\/pre>\n\n\n\n<p>Since we have included the OWASP Rules, proceed to install them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install OWASP ModSecurity Core Rule Set (CRS)<\/h3>\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 Rocky Linux 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=\"scroll-box\"><code>...\n&gt;Directory \"\/var\/www\/html\"&lt;\n    <strong>modsecurity on\n    modsecurity_rules_file \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf<\/strong>\n    Options Indexes FollowSymLinks\n    AllowOverride None\n    Require all granted\n&gt;\/Directory&lt;\n...\n<\/code><\/pre>\n\n\n\n<p>The lines;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code> <strong>modsecurity on\n modsecurity_rules_file \/etc\/httpd\/conf.d\/modsecurity.d\/rules.conf<\/strong><\/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=\"scroll-box\"><code>---AzdMfmgc---B--\nGET \/index.html?exec=\/bin\/bash HTTP\/1.1\nHost: localhost\nUser-Agent: curl\/7.61.1\nAccept: *\/*\n\n---AzdMfmgc---D--\n\n---AzdMfmgc---F--\nHTTP\/1.1 403\n\n---AzdMfmgc---H--\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 \"\"] [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\"] \n[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 \"rocky8.kifarunix-demo.com\"] [uri \"\/index.html\"] [unique_id \"1629389313\"] [ref \"o1,8v21,9t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase\"]\n<strong>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 \"80\"] [id \"949110\"] [rev \"\"] [msg \"Inbound Anomaly Score Exceeded (Total Score: 5)\"] \n[data \"\"] [severity \"2\"] [ver \"OWASP_CRS\/3.2.0\"] [maturity \"0\"] [accuracy \"0\"] [tag \"application-multi\"] [tag \"language-multi\"] [tag \"platform-multi\"] [tag \"attack-generic\"] [hostname \"rocky8.kifarunix-demo.com\"] [uri \"\/index.html\"] [unique_id \"1629389313\"] [ref \"\"]<\/strong>\n\n---AzdMfmgc---I--\n\n---AzdMfmgc---J--\n\n---AzdMfmgc---Z--\n<\/code><\/pre>\n\n\n\n<p>You will also find such logs on Apache error log files;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail \/var\/log\/httpd\/error_log<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>...\n[Thu Aug 19 19:08:33.445040 2021] [:error] [pid 1658:tid 140385787549440] [client ::1:58424] 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\"] \n[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\"] \n[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 \"rocky8.kifarunix-demo.com\"] [uri \"\/index.html\"] [unique_id \"1629389313\"] [ref \"o1,8v21,9t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase\"]\n...\n\n<\/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<h2 class=\"wp-block-heading\">Other Tutorials<\/h2>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/protect-wordpress-against-brute-force-attacks-using-fail2ban\/\" target=\"_blank\" rel=\"noreferrer noopener\">Protect WordPress Against Brute force Attacks Using Fail2ban<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/restrict-access-to-wordpress-login-page-to-specific-ips-with-libmodsecurity\/\" target=\"_blank\" rel=\"noreferrer noopener\">Restrict Access to WordPress Login Page to Specific IPs with libModSecurity<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-ldap-based-http-basic-authentication\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure LDAP Based HTTP Basic Authentication<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to install ModSecurity with Apache on Rocky Linux 8. This guide focusses on installing the&nbsp;LibMosecurity,&nbsp;also known<\/p>\n","protected":false},"author":3,"featured_media":10111,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[34,254,121,1207,304,253],"tags":[3971,3968,1140,3970,3969,3972],"class_list":["post-10092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security","category-apache","category-howtos","category-modsecurity","category-nginx","category-web-servers","tag-apache-modsecurity","tag-install-modsecurity-on-rocky-linux","tag-libmodsecurity","tag-modsecurity-3-with-apache","tag-setup-modsecurity-with-apache","tag-web-application-modsecurity","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\/10092"}],"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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=10092"}],"version-history":[{"count":8,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10092\/revisions"}],"predecessor-version":[{"id":21689,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10092\/revisions\/21689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/10111"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=10092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=10092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=10092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}