从 apache 文档(https://httpd.apace.org/docs/2.4/mod/mod_remoteip.html)我们在我们的服务器上实现了以下任务:
RemoteIPHeader X-Forwarded-For
获取客户端的 IP 而不是 ELB 的 IP。但是,我们没有注意到 ELB 还将所有其他X-Forwarded-For
值附加到该字符串的左侧。因此,这不是获取客户端 IP 的安全方式。
我们已经使用LogFormat
with\"%{X-Forwarded-For}i\"
来验证传入的值是否符合文档记录。
192.168.0.0, 10.0.0.0
如果192.168.0.0
标头被传递并且10.0.0.0
是客户端的请求机器,将会发生什么。没有标头的标准请求是:
10.0.0.0
有没有办法提取最右边的IP?我在想类似的东西,
RemoteIPHeader ('(?:\d{1,3}[.]){3}\d{1,3}$', X-Forwarded-For,)[0]
但无法在 apache 中找到一个函数来配置它。我可以在 PHP 中执行此操作,但是我的日志中都会记录错误的 IP。
我试过了:
SetEnvIf X-Forwarded-For '((?:\d{1,3}[.]){3}\d{1,3})$' ip_is=$1
RemoteIPHeader %{ip_is}
但这没有影响。
更新:
Apache 配置运行:
LogFormat "%{%Y-%m-%d %H:%M:%S}t %a %u %A %p %m %U %q %>s \"%{User-agent}i\" %T/%D \"%{X-Forwarded-For}i\"" w3c_extended
CustomLog /var/log/httpd/example.com/access.log w3c_extended
我目前收到:
2021-02-27 14:29:06 10.0.21.150 - 10.0.20.222 443 GET /IPtest.php ?v=1 200 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" 0/2822 "73.149.97.219"
或者
2021-02-27 14:29:06 10.0.21.150 - 10.0.20.222 443 GET /IPtest.php ?v=1 200 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" 0/2822 "10.0.21.150, 73.149.97.219"
我需要73.149.97.219
在这两种情况下捕获。