简体   繁体   English

WordPress网站上的ModSecurity的Apache LocationMatch通配符

[英]Apache LocationMatch wildcard for ModSecurity on wordpress site

I'm have mod_security installed on an Ubuntu 14.04 Apache 2.4.7 running a WordPress site. 我在运行WordPress站点的Ubuntu 14.04 Apache 2.4.7上安装了mod_security。 I have a handful of rules that I need to ignore, but I'm having trouble implementing some wildcard rules so that I don't have to specify each and every page.. 我有一些我需要忽略的规则,但是我在实现一些通配符规则时遇到了麻烦,所以我不必指定每一页。

What I have (in my site.conf file) is... 我所拥有的(在我的site.conf文件中)是......

  <LocationMatch "/wp-admin/post.php">
     SecRuleRemoveById 300016
  </LocationMatch>

  <LocationMatch "/wp-admin/nav-menus.php">
     SecRuleRemoveById 300016
  </LocationMatch>

  <LocationMatch "(/wp-admin/|/wp-login.php)">
     SecRuleRemoveById 950117
     SecRuleRemoveById 950005
     SecRuleRemovebyID 981173
     SecRuleRemovebyId 960024
  </LocationMatch>

    <LocationMatch "/wp-admin/load-scripts.php">
     SecRuleRemoveById 981173
    </LocationMatch>


    <LocationMatch "/wp-admin/plugins.php">
     SecRuleRemoveById 981173
    </LocationMatch>

    <LocationMatch "/wp-admin/customize.php">
     SecRuleRemoveById 981173
    </LocationMatch>

What I want is to consolidate everything into a single rule that uses a wildcard on wp-admin and wp-login . 我想要的是将所有内容整合到一个在wp-adminwp-login上使用通配符的规则中。

I've tried the following but it seems to be ignored as mod_security is throwing denials.. 我尝试过以下但是因为mod_security抛出了拒绝而似乎被忽略了。

<LocationMatch "(/wp-admin/*|/wp-login/*)">
....

and also 并且

<LocationMatch "(/wp-admin/*)">
....

and also 并且

<Location "/wp-admin/*">
....

I've done some research on LocationMatch and regex but I'm not getting something here. 我已经对LocationMatch和regex做了一些研究,但我在这里没有得到什么。 Is what I'm waning to do possible? 我正在减少做什么?

EDIT: The referrer URL in the modsec_audit.log is http://www.<site>.com/wp-admin/customize.php?theme=modality 编辑:modsec_audit.log中的引荐来源网址是http://www.<site>.com/wp-admin/customize.php?theme=modality

While Carsten's answer is correct, it should be noted that Location and Location directives run after phase 1 ModSecurity rules. 虽然Carsten的答案是正确的,但应该注意位置和位置指令第1阶段ModSecurity规则之后运行。 However it looks like your rules are all phase 2 anyway, so this doesn't particularly matter in this case - though I don't have access to rule 300016 so couldn't be 100% sure about that. 然而,看起来你的规则无论如何都是第二阶段,所以在这种情况下这并不特别重要 - 尽管我无法访问规则300016,因此无法100%确定。

Anyway for this reason I don't like using Location and LocationMatch and personally prefer to do Location filtering within ModSecurity with a new rule such as this: 无论如何,由于这个原因,我不喜欢使用Location和LocationMatch,并且个人更喜欢在ModSecurity中使用新规则进行位置过滤,例如:

SecRule REQUEST_URI "@beginsWith /wp-(admin|login)/" \
   "phase:2,id:1000,nolog,pass,ctl:ruleRemoveById=300016,\
   ctl:ruleRemoveById=950117,\
   ctl:ruleRemoveById=950005
   ...etc.

