You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何通过Selenium Python修改Chrome浏览器指纹(Canvas、WaveGL等)

解决Selenium中Chrome指纹(Canvas/WebGL等)伪造问题

核心思路

直接通过ChromeOptions修改UA或禁用GPU无法彻底解决指纹伪造问题,因为Canvas、WebGL等指纹是浏览器底层API生成的。需要通过Chrome DevTools Protocol (CDP) 注入自定义脚本,重写浏览器原生API,强制返回你指纹文件中的预设值。

具体步骤与代码实现

1. 读取指纹文件

假设你的指纹文件是JSON格式(fingerprint.json),包含以下字段:

{
  "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
  "canvas_data_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
  "webgl_renderer": "Google SwiftShader",
  "webgl_version": "WebGL 1.0 (OpenGL ES 2.0 Chromium)",
  "webgl_vendor": "Google Inc.",
  "webgl2_renderer": "Google SwiftShader",
  "webgl2_version": "WebGL 2.0 (OpenGL ES 3.0 Chromium)",
  "webgl2_vendor": "Google Inc."
}

先读取文件内容:

import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 加载本地指纹文件
with open("fingerprint.json", "r", encoding="utf-8") as f:
    fingerprint = json.load(f)

2. 配置Chrome基础选项

禁用自动化检测特征,避免被网站识别为Selenium驱动:

chrome_options = Options()
# 禁用自动化控制提示
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
# 移除自动化扩展和开关
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)
# 可选:禁用硬件加速(但核心是重写API,而非禁用GPU)
# chrome_options.add_argument("--disable-gpu")

3. 注入指纹替换脚本

通过CDP和execute_script注入脚本,重写Canvas、WebGL的原生API:

设置自定义UA

driver = webdriver.Chrome(options=chrome_options)
# 通过CDP设置UA,比ChromeOptions更隐蔽
driver.execute_cdp_cmd("Network.setUserAgentOverride", {
    "userAgent": fingerprint["user_agent"]
})

替换Canvas指纹

重写toDataURL和toBlob方法,返回指纹文件中的预设值:

canvas_override_script = f"""
// 重写Canvas的toDataURL方法
HTMLCanvasElement.prototype.toDataURL = function() {{
    return "{fingerprint['canvas_data_url']}";
}};

// 重写toBlob方法(部分网站会用这个检测)
HTMLCanvasElement.prototype.toBlob = function(callback, type) {{
    const blob = new Blob([atob("{fingerprint['canvas_data_url'].split(',')[1]}")], {{
        type: type || 'image/png'
    }});
    callback(blob);
}};
"""
driver.execute_script(canvas_override_script)

替换WebGL/WebGL2指纹

重写getParameter方法,返回指纹文件中的GPU相关参数:

webgl_override_script = f"""
// 重写WebGL1的getParameter方法
const originalWebGLGetParam = WebGLRenderingContext.prototype.getParameter;
WebGLRenderingContext.prototype.getParameter = function(param) {{
    switch(param) {{
        case this.RENDERER:
            return "{fingerprint['webgl_renderer']}";
        case this.VERSION:
            return "{fingerprint['webgl_version']}";
        case this.VENDOR:
            return "{fingerprint['webgl_vendor']}";
        default:
            return originalWebGLGetParam.call(this, param);
    }}
}};

// 重写WebGL2的getParameter方法
const originalWebGL2GetParam = WebGL2RenderingContext.prototype.getParameter;
WebGL2RenderingContext.prototype.getParameter = function(param) {{
    switch(param) {{
        case this.RENDERER:
            return "{fingerprint['webgl2_renderer']}";
        case this.VERSION:
            return "{fingerprint['webgl2_version']}";
        case this.VENDOR:
            return "{fingerprint['webgl2_vendor']}";
        default:
            return originalWebGL2GetParam.call(this, param);
    }}
}};
"""
driver.execute_script(webgl_override_script)

4. 测试验证

访问指纹检测网站验证替换效果:

driver.get("https://browserleaks.com/canvas")
driver.get("https://browserleaks.com/webgl")

关键注意事项

  • 指纹文件中的值必须是真实浏览器生成的有效数据,否则会被网站检测到异常。
  • 除了Canvas和WebGL,还需覆盖其他指纹项(如字体列表、时区、屏幕分辨率),可通过类似的脚本注入实现。
  • 若网站检测脚本注入,可将替换逻辑打包为Chrome扩展,在启动浏览器时加载扩展(通过chrome_options.add_extension("path/to/extension")),隐蔽性更强。

内容的提问来源于stack exchange,提问作者Rariti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:05:53