{"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
Welcome to our guide on how to install and configure NAXSI Nginx WAF on Ubuntu 18.04 LTS.<\/p>\n\n\n
NAXSI is an acronym for Nginx Anti XSS and SQL injection. It is an opensource, 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 Update and upgrade your system packages;<\/p>\n\n\n\n Install the required dependencies.<\/p>\n\n\n\n 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 Download the latest sources of Nginx from here<\/a>. You can simply obtain the link and pull it with Unzip the source code;<\/p>\n\n\n\n Download and unzip naxis.<\/p>\n\n\n\n Navigate to Nginx source directory and run the If all is well, you should be able to see the configuration summary;<\/p>\n\n\n\n The above command generates a Once the compilation is done, run the install script.<\/p>\n\n\n\n Once the installation is complete, create Nginx dynamic data libraries directories.<\/p>\n\n\n\n To begin with, copy the Naxsi core rules, 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 Next, configure Nginx server to include these rules such that it looks like the below without comments.<\/p>\n\n\n\n Save the file and verify that there is no syntactical errors.<\/p>\n\n\n\n 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 Run the command below to start nginx.<\/p>\n\n\n\n You can check the status using the command below;<\/p>\n\n\n\n If you see a line, 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. As you can see, the SQLi is blocked. Next, run the command below to test Cross-Site Scripting (XSS) by executing the command; Beautiful. Naxsi has successfull blocked attempted SQLi and XSS attacks on Nginx web server. Feel free to expore more about NASXI on their wiki page<\/a>.<\/p>\n\n\n\n Install Modsecurity with Nginx on Rocky Linux 8<\/a><\/p>\n\n\n\nInstalling NAXSI Nginx WAF on Ubuntu 18.04 LTS<\/h2>\n\n\n\n
Install Nginx-Naxsi on Ubuntu 18.04 LTS<\/h3>\n\n\n\n
apt update\napt upgrade<\/code><\/pre>\n\n\n\n
apt install libpcre3-dev libssl-dev unzip build-essential daemon libxml2-dev libxslt1-dev libgd-dev libgeoip-dev<\/code><\/pre>\n\n\n\n
wget<\/code> as shown below;<\/p>\n\n\n\n
wget http:\/\/nginx.org\/download\/nginx-1.15.8.tar.gz<\/code><\/pre>\n\n\n\n
tar xzf nginx-1.15.8.tar.gz<\/code><\/pre>\n\n\n\n
wget https:\/\/github.com\/nbs-system\/naxsi\/archive\/master.zip<\/code><\/pre>\n\n\n\n
unzip master.zip<\/code><\/pre>\n\n\n\n
Configure Nginx for Naxsi support<\/h3>\n\n\n\n
configure<\/code> script to prepare Nginx for compilation as shown below;<\/p>\n\n\n\n
cd nginx-1.15.8<\/code><\/pre>\n\n\n\n
\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
\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
Makefile<\/code> that can be used to compile Nginx. The compilation is done using the
make<\/code> command.<\/p>\n\n\n\n
make<\/code><\/pre>\n\n\n\n
make install<\/code><\/pre>\n\n\n\n
mkdir -p \/var\/lib\/nginx\/{body,fastcgi}<\/code><\/pre>\n\n\n\n
Configuring Nginx NAXSI<\/h3>\n\n\n\n
naxsi_core.rules<\/code> to Nginx configuration directory. The core rules are what makes the base of a WAF.<\/p>\n\n\n\n
cp ~\/naxsi-master\/naxsi_config\/naxsi_core.rules \/etc\/nginx\/<\/code><\/pre>\n\n\n\n
vim \/etc\/nginx\/naxsi.rules<\/code><\/pre>\n\n\n\n
\nSecRulesEnabled;\nDeniedUrl \"\/RequestDenied\";\n\n## Check Naxsi rules\nCheckRule \"$SQL >= 8\" BLOCK;\nCheckRule \"$RFI >= 8\" BLOCK;\nCheckRule \"$TRAVERSAL >= 4\" BLOCK;\nCheckRule \"$EVADE >= 4\" BLOCK;\nCheckRule \"$XSS >= 8\" BLOCK;\n<\/code><\/pre>\n\n\n\n
\n
SecRulesEnabled<\/code> enables the Naxsi rules in a specific web location.
SecRulesEnabled<\/code> directive disables this.<\/li>\n\n\n\n
LearningMode<\/code>, where it automatically generates whitelisting rules based on website’s behavior. In this mode, Naxsi doesn’t block any attack.<\/li>\n\n\n\n
DeniedUrl<\/code> defines where Naxsi will redirect blocked requests.<\/li>\n\n\n\n
CheckRule<\/code> directive asks Naxsi to act on a specific request based on the score. The action ca either be
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
vim \/etc\/nginx\/nginx.conf<\/code><\/pre>\n\n\n\n
\nuser www-data;<\/strong>\nworker_processes 1;\nevents {\n worker_connections 1024;\n}\nhttp {\n include mime.types;\n include \/etc\/nginx\/naxsi_core.rules;<\/strong>\n include \/etc\/nginx\/conf.d\/*.conf;<\/strong>\n include \/etc\/nginx\/sites-enabled\/*;<\/strong>\n default_type application\/octet-stream;\n access_log \/var\/log\/nginx\/access.log;<\/strong>\n 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 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
nginx -t<\/code><\/pre>\n\n\n\n
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
Create Nginx Startup Service<\/h3>\n\n\n\n
vim \/lib\/systemd\/system\/nginx.service<\/code><\/pre>\n\n\n\n
\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
Reload Systemd Configurations<\/h3>\n\n\n\n
systemctl daemon-reload<\/code><\/pre>\n\n\n\n
Start Nginx<\/h3>\n\n\n\n
systemctl start nginx<\/code><\/pre>\n\n\n\n
systemctl status nginx<\/code><\/pre>\n\n\n\n
\n\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: 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
nginx.service: Failed to parse PID from file \/run\/nginx.pid: Invalid argument<\/code>, here is a workaround.<\/p>\n\n\n\n
mkdir \/etc\/systemd\/system\/nginx.service.d<\/code><\/pre>\n\n\n\n
printf \"[Service]\\nExecStartPost=\/bin\/sleep 0.1\\n\" > \/etc\/systemd\/system\/nginx.service.d\/override.conf\nsystemctl daemon-reload<\/code><\/pre>\n\n\n\n
Test Nginx-Naxsi WAF<\/h3>\n\n\n\n
From a remote server, execute, 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
tail -f \/var\/log\/nginx\/error.log<\/code><\/pre>\n\n\n\n
\n2019\/01\/23 20:17:36 [error] 28470#0: *3 NAXSI_FMT: ip=192.168.43.149&server=192.168.43.46&uri=\/&vers=0.56&total_processed=3&total_blocked=3&config=block&cscore0=$SQL&score0=40&cscore1=$XSS&score1=40&zone0=ARGS&id0=1001&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
curl 'http:\/\/192.168.43.46\/?q=<script>alert(0);<\/script>'<\/code><\/p>\n\n\n\n
tail -f \/var\/log\/nginx\/error.log<\/code><\/pre>\n\n\n\n
\n2019\/01\/23 20:27:47 [error] 28470#0: *5 NAXSI_FMT: ip=192.168.43.149&server=192.168.43.46&uri=\/&vers=0.56&total_processed=5&total_blocked=5&config=block&cscore0=$SQL&score0=4&cscore1=$XSS&score1=8&zone0=ARGS&id0=1008&var_name0=q, client: 192.168.43.149, server: localhost, request: \"GET \/?q=<script>alert(0);<\/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=<script>alert(0);<\/script> HTTP\/1.1\", host: \"192.168.43.46\"\n<\/code><\/pre>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n