All posts

Blog

ModSecurity Explained: How This Web Application Firewall Protects Your Site

Illustrated cover for the Security & SSL guide "ModSecurity Explained: How This Web Application Firewall Protects Your Site"

If you've ever saved a WordPress page and been greeted by a blunt "403 Forbidden" or "Not Acceptable" error, there's a good chance you met ModSecurity. Most of the time it works invisibly, turning away malicious requests before they reach your site. Occasionally it mistakes something harmless for an attack, and that's usually the only time website owners hear its name.

This guide explains what ModSecurity is, how it decides what to block, how it fits with other security tools, and what to do when it gets in your way.

What is ModSecurity?

ModSecurity is an open-source web application firewall (WAF) engine. It started as a module for the Apache web server and is now also used with Nginx and other servers through its newer library version (often called libmodsecurity or ModSecurity v3). LiteSpeed Web Server supports ModSecurity-compatible rules as well.

Unlike a network firewall, which allows or blocks traffic based on IP addresses and ports, ModSecurity looks inside web requests: URLs, query strings, form data, cookies, headers and uploaded files. If you want the bigger picture of WAFs in general, see our guide to what a web application firewall is.

On many shared hosting servers, ModSecurity is enabled server-wide by the host, so every site on the server benefits without the site owner configuring anything.

How ModSecurity works

ModSecurity itself is an engine. On its own it does very little. Its behavior comes from rules, written in a dedicated rule language, that describe what to look for and what to do about it.

When a request arrives, ModSecurity processes it in phases:

  1. Request headers: examining the URL, method and headers
  2. Request body: examining form submissions, JSON and uploads
  3. Response headers and response body: optionally inspecting what the site sends back, for example to catch data leaks
  4. Logging: recording what happened

At each phase, rules can inspect parts of the request and take actions such as allowing it, logging it, blocking it with an error code, or adding to an "anomaly score."

A simple rule example

Rules look something like this:

SecRule ARGS "@contains <script>" \
    "id:100001,phase:2,deny,status:403,msg:'Possible XSS attempt'"

In plain English: check all request arguments, and if any contain <script>, deny the request with a 403 error and log the message. Real-world rules are far more sophisticated, but the idea is the same.

Detection mode vs blocking mode

ModSecurity can run in two main modes, controlled by the SecRuleEngine setting:

  • DetectionOnly: logs what it would have blocked, but lets requests through. Useful for testing new rules.
  • On: actively blocks requests that trigger blocking rules.

The OWASP Core Rule Set

Writing good rules from scratch is hard, so most servers use a rule set. The best known is the OWASP Core Rule Set (CRS), a free, community-maintained collection designed to protect against common attack categories, including:

  • SQL injection
  • Cross-site scripting (XSS)
  • Local and remote file inclusion
  • Remote code execution attempts
  • Protocol violations and malformed requests
  • Scanners and known bad bots

The CRS uses anomaly scoring. Instead of blocking on the first suspicious sign, each matching rule adds points. If the total crosses a threshold, the request is blocked. This reduces false positives because a single unusual character doesn't automatically trigger a block.

CRS also has paranoia levels, from 1 (the default, fewer false positives) up to higher levels with stricter rules that catch more attacks but block more legitimate traffic.

Hosting providers may also use commercial rule sets or their own custom rules tuned for popular applications like WordPress.

What ModSecurity is good at, and what it isn't

Good at:

  • Blocking common, automated attacks against known vulnerability patterns
  • Providing "virtual patches" that block exploitation of a vulnerability before every site has updated
  • Adding a protective layer in front of old or poorly written code
  • Logging suspicious activity for investigation

Not good at:

  • Stopping attacks that use valid credentials, such as a stolen admin password
  • Absorbing large traffic floods. For that, see what a DDoS attack is
  • Replacing updates. A WAF reduces risk from vulnerable plugins, but updating or removing them is still the fix
  • Understanding your application's business logic

Why ModSecurity sometimes blocks legitimate actions

Because rules look for patterns that resemble attacks, some normal activity can trip them. Common examples:

  • Saving posts or pages containing code, such as HTML, JavaScript or SQL in a tutorial
  • Page builders that send large, complex requests full of markup and encoded data
  • Form submissions with unusual characters or lots of links
  • Admin actions in some plugins that pass file paths or commands as parameters
  • API calls from integrations that format data in unexpected ways

Typical symptoms:

  • A 403 Forbidden or 406 Not Acceptable error when saving or submitting
  • A form that silently fails
  • An admin page that works for some actions but not others

Our guide to fixing 403 Forbidden errors covers other causes, so rule those out too.

What to do when ModSecurity blocks you

1. Note the details

Write down the exact time (including time zone), the URL, the action you were performing and your IP address. These details let your host find the matching log entry.

2. Check your logs if you can

Some control panels show ModSecurity hits in a dedicated tool or in the error log. Look for entries mentioning ModSecurity along with a rule ID. Our guide to reading server error logs explains where to look.

3. Ask your host to whitelist the specific rule

On shared hosting, contact support with the details above. A good host can disable a specific rule for a specific URL or domain, rather than turning off the firewall completely.

4. Avoid turning it off entirely

Some control panels let you switch ModSecurity off for a domain. It's tempting when you're frustrated, but it removes protection from your whole site. If you do it to confirm the cause, turn it back on immediately and request a targeted exception instead.

5. On your own server, tune carefully

If you manage your own VPS, you can create rule exclusions. A common pattern is removing a specific rule only for a specific path:

SecRule REQUEST_URI "@beginsWith /wp-admin/post.php" \
    "id:1000,phase:1,pass,nolog,ctl:ruleRemoveById=941100"

Replace the rule ID with the one shown in your audit log. Keep exclusions as narrow as possible, document why each exists, and run new rule sets in DetectionOnly mode first to see what they'd block.

ModSecurity on your own VPS

If you run an unmanaged server, you can install ModSecurity yourself. The general steps:

  1. Install the ModSecurity module for your web server from your distribution's packages or from source.
  2. Enable it and copy the recommended base configuration.
  3. Download the OWASP Core Rule Set and include it in your configuration.
  4. Start in DetectionOnly mode and watch the audit log for false positives.
  5. Add targeted exclusions, then switch to On.
  6. Keep the rule set updated.

Note that the original ModSecurity project's stewardship changed hands in recent years and is now maintained under OWASP, so check the current official project pages for installation instructions and supported versions. Combine it with the basics in our Linux server hardening checklist.

Wrapping up

ModSecurity is a widely used web application firewall engine that inspects every request and blocks those matching attack patterns, usually with the help of the OWASP Core Rule Set. It's a valuable layer of defense, especially for sites running popular CMS software. When it blocks something legitimate, collect the details, find the rule ID, and ask for a narrow exception rather than switching protection off.

Related reading: How to Fix a 403 Forbidden Error on Your Website; How to Read Server Error Logs: A Beginner's Guide to Finding What Broke; Linux Server Hardening Checklist: 15 Steps Beyond the Basics.