Configure NGINX and install a Web Application Firewall (WAF) to enhance the security of your web application.
- Install Nginx:
- If you haven't installed Nginx, do so based on your operating system. For example:
- Ubuntu/Debian:
sudo apt-get install nginx - CentOS:
sudo yum install nginx
- Ubuntu/Debian:
- If you haven't installed Nginx, do so based on your operating system. For example:
-
Install all the dependencies required for the build and compilation process with the following command:
sudo apt-get install bison build-essential ca-certificates curl dh-autoreconf doxygen \ flex gawk git iputils-ping libcurl4-gnutls-dev libexpat1-dev libgeoip-dev liblmdb-dev \ libpcre3-dev libpcre++-dev libssl-dev libtool libxml2 libxml2-dev libyajl-dev locales \ lua5.3-dev pkg-config wget zlib1g-dev libxslt1-dev libgd-dev
-
Clone the ModSecurity Github repository from the /opt directory:
cd /opt && sudo git clone https://github.com/SpiderLabs/ModSecurity && cd ModSecurity
-
Run the following git commands to initialize and update the submodule:
sudo git submodule init sudo git submodule update
-
Run the build.sh script:
sudo ./build.sh
-
Run the configure file, which is responsible for getting all the dependencies for the build process:
sudo ./configure
-
Run the make command to build ModSecurity:
sudo make
-
After the build process is complete, install ModSecurity by running the following command:
sudo make install
-
Clone the Nginx-connector from the /opt directory:
cd /opt && sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
-
Enumerate the version of Nginx you have installed:
nginx -v
-
Download the exact version of Nginx running on your system into the /opt directory:
cd /opt && sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
-
Extract the tarball:
sudo tar -xvzmf nginx-1.18.0.tar.gz
-
Change your directory to the tarball directory you just extracted:
cd nginx-1.18.0 -
Display the configure arguments used for your version of Nginx:
nginx -V
-
Below is an example output for Nginx 1.18.0:
root@vagrant:/var/log/nginx# nginx -V nginx version: nginx/1.18.0 (Ubuntu) built with OpenSSL 1.1.1f 31 Mar 2020 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
-
-
Compile the Modsecurity module, copy all of the arguments following configure arguments: from your output of the above command and paste them in place of
Configure Argumentsin the following command:sudo ./configure --add-dynamic-module=../ModSecurity-nginx <Configure Arguments>
-
Build the modules with the following command:
sudo make modules
-
Create a directory for the Modsecurity module in your system’s Nginx configuration folder:
sudo mkdir /etc/nginx/modules
-
Copy the compiled Modsecurity module into your Nginx configuration folder:
sudo cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules
-
Open the
/etc/nginx/nginx.conffile with a text editor such a vim and add the following line:load_module /etc/nginx/modules/ngx_http_modsecurity_module.so;
-
Delete the current rule set that comes prepackaged with ModSecurity by running the following command: :
sudo rm -rf /usr/share/modsecurity-crs
-
Clone the OWASP-CRS GitHub repository into the
/usr/share/modsecurity-crsdirectory: :sudo git clone https://github.com/coreruleset/coreruleset /usr/local/modsecurity-crs
-
Rename the
crs-setup.conf.exampletocrs-setup.conf:sudo mv /usr/local/modsecurity-crs/crs-setup.conf.example /usr/local/modsecurity-crs/crs-setup.conf
-
Rename the default request exclusion rule file:
sudo mv /usr/local/modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example /usr/local/modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
-
Create a ModSecurity directory in the
/etc/nginx/directory:sudo mkdir -p /etc/nginx/modsec
-
Copy over the unicode mapping file and the ModSecurity configuration file from your cloned ModSecurity GitHub repository:
sudo cp /opt/ModSecurity/unicode.mapping /etc/nginx/modsec sudo cp /opt/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf
-
Change the value for
SecRuleEnginetoOnin/etc/modsecurity/modsecurity.confsudo sed -i 's/SecRuleEngine/On/g' /etc/modsecurity/modsecurity.conf -
Create a new configuration file called
main.confunder the/etc/nginx/modsecdirectory:sudo touch /etc/nginx/modsec/main.conf
-
Specify the rules and the Modsecurity configuration file for Nginx by inserting following lines:
Include /etc/nginx/modsec/modsecurity.conf Include /usr/local/modsecurity-crs/crs-setup.conf Include /usr/local/modsecurity-crs/rules/*.conf
-
Edit
/etc/nginx/sites-available/defaultand insert the following lines in your server block: :modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf;
-
Restart the nginx service to apply the configuration:
sudo nginx -t sudo systemctl reload nginx
-
Test ModSecurity by performing a simple local file inclusion attack by running the following command:
curl http://<SERVER-IP>/index.html?exec=/bin/bash
- If ModSecurity has been configured correctly and is actively blocking attacks, the following error is returned:
root@vagrant:/var/log/nginx# curl http://127.0.0.1/index.html?exec=/bin/bash <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.18.0 (Ubuntu)</center> </body> </html> root@vagrant:/var/log/nginx#
-
Check nginx error log