【发布时间】:2021-05-25 07:29:38
【问题描述】:
根据 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 和\"%{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。
【问题讨论】:
-
当你使用remoteIP时你不必使用%{X-Forwarded-For}i,但是%a,它的输出是什么?
-
@on8tom
%a不是 remoteaddr 地址吗?这是 ELB 的地址,10.x.x.x. -
明确一点,如果客户端的ip地址是1.2.3.4,但是添加了自己的header "x-forwarded-for: 192.2.0.1",那么ELB会改变x-forwarded for标头为“x-forwarded-for: 192.2.0.1, 1.2.3.4”,而您只对 1.2.3.4 地址感兴趣?
-
@on8tom 正确
-
嗯,%a 不是您想要的吗?
标签: amazon-web-services apache apache2 amazon-elb x-forwarded-for