That way I can be consistent in rule filtering (rather than have phase 1 rules filtered with a rule like above, and other rules filtered by location matching). 这样我可以在规则过滤中保持一致(而不是使用上面的规则过滤第1阶段规则,以及通过位置匹配过滤的其他规则)。 You will however need one rule for each phase. 但是,每个阶段都需要一个规则。 Anyway, as I say, that doesn't really matter if all of these are phase 2 or above rules for now. 无论如何,正如我所说,如果所有这些都是现在的第2阶段或更高规则,那并不重要。

Another funny worth remembering is that, if you use SecRuleRemoveById you need to specify this after the rule you are removing, whereas if you use ctl:ruleRemoveById you need to specify this before the rule you are removing. 另一个有趣值得记住的是,如果您使用SecRuleRemoveById,则需要删除规则指定此项,而如果使用ctl:ruleRemoveById,则需要删除规则之前指定此项。

However most Wordpress attacks will be against these very URLs. 但是大多数Wordpress攻击都会针对这些URL。 So you want to be very sure that you don't need these rules. 所以,你要非常肯定的说,你不需要这些规则。 And by moving to a more generic exceptions like this, you are matching each rule against more than it needs to. 通过转向更通用的例外情况,您可以将每个规则与超出需要的规则进行匹配。 For example if looks like from your original set up, /wp-login.php only needs 4 of these exceptions but you will move it having them all. 例如,如果看起来像是从原始设置开始,/ wp-login.php只需要其中4个例外,但是你会将它们全部移动。

I would suggest that instead of loosening the rules to match more, you should aim to keep them tight and not make this change. 我建议,不要放松规则以匹配更多,你应该设法保持紧张, 不要做出这种改变。

In fact you could tighten them further to only match certain arguments that are causing the need for the exception. 事实上,你可以进一步收紧它们,只匹配导致需要异常的某些参数。 For example if only the username field is causing you issues then you could tighten the rules to this: 例如,如果只有用户名字段导致您出现问题,那么您可以收紧规则:

<LocationMatch "(/wp-admin/|/wp-login.php)">
     SecRuleUpdateTargetById 950117 !ARGS:'username'
     SecRuleUpdateTargetById 950005 !ARGS:'username'
     SecRuleUpdateTargetById 981173 !ARGS:'username'
     SecRuleUpdateTargetById 960024 !ARGS:'username'
</LocationMatch>

or in the other format to this: 或者以其他格式:

SecRule REQUEST_URI "@beginsWith /wp-(admin|login)/" \
  "phase:2,id:1000,nolog,pass,\
  ctl:ruleRemoveTargetById=950117;ARGS:username,\
  ctl:ruleRemoveTargetById=950005;ARGS:username,\
  ctl:ruleRemoveTargetById=981173;ARGS:username,\
  ctl:ruleRemoveTargetById=960024;ARGS:username

Yes this may mean extra work, and I know you came here looking to consolidate your rules to make this easer, but there is little point in running a WAF like ModSecurity if you end up disabling a lot of the rules that you are aiming to protect against and, unfortunately, Wordpress is an active target for a lot of bad people due in part to it's popularity. 是的,这可能意味着额外的工作,而且我知道你来到这里是为了整合你的规则以使这个更容易,但是如果你最终禁用了许多你想要保护的规则,那么运行类似ModSecurity的WAF没有什么意义。反对和不幸的是,Wordpress是许多坏人的活跃目标,部分原因在于它的受欢迎程度。

So, while I didn't directly answer your question, I hope that's useful and gives you food for thought. 所以,虽然我没有直接回答你的问题,但我希望这是有用的,让你有所思考。

This should work: 这应该工作:

<LocationMatch "/wp-(admin|login)/">

You don't need a wildcard here, because you just want to detect the beginning of the path and it doesn't matter, what comes after the 2nd slash. 你不需要在这里使用通配符,因为你只想检测路径的开头并且无关紧要,在第二个斜杠后面会发生什么。

For Location , you need a ~ to trigger the regex interpretation: 对于Location ,您需要~来触发正则表达式解释:

<Location ~ "/wp-(admin|login)/">

More details: 更多细节:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM