简体   繁体   English

如何在 .htaccess 文件中禁用 mod_security?

[英]How can I disable mod_security in .htaccess file?

How can we disable mod_security by using .htaccess file on Apache server?我们如何通过在 Apache 服务器上使用.htaccess文件来禁用mod_security

I am using WordPress on my personal domain and posting a post which content has some code block and as per my hosting provider said mod_security gives an error and my IP has gone into firewall because of mod_security .我在我的个人域上使用 WordPress 并发布了一个内容有一些代码块的帖子,并且根据我的托管服务提供商所说的mod_security给出了一个错误并且我的 IP 已经进入防火墙因为mod_security

So I want to disable mod_security by using .htaccess file.所以我想通过使用.htaccess文件来禁用mod_security

It is possible to do this, but most likely your host implemented mod_security for a reason.这样做是可能的,但很可能您的主机出于某种原因实施了mod_security Be sure they approve of you disabling it for your own site.确保他们同意您为自己的站点禁用它。

That said, this should do it;也就是说,应该这样做;

<IfModule mod_security.c>
  SecFilterEngine Off
  SecFilterScanPOST Off
</IfModule>

On some servers and web hosts, it's possible to disable ModSecurity via .htaccess , but be aware that you can only switch it on or off, you can't disable individual rules.某些服务器和 Web 主机上,可以通过.htaccess禁用 ModSecurity,但请注意,您只能打开或关闭它,不能禁用个别规则。

But a good practice that still keeps your site secure is to disable it only on specific URLs, rather than your entire site.但是,仍然保持您网站安全的一个好习惯是仅在特定 URL 上禁用它,而不是在整个网站上禁用它。 You can specify which URLs to match via the regex in the <If> statement below...您可以在下面的<If>语句中通过正则表达式指定要匹配的 URL...

### DISABLE mod_security firewall
### Some rules are currently too strict and are blocking legitimate users
### We only disable it for URLs that contain the regex below
### The regex below should be placed between "m#" and "#" 
### (this syntax is required when the string contains forward slashes)
<IfModule mod_security.c>
  <If "%{REQUEST_URI} =~ m#/admin/#">
    SecFilterEngine Off
    SecFilterScanPOST Off
  </If>
</IfModule>

When the above solution doesn't work try this:当上述解决方案不起作用时,试试这个:

<IfModule mod_security.c>
  SecRuleEngine Off
  SecFilterInheritance Off
  SecFilterEngine Off
  SecFilterScanPOST Off
  SecRuleRemoveById 300015 3000016 3000017
</IfModule>

Just to update this question for mod_security 2.7.0+ - they turned off the ability to mitigate modsec via htaccess unless you compile it with the --enable-htaccess-config flag.只是为了更新 mod_security 2.7.0+ 的这个问题——他们关闭了通过 htaccess 缓解 modsec 的能力,除非你用--enable-htaccess-config标志编译它。 Most hosts do not use this compiler option since it allows too lax security.大多数主机不使用此编译器选项,因为它允许过于宽松的安全性。 Instead, vhosts in httpd.conf are your go-to option for controlling modsec.相反,httpd.conf 中的虚拟主机是您控制 modsec 的首选选项。

Even if you do compile modsec with htaccess mitigation, there are less directives available.即使您使用 htaccess 缓解措施编译 modsec,可用的指令也较少。 SecRuleEngine can no longer be used there for example.例如, SecRuleEngine不能再在那里使用。 Here is a list that is available to use by default in htaccess if allowed (keep in mind a host may further limit this list with AllowOverride ):如果允许,这里是默认情况下可以在 htaccess 中使用的列表(请记住,主机可能会使用AllowOverride进一步限制此列表):

    - SecAction
    - SecRule

    - SecRuleRemoveByMsg
    - SecRuleRemoveByTag
    - SecRuleRemoveById

    - SecRuleUpdateActionById
    - SecRuleUpdateTargetById
    - SecRuleUpdateTargetByTag
    - SecRuleUpdateTargetByMsg

More info on the official modsec wiki 有关官方 modsec wiki 的更多信息

As an additional note for 2.x users: the IfModule should now look for mod_security2.c instead of the older mod_security.c作为 2.x 用户的附加说明: IfModule现在应该寻找mod_security2.c而不是旧的mod_security.c

With some web hosts including NameCheap , it's not possible to disable ModSecurity using .htaccess .对于包括NameCheap在内的某些网络主机,无法使用.htaccess禁用 ModSecurity。 The only option is to contact tech support and ask them to alter the configuration for you.唯一的选择是联系技术支持并要求他们为您更改配置。

In .htaccess file at site root directory edit following line:在站点根目录的 .htaccess 文件中编辑以下行:

 <ifmodule mod_security.c> SecFilterEngine Off SecFilterScanPOST Off </ifmodule> <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME}.-f RewriteCond %{REQUEST_FILENAME}.-d RewriteRule . /index.php [L] </IfModule>

Just keep the mod_security rules like SecFilterEngine and parts apart from each other.只需将 SecFilterEngine 之类的 mod_security 规则和部件彼此分开即可。 Its works for apache server它适用于 apache 服务器

For anyone that simply are looking to bypass the ERROR page to display the content on shared hosting.对于只是想绕过错误页面以在共享主机上显示内容的任何人。 You might wanna try and use redirect in .htaccess file.您可能想尝试在 .htaccess 文件中使用重定向。 If it is say 406 error, on UnoEuro it didn't seem to work simply deactivating the security.如果是 406 错误,在 UnoEuro 上它似乎并没有简单地停用安全性。 So I used this instead:所以我改用这个:

ErrorDocument 406 /

Then you can always change the error status using PHP.然后您可以随时使用 PHP 更改错误状态。 But be aware that in my case doing so means I am opening a door to SQL injections as I am bypassing WAF.但请注意,在我的情况下,这样做意味着我在绕过 WAF 时打开了 SQL 注入的大门。 So you will need to make sure that you either have your own security measures or enable the security again asap.因此,您需要确保您拥有自己的安全措施或尽快再次启用安全措施。

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

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