簡體   English   中英

AWS ELB Apache 獲取客戶端 IP,避免 X-Forwarded-For 欺騙

[英]AWS ELB Apache Get Client IP, Avoid X-Forwarded-For Spoofing

從 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是 header 被傳遞並且10.0.0.0是客戶端的請求機器,將會發生什么。 沒有 header 的標准請求將是:

10.0.0.0

有沒有辦法提取最右邊的 IP? 我在想類似的事情,

RemoteIPHeader ('(?:\d{1,3}[.]){3}\d{1,3}$', X-Forwarded-For,)[0]

但在 apache 中找不到 function 來配置它。 我可以在 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

我正在使用 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"

我在不同的上下文中遇到了這個問題 - apache 正在從操縱X-Forwarded-For header 解析主機名。 例如X-Forwarded-For: mumblemuble.burpcollaborator.com, 1.1.1.1, anotherdomainapachewillresolve.com

My solution was to modify the X-Forwarded-For header to only include the IP address added by ELB, and discard IP addresses which ELB appended to the header.

代替其他反向代理,ELB 在客戶端 IP之前附加現有的X-Forwarded-For地址和主機名。 對於其他反向代理,您應該修改正則表達式以丟棄第一個逗號后的所有內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM