Nginx透过代理获取真实客户端IP

本系列中的故事纯属虚构,如有雷同实属巧合

小B是’柒’公司的安全攻城狮,为了完成任务小B开始做起了调研(欲知背景如何,且听下回分说)。

首先小B弄明白了’柒’公司的应用系统架构是:Client --> CDN --> SLB --> Server

发现在应用服务器上Nginx日志中采集的关于定位用户身份信息的IP维度数据不准确。不准确的原因是:因为在应用服务器中Nginx使用XFFremote_addr字段采集客户IP,XFF字段很好被攻击者伪造,而remote_addr字段一般采集都是直连时的IP,在经过多层代理、网关等设备时,更容易导致后端服务器获取的客户端IP不真实。

于是乎小B开始研究"Nginx如何获取客户端真实IP",下文是一些研究总结:

默认设置获取到不真实的IP

代理与服务器配置

  • Nginx_Server配置:vim /opt/nginx/conf/nginx.conf,服务器不作任何修改
    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,配置代理转发
    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,配置代理转发
    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;
        }
    }

正常访问的日志情况

此时我们的网络架构为:

# 客户端使用命令访问
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" "-"

此时在Nginx_Server中无法获取客户端真实IP。

伪造XFF的日志情况

此时我们的网络架构为:

# 客户端访问时使用XFF
curl -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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值