PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿
生活随笔
收集整理的這篇文章主要介紹了
PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
外界流傳的JAVA/PHP服務器端獲取客戶端IP都是這么取的: 偽代碼: 1)ip = request.getHeader("X-FORWARDED-FOR") ????可偽造,參考附錄A 2)如果該值為空或數組長度為0或等于"unknown",那么: ip = request.getHeader("Proxy-Client-IP") 3)如果該值為空或數組長度為0或等于"unknown",那么: ip = request.getHeader("WL-Proxy-Client-IP") 4)如果該值為空或數組長度為0或等于"unknown",那么: ip = request.getHeader("HTTP_CLIENT_IP") ????可偽造 5)如果該值為空或數組長度為0或等于"unknown",那么: ip = request.getRemoteAddr() ????可對于匿名代理服務器,可隱匿原始ip,參考附錄B ? 之所以搞這么麻煩,是因為存在很多種網絡結構,如 Nginx+Resin、Apache+WebLogic、Squid+Nginx。下面挨個兒講一下。 首先,明確一下,Nginx 配置一般如下所示: ? ? ? ? ? ? ? location / {
? ? ? ? ? ? ? ? ? ? ? ?proxy_pass ? ? ? http://yourdomain.com;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? Host ? ? ? ? ? ? $host;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? X-Real-IP ? ? ? ?$remote_addr;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? X-Forwarded-For ?$proxy_add_x_forwarded_for;
? ? ? ? ? ? ? } 注意看紅色字體,這些配置與下面的闖關拿IP有關。 ? ——————————————————————————————— ——第一關|X-Forwarded-For :背景—— 這是一個 Squid 開發的字段,并非 RFC 標準。 簡稱?XFF 頭,只有在通過了 HTTP 代理或者負載均衡服務器時才會添加該項。在 Squid 開發文檔中可以找到該項的詳細介紹。 XFF 格式如下: X-Forwarded-For: client1, proxy1, proxy2 可以看出,XFF 頭信息可以有多個,中間用逗號分隔,第一項為真實的客戶端ip,剩下的就是曾經經過的代理或負載均衡服務器的ip地址。 ——第一關|X-Forwarded-For :場景=客戶端--CDN--Nginx—— 當用戶請求經過 CDN 后到達 Nginx 負載均衡服務器時,其 XFF 頭信息應該為 “客戶端IP,CDN的IP”。 一般情況下CDN服務商出于自身安全考慮會將屏蔽CDN的ip,只保留客戶端ip。 那么請求頭到達 Nginx 時:
內網IP:172.16.100.10
客戶端IP:123.123.123.123
測試頁面: test.jsp
<%
out.println("x-forwarded-for: " + request.getHeader("x-forwarded-for"));
out.println("remote hosts: " + request.getRemoteAddr());
%>
nginx 配置一 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; wget測試 wget -O aa --header="X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp" 頁面返回結果: x-forwarded-for: 192.168.0.1, 123.123.123.123 remote hosts: 172.16.100.10 curl測試 curl -H "X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp" x-forwarded-for: 192.168.0.1, 123.123.123.123 remote hosts: 172.16.100.10
nginx 配置二
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
wget測試:
wget -O aa --header="X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp"
頁面返回結果:
x-forwarded-for: 123.123.123.123
remote hosts: 172.16.100.10
curl測試
curl -H "X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp"
x-forwarded-for: 123.123.123.123
remote hosts: 172.16.100.10
測試結果:
1、配置?? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
增加了一個真實ip X-Forwarded-For,并且順序是增加到了“后面”。
2、配置?? proxy_set_header X-Forwarded-For $remote_addr;
清空了客戶端偽造傳入的X-Forwarded-For,
保證了使用 request.getHeader("x-forwarded-for") 獲取的ip為真實ip,
或者用“,”分隔,截取 X-Forwarded-For 最后的值。 +++附錄B 搜狗瀏覽器高速模式的測試用例+++ 訪問路徑: 搜狗瀏覽器“高速”模式(即使用代理)-->LVS-->Apache 獲得的值為: x-forwarded-for:180.70.92.43? ?(即真實ip) Proxy-Client-IP:null WL-Proxy-Client-IP:null? getRemoteAddr:123.126.50.185??(即搜狗代理ip) ×××參考資源:××× 1,http://bbs.linuxtone.org/thread-9050-1-1.html 2,http://hi.baidu.com/thinkinginlamp/item/e2cf05263eb4d18e6e2cc3e6 3,http://bbs.chinaunix.net/thread-3659453-1-1.html 轉自:http://www.cnblogs.com/zhengyun_ustc/archive/2012/09/19/getremoteaddr.html
? ? ? ? ? ? ? ? ? ? ? ?proxy_pass ? ? ? http://yourdomain.com;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? Host ? ? ? ? ? ? $host;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? X-Real-IP ? ? ? ?$remote_addr;
? ? ? ? ? ? ? ? ? ? ? ?proxy_set_header ? X-Forwarded-For ?$proxy_add_x_forwarded_for;
? ? ? ? ? ? ? } 注意看紅色字體,這些配置與下面的闖關拿IP有關。 ? ——————————————————————————————— ——第一關|X-Forwarded-For :背景—— 這是一個 Squid 開發的字段,并非 RFC 標準。 簡稱?XFF 頭,只有在通過了 HTTP 代理或者負載均衡服務器時才會添加該項。在 Squid 開發文檔中可以找到該項的詳細介紹。 XFF 格式如下: X-Forwarded-For: client1, proxy1, proxy2 可以看出,XFF 頭信息可以有多個,中間用逗號分隔,第一項為真實的客戶端ip,剩下的就是曾經經過的代理或負載均衡服務器的ip地址。 ——第一關|X-Forwarded-For :場景=客戶端--CDN--Nginx—— 當用戶請求經過 CDN 后到達 Nginx 負載均衡服務器時,其 XFF 頭信息應該為 “客戶端IP,CDN的IP”。 一般情況下CDN服務商出于自身安全考慮會將屏蔽CDN的ip,只保留客戶端ip。 那么請求頭到達 Nginx 時:
- 在默認情況下,Nginx 并不會對 XFF 頭做任何處理
- 此時 Nginx 后面的 Resin/Apache/Tomcat 通過?request.getHeader("X-FORWARDED-FOR")?獲得的ip仍然是原始ip。
- 當 Nginx 設置 X-Forwarded-For 等于 $proxy_add_x_forwarded_for 時:
- 如果從CDN過來的請求沒有設置 XFF 頭(通常這種事情不會發生),XFF 頭為 CDN 的ip
- 此時相對于 Nginx 來說,客戶端就是 CDN?
- 如果 CDN 設置了 XFF 頭,我們這里又設置了一次,且值為$proxy_add_x_forwarded_for 的話:
- XFF 頭為“客戶端IP,Nginx負載均衡服務器IP”,這樣取第一個值即可
- 這也就是大家所常見的場景!
- 如果從CDN過來的請求沒有設置 XFF 頭(通常這種事情不會發生),XFF 頭為 CDN 的ip
內網IP:172.16.100.10
客戶端IP:123.123.123.123
測試頁面: test.jsp
<%
out.println("x-forwarded-for: " + request.getHeader("x-forwarded-for"));
out.println("remote hosts: " + request.getRemoteAddr());
%>
nginx 配置一 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; wget測試 wget -O aa --header="X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp" 頁面返回結果: x-forwarded-for: 192.168.0.1, 123.123.123.123 remote hosts: 172.16.100.10 curl測試 curl -H "X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp" x-forwarded-for: 192.168.0.1, 123.123.123.123 remote hosts: 172.16.100.10
nginx 配置二
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
wget測試:
wget -O aa --header="X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp"
頁面返回結果:
x-forwarded-for: 123.123.123.123
remote hosts: 172.16.100.10
curl測試
curl -H "X-Forwarded-For:192.168.0.1" "http://test.com/test.jsp"
x-forwarded-for: 123.123.123.123
remote hosts: 172.16.100.10
測試結果:
1、配置?? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
增加了一個真實ip X-Forwarded-For,并且順序是增加到了“后面”。
2、配置?? proxy_set_header X-Forwarded-For $remote_addr;
清空了客戶端偽造傳入的X-Forwarded-For,
保證了使用 request.getHeader("x-forwarded-for") 獲取的ip為真實ip,
或者用“,”分隔,截取 X-Forwarded-For 最后的值。 +++附錄B 搜狗瀏覽器高速模式的測試用例+++ 訪問路徑: 搜狗瀏覽器“高速”模式(即使用代理)-->LVS-->Apache 獲得的值為: x-forwarded-for:180.70.92.43? ?(即真實ip) Proxy-Client-IP:null WL-Proxy-Client-IP:null? getRemoteAddr:123.126.50.185??(即搜狗代理ip) ×××參考資源:××× 1,http://bbs.linuxtone.org/thread-9050-1-1.html 2,http://hi.baidu.com/thinkinginlamp/item/e2cf05263eb4d18e6e2cc3e6 3,http://bbs.chinaunix.net/thread-3659453-1-1.html 轉自:http://www.cnblogs.com/zhengyun_ustc/archive/2012/09/19/getremoteaddr.html
轉載于:https://www.cnblogs.com/JohnABC/p/4809359.html
總結
以上是生活随笔為你收集整理的PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将若干字符串按字母顺序(由小到大)输出(
- 下一篇: 实验一个最小的PYTHON服务器编程