- Overview
- Requirements
- Usage
- Configuration Options
- What the Script Does
- Post Installation
- Troubleshooting
- Security Considerations
- Contributing
The setup-modsec.sh script installs and configures:
- ModSecurity v3 (from source)
- Nginx with ModSecurity connector
- OWASP Core Rule Set (CRS)
- Ubuntu 20.04 to 22.04 LTS
- x86_64 architecture
- Root privileges
- Nginx version 1.21.5 or higher (will be installed if not present)
Note: This script has been tested only on Ubuntu 20.04 and 22.04 LTS. Other Ubuntu versions or Linux distributions may not work correctly.
-
Clone the repository:
git clone https://github.com/zAbuQasem/Setup-Modsecurity-v3.git cd Setup-Modsecurity-v3 -
Run the script with root user:
sudo ./setup-modsec.sh
The script supports the following environment variables:
AUTO_INSTALL: Set tofalse(default). Set totruefor automated installation without prompts.
Example with custom configuration:
sudo AUTO_INSTALL=true ./setup-modsec.sh- Checks system requirements (Ubuntu version, architecture, Nginx version)
- Installs necessary dependencies
- Compiles ModSecurity v3 from source
- Installs or configures Nginx with ModSecurity connector
- Installs and configures OWASP Core Rule Set
After running the script, you need to enable ModSecurity in your Nginx configuration:
-
Edit the Nginx main configuration to load the ModSecurity module:
sudo nano /etc/nginx/nginx.conf
Add this line at the beginning of the file:
load_module /etc/nginx/modules-enabled/ngx_http_modsecurity_module.so; -
Modify your server block to activate ModSecurity:
sudo nano /etc/nginx/sites-enabled/default
Add these lines inside the server block:
modsecurity on; modsecurity_rules_file /etc/nginx/modsecurity.conf; -
Enable the ModSecurity rule engine:
sudo nano /etc/nginx/modsecurity.conf
Change
SecRuleEngine DetectionOnlyto:SecRuleEngine On -
Test and restart Nginx:
sudo nginx -t sudo systemctl restart nginx
If the response returns a 403 Forbidden status, it indicates that ModSecurity is functioning as expected.
curl http://localhost --data "testparam=<script>alert(1)</script>"If you encounter issues:
- Check the script output for error messages
- Verify your system meets the requirements
- Ensure Nginx configuration is correct:
nginx -t - Check ModSecurity logs for rule violations
sudo tail -f /var/log/modsec_audit.log
- The script installs ModSecurity in detection mode by default
- Review and customize the rule set according to your security needs
- Regularly update the Core Rule Set to protect against new vulnerabilities
Contributions are welcome! Please feel free to submit a Pull Request.