仅清除 Cookie、轮换 IP 地址或新建浏览器配置文件,并不足以阻止网站识别底层设备。追踪脚本与反爬虫脚本会绘制不可见的 2D 图形、测量亚像素字形几何信息,并将像素和浮点数组哈希为跨会话稳定的硬件标识符。
由于 Fuzio 内置完整的 Chromium,您可以将 Manifest V3 扩展安装到 Fuzio Profile 中,在任何追踪脚本运行之前重写页面的 JavaScript API。
在本系列文章中,我们将构建一个用于 Fuzio 的反指纹追踪 Chromium 扩展。第一部分聚焦 2D 图形与页面布局,涵盖以下五个表面:
HTMLCanvasElement与CanvasRenderingContext2D(标准 2D Canvas 绘图)OffscreenCanvas(离屏 2D Canvas 渲染)TextMetrics(亚像素文本宽度与字形边界框测量)SVG文本元素(矢量文本长度与边界框)DOMRect(HTML 元素与文本选区的浮点布局矩形)
前置条件
- JDK 17 或更高版本。
- 有效的 Fuzio 许可证密钥(可在官网申请 30 天免费试用)。
- 已配置 Fuzio 与 Swing 的 Gradle 项目(新项目可参考快速入门指南)。
2D 图形指纹的原理
当网页绘制文本、渐变或图形时,Chromium 会将命令交由 2D 图形引擎(Skia)及操作系统字体引擎(Linux 上的 FreeType、Windows 上的 DirectWrite 或 macOS 上的 CoreText)处理。
即使两台电脑运行完全相同的 Chromium 版本并绘制同一场景,不同的 GPU 抗锯齿舍入、字体微调(hinting)与字距规则,以及 Blink 子像素布局舍入,都会使 JavaScript 读取到的原始像素字节与浮点度量出现细微但跨会话稳定的差异。追踪脚本只需将这些数值哈希,即可持续识别同一台设备。
唯一性悖论与确定性微扰(Farbling)
对于 navigator.hardwareConcurrency 这类简单属性,可以统一返回常见值(如 8)来融入人群;但 2D 图形与布局 API 必须实际绘制并测量页面请求的内容,返回固定值会破坏页面显示。
因此反指纹浏览器普遍采用 Farbling(确定性微扰):在颜色通道最低位或亚像素文本宽度上叠加人眼不可感知的微小偏移,从而改变最终哈希。遵循以下三条规则,即可在阻止跨站点追踪的同时避免被反爬脚本识破:
- 将浏览器会话与当前网站域名共同纳入种子计算,使不同配置文件或不同站点产生不同哈希。
- 在单次页面访问内保持种子确定性,使同一站点上对同一画布的多次读取返回完全相同的哈希。
- 不留下反爬脚本检测的副作用(如暴露
Function.prototype.toString()包装代码、破坏整数容器宽度或遗漏沙箱 iframe)。
第一步:创建 Manifest V3 扩展
默认情况下,扩展内容脚本运行在 ISOLATED 世界中,无法覆盖页面 MAIN 世界上的原型方法。我们在 manifest.json 中配置 "world": "MAIN""run_at": "document_start"data:/blob: URL:
{
"manifest_version": 3,
"name": "Fuzio Fingerprint Shield",
"version": "1.0.0",
"description": "Reduces 2D graphics and layout fingerprinting.",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start",
"world": "MAIN",
"all_frames": true,
"match_about_blank": true,
"match_origin_as_fallback": true
}
]
}
第二步:生成按配置文件与按域名隔离的种子
在 content.js 中,将所有状态与辅助函数封装在立即执行函数表达式(IIFE)中以避免污染全局作用域,并将每次会话生成的 32 位随机种子(保存在 sessionStorage 中)与 window.location.hostname 的 FNV-1a 哈希混合,生成 DOMAIN_SESSION_SEED。
借助 MurmurHash3 32 位混合函数(fmix32),我们计算出 1 位像素翻转值 computeNoiseBit 以及按 Blink 布局单位(1 / 641 / 1024computeFloatNoise——既远小于 Blink 的布局对齐网格以免影响换行,又足以在 SVGRect 的 32 位浮点精度下保留扰动:
(() => {
const BLINK_LAYOUT_UNIT_PX = 1 / 64;
const SUBPIXEL_NOISE_STEP = BLINK_LAYOUT_UNIT_PX / 1024;
const MURMUR3_FMIX_C1 = 0x85ebca6b;
const MURMUR3_FMIX_C2 = 0xc2b2ae35;
function mixHash(value, seed) {
let state = Math.imul(value ^ seed, MURMUR3_FMIX_C1);
state = Math.imul(state ^ (state >>> 13), MURMUR3_FMIX_C2);
return (state ^ (state >>> 16)) >>> 0;
}
function deriveDomainSessionSeed(win) {
const sessionSeed = getOrCreateSessionSeed(win);
const hostHash = fnv1aHash(win.location.hostname || "local");
return mixHash(hostHash, sessionSeed);
}
function computeNoiseBit(index, seed) {
return mixHash(index, seed) & 1;
}
function computeFloatNoise(value, seed) {
const key = Math.round(value * 1000);
const byteVal = (mixHash(key, seed) >>> 8) & 0xff;
const offset = byteVal - 128;
const nonZeroOffset = offset === 0 ? 1 : offset;
return nonZeroOffset * SUBPIXEL_NOISE_STEP;
}
// ...
})();
第三步:微扰 2D Canvas 与 OffscreenCanvas 像素
在 patchCanvas2D 中拦截 getImageData()、toDataURL() 和 toBlob()。对 toDataURL() 和 toBlob(),先将原始画布复制到临时离屏 <canvas> 再应用 applyPixelNoise,确保噪声不会在页面可见画布上不断累积:
function applyPixelNoise(imageData, seed) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 16) {
data[i] = data[i] ^ computeNoiseBit(i, seed);
}
return imageData;
}
function cloneWithNoise(canvas, seed) {
const clone = canvas.ownerDocument.createElement("canvas");
clone.width = canvas.width;
clone.height = canvas.height;
const ctx = clone.getContext("2d");
ctx.drawImage(canvas, 0, 0);
const pixels = ctx.getImageData(0, 0, clone.width, clone.height);
ctx.putImageData(applyPixelNoise(pixels, seed), 0, 0);
return clone;
}
function patchCanvas2D(win, seed) {
const ctxProto = win.CanvasRenderingContext2D.prototype;
const canvasProto = win.HTMLCanvasElement.prototype;
const origGetImageData = ctxProto.getImageData;
const origToDataURL = canvasProto.toDataURL;
const origToBlob = canvasProto.toBlob;
ctxProto.getImageData = function (sx, sy, sw, sh, settings) {
const pixels = origGetImageData.call(this, sx, sy, sw, sh, settings);
return applyPixelNoise(pixels, seed);
};
canvasProto.toDataURL = function (...args) {
return origToDataURL.apply(cloneWithNoise(this, seed), args);
};
canvasProto.toBlob = function (callback, type, quality) {
return origToBlob.call(
cloneWithNoise(this, seed), callback, type, quality
);
};
}
同样在 patchOffscreenCanvas 中对 OffscreenCanvasRenderingContext2D.prototype.getImageData 与 OffscreenCanvas.prototype.convertToBlob 应用相同的逻辑,保持两者输出一致。
第四步:微扰亚像素文本、SVG 与 DOM 布局度量
扰动 TextMetrics 的七个属性
在 patchCanvasTextMetrics 中包装 TextMetrics.prototype 的全部七个 getter,保留 0 不变,对非零值叠加 computeFloatNoise:
function patchCanvasTextMetrics(win, seed) {
const metricsProto = win.TextMetrics.prototype;
const properties = [
"width",
"actualBoundingBoxLeft",
"actualBoundingBoxRight",
"actualBoundingBoxAscent",
"actualBoundingBoxDescent",
"fontBoundingBoxAscent",
"fontBoundingBoxDescent"
];
for (const prop of properties) {
const origGetter = Object.getOwnPropertyDescriptor(
metricsProto, prop
).get;
Object.defineProperty(metricsProto, prop, {
get() {
const val = origGetter.call(this);
return val === 0 ? 0 : val + computeFloatNoise(val, seed);
}
});
}
}
扰动 SVG 文本与边界框
function patchSvgTextMetrics(win, seed) {
const svgTextProto = win.SVGTextContentElement.prototype;
const svgGraphicsProto = win.SVGGraphicsElement.prototype;
const origGetLength = svgTextProto.getComputedTextLength;
const origGetBBox = svgGraphicsProto.getBBox;
svgTextProto.getComputedTextLength = function () {
const len = origGetLength.call(this);
return len === 0 ? 0 : len + computeFloatNoise(len, seed);
};
svgGraphicsProto.getBBox = function (...args) {
const box = origGetBBox.apply(this, args);
if (box.width > 0) box.width += computeFloatNoise(box.width, seed);
if (box.height > 0) box.height += computeFloatNoise(box.height, seed);
return box;
};
}
仅扰动非整数 DOMRect 以保护整数布局
标准页面容器的宽高通常为整数(如 1280),而字体度量元素往往带有小数像素(如 129.53125)。通过检查 !Number.isInteger(rect.width) 与 !Number.isInteger(rect.height),可以只扰动亚像素字体测量而不破坏页面整数布局:
function perturbRectIfFractional(rect, seed) {
if (rect.width > 0 && !Number.isInteger(rect.width)) {
rect.width += computeFloatNoise(rect.width, seed);
}
if (rect.height > 0 && !Number.isInteger(rect.height)) {
rect.height += computeFloatNoise(rect.height, seed);
}
return rect;
}
function patchDomRectMetrics(win, seed) {
for (const proto of [win.Element.prototype, win.Range.prototype]) {
const origGetBounding = proto.getBoundingClientRect;
const origGetRects = proto.getClientRects;
proto.getBoundingClientRect = function () {
return perturbRectIfFractional(origGetBounding.call(this), seed);
};
proto.getClientRects = function () {
const list = origGetRects.call(this);
for (const rect of list) perturbRectIfFractional(rect, seed);
return list;
};
}
}
第五步:防止 toString 检测与沙箱 iframe 绕过
伪装 Function.prototype.toString
保存原始函数的 "[native code]" 字符串并在重写 Function.prototype.toString 时返回该字符串:
function maskFunction(win, patchedFn, originalFn) {
const origToString = win.Function.prototype.toString;
maskedFunctions.set(patchedFn, origToString.call(originalFn));
}
function patchFunctionToString(win) {
const proto = win.Function.prototype;
const origToString = proto.toString;
const patchedToString = function toString() {
return maskedFunctions.has(this)
? maskedFunctions.get(this)
: origToString.call(this);
};
maskedFunctions.set(patchedToString, origToString.call(origToString));
Object.defineProperty(proto, "toString", {
value: patchedToString,
configurable: true,
writable: true
});
}
拦截沙箱同源 iframe
<iframe sandbox="allow-same-origin">(不含 allow-scripts)会阻止内容脚本在子 frame 内运行,同时允许父页面访问未打补丁的子 Window。通过重写 HTMLIFrameElement.prototype 的 contentWindow 与 contentDocument getter,在返回子窗口前先应用所有补丁:
function patchSandboxedIframes(win, applyRealmPatches) {
const iframeProto = win.HTMLIFrameElement.prototype;
const origWinGetter = Object.getOwnPropertyDescriptor(
iframeProto, "contentWindow"
).get;
const origDocGetter = Object.getOwnPropertyDescriptor(
iframeProto, "contentDocument"
).get;
Object.defineProperty(iframeProto, "contentWindow", {
get() {
const childWin = origWinGetter.call(this);
if (childWin) applyRealmPatches(childWin);
return childWin;
}
});
Object.defineProperty(iframeProto, "contentDocument", {
get() {
const childWin = origWinGetter.call(this);
if (childWin) applyRealmPatches(childWin);
return origDocGetter.call(this);
}
});
}
function applyAllTechniques(targetWindow) {
if (!targetWindow || patchedRealms.has(targetWindow)) return;
patchedRealms.add(targetWindow);
patchCanvas2D(targetWindow, DOMAIN_SESSION_SEED);
patchOffscreenCanvas(targetWindow, DOMAIN_SESSION_SEED);
patchCanvasTextMetrics(targetWindow, DOMAIN_SESSION_SEED);
patchSvgTextMetrics(targetWindow, DOMAIN_SESSION_SEED);
patchDomRectMetrics(targetWindow, DOMAIN_SESSION_SEED);
patchSandboxedIframes(targetWindow, applyAllTechniques);
patchFunctionToString(targetWindow);
}
applyAllTechniques(window);
第六步:打包为 CRX3 并在 Fuzio 中启动对比
Fuzio 通过 Profile.extensions().install(Path) 安装 .crx 扩展文件。CrxPacker.packExtensionToTempCrx()(位于配套示例仓库)可在启动时打包扩展目录、用临时 RSA-2048 密钥签名并添加 12 字节 "Cr24" CRX3 文件头。
下面在同一个 Engine 中创建默认基线配置文件与受保护配置文件,并在 JSplitPane 中并排打开 https://browserleaks.com/canvas:
public static void main(String[] args) {
var engine = Engine.newInstance(HARDWARE_ACCELERATED);
var profiles = engine.profiles();
var baselineProfile = profiles.defaultProfile();
var protectedProfile = profiles.newProfile("FingerprintShieldProfile");
var crxPath = CrxPacker.packExtensionToTempCrx();
protectedProfile.extensions().install(crxPath);
var baselineBrowser = baselineProfile.newBrowser();
var protectedBrowser = protectedProfile.newBrowser();
invokeLater(() -> {
var splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
BrowserView.newInstance(baselineBrowser),
BrowserView.newInstance(protectedBrowser)
);
splitPane.setResizeWeight(0.5);
var frame = new JFrame("Fuzio Fingerprint Shield Comparison");
frame.add(splitPane, BorderLayout.CENTER);
frame.setSize(1400, 820);
frame.setVisible(true);
var testUrl = "https://browserleaks.com/canvas";
baselineBrowser.navigation().loadUrl(testUrl);
protectedBrowser.navigation().loadUrl(testUrl);
});
}
结果与演示
使用 Gradle 运行应用:
./gradlew run -Dfuzio.license.key=<YOUR_LICENSE_KEY>
在 BrowserLeaks Canvas 上对比默认配置文件与两个独立受保护配置文件,可以看到每个配置文件都会产生不同的 2D 图形与布局哈希,同一页面内的第二次读取完全一致,且整数布局尺寸保持不变:
| 检测项 | 无扩展基线 | Profile 1(第 1 次) | Profile 1(第 2 次) | Profile 2(第 1 次) |
|---|---|---|---|---|
| Canvas 哈希 | 2F315868... | 331F79AA... | 331F79AA... | A33EE872... |
OffscreenCanvas | 4207f105 | cf8ae174 | cf8ae174 | 40882f35 |
TextMetrics | 196.86132 | 196.86133 | 196.86133 | 196.86131 |
SVG.getBBox() | 119.57812 | 119.57858 | 119.57858 | 119.57744 |
DOMRect(浮点) | 129.53125 | 129.53126 | 129.53126 | 129.53124 |
DOMRect(整数) | 1280 | 1280 | 1280 | 1280 |
完整可运行代码见 Gitee 示例仓库。
总结
在本篇中,我们在 Fuzio 中构建了一个 Manifest V3 扩展,对 2D Canvas 位图、TextMetrics、SVG 文本及浮点 DOMRect 注入按域名隔离的确定性微扰,并通过伪装 Function.prototype.toString() 与修补沙箱 iframe 避免检测绕过。
在下一篇中,我们将继续防护 WebGL、WebGL2、WebGPU、window.screen 与高熵 CSS 媒体查询等 3D GPU 与显示表面。
在您的 Java 桌面应用中集成基于 Chromium 的 Fuzio 浏览器引擎。
免费试用

