{"id":10109,"date":"2021-08-19T21:16:26","date_gmt":"2021-08-19T18:16:26","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10109"},"modified":"2024-03-18T18:49:37","modified_gmt":"2024-03-18T15:49:37","slug":"install-modsecurity-with-nginx-on-rocky-linux-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-modsecurity-with-nginx-on-rocky-linux-8\/","title":{"rendered":"Install Modsecurity with Nginx on Rocky Linux 8"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to Install Modsecurity with Nginx on Rocky Linux 8. LibMosecurity is the newest version of&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/modsecurity.org\/documentation.html\" target=\"_blank\">ModSecurity<\/a>. It is therefore known as ModSecurity version 3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Modsecurity with Nginx 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>Both Nginx and 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 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<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf --enablerepo=powertools install doxygen yajl-devel -y<\/code><\/pre>\n\n\n\n<p>Install GeoIP Libraries<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install epel-release https:\/\/rpms.remirepo.net\/enterprise\/remi-release-8.rpm -y<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf --enablerepo=remi install GeoIP-devel -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 \/tmp\/modsec<\/code><\/pre>\n\n\n\n<p>Run the command below to &nbsp;clone the latest LibModsecurity GitHub repository.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd \/tmp\/modsec<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>git clone --depth 1 -b v3\/master --single-branch https:\/\/github.com\/SpiderLabs\/ModSecurity<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile and Install Modsecurity on Rocky Linux 8<\/h3>\n\n\n\n<p>Navigate to the Modsecurity source directory, configure, compile and install it<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd ModSecurity<\/code><\/pre>\n\n\n\n<p>Download libInjection code which is available as part of ModSecurity source code in a format of a git-submodule<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>git submodule init<\/code><\/pre>\n\n\n\n<pre id=\"block-09219962-63df-4103-89a1-cc2494d03a2e\" class=\"wp-block-preformatted\">git submodule update<\/code><\/pre>\n\n\n\n<p>Configure Modsecurity to adapt it to your system and check if any required dependency is missing.<\/p>\n\n\n\n<pre id=\"block-05b4d994-441c-4ae3-b817-9a99f4de0bf2\" class=\"wp-block-preformatted\">.\/build.sh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure<\/code><\/pre>\n\n\n\n<p>The&nbsp;<strong>fatal: No names found, cannot describe anything<\/strong>&nbsp;can be safely ignored.<\/p>\n\n\n\n<p>Fix any dependency issue just in case there is any before you can proceed.<\/p>\n\n\n\n<p>Compile and install ModSecurity.<\/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 Nginx with LibModsecurity Support<\/h3>\n\n\n\n<p>To setup Nginx with LibModsecurity, you need to compile Nginx with support for LibModsecurity.<\/p>\n\n\n\n<p>Therefore, download the ModSecurity-nginx connector which provides a communication channel between Nginx and LibModsecurity by cloning its git repository.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd \/tmp\/modsec\n<code>git clone https:\/\/github.com\/SpiderLabs\/ModSecurity-nginx.git<\/code><\/pre>\n\n\n\n<p>Next, download the latest staple version of Nginx from&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/nginx.org\/download\/\" target=\"_blank\">Nginx downloads<\/a>. The latest version as of this writing is version <a href=\"https:\/\/nginx.org\/download\/nginx-1.19.10.tar.gz\" target=\"_blank\" rel=\"noreferrer noopener\">nginx-1.19.10.tar.gz<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget http:\/\/nginx.org\/download\/nginx-1.19.10.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the archive.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf nginx-1.19.10.tar.gz<\/code><\/pre>\n\n\n\n<p>Create a non-privileged Nginx system user and group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -r -M -s \/sbin\/nologin -d \/usr\/local\/nginx nginx<\/code><\/pre>\n\n\n\n<p>Navigate to Nginx source directory and configure it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd nginx-1.19.10<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure --user=nginx --group=nginx --with-pcre-jit --with-debug --with-http_ssl_module --with-http_realip_module --add-module=\/tmp\/modsec\/ModSecurity-nginx<\/code><\/pre>\n\n\n\n<p>Configuration summary<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Configuration summary\n  + using system PCRE library\n  + using system OpenSSL library\n  + using system zlib library\n\n  nginx path prefix: \"\/usr\/local\/nginx\"\n  nginx binary file: \"\/usr\/local\/nginx\/sbin\/nginx\"\n  nginx modules path: \"\/usr\/local\/nginx\/modules\"\n  nginx configuration prefix: \"\/usr\/local\/nginx\/conf\"\n  nginx configuration file: \"\/usr\/local\/nginx\/conf\/nginx.conf\"\n  nginx pid file: \"\/usr\/local\/nginx\/logs\/nginx.pid\"\n  nginx error log file: \"\/usr\/local\/nginx\/logs\/error.log\"\n  nginx http access log file: \"\/usr\/local\/nginx\/logs\/access.log\"\n  nginx http client request body temporary files: \"client_body_temp\"\n  nginx http proxy temporary files: \"proxy_temp\"\n  nginx http fastcgi temporary files: \"fastcgi_temp\"\n  nginx http uwsgi temporary files: \"uwsgi_temp\"\n  nginx http scgi temporary files: \"scgi_temp\"\n<\/code><\/pre>\n\n\n\n<p>Compile and install Nginx on Rocky Linux 8.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make\nmake install<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Confgure Nginx with ModSecurity on Rocky Linux 8<\/h3>\n\n\n\n<p>To begin with, copy the sample ModSecurity configuration file on the source directory to Nginx configuration directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/tmp\/modsec\/ModSecurity\/modsecurity.conf-recommended \/usr\/local\/nginx\/conf\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>Copy the&nbsp;<code>unicode.mapping<\/code>&nbsp;file from ModSecurity source directory to Nginx configuration directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/tmp\/modsec\/ModSecurity\/unicode.mapping \/usr\/local\/nginx\/conf\/<\/code><\/pre>\n\n\n\n<p>Next, edit Nginx configuration file and make the changes as shown below.<\/p>\n\n\n\n<p>Create a backup of the Nginx configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/usr\/local\/nginx\/conf\/nginx.conf{,.bak}<\/code><\/pre>\n\n\n\n<p>Open the configuration file for editing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nginx\/conf\/nginx.conf<\/code><\/pre>\n\n\n\n<p>Configure Nginx such that your configuration may look like;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>user  nginx;\nworker_processes  1;\npid        \/run\/nginx.pid;\nevents {\n    worker_connections  1024;\n}\nhttp {\n    include       mime.types;\n    default_type  application\/octet-stream;\n    sendfile        on;\n    keepalive_timeout  65;\n    server {\n        listen       80;\n        server_name  nginx.kifarunix-demo.com;\n        <strong>modsecurity  on;\n        modsecurity_rules_file  \/usr\/local\/nginx\/conf\/modsecurity.conf<\/strong>;\n        access_log  \/var\/log\/nginx\/access_kifarunix-demo.log;\n        error_log  \/var\/log\/nginx\/error_kifarunix-demo.log;\n        location \/ {\n            root   html;\n            index  index.html index.htm;\n        }\n        error_page   500 502 503 504  \/50x.html;\n        location = \/50x.html {\n            root   html;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Note that the line;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>modsecurity  on;\nmodsecurity_rules_file  \/usr\/local\/nginx\/conf\/modsecurity.conf;<\/strong><\/code><\/pre>\n\n\n\n<p>Turns on Modsecurity and specifies the location of the Modsecurity rules.<\/p>\n\n\n\n<p>Note that ModSecurity 3 can be turned on per directory basis.<\/p>\n\n\n\n<p>Create Nginx log directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir \/var\/log\/nginx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create Nginx Systemd Service<\/h4>\n\n\n\n<p>To be able to run Nginx as a service, create a systemd service as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/etc\/systemd\/system\/nginx.service &lt;&lt; 'EOL'\n&#91;Unit]\nDescription=The nginx HTTP and reverse proxy server\nAfter=network.target remote-fs.target nss-lookup.target\n\n&#91;Service]\nType=forking\nPIDFile=\/run\/nginx.pid\nExecStartPre=\/usr\/bin\/rm -f \/run\/nginx.pid\nExecStartPre=\/usr\/sbin\/nginx -t\nExecStart=\/usr\/sbin\/nginx\nExecReload=\/bin\/kill -s HUP $MAINPID\nKillSignal=SIGQUIT\nTimeoutStopSec=5\nKillMode=mixed\nPrivateTmp=true\n\n&#91;Install]\nWantedBy=multi-user.target\nEOL<\/code><\/pre>\n\n\n\n<p>Create a symbolic link of Nginx binary to&nbsp;<code>\/usr\/sbin\/<\/code>&nbsp;path.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ln -s \/usr\/local\/nginx\/sbin\/nginx \/usr\/sbin\/<\/code><\/pre>\n\n\n\n<p>Reload systemd configurations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl daemon-reload<\/code><\/pre>\n\n\n\n<p>Run Nginx configuration syntax error verification.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx: the configuration file \/usr\/local\/nginx\/conf\/nginx.conf syntax is ok\nnginx: configuration file \/usr\/local\/nginx\/conf\/nginx.conf test is successful<\/code><\/pre>\n\n\n\n<p>If all is well, start and enable Nginx to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable --now nginx<\/code><\/pre>\n\n\n\n<p>Check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status nginx<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\u25cf nginx.service - The nginx HTTP and reverse proxy server\n   Loaded: loaded (\/etc\/systemd\/system\/nginx.service; disabled; vendor preset: disabled)\n   Active: active (running) since Thu 2021-08-19 20:46:33 EAT; 11s ago\n  Process: 31623 ExecStart=\/usr\/sbin\/nginx (code=exited, status=0\/SUCCESS)\n  Process: 31621 ExecStartPre=\/usr\/sbin\/nginx -t (code=exited, status=0\/SUCCESS)\n  Process: 31620 ExecStartPre=\/usr\/bin\/rm -f \/run\/nginx.pid (code=exited, status=0\/SUCCESS)\n Main PID: 31625 (nginx)\n    Tasks: 2 (limit: 4938)\n   Memory: 3.2M\n   CGroup: \/system.slice\/nginx.service\n           \u251c\u250031625 nginx: master process \/usr\/sbin\/nginx\n           \u2514\u250031626 nginx: worker process\n\nAug 19 20:46:33 rocky8 systemd[1]: Starting The nginx HTTP and reverse proxy server...\nAug 19 20:46:33 rocky8 nginx[31621]: nginx: the configuration file \/usr\/local\/nginx\/conf\/nginx.conf syntax is ok\nAug 19 20:46:33 rocky8 nginx[31621]: nginx: configuration file \/usr\/local\/nginx\/conf\/nginx.conf test is successful\nAug 19 20:46:33 rocky8 systemd[1]: nginx.service: Failed to parse PID from file \/run\/nginx.pid: Invalid argument\nAug 19 20:46:33 rocky8 systemd[1]: Started The nginx HTTP and reverse proxy server.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Turn on ModSecurity Rule Engine<\/h3>\n\n\n\n<p>By default, ModSecurity is set on detection only mode where it only logs the requests based on the triggered rules without blocking anything. This can be changed by setting 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\/' \/usr\/local\/nginx\/conf\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>You can also change 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\/nginx\/modsec_audit.log#' \/usr\/local\/nginx\/conf\/modsecurity.conf<\/code><\/pre>\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>\/usr\/local\/nginx\/conf\/<\/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 \/usr\/local\/nginx\/conf\/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>sudo cp \/usr\/local\/nginx\/conf\/owasp-crs\/crs-setup.conf{.example,}<\/code><\/pre>\n\n\n\n<p>Once the OWASP rules are in place, configure ModSecurity to use these rules. You therefore need to enter the following lines on the ModSecurity configuration file to tell it where to find the rules.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo -e \"Include owasp-crs\/crs-setup.conf\\nInclude owasp-crs\/rules\/*.conf\" &gt;&gt; \/usr\/local\/nginx\/conf\/modsecurity.conf<\/code><\/pre>\n\n\n\n<p>This command will append the lines below on&nbsp;<code>\/usr\/local\/nginx\/conf\/modsecurity.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Include owasp-crs\/crs-setup.conf\nInclude owasp-crs\/rules\/*.conf<\/code><\/pre>\n\n\n\n<p>Verify Nginx configuration file again.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx -t<\/code><\/pre>\n\n\n\n<p>Next, restart Nginx if everything is Okay.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nginx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing ModSecurity on Nginx<\/h3>\n\n\n\n<p>Next, you can 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<p>You should get&nbsp;<strong><code>403 Forbidden<\/code><\/strong>&nbsp;if the rules are file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;&lt;title&gt;403 Forbidden&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n&lt;center&gt;&lt;h1&gt;403 Forbidden&lt;\/h1&gt;&lt;\/center&gt;\n&lt;hr&gt;&lt;center&gt;nginx\/1.19.10&lt;\/center&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Check the logs file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail -100 \/var\/log\/nginx\/modsec_audit.log<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>---iQWU4H4y---A--\n[19\/Aug\/2021:20:48:26 +0300] 1629395306 127.0.0.1 37240 127.0.0.1 80\n---iQWU4H4y---B--\nGET \/index.html?exec=\/bin\/bash HTTP\/1.1\nHost: localhost\nUser-Agent: curl\/7.61.1\nAccept: *\/*\n\n---iQWU4H4y---D--\n\n---iQWU4H4y---E--\n&lt;html&gt;\\x0d\\x0a&lt;head&gt;&lt;title&gt;403 Forbidden&lt;\/title&gt;&lt;\/head&gt;\\x0d\\x0a&lt;body&gt;\\x0d\\x0a&lt;center&gt;&lt;h1&gt;403 Forbidden&lt;\/h1&gt;&lt;\/center&gt;\\x0d\\x0a&lt;hr&gt;&lt;center&gt;nginx\/1.19.10&lt;\/center&gt;\\x0d\\x0a&lt;\/body&gt;\\x0d\\x0a&lt;\/html&gt;\\x0d\\x0a\n\n---iQWU4H4y---F--\nHTTP\/1.1 403\nServer: nginx\/1.19.10\nDate: Thu, 19 Aug 2021 17:48:26 GMT\nContent-Length: 154\nContent-Type: text\/html\nConnection: keep-alive\n\n---iQWU4H4y---H--\nModSecurity: Warning. Matched \"Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:exec' (Value: `\/bin\/bash' ) [file \"\/usr\/local\/nginx\/conf\/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\"] [tag \"application-multi\"] [tag \"language-shell\"]\n[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 \"127.0.0.1\"] [uri \"\/index.html\"] [unique_id \"1629395306\"] [ref \"o1,8v21,9t: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' )\n[file \"\/usr\/local\/nginx\/conf\/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 \"127.0.0.1\"] [uri \"\/index.html\"] [unique_id \"1629395306\"] [ref \"\"]\n\n---iQWU4H4y---I--\n\n---iQWU4H4y---J--\n\n---iQWU4H4y---Z--\n<\/code><\/pre>\n\n\n\n<p>Similarly, the logs are written to Nginx error logs file.<\/p>\n\n\n\n<p>You can also create your own test rules. For example, append the line below on your&nbsp;<code>\/usr\/local\/nginx\/conf\/modsecurity.conf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SecRule ARGS \"@streq test\" \"id:1,phase:1,deny,msg:\\'Mytest Rule\\'\"<\/code><\/pre>\n\n\n\n<p>Save the configuration file.<\/p>\n\n\n\n<p>You can also create your custom rules file and make sure it is included on the main ModSecurity configuration file.<\/p>\n\n\n\n<p>Restart Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nginx<\/code><\/pre>\n\n\n\n<p>Test the rule.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>curl localhost\/index.html?a=test<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;&lt;title&gt;403 Forbidden&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n&lt;center&gt;&lt;h1&gt;403 Forbidden&lt;\/h1&gt;&lt;\/center&gt;\n&lt;hr&gt;&lt;center&gt;nginx\/1.19.10&lt;\/center&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Check the log file;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail \/var\/log\/nginx\/error_kifarunix-demo.log<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2021\/08\/19 20:48:26 &#91;error] 31687#0: *1 &#91;client 127.0.0.1] ModSecurity: Access denied with code 403 (phase 2). Matched \"Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `5' ) &#91;file \"\/usr\/local\/nginx\/conf\/owasp-crs\/rules\/REQUEST-949-BLOCKING-EVALUATION.conf\"] &#91;line \"80\"] &#91;id \"949110\"] &#91;rev \"\"] &#91;msg \"Inbound Anomaly Score Exceeded (Total Score: 5)\"] &#91;data \"\"] &#91;severity \"2\"] &#91;ver \"OWASP_CRS\/3.2.0\"] &#91;maturity \"0\"] &#91;accuracy \"0\"] &#91;tag \"application-multi\"] &#91;tag \"language-multi\"] &#91;tag \"platform-multi\"] &#91;tag \"attack-generic\"] &#91;hostname \"127.0.0.1\"] &#91;uri \"\/index.html\"] &#91;unique_id \"1629395306\"] &#91;ref \"\"], client: 127.0.0.1, server: nginx.kifarunix-demo.com, request: \"GET \/index.html?exec=\/bin\/bash HTTP\/1.1\", host: \"localhost\"\n2021\/08\/19 20:54:03 &#91;error] 31765#0: *1 &#91;client 127.0.0.1] ModSecurity: Access denied with code 403 (phase 1). Matched \"Operator `StrEq' with parameter `test' against variable `ARGS:a' (Value: `test' ) &#91;file \"\/usr\/local\/nginx\/conf\/modsecurity.conf\"] &#91;line \"261\"] &#91;id \"1\"] &#91;rev \"\"] &#91;msg \"\\'Mytest Rule\\'\"] &#91;data \"\"] &#91;severity \"0\"] &#91;ver \"\"] &#91;maturity \"0\"] &#91;accuracy \"0\"] &#91;hostname \"127.0.0.1\"] &#91;uri \"\/index.html\"] &#91;unique_id \"1629395643\"] &#91;ref \"v18,4\"], client: 127.0.0.1, server: nginx.kifarunix-demo.com, request: \"GET \/index.html?a=test HTTP\/1.1\", host: \"localhost\"<\/code><\/pre>\n\n\n\n<p>The ModSecurity is now running and protecting your web server.<\/p>\n\n\n\n<p>You can check our other guides on ModSecurity by following the links below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/process-and-visualize-modsecurity-logs-on-elk-stack\/\" target=\"_blank\" rel=\"noreferrer noopener\">Process and Visualize ModSecurity Logs on ELK Stack<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/create-kibana-visualization-dashboards-for-modsecurity-logs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create Kibana Visualization Dashboards for ModSecurity Logs<\/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","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to Install Modsecurity with Nginx on Rocky Linux 8. LibMosecurity is the newest version of&nbsp;ModSecurity. It<\/p>\n","protected":false},"author":3,"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,304],"tags":[3973,1140,1141,229,3974],"class_list":["post-10109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security","category-howtos","category-modsecurity","category-nginx","tag-install-modsecurity-with-nginx-rocky-linux-8","tag-libmodsecurity","tag-modsecurity-3","tag-nginx","tag-rocky-linux-8-modsecurity-with-nginx","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\/10109"}],"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=10109"}],"version-history":[{"count":3,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10109\/revisions"}],"predecessor-version":[{"id":21687,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10109\/revisions\/21687"}],"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=10109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=10109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=10109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}