【问题标题】:Apache dynamic IP whitelist for specific directory and files特定目录和文件的 Apache 动态 IP 白名单
【发布时间】:2019-05-10 11:12:03
【问题描述】:

我正在尝试将 IP 动态列入白名单以授予对特定目录的访问权限。一个 php 脚本将不断修改 whitelist.txt 文件以添加/删除条目。

我知道使用 RewriteMap 处理这个问题的正确方法,但我不确定如何设置它。

例如,我希望用户在访问 example.com 时能够正常访问我的网站,但是我想拒绝所有访问块路径/目录“http://example.com/block”中的任何内容的用户访问,但白名单中的那些 IP 地址除外.txt,此外,whitelist.txt 中的那些 IP 地址将只能访问“块”目录中的特定文件夹和文件,请求 eg:

http://example.com/block/123/123.txt

我已经尝试了下面的代码(这是一个粗略的草图,我敢肯定它完全是错误的,但只是为了理解):

RewriteEngine on

RewriteCond %{THE_REQUEST} ^\/block+\ ##apply rules only for /block directory

RewriteMap ipmap txt://var/whitelist.txt

RewriteCond ${ipmap:%{REMOTE_ADDR}} ^\/([0-9]*).txt$ $1 [NC] ##check whitelist for matching IP AND specific dir and file

RewriteRule .* - [F,L]

这当然行不通。当我访问 example.com 时,我的网站会进入无限重定向循环。

whitelist.txt 文件如下所示:

170.172.100.162 123
152.109.211.250 43
62.55.254.83 2345
227.202.162.48 32
203.52.248.55 533

所以 IP 地址 170.172.100.162 将可以访问http://www.example.com/block/123/123.txt

IP 地址 152.109.211.250 将可以访问http://www.example.com/block/43/43.txt

... 等等。

【问题讨论】:

  • 看起来,人们不再看htaccess 问题了,我在看一些关于htaccess 的问题,他们甚至几个月都没有答案,很高兴有人能提供帮助..

标签: php apache .htaccess http mod-rewrite


【解决方案1】:

我已经从你的规则开始玩了一点,并得到了这个:

RewriteEngine On

RewriteCond %{THE_REQUEST} \/block\/? 
# apply rules only for /block directory

RewriteMap ipmap txt:/var/whitelist.txt

RewriteCond ${ipmap:%{REMOTE_ADDR}} ^$ [NC]
RewriteRule .* /block [R=403,L]
# redirect to /block with 403 when IP address not in the whitelist 

RewriteCond %{REQUEST_URI} /+[^\.]+$ [NC]
# stops when the request finds a dot '.', assuming a file
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^\d+$ [NC]
# does the redirect only when the IP is in the whitelist
RewriteRule .* /block/${ipmap:%{REMOTE_ADDR}}/${ipmap:%{REMOTE_ADDR}}.txt [R=permanent,L] 
# will redirect everything from /block to /block/x/x.txt -> x = numeric value corresponding to the request IP from the whitelist.txt file

经过测试,它的工作原理如下:

希望这会有所帮助。

编辑

  • 如果在 whitelist.txt 中找不到 IP,则会执行重定向到 /block 并使用 403 代码

编辑 2

目前任何拥有 whitelist.txt 文件 IP 的用户都可以访问其他用户目录。我试图找到一个条件,但没有真正找到任何东西。所以我现在能想到的是在目录级别拥有.htaccess 文件,如下所示:

deny from all
allow from 1.2.3.4
#1.2.3.4 is arbitrary

【讨论】:

  • 谢谢,我会对此进行测试并回复您。您知道是否可以使用通配符 IP,例如 170.172.100.0/24 ?有时最后一组数字会有所不同。也许在白名单中使用 170.172.100.*,但是如何将它与 RewriteRule 一起使用?
  • 另外,使用您的解决方案,当从不同的设备访问时 - 当它被重定向到白名单文件中的条目时,您是什么意思?我希望它返回 403 访问被拒绝,但不重定向?
  • 网络中不同的设备意味着不同的IP地址。通过这种方式,我测试了如何从不同的 IP 将其来自 whitelist.txt 文件的相应条目馈送到 RewriteRule。
  • 我想出了这个以按我想要的方式工作,但我有一个小问题:捕获 %1 在 RewriteCond 中不起作用,我不知道为什么。测试网址:example.com/block/0/1/file.txtwhitelist.txt:170.172.100.12 1 RewriteEngine On RewriteCond %{THE_REQUEST} \/block\/? RewriteMap ipmap txt:/var/www/html/block/whitelist.txt RewriteCond %{THE_REQUEST} \/block\/[0-9]\/([0-9]*)\/ RewriteCond ${ipmap:%{REMOTE_ADDR}} !^%1$ [NC] RewriteRule .* - [F,L]
  • @user969622 我不知道为什么我的解决方案不适合您,我已经在 OSX Apache 2.4.18 和 Ubuntu Apache 2.4.7 上对其进行了测试。您使用哪个操作系统/Apache 版本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 2013-05-11
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
相关资源
最近更新 更多