how to install modsecurity for nginx and install owasp core rule set
- User
how to install modsecurity for nginx and install owasp core rule set
- Assistant
how to install modsecurity for nginx and install owasp core rule set
Certainly! Below are step-by-step instructions for installing ModSecurity as a module for Nginx and installing the OWASP Core Rule Set (CRS) on a Linux system (example based on Ubuntu/Debian). Similar steps apply to other distros with slight variations in package management.
Update package lists and install required packages to build ModSecurity:
sudo apt update
sudo apt install -y git build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev libxml2 libxml2-dev libyajl-dev
The newer ModSecurity (v3) is a standalone library that works with Nginx connector.
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git submodule init
git submodule update
./build.sh
./configure
make
sudo make install
cd ..
You need the ModSecurity Nginx module source to build Nginx with it.
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
To run ModSecurity with Nginx, you need to compile Nginx from source with the ModSecurity module.
nginx -v
NGINX_VERSION=<your_nginx_version> # e.g. 1.22.0
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
tar zxvf nginx-$NGINX_VERSION.tar.gz
cd nginx-$NGINX_VERSION/
./configure --add-dynamic-module=../ModSecurity-nginx
make modules
sudo cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/
If you want, you can also recompile nginx fully to replace your binary.
Some distros or third-party repos provide Nginx with ModSecurity included.
Edit /etc/nginx/nginx.conf or main conf file:
Add at the top (main context):
load_module modules/ngx_http_modsecurity_module.so;
Create ModSecurity configuration directory:
sudo mkdir /etc/modsecurity
sudo cp /usr/local/modsecurity/etc/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
Edit /etc/modsecurity/modsecurity.conf:
SecRuleEngine On (to enable detection + blocking modes):SecRuleEngine On
Download CRS:
cd /etc/modsecurity
sudo git clone https://github.com/coreruleset/coreruleset.git
cd coreruleset
sudo mv crs-setup.conf.example crs-setup.conf
Edit your Nginx site config (e.g., /etc/nginx/sites-enabled/default) or main conf to include ModSecurity:
server {
listen 80;
server_name example.com;
modsecurity on;
modsecurity_rules_file /etc/modsecurity/modsecurity.conf;
location / {
# Your usual configs here
proxy_pass http://localhost:8080;
}
}
Optionally, include CRS inside /etc/modsecurity/modsecurity.conf or via a modsecurity_rules_file:
Create a file /etc/modsecurity/crs.conf with:
Include /etc/modsecurity/coreruleset/crs-setup.conf
Include /etc/modsecurity/coreruleset/rules/*.conf
And in your site config:
modsecurity_rules_file /etc/modsecurity/crs.conf;
Test config:
sudo nginx -t
Reload:
sudo systemctl reload nginx
/var/log/nginx/error.log) for ModSecurity loading errors./var/log/modsec_audit.logIf you prefer use docker or other OS, let me know!
Not yet included in a published release.