{"id":335551,"date":"2023-06-13T14:13:42","date_gmt":"2023-06-13T21:13:42","guid":{"rendered":"https:\/\/linuxhint.com\/?p=335551"},"modified":"2023-06-25T14:08:52","modified_gmt":"2023-06-25T21:08:52","slug":"nginx-modsecurity","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/nginx-modsecurity\/","title":{"rendered":"Nginx ModSecurity"},"content":{"rendered":"<div id=\"wpbody\">\nModSecurity is a free and open-source web application firewall module that provides security features for web applications. ModSecurity mainly acts as a filter between the web application and any external entities which helps you to detect, log, or even block various attacks. This can include attacks such as SQL injections, cross-site scripting, remote file inclusions, etc. <\/p>\n<p>In this tutorial, we will learn the basics of configuring NGINX with ModSecurity. <\/p>\n<p><strong>Requirements:<\/strong><br \/>\nFor this tutorial, we assume that you have the following:<\/p>\n<ol>\n<li>An Ubuntu or Debian-based server<\/li>\n<li>Installed NGINX on your server<\/li>\n<li>Sudo or root permissions on your server<\/li>\n<\/ol>\n<p>With the given requirements met, we can learn how to install and configure the ModSecurity with NGINX. <\/p>\n<h2>Install the Necessary Packages<\/h2>\n<p>Let&#8217;s start by refreshing the system repositories and installing the required dependencies. We can do this by running the following commands:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">apt-get update<\/span><br \/>\n$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">apt-get install<\/span> <span class=\"kw2\">git<\/span> libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libtool<\/div><\/div>\n<p>With the given packages installed, we can proceed and clone the ModSecurity repository. <\/p>\n<h2>Clone the ModSecurity Repo<\/h2>\n<p>Start by cloning the ModSecurity GitHub repository with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">git clone<\/span> https:<span class=\"sy0\">\/\/<\/span>github.com<span class=\"sy0\">\/<\/span>SpiderLabs<span class=\"sy0\">\/<\/span>ModSecurity<\/div><\/div>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-1.png\" alt=\"\" width=\"602\" height=\"123\" class=\"alignnone size-full wp-image-335552\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-1.png 602w, https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-1-300x61.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/p>\n<p>Next, navigate into the clone directory with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">cd<\/span> ModSecurity<\/div><\/div>\n<p>Next, run the following commands to compile the ModSecurity:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>.<span class=\"sy0\">\/<\/span>build.sh<\/div><\/div>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-2.png\" alt=\"\" width=\"545\" height=\"292\" class=\"alignnone size-full wp-image-335553\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-2.png 545w, https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-2-300x161.png 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/p>\n<p>Finally, run the \u201cmake\u201d and \u201cmake install\u201d commands as shown in the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ .<span class=\"sy0\">\/<\/span>configure<br \/>\n$ <span class=\"kw2\">make<\/span><br \/>\n$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">make<\/span> <span class=\"kw2\">install<\/span><\/div><\/div>\n<p>Once completed, we can proceed and configure NGINX with ModSecurity.<\/p>\n<h2>Install the ModSecurity-Nginx Connector<\/h2>\n<p>The next step is to install the ModSecurity-Nginx connector which allows us to integrate the ModSecurity with the NGINX server. <\/p>\n<p>Change to the root directory and clone the connector repository.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">cd<\/span> .. <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw2\">git clone<\/span> https:<span class=\"sy0\">\/\/<\/span>github.com<span class=\"sy0\">\/<\/span>SpiderLabs<span class=\"sy0\">\/<\/span>ModSecurity-nginx.git<\/div><\/div>\n<p>Once completed, change to the NGINX source directory and run the following commands to compile the connector:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ .<span class=\"sy0\">\/<\/span>configure <span class=\"re5\">--add-dynamic-module<\/span>=..<span class=\"sy0\">\/<\/span>ModSecurity-nginx<br \/>\n$ <span class=\"kw2\">make<\/span><br \/>\n$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">make<\/span> <span class=\"kw2\">install<\/span><\/div><\/div>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-3.png\" alt=\"\" width=\"602\" height=\"196\" class=\"alignnone size-full wp-image-335554\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-3.png 602w, https:\/\/linuxhint.com\/wp-content\/uploads\/2023\/06\/Nginx-ModSecurity-3-300x98.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/p>\n<p>You can download the NGINX source as shown in the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ <span class=\"kw2\">wget<\/span> http:<span class=\"sy0\">\/\/<\/span>nginx.org<span class=\"sy0\">\/<\/span>download<span class=\"sy0\">\/<\/span>nginx-1.25.0.tar.gz<br \/>\n$ <span class=\"kw2\">tar<\/span> zxvf nginx-1.25.0.tar.gz<br \/>\n$ <span class=\"kw3\">cd<\/span> nginx-1.25.0<\/div><\/div>\n<p>We can enable the ModSecurity feature once we have NGINX compiled with ModSecurity. <\/p>\n<h2>Enable the ModSecurity<\/h2>\n<p>Start by heading over to the ModSecurity directory and copy the \u201cunicode.mapping\u201d file to the \/etc\/nginx directory.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">cd<\/span> ..<span class=\"sy0\">\/<\/span>ModSecurity <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">cp<\/span> unicode.mapping <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span><\/div><\/div>\n<p>Next, move to the ModSecurity configuration directory and copy the ModSecurity configuration files to the \/etc\/nginx directory:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ <span class=\"kw3\">cd<\/span> modsecurity<br \/>\n$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">cp<\/span> modsecurity.conf-recommended <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span>modsecurity.conf<br \/>\n$ <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">cp<\/span> unicode.mapping <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span><\/div><\/div>\n<h2>Modify the NGINX Configuration<\/h2>\n<p>Once completed, modify the NGINX configuration file to include the ModSecurity. For example, add the following commands in the http block:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">modsecurity on;<br \/>\nmodsecurity_rules_file <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span>modsecurity.conf;<\/div><\/div>\n<p>An example configuration file is as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">http <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; modsecurity on;<br \/>\n&nbsp; &nbsp; modsecurity_rules_file <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span>modsecurity.conf;<br \/>\n<br \/>\n&nbsp; &nbsp; server <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; listen <span class=\"nu0\">80<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; server_name localhost;<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; location <span class=\"sy0\">\/<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root html;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index index.html index.htm;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Save the file and close the editor. Once completed, restart the NGINX service with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> service nginx restart<\/div><\/div>\n<p>To confirm that ModSecurity is running with NGINX, run the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>nginx <span class=\"re5\">-V<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">&gt;&amp;<\/span><span class=\"nu0\">1<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> <span class=\"re5\">-o<\/span> with-http_modsecurity_module<\/div><\/div>\n<p><strong>Output<\/strong>:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">with-http_modsecurity_module<\/div><\/div>\n<h2>Conclusion<\/h2>\n<p>This tutorial taught us how to compile and configure the ModSecurity WAF with the Nginx web server in simple steps. It is good to remember that the steps that are outlined in this post configure the fundamentals with basic ModSecurity features. Consider checking the documentation for extensive rules and configuration to secure your web server.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on the basics of configuring NGINX with ModSecurity and how to compile and configure the ModSecurity WAF with the NGINX web server in simple steps.<\/p>\n","protected":false},"author":111,"featured_media":335556,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1987],"tags":[],"class_list":["post-335551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/335551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=335551"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/335551\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/335556"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=335551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=335551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=335551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}