A comprehensive guide to configuring ModSecurity Web Application Firewall (WAF) in Apache2 for enhanced security.
Before configuring ModSecurity on Apache2, ensure you have:
# For Ubuntu/Debian
sudo apt-get update
sudo apt-get install libapache2-mod-security2
# For CentOS/RHEL
sudo yum install mod_security
# Enable the module
sudo a2enmod security2
sudo systemctl restart apache2
# /etc/apache2/mods-available/security2.conf
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess On
SecRequestBodyLimit 13107200
SecRequestBodyNoFilesLimit 131072
SecRequestBodyInMemoryLimit 131072
SecRequestBodyLimitAction Reject
SecRule REQUEST_HEADERS:Content-Type "text/xml" \
"id:'200000',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=XML"
Key directives explained:
# Download OWASP CRS
cd /etc/apache2/
sudo git clone https://github.com/coreruleset/coreruleset.git
# Include in Apache configuration
Include /etc/apache2/coreruleset/crs-setup.conf
Include /etc/apache2/coreruleset/rules/*.conf
Common rule categories:
# ModSecurity configuration will appear here