{"id":1882,"date":"2019-01-23T23:41:42","date_gmt":"2019-01-23T20:41:42","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1882"},"modified":"2024-03-11T22:01:53","modified_gmt":"2024-03-11T19:01:53","slug":"install-and-configure-naxsi-nginx-waf-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-configure-naxsi-nginx-waf-on-ubuntu-18-04-lts\/","title":{"rendered":"Install and Configure NAXSI Nginx WAF on Ubuntu 18.04 LTS"},"content":{"rendered":"\n<p>Welcome to our guide on how to install and configure NAXSI Nginx WAF on Ubuntu 18.04 LTS.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/NAXSI.png\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"427\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/NAXSI.png\" alt=\"Install and Configure NAXSI Nginx WAF on Ubuntu 18.04 LTS\" class=\"wp-image-1884\" title=\"Install and Configure NAXSI Nginx WAF on Ubuntu 18.04 LTS\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/NAXSI.png 773w, https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/NAXSI-768x424.png 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/a><\/figure><\/div>\n\n\n<p>NAXSI is an acronym for Nginx Anti XSS and SQL injection. It is an opensource, <span class=\"text-gray-dark mr-2\">high performance and low rules maintenance web application firewall (WAF) module for NGINX. Unlike other WAFs that rely on signatures to detect and prevent web attacks such as SQLi, XSS etc, Naxsi relies on unexpected characters contained on the HTTP GET and POST requests. To achieve this, it uses simple rules that contains 99% of the known patterns involved in web vulnerabilities.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing NAXSI Nginx WAF on Ubuntu 18.04 LTS<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Nginx-Naxsi on Ubuntu 18.04 LTS<\/h3>\n\n\n\n<p>Update and upgrade your system packages;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update\napt upgrade<\/code><\/pre>\n\n\n\n<p>Install the required dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install libpcre3-dev libssl-dev unzip build-essential daemon libxml2-dev libxslt1-dev libgd-dev libgeoip-dev<\/code><\/pre>\n\n\n\n<p>This guide assumes that this is a totally new Nginx deployment. Since Nginx-Naxsi package is not available on the default Ubuntu 18.04 repositories, you have to download and compile both Nginx and Naxsi from the source.<\/p>\n\n\n\n<p>Download the latest sources of Nginx from <a href=\"http:\/\/nginx.org\/download\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. You can simply obtain the link and pull it with <code>wget<\/code> as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget http:\/\/nginx.org\/download\/nginx-1.15.8.tar.gz<\/code><\/pre>\n\n\n\n<p>Unzip the source code;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf nginx-1.15.8.tar.gz<\/code><\/pre>\n\n\n\n<p>Download and unzip naxis.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget https:\/\/github.com\/nbs-system\/naxsi\/archive\/master.zip<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip master.zip<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Nginx for Naxsi support<\/h3>\n\n\n\n<p>Navigate to Nginx source directory and run the <code>configure<\/code> script to prepare Nginx for compilation as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd nginx-1.15.8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n.\/configure \\\n--conf-path=\/etc\/nginx\/nginx.conf \\\n--add-module=..\/naxsi-master\/naxsi_src\/ \\\n--error-log-path=\/var\/log\/nginx\/error.log \\\n--http-client-body-temp-path=\/var\/lib\/nginx\/body \\\n--http-fastcgi-temp-path=\/var\/lib\/nginx\/fastcgi \\\n--http-log-path=\/var\/log\/nginx\/access.log \\\n--http-proxy-temp-path=\/var\/lib\/nginx\/proxy \\\n--lock-path=\/var\/lock\/nginx.lock \\\n--pid-path=\/var\/run\/nginx.pid \\\n--user=www-data \\\n--group=www-data \\\n--with-http_ssl_module \\\n--without-mail_pop3_module \\\n--without-mail_smtp_module \\\n--without-mail_imap_module \\\n--without-http_uwsgi_module \\\n--without-http_scgi_module \\\n--prefix=\/usr\n<\/code><\/pre>\n\n\n\n<p>If all is well, you should be able to see the configuration summary;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\ncreating objs\/Makefile\n\nConfiguration summary\n  + using system PCRE library\n  + using system OpenSSL library\n  + using system zlib library\n\n  nginx path prefix: \"\/usr\"\n  nginx binary file: \"\/usr\/sbin\/nginx\"\n  nginx modules path: \"\/usr\/modules\"\n  nginx configuration prefix: \"\/etc\/nginx\"\n  nginx configuration file: \"\/etc\/nginx\/nginx.conf\"\n  nginx pid file: \"\/var\/run\/nginx.pid\"\n  nginx error log file: \"\/var\/log\/nginx\/error.log\"\n  nginx http access log file: \"\/var\/log\/nginx\/access.log\"\n  nginx http client request body temporary files: \"\/var\/lib\/nginx\/body\"\n  nginx http proxy temporary files: \"\/var\/lib\/nginx\/proxy\"\n  nginx http fastcgi temporary files: \"\/var\/lib\/nginx\/fastcgi\"\n<\/code><\/pre>\n\n\n\n<p>The above command generates a <code>Makefile<\/code> that can be used to compile Nginx. The compilation is done using the <code>make<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make<\/code><\/pre>\n\n\n\n<p>Once the compilation is done, run the install script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make install<\/code><\/pre>\n\n\n\n<p>Once the installation is complete, create Nginx dynamic data libraries directories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir -p \/var\/lib\/nginx\/{body,fastcgi}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring Nginx NAXSI<\/h3>\n\n\n\n<p>To begin with, copy the Naxsi core rules, <code>naxsi_core.rules<\/code> to Nginx configuration directory. The core rules are what makes the base of a WAF.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp ~\/naxsi-master\/naxsi_config\/naxsi_core.rules \/etc\/nginx\/<\/code><\/pre>\n\n\n\n<p>Now that the rules are in place, you need to enable these rules to act on per location basis. You can also define different types of attacks that can be blocked by Naxsi.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/nginx\/naxsi.rules<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nSecRulesEnabled;\nDeniedUrl \"\/RequestDenied\";\n\n## Check Naxsi rules\nCheckRule \"$SQL &gt;= 8\" BLOCK;\nCheckRule \"$RFI &gt;= 8\" BLOCK;\nCheckRule \"$TRAVERSAL &gt;= 4\" BLOCK;\nCheckRule \"$EVADE &gt;= 4\" BLOCK;\nCheckRule \"$XSS &gt;= 8\" BLOCK;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SecRulesEnabled<\/code> enables the Naxsi rules in a specific web location. <code>SecRulesEnabled<\/code> directive disables this.<\/li>\n\n\n\n<li>Note that you can set Naxsi in learning mode using the directive, <code>LearningMode<\/code>, where it automatically generates whitelisting rules based on website&#8217;s behavior. In this mode, Naxsi doesn&#8217;t block any attack.<\/li>\n\n\n\n<li>The <code>DeniedUrl<\/code> defines where Naxsi will redirect blocked requests.<\/li>\n\n\n\n<li>The <code>CheckRule<\/code> directive asks Naxsi to act on a specific request based on the score. The action ca either be <code>ALLOW, BLOCK, LOG, DROP<\/code>. The score level is between 0-9 and is set by the specific rules.<\/li>\n<\/ul>\n\n\n\n<p>Next, configure Nginx server to include these rules such that it looks like the below without comments.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/nginx\/nginx.conf<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>user  www-data;<\/strong>\nworker_processes  1;\nevents {\n    worker_connections  1024;\n}\nhttp {\n    include       mime.types;\n<strong>    include \/etc\/nginx\/naxsi_core.rules;<\/strong>\n<strong>    include \/etc\/nginx\/conf.d\/*.conf;<\/strong>\n<strong>    include \/etc\/nginx\/sites-enabled\/*;<\/strong>\n    default_type  application\/octet-stream;\n<strong>    access_log \/var\/log\/nginx\/access.log;<\/strong>\n<strong>    error_log \/var\/log\/nginx\/error.log;<\/strong>\n    sendfile        on;\n    keepalive_timeout  65;\n    server {\n        listen       80;\n        server_name  localhost;\n        location \/ {\n<strong>            include \/etc\/nginx\/naxsi.rules;<\/strong>\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>Save the file and verify that there is no syntactical errors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create Nginx Startup Service<\/h3>\n\n\n\n<p>Since Nginx was installed from the source, you need to create its startup script. Hence create Nginx systemd unit file as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/lib\/systemd\/system\/nginx.service<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n[Unit]\nDescription=A high performance web server and a reverse proxy server\nDocumentation=man:nginx(8)\nAfter=syslog.target network.target remote-fs.target nss-lookup.target\n\n[Service]\nType=forking\nPIDFile=\/run\/nginx.pid\nExecStartPre=\/usr\/sbin\/nginx -t\nExecStart=\/usr\/sbin\/nginx\nExecReload=\/usr\/sbin\/nginx -s reload\nExecStop=\/bin\/kill -s QUIT $MAINPID\nPrivateTmp=true\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reload Systemd Configurations<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl daemon-reload<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Start Nginx<\/h3>\n\n\n\n<p>Run the command below to start nginx.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl start nginx<\/code><\/pre>\n\n\n\n<p>You can check the status using the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status nginx<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<span style=\"color: #00ff00;\">\u25cf<\/span> nginx.service - A high performance web server and a reverse proxy server\n   Loaded: loaded (\/lib\/systemd\/system\/nginx.service; disabled; vendor preset: enabled)\n   Active: <span style=\"color: #00ff00;\">active (running)<\/span> since Wed 2019-01-23 19:57:22 UTC; 2min 54s ago\n     Docs: man:nginx(8)\n  Process: 28385 ExecStart=\/usr\/sbin\/nginx (code=exited, status=0\/SUCCESS)\n  Process: 28384 ExecStartPre=\/usr\/sbin\/nginx -t (code=exited, status=0\/SUCCESS)\n Main PID: 28386 (nginx)\n    Tasks: 2 (limit: 2325)\n   CGroup: \/system.slice\/nginx.service\n           \u251c\u250028386 nginx: master process \/usr\/sbin\/nginx\n           \u2514\u250028388 nginx: worker process\n<\/code><\/pre>\n\n\n\n<p>If you see a line, <code>nginx.service: Failed to parse PID from file \/run\/nginx.pid: Invalid argument<\/code>, here is a workaround.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/etc\/systemd\/system\/nginx.service.d<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>printf \"&#91;Service]\\nExecStartPost=\/bin\/sleep 0.1\\n\" &gt; \/etc\/systemd\/system\/nginx.service.d\/override.conf\nsystemctl daemon-reload<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Test Nginx-Naxsi WAF<\/h3>\n\n\n\n<p>Since Nginx-Naxsi is set up successfully, it is time to verify that Naxsi can actually block the attempted attacks. Let us begin by testing SQLi.<br>From a remote server, execute, <code>curl 'http:\/\/192.168.43.46\/?q=1\" or \"1\"=\"1\"'<\/code> while tailing error logs on the web server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/log\/nginx\/error.log<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n2019\/01\/23 20:17:36 [error] 28470#0: *3 NAXSI_FMT: ip=192.168.43.149&amp;server=192.168.43.46&amp;uri=\/&amp;vers=0.56&amp;total_processed=3&amp;total_blocked=3&amp;config=block&amp;cscore0=$SQL&amp;score0=40&amp;cscore1=$XSS&amp;score1=40&amp;zone0=ARGS&amp;id0=1001&amp;var_name0=q, client: 192.168.43.149, server: localhost, request: \"GET \/?q=1\" or \"1\"=\"1\" HTTP\/1.1\", host: \"192.168.43.46\"\n2019\/01\/23 20:17:36 [error] 28470#0: *3 open() \"\/usr\/html\/RequestDenied\" failed (2: No such file or directory), client: 192.168.43.149, server: localhost, request: \"GET \/?q=1\" or \"1\"=\"1\" HTTP\/1.1\", host: \"192.168.43.46\"\n<\/code><\/pre>\n\n\n\n<p>As you can see, the SQLi is blocked. Next, run the command below to test Cross-Site Scripting (XSS) by executing the command; <code>curl 'http:\/\/192.168.43.46\/?q=&lt;script&gt;alert(0);&lt;\/script&gt;'<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/log\/nginx\/error.log<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n2019\/01\/23 20:27:47 [error] 28470#0: *5 NAXSI_FMT: ip=192.168.43.149&amp;server=192.168.43.46&amp;uri=\/&amp;vers=0.56&amp;total_processed=5&amp;total_blocked=5&amp;config=block&amp;cscore0=$SQL&amp;score0=4&amp;cscore1=$XSS&amp;score1=8&amp;zone0=ARGS&amp;id0=1008&amp;var_name0=q, client: 192.168.43.149, server: localhost, request: \"GET \/?q=&lt;script>alert(0);&lt;\/script> HTTP\/1.1\", host: \"192.168.43.46\"\n2019\/01\/23 20:27:47 [error] 28470#0: *5 open() \"\/usr\/html\/RequestDenied\" failed (2: No such file or directory), client: 192.168.43.149, server: localhost, request: \"GET \/?q=&lt;script>alert(0);&lt;\/script> HTTP\/1.1\", host: \"192.168.43.46\"\n<\/code><\/pre>\n\n\n\n<p>Beautiful. Naxsi has successfull blocked attempted SQLi and XSS attacks on Nginx web server. Feel free to expore more about NASXI on their <a href=\"https:\/\/github.com\/nbs-system\/naxsi\/wiki\" target=\"_blank\" rel=\"noopener\">wiki page<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-modsecurity-with-nginx-on-rocky-linux-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Modsecurity with Nginx on Rocky Linux 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-modsecurity-3-with-apache-in-a-docker-container\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install ModSecurity 3 with Apache in a Docker Container<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our guide on how to install and configure NAXSI Nginx WAF on Ubuntu 18.04 LTS. NAXSI is an acronym for Nginx Anti XSS<\/p>\n","protected":false},"author":1,"featured_media":1884,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,304,34,305,253],"tags":[4884,306,4885,229,67,307],"class_list":["post-1882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-nginx","category-security","category-waf","category-web-servers","tag-install-naxsi-on-ubuntu","tag-naxsi","tag-naxsi-waf","tag-nginx","tag-ubuntu-18-04","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\/1882"}],"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=1882"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1882\/revisions"}],"predecessor-version":[{"id":21091,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1882\/revisions\/21091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/1884"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=1882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}