【问题标题】:AWS ELB Apache Get Client IP, Avoid X-Forwarded-For SpoofingAWS ELB Apache 获取客户端 IP,避免 X-Forwarded-For 欺骗
【发布时间】: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


【解决方案1】:

我正在使用 apache 2.4 和 remoteIP 进行测试

RemoteIPHeader x-forwarded-for

您还需要告诉您的 apache 您的负载均衡器(反向代理)的内部 IP 地址是什么。没有可靠的简单方法来确定内部 IP,它可以通过“任何”RFC1918(私有)IP 地址 所以你需要添加这 3 个子网。

RemoteIPTrustedProxy 10.0.0.0/8
RemoteIPTrustedProxy 172.16.0.0/12
RemoteIPTrustedProxy 192.168.0.0/16

然后使用 %a 作为 logFormat

LogFromat %a

我已经测试过

RemoteIPTrustedProxy 127.0.0.1
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

用 curl 测试过

curl 127.0.0.1/so 
127.0.0.1 - - [27/Feb/2021:15:02:38 +0100] "GET /so/ HTTP/1.1" 200 167 "-" "curl/7.52.1"

curl 127.0.0.1/so/ -H "x-forwarded-for:  1.2.3.4"
1.2.3.4 - - [27/Feb/2021:15:04:06 +0100] "GET /so/ HTTP/1.1" 200 167 "-" "curl/7.52.1"

curl 127.0.0.1/so/ -H "x-forwarded-for:  8.5.4.1, 1.2.3.4"
1.2.3.4 - - [27/Feb/2021:15:04:31 +0100] "GET /so/ HTTP/1.1" 200 167 "-" "curl/7.52.1"

【讨论】:

  • RemoteIPHeader x-forwarded-for 按预期通过了我的 WAN。 RemoteIPTrustedProxy 带有 LAN 地址,RemoteIPInternalProxy 带有不同的 LAN 地址。我还没有测试转发头,因为标准请求仍然需要映射。 (我交替使用LANprivate ip
  • 我不知道如何解释您的最后评论。但您也可以添加 RemoteIPInternalProxy 10.0.0.0/8 以信任 10/8 子网中的所有私有 IP 地址
  • 我添加了一个简短的更新,是不是更清楚?如果这个答案足以满足您的需要,您可以输入一个概要,例如RemoteIPHeader x-forwarded-for\nRemoteIPTrustedProxy .... 或他们应该输入的任何顺序。也许我的输入不正确,一个会覆盖另一个
【解决方案2】:

我在不同的上下文中遇到了这个问题 - apache 正在从操纵的 X-Forwarded-For 标头解析主机名。例如。 X-Forwarded-For: mumblemuble.burpcollaborator.com, 1.1.1.1, anotherdomainapachewillresolve.com

我的解决方案是修改X-Forwarded-For标头,使其仅包含ELB添加的IP地址,并丢弃ELB附加到标头的IP地址。

代替其他反向代理,ELB 将现有的X-Forwarded-For 地址和主机名添加到客户端 IP 之前。对于其他反向代理,您应该修改正则表达式以丢弃第一个逗号后的所有内容。

# because of "early", headers can only be set in a main server or virtual host context. 
RequestHeader edit  "X-Forwarded-For" "^(.+),(.+)$" "$2" early

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2021-01-23
    • 1970-01-01
    • 2014-04-23
    • 2021-06-08
    • 2016-02-17
    • 2021-11-10
    • 2013-09-04
    • 2016-01-25
    相关资源
    最近更新 更多