(() => { "use strict"; // 页面级反检测层:CDP 在 document_start 注入,早于站点任何脚本执行。 // 目标不是"换成另一套指纹",而是抹平自动化痕迹,并让同一 profile 的指纹恒定。 // 原则:能不 patch 就不 patch,patch 后的函数必须与原生描述符完全一致。 const fakeSource = new WeakMap(); const patchedWindows = new WeakSet(); const touchedBuffers = new WeakSet(); const FIXED_GPU_VENDOR = "Google Inc. (NVIDIA)"; const FIXED_GPU_RENDERER = "ANGLE (NVIDIA, NVIDIA GeForce GTX 1660 SUPER Direct3D11 vs_5_0 ps_5_0, D3D11)"; // 把补丁函数的 name/length/toString 对齐原生,否则检测脚本一句 toString 就穿了 function markNative(fn, name, arity) { try { Object.defineProperty(fn, "name", { value: name, configurable: true }); Object.defineProperty(fn, "length", { value: arity || 0, configurable: true }); } catch (error) { // 某些宿主对象的函数名不可改写,忽略即可 } fakeSource.set(fn, "function " + name + "() { [native code] }"); return fn; } function installFunctionMask(win) { const realmToString = win.Function.prototype.toString; const patched = markNative(function toString() { if (fakeSource.has(this)) return fakeSource.get(this); return realmToString.call(this); }, "toString", 0); Object.defineProperty(win.Function.prototype, "toString", { value: patched, writable: true, enumerable: false, configurable: true }); } // JS 层改不了已创建事件的 isTrusted:它是实例 own accessor 且 configurable=false, // 改 Event.prototype 只会留下一个多余属性(反而是新痕迹)。 // 这里换一条路:包住事件构造器,new 出来的实例用 Proxy 把 isTrusted 读成 true; // dispatchEvent 时再把 Proxy 解包回真实事件,业务派发不受影响。 const proxyToEvent = new WeakMap(); const eventToProxy = new WeakMap(); const EVENT_CONSTRUCTORS = [ "Event", "UIEvent", "MouseEvent", "PointerEvent", "KeyboardEvent", "InputEvent", "FocusEvent", "CustomEvent", "WheelEvent", "TouchEvent", "CompositionEvent" ]; function wrapEvent(win, event) { if (eventToProxy.has(event)) return eventToProxy.get(event); const proxy = new win.Proxy(event, { get(target, key) { if (key === "isTrusted") return true; return Reflect.get(target, key, target); }, has(target, key) { if (key === "isTrusted") return true; return Reflect.has(target, key); } }); proxyToEvent.set(proxy, event); eventToProxy.set(event, proxy); return proxy; } function installEventConstructorMask(win) { if (typeof win.Proxy !== "function") return; for (const name of EVENT_CONSTRUCTORS) { const Original = win[name]; if (typeof Original !== "function") continue; const Wrapped = function (type, init) { return wrapEvent(win, new Original(type, init)); }; markNative(Wrapped, name, Original.length); try { // 构造函数返回 Proxy 时,原型链要指回原构造器的 prototype,否则 instanceof 会崩 Object.defineProperty(Wrapped, "prototype", { value: Original.prototype, writable: false, enumerable: false, configurable: false }); Object.defineProperty(win, name, { value: Wrapped, writable: true, enumerable: false, configurable: true }); // event.constructor 必须指回新的构造器,否则 === Event 的比较会露馅 Original.prototype.constructor = Wrapped; } catch (error) { // 个别构造器不可覆写,跳过 } } const documentProto = win.Document && win.Document.prototype; if (documentProto && typeof documentProto.createEvent === "function") { const originalCreate = documentProto.createEvent; documentProto.createEvent = markNative(function createEvent(type) { return wrapEvent(win, originalCreate.call(this, type)); }, "createEvent", 1); } const targetProto = win.EventTarget && win.EventTarget.prototype; if (targetProto && typeof targetProto.dispatchEvent === "function") { const originalDispatch = targetProto.dispatchEvent; targetProto.dispatchEvent = markNative(function dispatchEvent(event) { return originalDispatch.call(this, proxyToEvent.get(event) || event); }, "dispatchEvent", 1); } } const TRACE_PREFIXES = [ "cdc_", "$cdc_", "__webdriver", "__driver", "__selenium", "_selenium", "callSelenium", "domAutomation", "__nightmare", "_phantom", "callPhantom", "__playwright", "__puppeteer", "__pw_" ]; function installAutomationTraces(win) { let names = []; try { names = Object.getOwnPropertyNames(win); } catch (error) { return; } for (const name of names) { if (!TRACE_PREFIXES.some((prefix) => name.startsWith(prefix))) continue; try { delete win[name]; } catch (error) { // 不可配置的残留属性删不掉,交给后续兜底 } } } // navigator.webdriver 优先靠启动参数 --disable-blink-features=AutomationControlled 归零, // 那样属性形态完全是原生的。只有浏览器仍报 true 时才兜底改写。 function installWebdriver(win) { let nav = null; try { nav = win.navigator; } catch (error) { return; } if (!nav || nav.webdriver !== true) return; try { Object.defineProperty(win.Navigator.prototype, "webdriver", { get: markNative(function webdriver() { return false; }, "get webdriver", 0), set: undefined, enumerable: true, configurable: true }); } catch (error) { // 忽略 } } function installPermissions(win) { const proto = win.Permissions && win.Permissions.prototype; if (!proto || typeof proto.query !== "function") return; const original = proto.query; proto.query = markNative(function query(descriptor) { const notification = win.Notification; if (descriptor && descriptor.name === "notifications" && notification && notification.permission === "denied") { return Promise.resolve({ state: "prompt", onchange: null }); } return original.apply(this, arguments); }, "query", 1); } // 只在检测到软件渲染(SwiftShader / llvmpipe)时替换:真机有头模式透传真值,痕迹最小。 function installWebGL(win) { const patch = (proto) => { if (!proto || typeof proto.getParameter !== "function") return; const original = proto.getParameter; let software = null; proto.getParameter = markNative(function getParameter(param) { const value = original.apply(this, arguments); if (param !== 37445 && param !== 37446) return value; if (software === null) { const renderer = param === 37446 ? String(value || "") : String(original.call(this, 37446) || ""); software = /swiftshader|llvmpipe|software|basic render/i.test(renderer); } if (!software) return value; return param === 37445 ? FIXED_GPU_VENDOR : FIXED_GPU_RENDERER; }, "getParameter", 1); }; patch(win.WebGLRenderingContext && win.WebGLRenderingContext.prototype); patch(win.WebGL2RenderingContext && win.WebGL2RenderingContext.prototype); } // canvas 指纹:只扰动小画布(指纹探针的典型尺寸),且扰动位置/幅度完全确定, // 同一画布反复读取结果一致,不会因为"两次读取不同"被抓。 function installCanvas(win) { const proto = win.CanvasRenderingContext2D && win.CanvasRenderingContext2D.prototype; if (!proto || typeof proto.getImageData !== "function") return; const original = proto.getImageData; proto.getImageData = markNative(function getImageData() { const data = original.apply(this, arguments); try { const canvas = this.canvas; if (!canvas || !data || !data.data || data.data.length < 8) return data; if (canvas.width > 320 || canvas.height > 320) return data; const pixels = data.data.length >>> 2; const seed = ((canvas.width * 31 + canvas.height * 131 + data.data.length) >>> 0); const index = (seed % pixels) * 4; const delta = (seed & 1) === 1 ? 1 : -1; const next = data.data[index] + delta; data.data[index] = next < 0 ? 0 : next > 255 ? 255 : next; } catch (error) { // 扰动失败就返回原数据 } return data; }, "getImageData", 4); } // AudioBuffer 的极小幅噪声:足以改变音频指纹哈希,听感与业务无影响。 function installAudio(win) { const proto = win.AudioBuffer && win.AudioBuffer.prototype; if (!proto || typeof proto.getChannelData !== "function") return; const original = proto.getChannelData; proto.getChannelData = markNative(function getChannelData() { const data = original.apply(this, arguments); try { if (!data || data.length < 64 || touchedBuffers.has(this)) return data; touchedBuffers.add(this); const seed = (data.length * 2654435761) >>> 0; const index = seed % data.length; data[index] = data[index] + ((seed & 1) === 1 ? 1e-7 : -1e-7); } catch (error) { // 忽略 } return data; }, "getChannelData", 1); } // 无头环境 enumerateDevices 返回空数组,是常见判定点;真机有设备列表时不动。 function installMediaDevices(win) { const proto = win.MediaDevices && win.MediaDevices.prototype; if (!proto || typeof proto.enumerateDevices !== "function") return; const original = proto.enumerateDevices; proto.enumerateDevices = markNative(function enumerateDevices() { return original.apply(this, arguments).then((list) => { if (list && list.length > 0) return list; return [ { deviceId: "default", kind: "audioinput", label: "", groupId: "default" }, { deviceId: "default", kind: "audiooutput", label: "", groupId: "default" }, { deviceId: "default", kind: "videoinput", label: "", groupId: "default" } ]; }); }, "enumerateDevices", 0); } // NetworkInformation 在容器/无网卡环境下 rtt、downlink 会归零,补成常见值。 function installConnection(win) { let connection = null; try { connection = win.navigator.connection; } catch (error) { return; } if (!connection) return; const fix = (key, value) => { try { const current = connection[key]; if (current !== undefined && current !== 0 && current !== "") return; Object.defineProperty(connection, key, { get: markNative(function () { return value; }, "get " + key, 0), set: undefined, enumerable: true, configurable: true }); } catch (error) { // 忽略 } }; fix("rtt", 50); fix("downlink", 10); fix("effectiveType", "4g"); } // 无头 UA 里带 HeadlessChrome,同步改掉 userAgent 与 UA-CH,避免两处不一致。 function installUaHints(win) { let ua = ""; try { ua = String(win.navigator.userAgent || ""); } catch (error) { return; } if (ua.indexOf("HeadlessChrome") === -1) return; const fixed = ua.replace("HeadlessChrome", "Chrome"); try { Object.defineProperty(win.Navigator.prototype, "userAgent", { get: markNative(function userAgent() { return fixed; }, "get userAgent", 0), set: undefined, enumerable: true, configurable: true }); } catch (error) { // 忽略 } } // iframe 里的检测最容易被漏掉:contentWindow 一被读取就先给子窗口打一遍补丁。 // CDP 的 addScriptToEvaluateOnNewDocument 本身覆盖子 frame,这里是双保险。 function installIframeHook(win) { const proto = win.HTMLIFrameElement && win.HTMLIFrameElement.prototype; if (!proto) return; let descriptor = null; try { descriptor = Object.getOwnPropertyDescriptor(proto, "contentWindow"); } catch (error) { return; } if (!descriptor || typeof descriptor.get !== "function") return; const original = descriptor.get; const getter = markNative(function contentWindow() { const child = original.call(this); if (child) { try { install(child); } catch (error) { // 跨域子窗口访问会抛,交给 CDP 自己注入 } } return child; }, "get contentWindow", 0); try { Object.defineProperty(proto, "contentWindow", { get: getter, set: undefined, enumerable: descriptor.enumerable, configurable: true }); } catch (error) { // 忽略 } } function install(win) { if (!win || patchedWindows.has(win)) return; patchedWindows.add(win); // toString 必须先装,后面所有 markNative 的函数才经得起 toString 检查 installFunctionMask(win); installEventConstructorMask(win); installAutomationTraces(win); installWebdriver(win); installPermissions(win); installWebGL(win); installCanvas(win); installAudio(win); installMediaDevices(win); installConnection(win); installUaHints(win); installIframeHook(win); } install(window); })();