ModSecurity and OWASP CRS

This cheat sheet brings together the settings of ModSecurity, the open-source WAF engine for web servers, and of OWASP CRS, the rule set that detects common attacks on websites: operating modes, request body inspection and the audit log, Paranoia Level and anomaly scoring, finding the rule that blocked a request in the log, and handling false positives the right way. You can search for a setting, filter by topic, and copy it with one click.

Enabling the engine and operating modes

  • SecRuleEngine DetectionOnly

    The engine inspects every request and logs the rules that matched, but doesn't block anything. This is the recommended mode to start in, and it's the value in the recommended configuration file that ships with ModSecurity.

  • SecRuleEngine On

    The engine inspects requests and blocks them according to the rules. Switch to this mode after a testing period in DetectionOnly and after you exclude the false positives.

  • SecRuleEngine Off

    Turns off rule processing. This is the engine's default when the directive isn't set. Caution: the website is left without WAF protection.

  • sudo cp modsecurity.conf-recommended modsecurity.conf

    The recommended configuration file ships with ModSecurity as modsecurity.conf-recommended. Copy it to modsecurity.conf and edit the copy.

  • LoadModule security2_module modules/mod_security2.so

    Loads ModSecurity as an Apache module.

  • load_module modules/ngx_http_modsecurity_module.so;

    Loads the ModSecurity-nginx connector as a dynamic Nginx module. The line goes at the top of the nginx.conf file.

  • modsecurity on;

    Enables ModSecurity in Nginx. You can place it in an http, server, or location block.

  • modsecurity_rules_file /etc/nginx/modsec/main.conf;

    The file Nginx loads the engine settings and the rules from. In this file, add Include lines for modsecurity.conf and for the CRS files. The path is an example.

  • modsecurity_rules 'SecRuleEngine DetectionOnly';

    Writes a ModSecurity directive directly in the Nginx configuration, for example to enable DetectionOnly mode (logging only) in a single location block.

Request body and audit log

  • SecRequestBodyAccess On

    Lets the engine inspect the request body, such as form data and POST requests. When this setting is off, the rules don't see what's submitted in forms.

  • SecRequestBodyLimit 13107200

    The maximum request body size in bytes, including uploaded files. In the recommended file, the value is about 12.5MB, and a larger request is rejected. Raise the value when the website accepts large uploads.

  • SecRequestBodyNoFilesLimit 131072

    The maximum request body size without the attached files, for example a long form or a JSON request. In the recommended file, the value is 128KB.

  • SecRequestBodyLimitAction Reject

    What happens to a request that exceeds the limit: Reject rejects it, and ProcessPartial inspects only the part within the limit. In DetectionOnly mode, the engine switches to ProcessPartial automatically.

  • SecResponseBodyAccess On

    Inspects the body of the response the server sends, for example to detect data leakage. In the recommended file, the value is On, and the engine's default is Off. The inspection uses extra memory and processing resources.

  • SecAuditEngine RelevantOnly

    The audit log records only requests that triggered a warning or an error, or that ended with a relevant status code. The value On records every request and is suitable only for a short test.

  • SecAuditLogRelevantStatus "^(?:5|4(?!04))"

    Which status codes count as relevant in RelevantOnly mode: here, all 5xx and 4xx errors except 404.

  • SecAuditLog /var/log/modsec_audit.log

    The file the audit log is written to. This is the path in the recommended file, and distributions and control panels sometimes change it.

  • SecAuditLogParts ABIJDEFHZ

    Which parts of the request and response are saved in each entry. Part H holds the messages of the rules that matched, including the ID of each rule.

  • SecAuditLogFormat JSON

    Writes the audit log in JSON format, which is convenient for log analysis tools. Requires ModSecurity to be built with YAJL support.

  • SecDebugLogLevel 3

    The verbosity of the debug log, from 0 (no logging) to 9 (everything). The file is set with SecDebugLog. Level 4 and above slows down the server, so raise it only for a short test.

