借助WinHTTP突破Cloudflare的反爬限制(TLS指纹识别)

张开发
2026/4/12 9:15:07 15 分钟阅读

分享文章

借助WinHTTP突破Cloudflare的反爬限制(TLS指纹识别)
被 Cloudflare 403 阻止【WAX云钱包】Cloudflare反爬虫突破SSL指纹识别在之前的文章中我们使用 Python 的 Requests 进行一些网页游戏的自动化操作其中涉及到一个WAX云钱包的签名操作import requests resp requests.get(https://public-wax-on.wax.io/wam/sign) print(resp.text)正常情况下这个GET请求应该返回一个json可以在 Chrome 浏览器中直接访问以验证这点但是我们使用 Python 发送请求后却收到了 403 Forbidden 错误并且返回一个这样的页面Please enable cookies.Sorry, you have been blockedYou are unable to access wax.ioWhy have I been blocked?This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.What can I do to resolve this?You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.Cloudflare Ray ID: 711e5eaf6819980c • Your IP: * • Performance security by Cloudflare显然这个 URL 使用了 Cloudflare 做 CDN 加速而我们被 Cloudflare 的反爬虫机制制裁了。经过一番研究我们终于知道 Cloudflare 是通过 TLS 指纹把我们识别出来了它通过 TLS 特征判断我们是脚本、机器人而不是正常的浏览器从而403阻止我们访问。解决方案在之前的文章中我们通过修改 Python 的 urllib3 的 SSL 配置来影响我们的 TLS 特征但这种做法过一段时间仍然被 Cloudflare 识别出来了原因很简单这些 TLS 特征仍然和正常的 Chrome / Edge 浏览器差异过大Cloudflare 认为你并不是用浏览器在访问。我们也使用过 cloudscraper 和 cloudflare-scrape 等开源项目来修改 TLS 特征这虽然对WAX云钱包的这个URL有用但是根据网友反馈这些方案在别的URL上仍然会被 Cloudflare 识别并拦截。当时我们想到的终极方案是借助 selenium 启动一个 Chrome 浏览器然后借助 Chrome 浏览器来发送 HTTP 请求这样它的 TLS 特征就完全和 Chrome 一致了因为本来就是 Chrome 发出的 HTTP 请求嘛。当时网上也有了现成的开源方案【undetected-chromedriver】https://github.com/ultrafunkamsterdam/undetected-chromedriver但这个方法还是太笨重了它需要启动一个 Chrome 进程如果你的脚本需要并发作业的话就要启动多个 Chrome 进程太消耗资源了。上篇文章的最后我们提到了一个思路就是借助 WinHTTP 来发送请求由于 WinHTTP 在 Windows 上是 IE / Edge 浏览器的底层库它发出的 HTTP 请求的 TLS 指纹与 IE / Edge 浏览器完全一致所以可以躲过 Cloudflare 的检测。当时我用 VC 调用 WinHTTP 进行了测试验证了可行性但由于项目上没有需求没有时间就暂时没有进一步研究了。后来最近有空了我终于基于 WinHTTP 库实现了一个 Python 的 http client。pywinhttp我将其命名为 pywinhttp 并放到 github 上开源【pywinhttp】https://github.com/encoderlee/pywinhttp当然我也上传到了 PYPI 可以直接用 PIP 安装pip install pywinhttp实现原理大概就是使用 Python 内建的 ctypes 直接调用 winhttp.dl 的 C 语言接口没有第三方依赖纯 Python 实现。当然 WinHTTP 这玩意儿只在 Windows 上有代码也只能在 Windows 上运行无法在 Linux 上运行。示例代码两个例子搞懂 pywinhttp 的用法importpywinhttp user_agentMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0clientpywinhttp.Session(user_agent)urlhttps://public-wax-on.wax.io/wam/signrespclient.get(url)print(resp.text)post_data{serializedTransaction:xxxxxxxxxxxxxxxxxxxxxxx,description:jwt is insecure,freeBandwidth:False,website:play.alienworlds.io,}headers{x-access-token:xxxxxxxxxxxxxxxxxxxxxxx}respclient.post(url,jsonpost_data,headersheaders)print(resp.text)user_agent 要完全模仿 Edge 支持 GET / POST 请求经过对 https://public-wax-on.wax.io/wam/sign 的测试没有再被 Cloudflare 403 拦截返回了正常的 JSON 值importpywinhttpfrompywinhttpimportHttpProxydefmain():clientpywinhttp.Session()client.proxyHttpProxy(127.0.0.1,8443,user,password)# set timeout 30 secondsclient.timeout30*1000urlhttps://httpbin.org/getrespclient.get(url)print(resp.text)if__name____main__:main()支持 HTTP/HTTPS 代理支持用户名密码验证可以设置 HTTP 超时值但不支持 SOCKS5 代理因为 WinHTTP 本来就不支持。。

更多文章