关于代理匿名等级检测

关于代理匿名等级检测
Page content

现在市面上我们可以了解到的代理类型分别为TRANSPARENTANONYMOUSDISTORTINGELITE四种。今天,我们就来简单聊聊如何根据请求Header进行代理的匿名检测。

定义匿名类

为了方便区分,除了以上4种代理,我们还加入了无代理未知代理

from enum import Enum, unique

@unique
class PROXIES(Enum):
    NONE = "无代理"
    TRANSPARENT = "透明代理"
    ANONYMOUS = "匿名代理"
    DISTORTING = "欺骗性代理"
    ELITE = "高匿代理"
    UNKNOW = "未知代理"

判断原理

  • Remote_Addr 末端代理服务器IP地址
  • Via 通过的代理服务器(ip或名称)
  • X_ForWarded_For 经过的代理IP地址集合(可伪造)

假设您的地址为1.2.3.4 并通过了A代理来连接站点

  1. TRANSPARENT

    • Remote_AddrA
    • X_ForWarded_For1.2.3.4, A
    • 存在 Via
  2. ANONYMOUS

    • Remote_AddrC
    • X_ForWarded_ForA, B, C
    • 存在 Via
  3. DISTORTING

    • Remote_Addr 可能为 ABC
    • X_ForWarded_For 为不可预期的结果
    • 存在 Via
  4. ELITE

    • Remote_Addr 可能为 BC
    • X_ForWarded_For 为空
    • 不存在 Via

原理图

服务器准备

为了快速开发,我们这里使用Caddy作为反向代理,以保障获取到X-Forwarded-for

0.0.0.0:80
gzip
proxy / 127.0.0.1:12888 {
    header_upstream X-Forwarded-Port {server_port}
    header_upstream X-Forwarded-Proto {scheme}
    header_upstream X-Real-IP {remote}
}

逻辑呈现

我们使用tornado作为服务来进行判断,并且,请求所经过代理的请求附带提交真实客户端IP及使用的代理IP

Server端

class Main(tornado.web.RequestHandler):
    async def get(self):
        client_real_ip = self.get_argument("client", "").split(":")[0].strip() # 起始客户端提供IP
        proxy_ip = self.get_argument("proxy", None)  # 起始客户端提供代理IP

        x_real_ip = self.request.remote_ip  # 最后一级客户端IP
        x_forwarded_for = self.request.headers.get("X-Forwarded-For", "")
        x_forwarded_for_ip = x_forwarded_for.split(",")[0].strip()  # 第一个代理IP地址
        http_via = self.request.headers.get("Via")

        types = PROXIES.UNKNOW.value # 默认代理类型
        if is_ip(client_real_ip):  # 是否有给定真实地址
            if http_via:  # 是否有via
                if proxy_ip and is_ip(proxy_ip):  # 是否有指明代理
                    if x_forwarded_for_ip == client_real_ip and x_real_ip == proxy_ip:  # 透明代理
                        types = PROXIES.TRANSPARENT.value
                    elif x_real_ip == proxy_ip and x_forwarded_for_ip != client_real_ip:  # 匿名代理
                        types = PROXIES.ANONYMOUS.value
                    elif x_forwarded_for_ip != proxy_ip and x_forwarded_for_ip:  # 随机代理
                        types = PROXIES.DISTORTING.value
            else:
                if client_real_ip != x_real_ip:
                    types = PROXIES.ELITE.value  # 高匿代理
                else:
                    types = PROXIES.NONE.value  # 未使用代理
            self.write({"status": True, "msg": "ok", "types": types, "end_ip": x_real_ip})
        else:
            self.write({"status": False,"msg": "check your client ip","types": types,"end_ip": x_real_ip})

Client端

我们编写如下脚本进行批量测试

from enum import Enum
import requests

SERVER_NET_PORT = 80

class IPs(Enum):
    CLIENT = "33.44.55.66"
    SERVER = "12.13.14.15"

def checker(ip,port=80,protocol='http'):
    proxy_uri = f"{protocol}://{ip}:{port}" # 代理地址
    proxies = {"http": proxy_uri, "https": proxy_uri} # 代理地址字典
    server_url =f"http://{IPs.SERVER.value}:{SERVER_NET_PORT}?client={IPs.CLIENT.value}&proxy={ip}" # 组合地址
    resp = requests.get(server_url,proxies=proxies,timeout=TEST_TIMEOUT) # 发送请求
    return {proxy_uri: resp.json()}


def main():
    proxy_list = [("200.255.122.170",8080)]
    datas = list(map(lambda item: checker(*item), proxy_list))
    print(datas)
Tor测试

或者我们简单粗暴一些,直接通过匿名网络来测试。只需要提供源IP即可

可以看到返回为ELITE(高匿)

设置后台任务

kill -9 `ps -ef|grep "caddy"|awk '{print $2}'`
nohup caddy >> /var/log/caddy.log  &

kill -9 `ps -ef|grep "python3 server.py"|awk '{print $2}'`
nohup python3 server.py >> /var/log/tornado.log  &

项目源码

https://github.com/aoii103/proxy_checker