【问题标题】:Apache: RewriteCond %{QUERY_STRING} issueApache:RewriteCond %{QUERY_STRING} 问题
【发布时间】:2020-12-01 21:12:56
【问题描述】:

我希望阻止来自用户的http://anything.com/something.php?hack_attempt=select * 之类的请求。

为此,我在 .htaccess 中执行此操作

RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

问题是这条规则也符合http://anything.com/update.php 据我所知,%{QUERY_STRING} 应该只在 ? 之后包含 get params 字符串,但它会命中 URI。 谁能建议问题出在哪里?

更新:完整规则

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} ^.*(localhost|loopback|127\.0\.0\.1).*                                [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(\.|\*|;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*                 [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*/ban_url/                                                           [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]
 RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|insert|cast|set|declare|drop).* [NC,OR]
 RewriteCond %{QUERY_STRING} ^.*\?.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

 RewriteRule ^(.*)$ - [R=400,L]
</IfModule>

如果我取消注释

RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

那么 Apache 将阻止 www.anything.com/update.php 但它应该只阻止www.anything.com/something.php?param=update

更新 2:完整配置

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} ^.*(localhost|loopback|127\.0\.0\.1).*                                [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(\.|\*|;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*                 [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*/ban_url/                                                           [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]
 RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|insert|cast|set|declare|drop).* [NC,OR]
 RewriteCond %{QUERY_STRING} ^.*\?.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

 RewriteRule ^(.*)$ - [R=400,L]

 RewriteCond %{REQUEST_URI} ^.*wp-*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*www\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*backup\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*public_html\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*\.tar\.gz*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*administrator*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*admin\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*admin/index\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*elrekt\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*_adminer*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*accesson*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*agentui*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*trackback*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*wp-login*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*router\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*AspCms_AdminAdd*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*public/js/wind*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*include/calendar/calendar-cn*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*app-ads*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*vendor/phpunit/*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*utility/*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*blackhat*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*data/admin/allowurl*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*js/varien*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*js/mage*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*magento_version*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*db_z\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*functions\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*lottery-index*                                                           [NC]

 RewriteRule ^(.*)$ - [R=400,L]


</IfModule>

这个 conf 文件被应用在站点 conf 中

<VirtualHost *:80>
    Include /var/www/url_blacklist.conf
        ServerName ...
        DocumentRoot ...
        ErrorLog ...
</VirtualHost>

【问题讨论】:

  • 请发布您的RewriteRule 并更具体地说明您遇到的问题。
  • @AmitVerma 更新
  • @MykolaMykolayovichDolynskyi,字符串 hack_attempt= 是您查询字符串中真实字符串的样本之一(如果不是,请提及样本中可能存在的任何内容)?请确认一次。
  • @RavinderSingh13 不真实,我只想搜索仅在获取参数(?符号之后)中的任何禁用词,但实际上它也在 URI 中找到它们。我不能使用示例,因为我不知道下次会部署哪种攻击机器人
  • “更新:完整规则” - 这是你现在的完整重写配置,还是你也在做其他事情?你所说的正在发生,不能用你目前所展示的来解释,像htaccess.madewithlove.be这样的测试工具也同意。

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

我假设您想阻止/禁止所有在其查询字符串中具有 select * 以及其 uri 没有请求 update.php 的请求,如果是这种情况,请尝试关注一次。这些条件仅根据要求的条件编写,您可以尝试单独对其进行测试,然后也可以尝试将它们与您现有的条件合并。

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/update\.php [NC]
RewriteCond %{QUERY_STRING} select \* [NC]
RewriteRule ^ - [R=301,F]

【讨论】:

  • 像这样,我希望将任何仅在获取参数中具有 SQL 表达式的请求重定向到 301。同时我需要允许像 /update.php 这样的 ULR(但 QUERY_STRING 也命中 URL,我认为它应该只包含 get params)
  • @MykolaMykolayovichDolynskyi,看起来你的条件像RewriteCond %{REQUEST_URI} ^.*wp-* 然后RewriteCond %{REQUEST_URI} ^.*www\.zip* 在这里有问题,你没有把OR 条件放在那里,所以所有的都被认为是一个不可能的AND条件通过查看您的正则表达式(这对我来说看起来不太好,因为一切都在检查它是否从^ 开始),请尝试在那里放置OR 条件?
  • [NC,OR] 似乎无处不在,可能是没有向右滚动代码框?
  • @MykolaMykolayovichDolynskyi,抱歉,通常情况下不会有很多空格,所以我没有向右滚动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 2020-07-31
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多