93

如何通过 Cloudflare 反爬虫检测

 3 years ago
source link: https://azhuge233.com/%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87-cloudflare-%e5%8f%8d%e7%88%ac%e8%99%ab%e6%a3%80%e6%b5%8b/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何通过 Cloudflare 反爬虫检测

21-01-06 更新:Selenium 无法通过的页面可以尝试更换到 PlayWright 并选择 Firefox/WebKit 浏览器,实测可以通过 SteamDB 的检测页面

20-12-30 更新:uc 版本更新到了 1.5.2,此次更新随机化了字符串,但是经测试无法通过 SteamDB 的检测,另外 SteamDB 的免费游戏页面增加了一行隐藏 tr,如果尝试打开隐藏行内的链接会遭到 SteamDB 的 IP 封禁

20-12-09 更新:undetected_chromedriver 更新了版本 1.5.1,现在已经可以通过 CloudFlare 的 DDoS 检测页面。原因参考 issue – Cloudflare DDOS protection 中 czoin 的回复,似乎 CloudFlare 是通过检测 undetected_chromedriver 中的特定字符串来判断是否是机器人的,czion 魔改了代码,每次运行时随机生成 CloudFlare 要检测的字符串,这样绕过了 DDoS 页面。 undetected_chromedriver 更新的 1.5.1 版本只是更改了该字符串的一个字母

20-10-27 更新:此方法对 SteamDB 已失效

与 Cloudflare 的检测页面简直是一场无休止的斗争,一开始 Cloudflare 不会检测 Selenium,所以只需要手动等待 5 秒即可跳转到原始页面,后来 Cloudflare 加了 hcaptcha,于是我又使用了验证码识别平台 2captcha,详见

就在我写完 2captcha 没多久,Cloudflare 又更换了检测策略,页面还是和原来 5 秒检测页面一样,但是会检测到 Selenium 自动化,让你无限卡在验证页面不停加载

本文将分别展示在 Python 和 C# 下,如何使用 Selenium 通过 Cloudflare 的 5 秒检测页面

当然此对策只适用于当前的检测手段,Cloudflare 未来必然会再次更改检测策略 : )

Python

Python 想要通过检测比较简单,更换使用的库即可

经过一番搜索,发现很多针对 Cloudflare 检测的爬虫库都已经 Archive 了,还在更新的 cfscrape 也有一堆 issue 表示失效。但是,在 cfscrape 最新的一个 issue 中找到了 chromedriver 的魔改版 ultrafunkamsterdam / undetected-chromedriver ,此项目在近期的 1.5.0 更新中解决了 Cloudflare 的爬虫检测问题

  1. 首先安装 undetected_chromedriver
    pip install undetected_chromedriver
    pip install undetected_chromedriver
  2. 按如下代码新建 driver 对象
    import undetected_chromedriver as uc
    browser = uc.Chrome()
    browser.get(url)
    time.sleep(delay)
    html = browser.page_source
    import undetected_chromedriver as uc
    
    browser = uc.Chrome()
    browser.get(url)
    time.sleep(delay)
    html = browser.page_source

这样就可以成功通过 Chloudflare 检测,实测 steamdb.info 可以成功通过

C#(.NET)

.NET 平台没有类似 undetected_chromedriver 这样的 Nuget 包,但是从源码中可以看出,undetected_chromedriver 相较于原版 chromedriver 只做了两件事:

  1. 魔改 chromedriver.exe
  2. 添加两个启动参数如何通过 Cloudflare 反爬虫检测

所以我们只需要拿到魔改后的 chromedriver,再添加与 undetected_chromedriver 中相同的两个参数即可通过反爬虫检测

魔改后的 chromedriver 可以直接复制 python 中 undetected_chromedriver 代码运行后生成的 chromedriver.exe,将其放在系统 PATH 内,C# 代码执行时会直接调用系统 PATH 的 chromedriver

代码如下:

using OpenQA.Selenium.Chrome;
internal ChromeDriver CreateDriver() {
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("start-maximized");
chromeOptions.AddArgument("--disable-blink-features=AutomationControlled");
var mychrome = new ChromeDriver(chromeOptions);
return mychrome;
using OpenQA.Selenium.Chrome;

internal ChromeDriver CreateDriver() {
    var chromeOptions = new ChromeOptions();
    chromeOptions.AddArgument("start-maximized");
    chromeOptions.AddArgument("--disable-blink-features=AutomationControlled");
    var mychrome = new ChromeDriver(chromeOptions);
    return mychrome;
}

关于 undetected_chromedriver 的绕过手段

undetected_chromedriver 的源码非常少,详见 __init__.py 它的运行流程图如下如何通过 Cloudflare 反爬虫检测

注意:如果在 python 代码中使用 undetected_chromedriver 新建 driver 对象时加入了自定义启动选项(chrome_options),则会覆盖包内的默认的启动选项,如果需要自定义,需要加入上文源码截图内的三个选项


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK