Skip to content

ModSecurity WAF on LiteSpeed: enabling and tuning the ruleset

How to enable ModSecurity on LiteSpeed Enterprise or OpenLiteSpeed, load the OWASP CRS, tune rules to reduce false positives, and verify WAF behaviour in production.

LiteSpeed has supported ModSecurity as a built-in WAF module since version 5.3. Unlike Apache where ModSecurity runs as a third-party module, on LiteSpeed it is compiled directly into the server binary and processes requests in the same event loop — with no inter-process communication overhead. Getting it right requires three things: loading a current ruleset (OWASP Core Rule Set 3.3+), running in Detection mode long enough to identify false positives, and then tuning per-site exceptions before switching to Prevention mode. Skipping the tuning phase on a WordPress site typically blocks legitimate admin traffic and breaks WooCommerce checkout within minutes of enabling Prevention mode.

How to verify

# Confirm ModSecurity is available in this LiteSpeed build
/usr/local/lsws/bin/lshttpd -v 2>&1 | grep -i modsec

# Check ModSecurity audit log for recent hits
tail -100 /usr/local/lsws/modsec_audit.log 2>/dev/null || \
  tail -100 /var/log/modsec_audit.log

# Test a known-bad request (SQL injection probe) — should return 403 in Prevention
curl -s -o /dev/null -w "%{http_code}" \
  "https://example.com/?id=1+UNION+SELECT+1,2,3--"

# Check rule ID from an audit log entry
grep "id \"" /usr/local/lsws/modsec_audit.log | tail -20

# ModSecurity status at server level (LiteSpeed Enterprise)
grep -i modsec /usr/local/lsws/conf/httpd_config.conf

What’s happening

ModSecurity on LiteSpeed operates as a request and response inspection layer inserted between the HTTP engine and your PHP application. Every incoming request is matched against the loaded ruleset; a match can produce a log entry (Detection mode), return a 403 (Prevention mode), or both. The OWASP Core Rule Set (CRS) is a maintained collection of generic attack detection rules covering SQL injection, XSS, path traversal, remote file inclusion, and other OWASP Top 10 vectors. Each rule has a numeric ID and a severity score; the CRS uses an anomaly scoring model where a request’s score must exceed a threshold before it triggers a block.

The anomaly scoring approach is important for WordPress sites because a single CRS rule matching on a benign WordPress admin request does not automatically block it — it only adds to the score. This means you can set the block threshold high (e.g., 10) during tuning to see what would be blocked, then lower it to 5 for production. Most false positives on WordPress come from the admin editor (which sends HTML in POST bodies, triggering XSS rules), WooCommerce product descriptions, and plugin-specific parameter names.

LiteSpeed reads ModSecurity configuration from files referenced in httpd_config.conf or per-vhost config. The SecRuleEngine directive controls the operating mode: DetectionOnly logs hits without blocking, On enforces blocks, Off disables the engine entirely.

The procedure

1. Install the OWASP CRS.

# Create the ModSecurity config directory
sudo mkdir -p /usr/local/lsws/conf/modsec

# Download and extract OWASP CRS 3.3
curl -sL https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.5.tar.gz \
  | sudo tar -xz -C /usr/local/lsws/conf/modsec --strip-components=1

# Copy the recommended CRS config
sudo cp /usr/local/lsws/conf/modsec/crs-setup.conf.example \
        /usr/local/lsws/conf/modsec/crs-setup.conf

# Create a site-specific exclusions file (start empty)
sudo touch /usr/local/lsws/conf/modsec/exclusions.conf

2. Create the ModSecurity main config file.

sudo tee /usr/local/lsws/conf/modsec/modsec_main.conf > /dev/null <<'MODSEC'
# Enable ModSecurity engine in Detection mode first
SecRuleEngine DetectionOnly

# Audit log — one file per request for easier parsing
SecAuditEngine RelevantOnly
SecAuditLog /usr/local/lsws/logs/modsec_audit.log
SecAuditLogParts ABIJDEFHZ
SecAuditLogType Serial

# Request body inspection
SecRequestBodyAccess On
SecRequestBodyLimit 13107200
SecRequestBodyLimitAction ProcessPartial

# Response body inspection (selective — performance cost)
SecResponseBodyAccess Off