OWASP CRS: files and load order

  • sudo mv crs-setup.conf.example crs-setup.conf

    The CRS configuration file, where you set the Paranoia Level, the score thresholds, and more. After installation, remove the .example extension and edit the file.

  • rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

    The file for exclusions that must run before the CRS rules, such as a rule with ctl that excludes a parameter on a specific URL. The file ships with the .example extension, which you remove. A CRS update doesn't overwrite this file.

  • rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

    The file for exclusions written after the rules are loaded, such as SecRuleRemoveById and SecRuleUpdateTargetById. It also ships with the .example extension.

  • IncludeOptional /etc/crs4/crs-setup.conf

    The first line of the CRS load order on Apache. After it come the plugin configuration files, the plugins loaded before the rules, the rules themselves, and finally the plugins loaded after them. The /etc/crs4 path is the example from the CRS documentation.

  • IncludeOptional /etc/crs4/rules/*.conf

    Loads all the CRS rule files, including the REQUEST-900 and RESPONSE-999 exclusion files, in name order. On Nginx, write the same lines with Include in the file set in modsecurity_rules_file.

  • IncludeOptional /etc/crs4/plugins/*-before.conf

    CRS plugins add ready-made rules or exclusions for applications. Each plugin comes as files ending in config, before, and after, which you copy to the plugins folder next to the rules folder.

  • wordpress-rule-exclusions-plugin

    The official CRS plugin for WordPress exclusions, which prevents false positives in normal admin dashboard actions. Application exclusions are no longer part of CRS itself; they come as plugins like this one.

Paranoia Level and anomaly scoring

  • SecAction "id:900000,phase:1,pass,t:none,nolog,setvar:tx.blocking_paranoia_level=1"

    The CRS blocking level, from 1 to 4. The default is 1, which produces almost no false positives. Each level adds rules, and with them more false positives to exclude. In crs-setup.conf, uncomment rule 900000 and change the value.

  • SecAction "id:900001,phase:1,pass,t:none,nolog,setvar:tx.detection_paranoia_level=2"

    Runs rules from a higher level for logging only, so they don't add to the score that decides blocking. This lets you see what would happen at the next level before you raise it. The value can't be lower than the blocking level.

  • SecAction "id:900110,phase:1,pass,t:none,nolog,setvar:tx.inbound_anomaly_score_threshold=5,setvar:tx.outbound_anomaly_score_threshold=4"

    The blocking score thresholds: 5 for requests and 4 for responses, by default. Every rule that matches adds to the request's score, and the block happens only when the score reaches the threshold. A higher threshold reduces false positives, but it also lowers the level of protection.

  • severity:'CRITICAL'

    A rule's severity sets how much it adds to the score: CRITICAL adds 5, ERROR adds 4, WARNING adds 3, and NOTICE adds 2. At the default threshold, a single critical rule is enough to block a request.

  • id "949110"

    The rule that blocks a request when its score reaches the threshold, with the message Inbound Anomaly Score Exceeded. It only sums up the score: the rules that caused the block appear in the log for the same request, and those are the ones you exclude. Don't remove 949110 itself.

Reading the logs and finding the rule ID

  • sudo grep ModSecurity /var/log/httpd/error_log | tail -n 20

    Shows the latest ModSecurity messages in the Apache error log. On Debian and Ubuntu, the log is at /var/log/apache2/error.log.

  • sudo grep -o 'id "[0-9]*"' /var/log/httpd/error_log | sort | uniq -c | sort -rn | head

    Counts how many times each rule matched and shows the most frequent ones. This is how you spot a rule that keeps matching a normal action on the website.

  • sudo grep 203.0.113.10 /var/log/httpd/error_log | grep -o 'id "[0-9]*"'

    Shows the IDs of the rules that matched requests from a specific IP address. When a visitor reports a 403 error, search by their address.

  • sudo grep ModSecurity /var/log/nginx/error.log | tail -n 20

    The same check in the Nginx error log, where the ModSecurity-nginx connector writes its messages.

  • sudo tail -f /var/log/modsec_audit.log

    Shows the audit log in real time. Useful while you repeat the blocked action on the website.

  • sudo grep -o 'id "[0-9]*"' /var/log/modsec_audit.log | sort | uniq -c | sort -rn | head

    Counts the rule IDs in the audit log, like the check in the error log.

  • Access denied with code 403

    This is what a block looks like in the log: the line includes the status code, the phase, the rule ID in the id field, its message in the msg field, and the URL that was inspected. In DetectionOnly mode, the line starts with Warning instead of Access denied, and the request isn't blocked.

Handling false positives

  • SecRuleUpdateTargetById 941320 "!ARGS:wp_post"

    Excludes one parameter from one rule, and all the other checks stay in place. This is the precise approach when the block is caused by a specific field, such as the content of a text editor. It goes in the RESPONSE-999 file.

  • SecRule REQUEST_URI "@beginsWith /dynamic/new_post" "id:1020,phase:1,pass,nolog,ctl:ruleRemoveTargetById=941150;ARGS:text_input"

    Excludes a parameter from a rule on one URL only, and the protection stays complete on the rest of the website. It goes in the REQUEST-900 file, before the CRS rules.

  • SecRule REQUEST_URI "@beginsWith /webapp/function.php" "id:1000,phase:1,pass,nolog,ctl:ruleRemoveById=920230"

    Disables an entire rule on one URL only. This kind of exclusion also goes in the REQUEST-900 file.

  • SecRuleRemoveById 933151

    Disables a rule across the whole website. Suitable for a rule that isn't relevant to the website, for example a rule that checks for a programming language the website doesn't use. You can list several IDs and ranges on one line. It goes in the RESPONSE-999 file.

  • SecRule REQUEST_URI "@beginsWith /api/webhook" "id:1050,phase:1,pass,nolog,ctl:ruleEngine=DetectionOnly"

    Switches one URL to DetectionOnly mode (logging only), for example while you test a new area of the website. Caution: the URL isn't protected until you remove the rule.

  • id:1000

    For local exclusion rules, choose IDs in the range 1 to 99,999. The range 900,000 to 999,999 is reserved for CRS rules. Every rule needs a unique ID.

Testing and reloading

  • sudo apachectl configtest

    Checks the syntax of the Apache configuration, including the ModSecurity directives, before a reload.

  • sudo systemctl reload httpd

    Reloads Apache with the new configuration. On Debian and Ubuntu, the service is named apache2.

  • sudo nginx -t

    Checks the syntax of the Nginx configuration and of the files loaded through modsecurity_rules_file.

  • sudo systemctl reload nginx

    Reloads Nginx with the new configuration.

  • da build set modsecurity yes

    In DirectAdmin: enables ModSecurity in the CustomBuild settings.

  • da build set modsecurity_ruleset owasp

    In DirectAdmin: selects OWASP CRS as the rule set. The value comodo selects the Comodo rule set.

  • da build modsecurity

    In DirectAdmin: installs ModSecurity and the selected rule set.

What a WAF is, and how ModSecurity differs from CRS

A WAF (Web Application Firewall) is a firewall for websites: it inspects every request before it reaches the application, and it can block requests that look like an attack attempt. The server firewall filters by addresses and ports, while the WAF inspects the content itself: the URL, the parameters, the headers, and the request body.

  • ModSecurity: the engine. It reads the rules, checks every request against them, and takes the action they define, such as logging or blocking. Without rules, the engine blocks nothing.
  • OWASP CRS (Core Rule Set): an open rule set from OWASP that detects common types of attacks on websites. Instead of blocking a request as soon as a single rule matches, the rules calculate an anomaly score for each request, and the block happens only when the score reaches the threshold.

Where ModSecurity stands today

  • An OWASP project: in early 2024, Trustwave, which maintained ModSecurity for years, handed the project over to OWASP, and its support ended in July 2024. Development continues in the owasp-modsecurity organization on GitHub.
  • Two branches: ModSecurity v2 is an Apache module that is still maintained. ModSecurity v3, also called libmodsecurity, is a rewrite of the engine as a standalone library that connects to the web server through a separate connector.
  • Which branch for which server: according to the CRS documentation, use ModSecurity v2 with Apache, and with Nginx, the v3 branch and the ModSecurity-nginx connector. The project itself describes the v3 connector for Apache as not ready for production use.
  • LiteSpeed and OpenLiteSpeed: LiteSpeed Enterprise has its own ModSecurity engine, which is compatible with ModSecurity rules. In OpenLiteSpeed, the mod_security module is based on the v3 branch and is enabled in the server configuration with modsecurity on and modsecurity_rules_file. After a change, restart the server. OpenLiteSpeed doesn't support the SecRuleRemoveById directive in .htaccess files, so an exclusion for a specific domain or URL is written as a rule with ctl in the REQUEST-900 file.
  • Other engines: according to the CRS documentation, the Coraza engine is also fully compatible with the rules, and it works with Caddy and Traefik.

How to enable the WAF without blocking visitors

  • Start in logging-only mode: set SecRuleEngine DetectionOnly and go through all the normal actions on the website: forms, file uploads, the admin dashboard, search, and checkout. Then check the log to see which rules matched.
  • Exclude before you block: every rule that matched a legitimate action gets a targeted exclusion, and only then do you switch to SecRuleEngine On.
  • Check the syntax before every reload: on Apache, run apachectl configtest, and on Nginx, run nginx -t. Reload the server only if the check passes.
  • Raise the level gradually: start at Paranoia Level 1. Before you move to a higher level, run it for logging only with tx.detection_paranoia_level, and exclude what's needed.

False positives: what to do and what to avoid

  • Find the rule: search the log for the blocked request and the IDs of the rules that matched it. Rule 949110 only sums up the score, and the rules before it are the ones to exclude.
  • Exclude as narrowly as possible: it's best to exclude one parameter on one URL with ctl:ruleRemoveTargetById. If the parameter appears across the whole website, use SecRuleUpdateTargetById, and disable an entire rule only when it isn't relevant to the website.
  • Don't edit the CRS rule files: direct changes to the files are lost in the next update. All exclusions go in the REQUEST-900 and RESPONSE-999 files or in a suitable plugin.
  • Don't turn off the engine: SecRuleEngine Off, or disabling ModSecurity for the whole website, leaves the website unprotected because of a single rule that's easy to exclude.
  • Common applications: WordPress, Drupal, Nextcloud, and other applications have ready-made CRS exclusion plugins. Install the matching plugin, and exclude manually only what's left.

ModSecurity in control panels

  • DirectAdmin: the administrator enables ModSecurity and selects the rule set in CustomBuild, and changes the global settings on the Modsecurity page in the Server Manager menu. Users enable or disable the engine and disable specific rules for their domains on the Web Application Firewall page in the Advanced Features menu.
  • cPanel and WHM: on the ModSecurity Tools page in WHM, the Hits List shows the rules that matched, with the ID of each rule, and you can disable a rule from it. You install and enable OWASP CRS on the ModSecurity Vendors page. In cPanel, ModSecurity can be turned on or off for each domain when the feature is enabled in the Feature Manager.

The IP address on this page, 203.0.113.10, belongs to a range reserved for documentation, so replace it with the real address. The paths of the logs and configuration files vary between distributions, control panels, and installations, so check them on the server itself.

Did you know?

On a Linux VPS, you connect over SSH with full root access, and install and configure ModSecurity and OWASP CRS on the web server of your choice, with the settings on this page: DetectionOnly mode (logging only), score thresholds, and targeted exclusions.

Linux VPS plans

ModSecurity and OWASP CRS FAQ

What's the difference between ModSecurity and OWASP CRS?

ModSecurity is the engine: it runs inside the web server, checks every request against the rules, and takes the action they define, such as logging or blocking. OWASP CRS is the rule set the engine loads, and it detects common types of attacks on websites. Without rules, the engine blocks nothing, so you install both together and configure CRS in the crs-setup.conf file.

How do I find which rule blocked a request with a 403 error?

Search the web server's error log or the ModSecurity audit log for the blocked request, for example by the visitor's IP address, and pull out the IDs in the id field. In OWASP CRS, the Access denied message usually comes from rule 949110, which only sums up the anomaly score. The rules that matched the same request before it are the ones that caused the block, and those are the ones you check and exclude.

How do I fix a false positive and keep ModSecurity running?

Exclude only what's needed. When the block is caused by one field on one URL, write a rule with ctl:ruleRemoveTargetById in the REQUEST-900 file that applies only to that URL. When the field appears across the whole website, use SecRuleUpdateTargetById in the RESPONSE-999 file, and disable a rule that isn't relevant to the website with SecRuleRemoveById. This way, the rest of the protection stays active, and your exclusions are kept after a CRS update.

What is the Paranoia Level, and which level should I start with?

The Paranoia Level sets how many CRS rules run, on levels 1 to 4. Level 1 is the default: it blocks common attacks and produces almost no false positives, so it's the level to start with. Each higher level adds rules and makes the inspection stricter, but it also requires more exclusions. Before you move to the next level, run it for logging only with tx.detection_paranoia_level, and check the log to see what it would have blocked.

Does ModSecurity work with Nginx and LiteSpeed?

Yes. With Nginx, you use the v3 branch of ModSecurity and the ModSecurity-nginx connector, and enable it with modsecurity on and modsecurity_rules_file. LiteSpeed Enterprise has its own engine that's compatible with ModSecurity rules, and OpenLiteSpeed runs the mod_security module. With Apache, the CRS documentation recommends ModSecurity v2.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement