日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

nginx curl命令有效 curl_setopt无效_日志分析系列(外传一):Nginx透过代理获取真实客户端IP...

發(fā)布時(shí)間:2024/9/15 Nginx 94 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx curl命令有效 curl_setopt无效_日志分析系列(外传一):Nginx透过代理获取真实客户端IP... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本系列中的故事純屬虛構(gòu),如有雷同實(shí)屬巧合

小B是Q公司的安全攻城獅,為了完成任務(wù)小B開始做起了調(diào)研(欲知背景如何,且聽下回分說)。

首先小B弄明白了Q公司的應(yīng)用系統(tǒng)架構(gòu)是:Client --> CDN --> SLB --> Server。

發(fā)現(xiàn)在應(yīng)用服務(wù)器上Nginx日志中采集的關(guān)于定位用戶身份信息的IP維度數(shù)據(jù)不準(zhǔn)確。不準(zhǔn)確的原因是:因?yàn)樵趹?yīng)用服務(wù)器中Nginx使用XFF與remote_addr字段采集客戶IP,XFF字段很好被攻擊者偽造,而remote_addr字段一般采集都是直連時(shí)的IP,在經(jīng)過多層代理、網(wǎng)關(guān)等設(shè)備時(shí),更容易導(dǎo)致后端服務(wù)器獲取的客戶端IP不真實(shí)。

于是乎小B開始研究"Nginx如何獲取客戶端真實(shí)IP",下文是一些研究總結(jié):

默認(rèn)設(shè)置獲取到不真實(shí)的IP

代理與服務(wù)器配置

  • Nginx_Server配置:vim /opt/nginx/conf/nginx.conf,服務(wù)器不作任何修改
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } }
  • Proxy_1配置:vim /opt/nginx/conf/nginx.conf,配置代理轉(zhuǎn)發(fā)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.99; } }
  • Proxy_2配置:vim /opt/nginx/conf/nginx.conf,配置代理轉(zhuǎn)發(fā)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.100; } }

正常訪問的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問curl -XGET "http://10.10.10.98"
  • Nginx_Server日志:
