【问题标题】:Query parameter And Url Rewriting And Url Blocking查询参数和 URL 重写和 URL 阻塞
【发布时间】:2020-01-22 18:15:10
【问题描述】:

在我的网站上,只允许少数查询查询参数。有些参数的长度应该只有 1 个字符,即 A-Z 或 a-z 或 1-0,但是,一些扫描仪或黑客试图使用我的 php 应用程序不支持的冗长参数访问 url,我可以在 php 应用程序中阻止它们级别,通过验证 $_GET 参数,但我的服务器正在加载,所以如果参数无效,我想显示 403/404

正确的 UR:: 示例

http://example.com/books/index.php?cat=A

但得到

http://example.com/books/index.php?cat=response.write(9687913*9040912
http://example.com/books/index.php?cat=/.././.././.././.././.././.././.././../etc/./passwd%2500
http://example.com/books/index.php?cat=%22%2bconvert(int%2cCHAR(52)%2bCHAR(67)%2bCHAR(117)%2bCHAR(84)%2bCHAR(117)%2bCHAR(79)%2bCHAR(115)%2bCHAR(84)%2bCHAR(107)%2bCHAR(68)%2bCHAR(76))%2b%22
http://example.com/books/index.php?cat=))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
http://example.com/books/index.php?cat=CWS%07%0e000x%9c%3d%8d1N%c3%40%10E%df%ae%8d%bdI%08)%d3%40%1d%a0%a2%05%09%11%89HiP%22%05D%8bF%8e%0bG%26%1b%d9%8e%117%a0%a2%dc%82%8a%1br%04X;%21S%8c%fe%cc%9b%f9%ff%aa%cb7Jq%af%7f%ed%f2.%f8%01>%9e%18p%c9c%9al%8b%aczG%f2%dc%beM%ec%abdkj%1

是否可以在 .htaccess 中限制某些参数只接受 1 个字符,否则向用户显示 404

【问题讨论】:

  • 当然可以……您可以通过%{QUERY_STRING} 访问查询字符串内容,因此您只需在 RewriteCond 中编写相应的条件即可。如果条件匹配,则以下 RewriteRule 可以拒绝访问。

标签: php .htaccess url-rewriting


【解决方案1】:

假设您想将参数 cat 限制为单个字母,然后使用此规则为 cat 值的所有其他情况返回 403 错误:

RewriteCond %{QUERY_STRING} !(?:^|&)cat=[a-z](?:&|$) [NC]
RewriteRule ^ - [F]

【讨论】:

  • 它包括 a-z 什么是 A-Z 和 0-9 以及其他特殊字符。因为我不知道正则表达式
  • 由于NC,它已经覆盖了[A-Z]。如果您还想包含[0-9],那么它会是:[a-z\d]。除此之外,您还想允许其他任何字符吗?
  • 或者,如果您只想允许任何字符的一个字符,请使用:RewriteCond %{QUERY_STRING} !(?:^|&)cat=\S(?:&|$) 其中\S 匹配任何非空格字符。
猜你喜欢
  • 2019-09-25
  • 2017-11-07
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2012-02-14
  • 1970-01-01
相关资源
最近更新 更多