Skip to content

ModSecurity on Apache with the OWASP Core Rule Set

Install and tune ModSecurity as a WAF in front of Apache — CRS install, paranoia levels, anomaly scoring, exclusions, and the audit log workflow.

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:

  1. ModSecurity inspects the request line, headers, and (if SecRequestBodyAccess On) the body.
  2. Each enabled CRS rule evaluates the request. Matches contribute to an “anomaly score” rather than blocking outright.
  3. After all rules have run, the cumulative score is compared to a threshold (SecAction "inbound_anomaly_score_threshold=5" by default).
  4. If the score exceeds the threshold, ModSecurity blocks the request with a 403 (or whatever SecAction declares).

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

  1. Install ModSecurity and the CRS. Recent Ubuntu and Debian ship a libapache2-mod-security2 package; the CRS is in modsecurity-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.conf

    The packaged CRS may lag the upstream release; for production, we install CRS from upstream coreruleset.org and pin the version.

  2. 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 Serial

    SecRuleEngine On enables blocking. Start with DetectionOnly for the first 48 hours to see what would have been blocked, then flip to On. SecAuditLogParts ABIJDEFHZ writes the request, response, and rule match details — verbose, but invaluable for tuning.

  3. 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
  4. 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.

  5. 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.

  6. Flip to enforcing. After 48 hours of DetectionOnly review:

    SecRuleEngine On

    Reload Apache. Watch the audit log for the first day — real false positives in the wild surface fast.

  7. 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.log through Loki or your SIEM, alert on >N blocks per minute against a single source.

Common pitfalls

  • SecRequestBodyAccess On requires Apache to buffer the entire request body before passing it to the upstream — for file uploads larger than SecRequestBodyLimit, ModSecurity returns 413 unless SecRequestBodyLimitAction ProcessPartial is 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_id module loaded, the audit log entries lack a correlation ID — debugging across the access log and audit log becomes painful.
  • A typo in Include paths silently skips CRS rules; configtest does not catch missing includes. Verify with apachectl -M | grep security and 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.