{"id":4290,"date":"2019-02-28T19:05:54","date_gmt":"2019-02-28T16:05:54","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4290"},"modified":"2024-03-11T22:18:51","modified_gmt":"2024-03-11T19:18:51","slug":"install-libmodsecurity-with-apache-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-libmodsecurity-with-apache-on-ubuntu-18-04\/","title":{"rendered":"Install LibModsecurity with Apache on Ubuntu 18.04"},"content":{"rendered":"\n<p>Welcome to our guide on how to install LibModsecurity with Apache on Ubuntu 18.04. <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/SpiderLabs\/ModSecurity\" target=\"_blank\">Libmodsecurity (Modsecurity v3)<\/a>, is an open source, cross platform web application firewall (WAF) developed by Trustwave&#8217;s SpiderLabs. It is a complete rewrite of ModSecurity v2 and it provides a robust event-based programming language which protects web applications against a wide range of attacks such as SQL injection, Cross-site Scripting (XSS), Local File Include, Remote File Include e.tc. It also allows for HTTP traffic monitoring, logging and real-time analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing LibModsecurity with Apache on Ubuntu 18.04<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Run System Update<\/h3>\n\n\n\n<p>Resynchronize your system packages to their latest versions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update\napt upgrade<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install LAMP Stack<\/h3>\n\n\n\n<p>Before you can install and configure LibModsecurity with Apache on Ubuntu 18.04, you need to have LAMP Stack installed and running your server. Follow the link below to setup LAMP stack on Ubuntu 18.04.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/howtos\/how-to-install-lamp-stack-apachemariadb-php-7-2-on-ubuntu-18-04-lts\/\" target=\"_blank\">How to install LAMP Stack on Ubuntu 18.04<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install Required Build Tools and Dependencies<\/h3>\n\n\n\n<p>To install Libmodsecurity or Modsecurity v3 on Ubuntu 18.04, we are going to build it from source. Hence, you need to install some required build tools and dependencies for a successful build.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install g++ flex bison curl apache2-dev doxygen libyajl-dev ssdeep liblua5.2-dev libgeoip-dev libtool dh-autoreconf libcurl4-gnutls-dev libxml2 libpcre++-dev libxml2-dev git<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile and Install LibModsecurity on Ubuntu 18.04<\/h3>\n\n\n\n<p>Once your system is setup, you can proceed to install Modsecurity v3 on Ubuntu 18.04.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Download LibModsecurity Source Code<\/h3>\n\n\n\n<p>To install the latest stable version of <strong>LibModsecurity<\/strong>, you need to compile it from the source.<\/p>\n\n\n\n<p>Therefore, 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>wget 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>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&nbsp;<strong>fatal:*<\/strong>&nbsp;error messages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure<\/code><\/pre>\n\n\n\n<p>Be sure to fix any dependency issue, if any, before you can proceed to compile and install LibModsecurity with Apache on Ubuntu.<\/p>\n\n\n\n<p>If the configure script above completes with no error, proceed to 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<p>LibModSecurity aka ModSecurity v3 has been installed on Ubuntu 18.04<\/p>\n\n\n\n<p>If you would like to install <strong>Modsecurity v2.9<\/strong>, it is available on Ubuntu repositories and can just be installed by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install libapache2-mod-security2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Install ModSecurity-Apache Connector<\/h2>\n\n\n\n<p>The ModSecurity-apache connector provides a communication channel between Apache and libModsecurity. Now that libmodsecurity is installed, follow through the following steps to install Modsecurity Apache connector.<\/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\nsudo make install<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Apache with LibModsecurity<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Load the Apache ModSecurity Connector Module<\/h4>\n\n\n\n<p>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\/lib\/apache2\/modules\/mod_security3.so\" | sudo tee -a \/etc\/apache2\/apache2.conf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure ModSecurity<\/h4>\n\n\n\n<p>Create ModSecurity configuration directory under <code>\/etc\/apache2<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo mkdir \/etc\/apache2\/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>sudo cp ~\/modsecurity-v3.0.4\/modsecurity.conf-recommended \/etc\/apache2\/modsecurity.d\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Also copy the <code>unicode.mapping<\/code> file from ModSecurity source directory to Apache Modsecurity configuration directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo cp ~\/modsecurity-v3.0.4\/unicode.mapping \/etc\/apache2\/modsecurity.d\/<\/code><\/pre>\n\n\n\n<p>Turn on ModSecurity by changing the value of <code>SecRuleEngine<\/code> to <code>On<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed -i 's\/SecRuleEngine DetectionOnly\/SecRuleEngine On\/' \/etc\/apache2\/modsecurity.d\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Next, you need to configure ModSecurity rules. Therefore, create a file where you can define the rules to include, say, <code>\/etc\/apache2\/modsecurity.d\/modsec_rules.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo vim \/etc\/apache2\/modsecurity.d\/modsec_rules.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Include \"\/etc\/apache2\/modsecurity.d\/modsecurity.conf\"\nInclude \"\/etc\/apache2\/modsecurity.d\/owasp-crs\/crs-setup.conf\"\nInclude \"\/etc\/apache2\/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 <strong>OWASP ModSecurity Core Rule Set (CRS)<\/strong> 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 <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/SpiderLabs\/owasp-modsecurity-crs\/\" target=\"_blank\">GitHub repository<\/a> to <code>\/etc\/apache2\/modsecurity.d\/<\/code> 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\/apache2\/modsecurity.d\/owasp-crs<\/code><\/pre>\n\n\n\n<p>Next, rename <code>crs-setup.conf.example<\/code> to <code>crs-setup.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo cp \/etc\/apache2\/modsecurity.d\/owasp-crs\/crs-setup.conf{.example,}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Activate ModSecurity<\/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&#8217;s default site configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/apache2\/sites-available\/000-default.conf<\/code><\/pre>\n\n\n\n<p>See our sample default site virtual host configuration with no comments;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost *:80&gt;\n\tmodsecurity on\n\tmodsecurity_rules_file \/etc\/apache2\/modsecurity.d\/modsec_rules.conf \n\tServerAdmin webmaster@localhost\n\tDocumentRoot \/var\/www\/html\n\tErrorLog ${APACHE_LOG_DIR}\/error.log\n\tCustomLog ${APACHE_LOG_DIR}\/access.log combined\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n\n\n\n<p>The lines;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\tmodsecurity on\n\tmodsecurity_rules_file \/etc\/apache2\/modsecurity.d\/modsec_rules.conf <\/code><\/pre>\n\n\n\n<p>Turns on Modsecurity and specifies the location of the Modsecurity rules.<\/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>apachectl configtest\nSyntax OK<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart apache2<\/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, for example, command injection. Run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>curl localhost?doc=\/bin\/ls<\/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 this resource.&lt;\/p&gt;\n&lt;hr&gt;\n&lt;address&gt;Apache\/2.4.29 (Ubuntu) Server at localhost Port 80&lt;\/address&gt;\n&lt;\/body&gt;&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Check Modsecurity logs;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail \/var\/log\/modsec_audit.log<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ModSecurity: Warning. Matched \"Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:doc' (Value: `\/bin\/ls' ) [file \"\/etc\/apache2\/modsecurity.d\/owasp-crs\/rules\/REQUEST-932-APPLICATION-ATTACK-RCE.conf\"] [line \"488\"] [id \"932160\"] [rev \"\"] [msg \"Remote Command Execution: Unix Shell Code Found\"] [data \"Matched Data: bin\/ls found within ARGS:doc: \/bin\/ls\"] [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 \"OWASP_CRS\"] [tag \"OWASP_CRS\/WEB_ATTACK\/COMMAND_INJECTION\"] [tag \"WASCTC\/WASC-31\"] [tag \"OWASP_TOP_10\/A1\"] [tag \"PCI\/6.5.2\"] [hostname \"web-01.kifarunix-demo.com\"] [uri \"\/\"] [unique_id \"156976184572.128853\"] [ref \"o1,6v10,7t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase\"]\nModSecurity: Access denied with code 403 (phase 2). Matched \"Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `5' ) [file \"\/etc\/apache2\/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 \"web-01.kifarunix-demo.com\"] [uri \"\/\"] [unique_id \"156976184572.128853\"] [ref \"\"]\nModSecurity: Warning. Matched \"Operator `Ge' with parameter `5' against variable `TX:INBOUND_ANOMALY_SCORE' (Value: `5' ) [file \"\/etc\/apache2\/modsecurity.d\/owasp-crs\/rules\/RESPONSE-980-CORRELATION.conf\"] [line \"76\"] [id \"980130\"] [rev \"\"] [msg \"Inbound Anomaly Score Exceeded (Total Inbound Score: 5 - SQLI=0,XSS=0,RFI=0,LFI=0,RCE=5,PHPI=0,HTTP=0,SESS=0): individual paranoia level scores: 5, 0, 0, 0\"] [data \"\"] [severity \"0\"] [ver \"\"] [maturity \"0\"] [accuracy \"0\"] [tag \"event-correlation\"] [hostname \"web-01.kifarunix-demo.com\"] [uri \"\/\"] [unique_id \"156976184572.128853\"] [ref \"\"]<\/code><\/pre>\n\n\n\n<p>As a result, this confirms that the Modsecurity is functioning as expected!<\/p>\n\n\n\n<p>Reference:<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/SpiderLabs\/ModSecurity-apache\" target=\"_blank\">ModSecurity-apache<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/SpiderLabs\/ModSecurity\" target=\"_blank\" rel=\"noopener\">ModSecurity<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Guides<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-libmodsecurity-with-nginx-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Configure LibModsecurity with Nginx on CentOS 8<\/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>Welcome to our guide on how to install LibModsecurity with Apache on Ubuntu 18.04. Libmodsecurity (Modsecurity v3), is an open source, cross platform web application<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,34,305],"tags":[202,3198,1140,1139,1141,67],"class_list":["post-4290","post","type-post","status-publish","format-standard","hentry","category-howtos","category-security","category-waf","tag-apache","tag-install-modsecurity-3-ubuntu","tag-libmodsecurity","tag-modsecurity","tag-modsecurity-3","tag-ubuntu-18-04","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4290"}],"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=4290"}],"version-history":[{"count":11,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4290\/revisions"}],"predecessor-version":[{"id":21107,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4290\/revisions\/21107"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}