Modsecurity:误报过多

Modsecurity: Excessive false positives

我刚刚在服务器上设置了 Apache modsecurity,原则上它运行良好,但我收到了很多误报。

我正在使用 OWASP ModSecurity 核心规则集 (CRS),本质上是 "out of the box"。

我运行处于"self-contained"(传统)模式而不是协作(异常)模式:

SecDefaultAction "phase:1,deny,log"
SecDefaultAction "phase:2,deny,log"

特别值得关注的是 SQL 注入组。双管道 (||) 双尖括号 (>>) 和一大堆其他输入将触发规则并导致页面被阻止。其中许多很容易出现在合法的用户输入中。

有没有一种优雅的方法可以选择性地允许不一定表示注入攻击通过的常见输入?我知道我目前 运行 开箱即用,但是对于几乎所有用例来说,阻塞双管道和尖括号肯定都太严格了吗?

首先要做的是切换到异常评分,而不是将默认操作设置为拒绝,这将真正减少你得到的错误 +ves 的数量。

其次,切换到 DetectionOnly 模式并保持 运行 几天,您将了解要解决的问题。 对于每个误报,请考虑:

  • 修改应用程序源
  • 禁用该特定规则
  • 为该参数禁用 modsec,URL,&c。如果你知道应用程序代码中有保护。

Ivan Ristic ModSecurity 这本书现在有点流行,但它是真正深入了解并为它背后的应用程序定制 modsec 的最佳资源,不幸的是它有一个废话索引所以我推荐你的电子版本可以搜索。 遗憾的是,在 modsec 中有许多隐藏的技巧,你必须通过查看源代码、偶然发现书中的一个小文本框或通过将调试级别调到 5 来逐一找到它们modsec 实际上在做什么。

Kully 有一些优点,我也绝对推荐 ModSecurity Handbook。我想说切换到异常模式确实需要一些时间来适应,并且在我看来需要额外的努力来进行监控,所以我个人更喜欢 运行 在阻塞模式下并关闭嘈杂的规则。

您确实需要 运行 在 DetectionOnly 模式下并在 CRS 规则变得有用之前对其进行调整,这确实需要时间。在我觉得足够舒服将其切换到阻塞模式之前,经过了几个月的迭代。

SQL 注入规则特别容易出现过度警报。

以下是您可能希望关闭或调整的一些 CRS v2 通用规则 - 特别是在传统警报模式下:

#Lots of browsers don't send accept header so disable rule 960015 which blocks that:
SecRuleRemoveById 960015

#ModSecurity does not handle gzip files and falsely alerts code leakage for such binary files so disable this rule
SecRuleRemoveById 970903

#Range header is set by some Android devices so ignore that rule
SecRuleRemoveById 958291

#We allow Google Tag Manager which uses small iframe so disable the rules that disallow small iframes:
SecRuleRemoveById 981000
SecRuleRemoveById 981001

#These produce many false positives as checking for things like lots of spaces and ' characters (valid in names and addresses). So disable.
SecRuleRemoveById 950109
SecRuleRemoveById 950901
SecRuleRemoveById 960024
SecRuleRemoveById 973338
SecRuleRemoveById 981172
SecRuleRemoveById 981173
SecRuleRemoveById 981231
SecRuleRemoveById 981242
SecRuleRemoveById 981243
SecRuleRemoveById 981245
SecRuleRemoveById 981246
SecRuleRemoveById 981248
SecRuleRemoveById 981257
SecRuleRemoveById 981260
SecRuleRemoveById 981318
SecRuleRemoveById 981319
SecRuleRemoveById 981320

#Allow Search argument (q) to include SQL words:
SecRuleUpdateTargetById 959071 !ARGS:'q'
SecRuleUpdateTargetById 959072 !ARGS:'q'
SecRuleUpdateTargetById 981247 !ARGS:'q'

#Passwords can (and arguable should!) contain special chars
SecRuleUpdateTargetById 950010 !ARGS:'/[pP](ass)?word/'
SecRuleUpdateTargetById 981240 !ARGS:'/[pP](ass)?word/'

#Email address can contain some SQL injection phrases
SecRuleUpdateTargetById 981241 !ARGS:'/[eE](-)?mail/'

#Remove checking of rules which checks for http calls in arguments will have URLs in them
SecRuleUpdateTargetById 950007 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 950120 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 973304 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 981241 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 981256 !ARGS:'utm_referrer'

CRS v3 经历了规则 ID 重新编号,并且删除了一些规则,因此下面给出了上面的 v3 等价物:

#Lots of browsers don't send accept header so disable rule 920300 which blocks that:
SecRuleRemoveById 920300

#These produce many false positives as checking for things like lots of spaces and ' characters (valid in names and addresses). So disable.
SecRuleRemoveById 920230
SecRuleRemoveById 942130
SecRuleRemoveById 942460
SecRuleRemoveById 941140
SecRuleRemoveById 942420
SecRuleRemoveById 942430
SecRuleRemoveById 942440
SecRuleRemoveById 942330
SecRuleRemoveById 942370
SecRuleRemoveById 942260
SecRuleRemoveById 942340
SecRuleRemoveById 942210
SecRuleRemoveById 942200
SecRuleRemoveById 942450
SecRuleRemoveById 942110
SecRuleRemoveById 942120
SecRuleRemoveById 942140

#Allow Search argument (q) to include SQL words:
SecRuleUpdateTargetById 942390 !ARGS:'q'
SecRuleUpdateTargetById 942400 !ARGS:'q'
SecRuleUpdateTargetById 942360 !ARGS:'q'

#Passwords can (and arguable should!) contain special chars
SecRuleUpdateTargetById 942300 !ARGS:'/[pP](ass)?word/'

#Email address can contain some SQL injection phrases
SecRuleUpdateTargetById 942230 !ARGS:'/[eE](-)?mail/'

#Remove checking of rules which checks for http calls in arguments will have URLs in them
SecRuleUpdateTargetById 931130 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 942230 !ARGS:'utm_referrer'
SecRuleUpdateTargetById 942250 !ARGS:'utm_referrer'

当然,完全关闭这些规则(就像我在上面代码片段的前半部分所做的那样)会降低 ModSecurity 的有效性,因此您需要决定这是否适合您的网站。

关闭特定参数的规则(就像我在上面代码片段的后半部分所做的那样)通常是首选,但同样只有您可以决定这些参数是否不需要规则提供的保护。

这是我们从下面 () 为我们的 wordpress modsec 配置使用的内容。

SecRuleRemoveById 950109
SecRuleRemoveById 950120
SecRuleRemoveById 950901
SecRuleRemoveById 960008
SecRuleRemoveById 960015
SecRuleRemoveById 973338
SecRuleRemoveById 981242
SecRuleRemoveById 981243
SecRuleRemoveById 981245
SecRuleRemoveById 981246
SecRuleRemoveById 981248
SecRuleRemoveById 981257
SecRuleRemoveById 981319
SecRuleRemoveById 981320