A web application firewall in front of Apache is not a silver bullet, but it is the cheapest single layer of defense against the long tail of automated probes that hit any internet-facing site. ModSecurity with the OWASP Core Rule Set (CRS) is the combination that has held up across a decade of changing exploit traffic. This article is the install on a modern Debian/Ubuntu box, the paranoia/anomaly tuning that makes the WAF usable in production, and the workflow for handling false positives without disabling the protection.
How to verify
Confirm what is installed and whether anything is actively scoring requests:
apachectl -M | grep -E 'security|unique_id'
ls /etc/modsecurity/ /usr/share/modsecurity-crs/ 2>/dev/null
grep -E 'SecRuleEngine|SecRequestBodyAccess' /etc/modsecurity/modsecurity.conf
sudo tail -f /var/log/apache2/modsec_audit.log
If ModSecurity is running with SecRuleEngine On, every blocked request appears in modsec_audit.log with the matched rule IDs, the request body, and the response. That audit log is the single most important source for tuning — false positives are diagnosed by reading the audit entry and finding the rule that matched.
What’s happening
ModSecurity is a request-inspection engine; the OWASP CRS is a curated rule set that runs on top of the engine. The flow on every request:
- ModSecurity inspects the request line, headers, and (if
SecRequestBodyAccess On) the body. - Each enabled CRS rule evaluates the request. Matches contribute to an “anomaly score” rather than blocking outright.
- After all rules have run, the cumulative score is compared to a threshold (
SecAction "inbound_anomaly_score_threshold=5"by default). - If the score exceeds the threshold, ModSecurity blocks the request with a 403 (or whatever
SecActiondeclares).
The anomaly scoring model is the key difference between CRS 3.x and older WAF rule sets. A single rule firing rarely means “block” — it adds 3 or 5 to the score. A real attack matches several rules and pushes the score above the threshold; a benign request that incidentally matches one rule does not get blocked.
The paranoia level controls how aggressive the rules are:
- PL1 (default) — stricter rules disabled, fewer false positives, good baseline.
- PL2 — adds tighter rules. Some legitimate traffic patterns may match.
- PL3 — strict patterns aimed at hardened apps.
- PL4 — maximum paranoia, expect false positives on most apps.
Most sites we run live at PL1 or PL2 with documented exclusions, not PL3 or PL4 across the board.
The procedure
-
Install ModSecurity and the CRS. Recent Ubuntu and Debian ship a
libapache2-mod-security2package; the CRS is inmodsecurity-crs:sudo apt install -y libapache2-mod-security2 modsecurity-crs sudo a2enmod security2 unique_id sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.confThe packaged CRS may lag the upstream release; for production, we install CRS from upstream
coreruleset.organd pin the version. -
Switch the engine on and configure the audit log. In
/etc/modsecurity/modsecurity.conf:SecRuleEngine On SecRequestBodyAccess On SecRequestBodyLimit 13107200 SecRequestBodyNoFilesLimit 524288 SecResponseBodyAccess Off SecAuditEngine RelevantOnly SecAuditLog /var/log/apache2/modsec_audit.log SecAuditLogParts ABIJDEFHZ SecAuditLogType SerialSecRuleEngine Onenables blocking. Start withDetectionOnlyfor the first 48 hours to see what would have been blocked, then flip toOn.SecAuditLogParts ABIJDEFHZwrites the request, response, and rule match details — verbose, but invaluable for tuning. -
Wire the CRS into Apache. Create
/etc/apache2/conf-available/modsecurity-crs.conf:<IfModule security2_module> Include /usr/share/modsecurity-crs/crs-setup.conf Include /usr/share/modsecurity-crs/rules/*.conf </IfModule>Then enable and reload:
sudo a2enconf modsecurity-crs sudo apache2ctl configtest sudo systemctl reload apache2 -
Set the paranoia level explicitly. In
/usr/share/modsecurity-crs/crs-setup.conf:SecAction "id:900000, phase:1, nolog, pass, t:none, setvar:tx.paranoia_level=1" SecAction "id:900110, phase:1, nolog, pass, t:none, \ setvar:tx.inbound_anomaly_score_threshold=5, \ setvar:tx.outbound_anomaly_score_threshold=4"Default thresholds (5 inbound, 4 outbound) are reasonable for PL1.
-
Run in DetectionOnly first, then flip. For 48 hours, run with
SecRuleEngine DetectionOnly. Watch the audit log for matches. Anything that would have blocked a legitimate request shows up; either tune the rule or add an exclusion.To exclude a specific rule for a specific URL or vhost:
# Within <VirtualHost> or <LocationMatch> <LocationMatch "^/api/upload"> SecRuleRemoveById 200002 200003 </LocationMatch>Find the rule ID in the audit log entry of the false positive.
-
Flip to enforcing. After 48 hours of
DetectionOnlyreview:SecRuleEngine OnReload Apache. Watch the audit log for the first day — real false positives in the wild surface fast.
-
Set up alerting on the audit log. A high block rate is a signal — either a real attack or a regression in the app. Pipe
modsec_audit.logthrough Loki or your SIEM, alert on >N blocks per minute against a single source.
Common pitfalls
SecRequestBodyAccess Onrequires Apache to buffer the entire request body before passing it to the upstream — for file uploads larger thanSecRequestBodyLimit, ModSecurity returns 413 unlessSecRequestBodyLimitAction ProcessPartialis set.- The CRS ships rules that block common patterns like single quotes in form fields — legitimate user input. Tune via exclusions, do not just lower paranoia.
- Without
unique_idmodule loaded, the audit log entries lack a correlation ID — debugging across the access log and audit log becomes painful. - A typo in
Includepaths silently skips CRS rules; configtest does not catch missing includes. Verify withapachectl -M | grep securityand at least one test block. - DetectionOnly mode does not block, but it does generate audit log entries — disk fills if you forget to rotate.
Stack Harbor runs ModSecurity + CRS on internet-facing Apache fronts as a default — the audit logs flow into a search index, the exclusions are kept under config management, and false positives become tickets rather than silent breakage. If you have Apache sites taking abusive automated traffic and no inline filtering, deploying a tuned WAF is one of the first things we land in Managed Operations.