10.10.10.99 - - [11/Dec/2019:09:04:42 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_1日志:
10.10.10.1 - - [11/Dec/2019:09:04:43 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志:
10.10.10.98 - - [11/Dec/2019:09:04:42 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"

此時(shí)在Nginx_Server中無法獲取客戶端真實(shí)IP。

偽造XFF的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端訪問時(shí)使用XFFcurl -XGET "http://10.10.10.98" -H "X-Forwarded-For: 10.10.10.5"
  • Nginx_Server日志:
10.10.10.99 - - [11/Dec/2019:09:07:33 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_1日志:
10.10.10.1 - - [11/Dec/2019:09:07:32 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_2日志:
10.10.10.98 - - [11/Dec/2019:09:07:32 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5"

此時(shí)在Nginx_Server中無法獲取客戶端真實(shí)IP。

使用X-Forwarded-For+Nginx readip模塊獲取

使用realip模塊可以獲取客戶端真實(shí)IP,該方法也是目前使用最多最有效的方法。

查看nginx的編譯參數(shù):/opt/nginx/sbin/nginx -V(如果是yum安裝Nginx,則該模塊是默認(rèn)安裝的,我這里是使用編譯安裝的)

  • set_real_ip_from:表示從何處獲取真實(shí)IP,只認(rèn)可自己信賴的IP,可以是網(wǎng)段,也可以設(shè)置多個(gè)。
  • real_ip_header:表示從哪個(gè)header屬性中獲取真實(shí)IP。
  • real_ip_recursive:遞歸檢索真實(shí)IP,如果從X-Forwarded-For中獲取,則需要遞歸檢索;如果中X-Real-IP中獲取,無需遞歸。

代理與服務(wù)器配置

  • Nginx_Server配置:vim /opt/nginx/conf/nginx.conf,主要是在Server中新增代理服務(wù)器信息。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; # 注意這里的key value之間使用Tab而不要使用單個(gè)空格 set_real_ip_from 10.10.10.98; set_real_ip_from 10.10.10.99; real_ip_header X-Forwarded-For; real_ip_recursive on; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } }

檢查配置文件是否正確:/opt/nginx/sbin/nginx -t,然后重新加載配置文件:/opt/nginx/sbin/nginx -s reload

  • Proxy_1配置:vim /opt/nginx/conf/nginx.conf,設(shè)置代理并且設(shè)置XFF字段信息。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.99; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
  • Proxy_2配置:vim /opt/nginx/conf/nginx.conf,設(shè)置代理并且設(shè)置XFF字段信息。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.100; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

正常訪問的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問curl -XGET "http://10.10.10.98"
  • Nginx_Server日志:
10.10.10.1 - - [09/Dec/2019:09:19:21 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1, 10.10.10.98"
  • Proxy_1日志:
10.10.10.1 - - [09/Dec/2019:09:19:21 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:09:19:21 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1"

此時(shí)在Nginx_Server中remote_addr就是用戶的真實(shí)IP。

偽造XFF的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端訪問時(shí)使用XFFcurl -XGET "http://10.10.10.98" -H "X-Forwarded-For: 10.10.10.5"
  • Nginx_Server日志:
10.10.10.1 - - [09/Dec/2019:09:20:03 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5, 10.10.10.1, 10.10.10.98"
  • Proxy_1日志:
10.10.10.1 - - [09/Dec/2019:09:20:03 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:09:20:03 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5, 10.10.10.1"

此時(shí)在Nginx_Server中XFF字段依舊代表客戶端的真實(shí)IP,并且偽造的IP并沒有傳遞到Nginx_Server中。

使用代理的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問,我這里配置的是終端全局代理,所以不用單獨(dú)指定代理參數(shù)curl -XGET "http://47.x.x.156"
  • Nginx_Server日志
43.x.x.74 - - [09/Dec/2019:14:58:02 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "43.x.x.74, 172.16.178.76"
  • Proxy_1日志
43.x.x.74 - - [09/Dec/2019:14:58:02 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志
172.16.178.76 - - [09/Dec/2019:14:58:02 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "43.x.x.74"

此時(shí)在Nginx_Server中XFF字段就是用戶的代理IP,并且可以看到單獨(dú)使用Nginx無法獲取客戶端的真實(shí)IP。

使用X-Forwarded-For與安全設(shè)置獲取

在第一層代理服務(wù)器位置,處理用戶傳遞的XFF信息,忽略用戶的XFF值。

代理與服務(wù)器配置

  • Nginx_Server配置:vim /opt/nginx/conf/nginx.conf,Nginx_Server配置不作任何修改,默認(rèn)即可。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } }
  • Proxy_1配置:vim /opt/nginx/conf/nginx.conf,定義XFF為remote_addr。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.99; proxy_set_header X-Forwarded-For $remote_addr; } }
  • Proxy_2配置:vim /opt/nginx/conf/nginx.conf,只做代理轉(zhuǎn)發(fā)。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.100; } }

正常訪問的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問curl -XGET "http://10.10.10.98"
  • Nginx_Server日志:
10.10.10.99 - - [09/Dec/2019:09:37:39 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1"
  • Proxy_1日志:
10.10.10.1 - - [09/Dec/2019:09:37:39 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:09:37:39 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1"

此時(shí)在Nginx_Server中XFF字段就代表用戶的真實(shí)IP。

偽造XFF的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端訪問時(shí)使用XFFcurl -XGET "http://10.10.10.98" -H "X-Forwarded-For: 10.10.10.5"
  • Nginx_Server日志:
10.10.10.99 - - [09/Dec/2019:09:41:53 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1"
  • Proxy_1日志:
10.10.10.1 - - [09/Dec/2019:09:41:53 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:09:41:53 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.1"

此時(shí)在Nginx_Server中XFF字段依舊代表客戶端的真實(shí)IP,并且偽造的IP并沒有傳遞到Nginx_Server中。

使用代理的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問,我這里配置的是終端全局代理,所以不用單獨(dú)指定代理參數(shù)curl -XGET "http://47.x.x.156"
  • Nginx_Server日志
172.16.178.77 - - [09/Dec/2019:15:07:45 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "43.x.x.74"
  • Proxy_1日志
43.x.x.74 - - [09/Dec/2019:15:07:44 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志
172.16.178.76 - - [09/Dec/2019:15:07:44 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "43.x.x.74"

此時(shí)在Nginx_Server中XFF字段就是用戶的代理IP,并且可以看到單獨(dú)使用Nginx無法獲取客戶端的真實(shí)IP。

使用X-Real-IP

代理與服務(wù)器配置

  • Nginx_Server配置:vim /opt/nginx/conf/nginx.conf,將日志中的remote_addr替換為http_x_real_ip。
# 注意日志配置的第一個(gè)字段,將remote_addr修改為http_x_real_ip log_format main '$http_x_real_ip - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } }
  • Proxy_1配置:vim /opt/nginx/conf/nginx.conf,設(shè)置代理與x-real-ip字段。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.99; proxy_set_header X-Real-IP $remote_addr; } }
  • Proxy_2配置:vim /opt/nginx/conf/nginx.conf,只做代理轉(zhuǎn)發(fā)。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; # ************* 省略了中間的配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; # 注意這里的key value之間使用Tab proxy_pass http://10.10.10.100; } }

正常訪問的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問curl -XGET "http://10.10.10.98"
  • Nginx_Server日志:
10.10.10.1 - - [09/Dec/2019:09:55:16 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_1日志:
10.10.10.1 - - [09/Dec/2019:09:55:16 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:09:55:16 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"

此時(shí)在Nginx_Server中第一個(gè)字段就代表客戶端的真實(shí)IP。

偽造XFF的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端訪問時(shí)使用XFFcurl -XGET "http://10.10.10.98" -H "X-Forwarded-For: 10.10.10.5"
  • Nginx_Server日志:
10.10.10.1 - - [09/Dec/2019:10:00:38 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_1日志
10.10.10.1 - - [09/Dec/2019:10:00:38 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "10.10.10.5"
  • Proxy_2日志:
10.10.10.98 - - [09/Dec/2019:10:00:38 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "10.10.10.5"

此時(shí)在Nginx_Server中第一個(gè)字段依舊代表客戶端真實(shí)IP,偽造的IP在XFF字段中。

使用代理的日志情況

此時(shí)我們的網(wǎng)絡(luò)架構(gòu)為:

# 客戶端使用命令訪問,我這里配置的是終端全局代理,所以不用單獨(dú)指定代理參數(shù)curl -XGET "http://47.x.x.156"
  • Nginx_Server日志:
43.x.x.74 - - [09/Dec/2019:15:16:05 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_1日志:
43.x.x.74 - - [09/Dec/2019:15:16:05 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"
  • Proxy_2日志:
172.16.178.76 - - [09/Dec/2019:15:16:05 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "-"

此時(shí)在Nginx_Server中第一個(gè)字段依舊代表客戶端真實(shí)IP。

云廠商如何獲取客戶端真實(shí)IP

  • 阿里云 如何獲取客戶端真實(shí)IP(https://help.aliyun.com/document_detail/54007.html)
  • 使用知道創(chuàng)宇云安全后如何獲取訪客真實(shí)IP(http://help.yunaq.com/faq/67/index.html)

總結(jié)一下

關(guān)于服務(wù)端獲取客戶端的真實(shí)IP可以實(shí)際場(chǎng)景實(shí)際分析吧!本文中提到的也只是一種很初級(jí)的網(wǎng)絡(luò)架構(gòu)。本文的適用范圍相對(duì)也比較狹窄。

如果是復(fù)雜的網(wǎng)絡(luò)結(jié)構(gòu),可以在每一層的產(chǎn)品上對(duì)對(duì)應(yīng)廠商進(jìn)行溝通:是否可以透?jìng)饔脩舻恼鎸?shí)IP,然后通過每一層的配置將真實(shí)IP傳遞到服務(wù)端使用合理的字段進(jìn)行存儲(chǔ)。

當(dāng)然了安全本質(zhì)就是不可信,傳遞的IP數(shù)據(jù)是否真實(shí)與客戶端偽造技術(shù)、各層級(jí)之間相關(guān)配置都息息相關(guān)。IP維度也只是后端分析識(shí)別的一個(gè)維度而已,我們?cè)诒M可能保證這個(gè)維度的準(zhǔn)確度時(shí),不用太過鉆牛角尖(除非是精準(zhǔn)度要求非常高的場(chǎng)景)。對(duì)于中小型的企業(yè),能結(jié)合IP、Location、Username、UA、Browser Banner、OS Banner等維度來做一些簡(jiǎn)單的關(guān)聯(lián)分析即可。

以上就是小B做日志分析的前期調(diào)研第一篇,小B后續(xù)還會(huì)寫一寫關(guān)于日志分析的其他文章。(WeChat:Lzero2012)

參考資料

  • Nginx多級(jí)反向代理下的IP透?jìng)?https://www.cnblogs.com/tea-melon/p/10977516.html)
  • Nginx之X-Forwarded-For中首個(gè)IP一定真實(shí)嗎?(https://juejin.im/entry/5bbb6e90f265da0a89304a43)

總結(jié)

以上是生活随笔為你收集整理的nginx curl命令有效 curl_setopt无效_日志分析系列(外传一):Nginx透过代理获取真实客户端IP...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。