# ModSecurity

**ModSecurity** 是一款开源、跨平台的 Web 应用防火墙（WAF）引擎，适用于 Apache、IIS 和 Nginx。它依据以 SecRule 语言编写的规则检查 HTTP 流量，提供针对一系列 Web 应用攻击的防护，并附带 HTTP 流量监控、日志记录和实时分析。<sup>[1](https://github.com/owasp-modsecurity/ModSecurity)</sup> 在生产环境中，它通常与 OWASP 核心规则集（CRS）配合使用——后者是占据主导地位的 WAF 规则集，提供通用的攻击检测规则。<sup>[2](https://owasp.org/projects/modsecurity?tab=main)</sup> 该项目自 2024 年起由 OWASP 托管。<sup>[3](https://modsecurity.org/history/)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 一款适用于 Apache、IIS 和 Nginx 的开源、跨平台 WAF 引擎，使用 SecRule 领域特定语言<sup>[1](https://github.com/owasp-modsecurity/ModSecurity)</sup><sup> • </sup><sup>[4](https://waf.owasp.org/)</sup> |
| 首次发布 | 2002 年 11 月，由伊万·里斯蒂奇（Ivan Ristić）以 Apache 模块形式发布<sup>[5](https://devguide.owasp.org/en/09-operations/03-modsecurity/)</sup><sup> • </sup><sup>[3](https://modsecurity.org/history/)</sup> |
| 当前版本 | v2 分支（传统模块）与 v3（libmodsecurity，由连接器调用的独立库）；最新版本包括 v3.0.17 和 v2.9.x<sup>[6](https://github.com/owasp-modsecurity/ModSecurity/releases)</sup> |
| 处理阶段 | 规则在五个阶段运行：请求头、请求体、响应头、响应体和日志记录<sup>[7](https://github.com/owasp-modsecurity/ModSecurity/wiki/Reference-Manual-(v3.x))</sup> |
| 标准规则集 | OWASP 核心规则集，具有四个偏执级别和异常评分模式<sup>[2](https://owasp.org/projects/modsecurity?tab=main)</sup><sup> • </sup><sup>[8](https://www.sciencedirect.com/science/article/pii/S0167404825004031)</sup> |
| 治理 | 2024 年托管权由 Trustwave 移交至 OWASP；OWASP 生产级项目<sup>[3](https://modsecurity.org/history/)</sup><sup> • </sup><sup>[4](https://waf.owasp.org/)</sup> |

## 历史

ModSecurity 始于 2002 年，最初是伊万·里斯蒂奇的一个项目，旨在让 Apache 能够理解和控制 HTTP 流量。到 2004 年，它已成为他在自己创办的 Thinking Stone 公司全力投入的工作。2006 年，Thinking Stone 被 Breach Security 收购，完全重写的 ModSecurity 2.0 完成，并在西雅图（Seattle）的 OWASP AppSec大会上亮相，这被广泛视为该项目的第一个重大里程碑。Breach 主导了 2008 年 2.5 版本的发布。<sup>[3](https://modsecurity.org/history/)</sup>

2010 年，Breach 被 Trustwave 收购，后者将 ModSecurity 以 Apache 许可证重新授权，使其更易于被其他厂商集成。在 Trustwave 旗下，该引擎于 2012 年被移植到 IIS 和 NGINX。libModSecurity（v3）的开发始于 2015 年，目标是打造一个独立的模块化引擎，通过连接器运行，而非绑定于某一特定 Web 服务器，3.x 系列于 2018 年面世。到 2021 年，Trustwave 已停止其商业版 ModSecurity 产品，2024 年托管权正式移交至 OWASP。<sup>[3](https://modsecurity.org/history/)</sup> OWASP开发者指南将接管时间定在 2024 年 1 月，Trustwave（现名 LevelBlue）为前任托管方。<sup>[5](https://devguide.owasp.org/en/09-operations/03-modsecurity/)</sup>

## 架构：v2 模块与 v3 库

该项目以两种形式发布。v2 分支是嵌入 Apache（并移植到 IIS 和 Nginx）的传统模块。v3 分支 libmodsecurity 仅包含库；Web 服务器集成由独立的连接器项目提供，例如 ModSecurity-nginx 连接器，每个连接器各有其发布周期。v3 的设计消除了对 Apache 内部机制的依赖——旧的 Nginx“独立”包装器正是需要这种依赖——并增加了诸如全局规则配置配合按位置定制等特性。<sup>[1](https://github.com/owasp-modsecurity/ModSecurity)</sup><sup> • </sup><sup>[9](https://github.com/owasp-modsecurity/ModSecurity-nginx)</sup>

ModSecurity 3.x 在 HTTP 事务的五个阶段评估规则：请求头、请求体、响应头、响应体和日志记录。每个阶段可用的数据是累积的，因此后续阶段能看到事务的更多内容；规则按阶段执行，而非严格按文件顺序。请求体阶段支持解析 `application/x-www-form-urlencoded`、`multipart/form-data`、XML 和 JSON 请求体；XML 和 JSON 处理器必须通过请求头阶段的规则显式启用。日志记录阶段在每次事务结束时运行，且无法阻断连接。<sup>[7](https://github.com/owasp-modsecurity/ModSecurity/wiki/Reference-Manual-(v3.x))</sup>

## 规则语言与配置

规则以 SecRule 语言编写，该语言由以下部分构成：变量（请求、响应、连接、事务和持久化集合）、运算符（字符串、正则表达式、数值、网络、校验和查找类型）、在匹配之前对编码或规避性输入进行规范化的变换，以及动作（中断性、日志性、元数据、流程和控制类指令）。配置指令控制引擎模式（On、Off 或 DetectionOnly）、请求和响应体检查、大小限制以及审计日志记录。<sup>[10](https://modsecurity.io/docs/modsecurity/)</sup> 部署可以在上线期间以阻断模式或仅检测模式运行，误报通过窄范围的目标排除和事务控制来处理，而非整体删除规则。<sup>[10](https://modsecurity.io/docs/modsecurity/)</sup>

## OWASP 核心规则集

OWASP CRS（前身为 OWASP ModSecurity 核心规则集）提供通用的企业级攻击检测规则，可与 ModSecurity、Coraza 以及兼容的商业引擎配合使用，涵盖 OWASP Top 10 所述的风险以及其他攻击向量。<sup>[4](https://waf.owasp.org/)</sup> CRS 通常以异常评分模式运行，在该模式下，匹配的规则会为事务评分加分，阻断决策依据阈值而非单条规则的匹配做出，这一设计旨在减少误报。<sup>[8](https://www.sciencedirect.com/science/article/pii/S0167404825004031)</sup>

CRS 规则分为四个偏执级别。级别 1 的误报最少，但代价是会漏掉一些复杂的载荷；更高级别增加了更严格的规则，同时提高检测率和误报率。在 CRS 4 中，许多规则从较低的偏执级别重新分配到更高级别，因此从 CRS 3 迁移的 PL1 部署会看到更低的异常评分基线和更少的误报，但一些此前在 PL1 即可检测到的攻击现在需要 PL2。<sup>[11](https://github.com/coreruleset/website/blob/main/content/blog/2026-04-20-migrating-from-crs-3-to-crs-4-part-4-scoring.md)</sup>

开箱即用的覆盖范围存在可测量的差距。一项 2026 年的对照研究在 Apache 2.4 上使用 ModSecurity 2.9.8-1 搭配 CRS 4.8.0-dev，对 Damn Vulnerable Web Application 进行测试，结果显示，针对测试中发现的差距编写的 146 条自定义规则在该环境中将检测精确率从 60.54% 提升至 97.46%，且误报并未增加。<sup>[8](https://www.sciencedirect.com/science/article/pii/S0167404825004031)</sup> 对同一技术栈的机器学习研究报告称，检测率提升可达 30%，同时舍弃多达 50% 的 CRS 规则，并且对对抗性 SQL 注入的健壮性有所提高。<sup>[12](https://arxiv.org/abs/2308.04964)</sup>

## 性能开销

WAF 的开销既取决于引擎，也取决于配置。在一项实测配置中（Nginx 1.25.5 + ModSecurity 3.0.12 + CRS 3.3.5，部署于 OWASP Juice Shop 前端），在偏执级别 4 并启用完整审计日志记录的情况下，提供静态文件的吞吐量从每秒 3,388 个请求降至 2,480 个，减少了 27%，每个请求增加 1.1 ms；在偏执级别 1 且使用 `RelevantOnly` 审计时，降幅为 18%，增加 0.6 ms。以该速率进行完整审计时，每百万个请求写入近 900 MB，因此生产部署要么轮转审计日志，要么将审计保持在 `RelevantOnly` 级别。面对每个请求约需 90 ms 的慢速后端，增加的毫秒数淹没在噪声之中。<sup>[13](https://florintodor.dev/en/blog/cuanto-cuesta-un-waf/)</sup>

引擎对比显示两个分支存在差异。在一项针对四套 WAF 技术栈、相同硬件、CRS 偏执级别 1 的基准测试中，Nginx + ModSecurity 3 在重复的无害 GET 请求下维持每秒 1,263 个请求，而 Apache + ModSecurity 2 维持 643 个；在包含 950 个表单字段的 103 KB URL 编码请求体上，v2 技术栈跌至约每秒 2 个请求，而 Nginx + ModSecurity 3 为 14.4 个。<sup>[14](https://karna.sicuranext.com/benchmark.html)</sup>

## 安全公告

作为处理不受信任 HTTP 输入的解析器，ModSecurity 自身也需要发布安全版本。版本 2.9.12 修复了错误处理不当漏洞 CVE-2025-54571；更早的 2.9.x 补丁版本修复了拒绝服务漏洞 CVE-2025-52891、CVE-2025-48866 和 CVE-2025-47947。<sup>[6](https://github.com/owasp-modsecurity/ModSecurity/releases)</sup> 版本 3.0.15 修复了 `@verifySSN`、`@verifyCPF` 和 `@verifySVNR` 运算符中的无符号整数下溢（CVE-2026-42268），以及 `hex_decode` 中的缓冲区溢出（CVE-2026-30923）。<sup>[6](https://github.com/owasp-modsecurity/ModSecurity/releases)</sup> 版本 3.0.16 修复了 CVE-2026-52747——一个高危的多部分（multipart）解析器缺陷（CVSS 8.6）：包含内嵌 `\r\n` 序列的合法多部分字段以拼接文本的形式暴露给规则，从而导致 WAF 绕过；修复在 v3.0.16 中落地，配套的 CVE-2026-52761 则在 v3.0.17 中修复。<sup>[6](https://github.com/owasp-modsecurity/ModSecurity/releases)</sup><sup> • </sup><sup>[15](https://modsecurity.org/20260629/about-cve-2026-52747-and-2026-52761/)</sup>

## 生态系统

ModSecurity 是 OWASP 一系列 WAF 项目的核心。Coraza 是一款用 Go 编写的下一代 WAF，几乎完全兼容 ModSecurity，面向云原生部署，同时也是一个 OWASP 生产级项目。<sup>[4](https://waf.owasp.org/)</sup> 商业和托管部署同样基于该引擎构建，包括 Atomicorp 的 Atomic规则以及通过 Varnish 进行的边缘部署。<sup>[16](https://modsecurity.io/integrations/)</sup>

## References

1. [owasp-modsecurity/ModSecurity, GitHub repository README](https://github.com/owasp-modsecurity/ModSecurity)
2. [OWASP ModSecurity, OWASP Foundation project page](https://owasp.org/projects/modsecurity?tab=main)
3. [Project History, ModSecurity.org](https://modsecurity.org/history/)
4. [OWASP WAF Projects](https://waf.owasp.org/)
5. [ModSecurity WAF, OWASP Developer Guide](https://devguide.owasp.org/en/09-operations/03-modsecurity/)
6. [Releases, owasp-modsecurity/ModSecurity, GitHub](https://github.com/owasp-modsecurity/ModSecurity/releases)
7. [Reference Manual (v3.x), owasp-modsecurity/ModSecurity Wiki](https://github.com/owasp-modsecurity/ModSecurity/wiki/Reference-Manual-(v3.x))
8. [An empirical study on the evaluation and enhancement of OWASP CRS (Core Rule Set) in ModSecurity, Computers & Security](https://www.sciencedirect.com/science/article/pii/S0167404825004031)
9. [owasp-modsecurity/ModSecurity-nginx, GitHub](https://github.com/owasp-modsecurity/ModSecurity-nginx)
10. [ModSecurity engine documentation, ModSecurity.io](https://modsecurity.io/docs/modsecurity/)
11. [Migrating from CRS 3 to CRS 4, part 4: scoring, coreruleset/website](https://github.com/coreruleset/website/blob/main/content/blog/2026-04-20-migrating-from-crs-3-to-crs-4-part-4-scoring.md)
12. [ModSec-AdvLearn: Countering Adversarial SQL Injections with Robust Machine Learning, arXiv](https://arxiv.org/abs/2308.04964)
13. [What a ModSecurity WAF costs, measured, Florin Todor](https://florintodor.dev/en/blog/cuanto-cuesta-un-waf/)
14. [Karna vs the open-source WAFs: Performance Benchmark](https://karna.sicuranext.com/benchmark.html)
15. [About CVE-2026-52747 and 2026-52761, ModSecurity.org](https://modsecurity.org/20260629/about-cve-2026-52747-and-2026-52761/)
16. [Web server and platform integrations, ModSecurity.io](https://modsecurity.io/integrations/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Network defense and threats › Firewalls and perimeter defense

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/modsecurity

English original: https://www.edgepedia.org/modsecurity
