Setting up ModSecurity WAF for Apache Webserver
2 minutes read •
Install ModSecurity
Setup Core Rule Set
We can learn more on Core Rule Set Website and Core Rule Set Github
First backup the existing ruleset
Download or clone the source and signature
- To Download:
- To clone:
Verify Signatures
- Import GPG Keys:
To retrieve the CRS project’s public key from public key servers using
gpg, execute:gpg --keyserver pgp.mit.edu --recv 0x38EEACA1AB8A6E72(this ID should be equal to the last sixteen hex characters in the fingerprint).
It is also possible to use gpg --fetch-key https://coreruleset.org/security.asc to retrieve the key directly.
- To verify the integrity of the release:
This should return:
If the signature was good then the verification succeeds. If a warning is displayed, like the above, it means the CRS project’s public key is known but is not trusted.
- To trust the CRS project’s public key:
)
- The result when verifying a release will then look like so:
Extract Core Rule Set
Activating the CRS
Note: We could also setup the crs in /etc/crs instead of /usr/share/modsecurity-crs.
(Optional)
In addition to crs-setup.conf.example, there are two other “.example” files within the CRS repository. These are:
These files are designed to provide the rule maintainer with the ability to modify rules (see false positives and tuning) without breaking forward compatibility with rule set updates. These two files should be renamed by removing the .example suffix. This will mean that installing updates will not overwrite custom rule exclusions. To rename the files in Linux, use a command similar to the following:
Configuring and Setting Up ModSecurity
Activate the config
 :
Configure Apache
# We can put these configs the very beginning of the config. You can try a different place if it causes any isues.
<IfModule
</IfModule>
Also it’s a good practice to include similar configs in the apache default sites-enabled config file.
# Now put the configs here. We can put the configs After "DocumentRoot" Inside "<VirtualHost *:80>".
# Note: In this config we didn't include "/etc/modsecurity/modsecurity.conf" because in our testing it was proving to be a misconfiguration.
<IfModule
</IfModule>
Activate ModSecurity
# Change the config from "SecRuleEngine DetectionOnly" to "SecRuleEngine On"
Now Restart Apache
Now we can test the security. (Recommended)
Always verify that CRS is installed correctly by sending a ‘malicious’ request to your site or application, for instance:
Depending on your configurated thresholds, this should be detected as a malicious request. If you use blocking mode, you should receive an Error 403. The request should also be logged to the audit log, which is usually in /var/log/modsec_audit.log.
Done!