# Temporary file paths
SecTmpDir /tmp/modsec
SecDataDir /tmp/modsec
SecUploadDir /tmp/modsec

# Include OWASP CRS setup
Include /usr/local/lsws/conf/modsec/crs-setup.conf

# Include all CRS rules
Include /usr/local/lsws/conf/modsec/rules/*.conf

# Site-specific exclusions (loaded last so they can disable CRS rules)
Include /usr/local/lsws/conf/modsec/exclusions.conf
MODSEC

sudo mkdir -p /tmp/modsec

3. Reference ModSecurity in the LiteSpeed server config.

In /usr/local/lsws/conf/httpd_config.conf, add inside the server block or per-vhost:

modsecurity  on
modsecurityRules  /usr/local/lsws/conf/modsec/modsec_main.conf

Validate and restart:

sudo /usr/local/lsws/bin/lswsctrl -t
sudo /usr/local/lsws/bin/lswsctrl restart

4. Run in Detection mode and collect false positives.

Browse your site normally — visit the admin dashboard, edit posts, run a WooCommerce test purchase. Then inspect the audit log:

# Most-triggered rule IDs
grep 'id "' /usr/local/lsws/logs/modsec_audit.log | \
  grep -oP 'id "\K[0-9]+' | sort | uniq -c | sort -rn | head -20

# Full detail on a specific rule hit
grep -A 20 'id "981176"' /usr/local/lsws/logs/modsec_audit.log | head -40

5. Add targeted exclusions for WordPress.

Edit /usr/local/lsws/conf/modsec/exclusions.conf:

# WordPress admin editor — disables HTML body checks for admin users
SecRule REQUEST_URI "@beginsWith /wp-admin/" \
  "id:9001,phase:1,pass,nolog,\
   ctl:ruleRemoveById=941000-941999,\
   ctl:ruleRemoveById=942000-942999"

# WooCommerce checkout — allow product description HTML in POST
SecRule REQUEST_URI "@beginsWith /checkout/" \
  "id:9002,phase:1,pass,nolog,\
   ctl:ruleRemoveById=941100,\
   ctl:ruleRemoveById=941160"

# XML-RPC (disable entirely if not needed, or exclude from CRS)
SecRule REQUEST_URI "@streq /xmlrpc.php" \
  "id:9003,phase:1,pass,nolog,\
   ctl:ruleEngine=Off"

After adding exclusions, reload:

sudo /usr/local/lsws/bin/lswsctrl restart

6. Switch to Prevention mode.

Once the audit log shows no legitimate traffic being flagged:

sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' \
  /usr/local/lsws/conf/modsec/modsec_main.conf
sudo /usr/local/lsws/bin/lswsctrl restart

Verify with a known-bad probe:

curl -s -o /dev/null -w "%{http_code}" \
  "https://example.com/?id=1+UNION+SELECT+1,2,3--"
# Expected: 403

Common pitfalls

Switching to Prevention mode without tuning causes immediate admin lockout. WordPress’s built-in post editor sends raw HTML in POST request bodies, which triggers XSS rules at the default CRS sensitivity. Always run Detection mode for at least 24 hours on a live site before switching.

The SecRequestBodyLimit must be higher than your largest form upload. The default of 13 MB covers most cases, but WooCommerce product imports and bulk CSV uploads can exceed this. A request that exceeds the limit is either rejected or processed partially, depending on SecRequestBodyLimitAction.

ModSecurity adds measurable latency. On a high-traffic LiteSpeed deployment, enabling ModSecurity with a full CRS ruleset adds 1–3ms per uncached request. This is acceptable but not zero. Responses served from the LSCache module bypass ModSecurity inspection, which is why enabling caching before enabling WAF is the correct order of operations.

CRS updates require config merge. When the OWASP CRS releases a new version, you must re-run the setup — overwriting rules/ but preserving crs-setup.conf and exclusions.conf. Maintain exclusions in a separate file (as shown above) to avoid losing them on upgrade.

Audit logs fill disk quickly on a busy site. SecAuditEngine RelevantOnly logs only requests that trigger rules, which is much less than On. Still, set up log rotation: logrotate with a 100M size trigger and 7 days retention is a reasonable baseline.

ModSecurity ruleset maintenance, CRS version upgrades, and false-positive triage on WordPress and WooCommerce sites are part of Stack Harbor’s managed operations security layer.