<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>dwt&#039;s life - 设备指纹</title>
<link>https://dwt.life/tag/%E8%AE%BE%E5%A4%87%E6%8C%87%E7%BA%B9/</link>
<atom:link href="https://dwt.life/feed/tag/%E8%AE%BE%E5%A4%87%E6%8C%87%E7%BA%B9/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description></description>
<lastBuildDate>Wed, 26 Jan 2022 23:37:00 +0800</lastBuildDate>
<pubDate>Wed, 26 Jan 2022 23:37:00 +0800</pubDate>
<item>
<title>Js设备指纹检测对抗及识别</title>
<link>https://dwt.life/archives/177/</link>
<guid>https://dwt.life/archives/177/</guid>
<pubDate>Wed, 26 Jan 2022 23:37:00 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[前言需要明确的是，“隐藏浏览器指纹”和“隐藏Webdriver/Selenium/Puppeteer”、“匿名浏览”都不是同一个问题。“隐藏Webdriver/Selenium/Puppetee...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<h1>前言</h1><p>需要明确的是，“隐藏浏览器指纹”和“隐藏Webdriver/Selenium/Puppeteer”、“匿名浏览”都不是同一个问题。“隐藏Webdriver/Selenium/Puppeteer”的目的是告诉服务端自己不是自动化爬虫（这个似乎可以尝试用 stealth.min.js 来做）；“匿名浏览”的目的是不让浏览记录在本地留下痕迹（这个可以用浏览器的匿名模式来实现）；“隐藏浏览器指纹”是为了不让服务端追踪“现在的我和之前的我是同一个我”（这个是我们这次想解决的问题）。</p><p>浏览器指纹一般是通过 Javascript 能提取或计算得到的一些具有一定稳定性和独特性的参数。风控的同学将这些参数做一定取舍和组合，就能得到一台浏览器的大致标识。在没有强制登陆的情况下，有了这个标识，风控的同学可以做反爬、广告的同学可以做推荐、数据的同学可以算uv等等，用处还是不小的。</p><p>常见的浏览器指纹会提取如下东西：</p><ol><li>UserAgent和平台信息</li><li>浏览器加载的字体信息</li><li>声卡指纹</li><li>Canvas指纹</li><li>显示器分辨率</li><li>语言、地区时区信息</li><li>CPU核数和可用内存信息</li><li>本地存储、Cookie等信息</li><li>浏览器安装的插件信息</li><li>科学计算的指纹信息</li><li>IP和代理信息</li></ol><p>可以看出来，这里有的指标稳定性比较好，比如UserAgent、分辨率、地区、语言、字体信息等，但是这些指标通常区分度不是很好。因此我们通常更喜欢用一些硬件指纹信息来进行区分。当然，为了衡量指标的这种“独特性”，<a href="https://pde.is/">Peter Eckersley</a> 提出了用一个指标对设备唯一指纹引入的熵来衡量指标的这种特性，下面是我的设备在 <a href="https://coveryourtracks.eff.org/">coveryyourtracks</a>中跑出来的一些常见指标的效果：</p><pre><code>USER AGENT
Bits of identifying information: 9.04
One in x browsers have this value: 524.71
 
HTTP_ACCEPT HEADERS
Bits of identifying information: 8.96
One in x browsers have this value: 496.75
 
BROWSER PLUGIN DETAILS
Bits of identifying information: 3.25
One in x browsers have this value: 9.54
 
TIME ZONE OFFSET
Bits of identifying information: 5.26
One in x browsers have this value: 38.38
 
TIME ZONE
Bits of identifying information: 6.83
One in x browsers have this value: 114.13
 
SCREEN SIZE AND COLOR DEPTH
Bits of identifying information: 6.28
One in x browsers have this value: 77.57
 
SYSTEM FONTS
Bits of identifying information: 3.92
One in x browsers have this value: 15.12
 
ARE COOKIES ENABLED?
Bits of identifying information: 0.13
One in x browsers have this value: 1.09
 
LIMITED SUPERCOOKIE TEST
Bits of identifying information: 1.41
One in x browsers have this value: 2.67
 
HASH OF CANVAS FINGERPRINT
Bits of identifying information: 9.73
One in x browsers have this value: 847.61
 
HASH OF WEBGL FINGERPRINT
Bits of identifying information: 9.75
One in x browsers have this value: 862.69
 
WEBGL VENDOR &amp; RENDERER
Bits of identifying information: 8.5
One in x browsers have this value: 362.9
 
DNT HEADER ENABLED?
Bits of identifying information: 0.95
One in x browsers have this value: 1.93
 
LANGUAGE
Bits of identifying information: 7.47
One in x browsers have this value: 176.69
 
PLATFORM
Bits of identifying information: 3.11
One in x browsers have this value: 8.62
 
TOUCH SUPPORT
Bits of identifying information: 0.76
One in x browsers have this value: 1.7
 
AD BLOCKER USED
Bits of identifying information: 0.15
One in x browsers have this value: 1.11
 
AUDIOCONTEXT FINGERPRINT
Bits of identifying information: 5.92
One in x browsers have this value: 60.59
 
CPU CLASS
Bits of identifying information: 0.12
One in x browsers have this value: 1.09
 
HARDWARE CONCURRENCY
Bits of identifying information: 1.93
One in x browsers have this value: 3.82
 
DEVICE MEMORY (GB)
Bits of identifying information: 2.31
One in x browsers have this value: 4.96</code></pre><p>其中，“Bits of identifying information” 表示这个指标对我的浏览器引入的唯一性的位数，这个位数越大，越表明这个指标更能区分我的浏览器和其他的浏览器；“One in x browsers have this value” 表示平均多少个浏览器的这个指标和我的浏览器的这个指标一样，这个数越大，越表明这个指标的区分度好。</p><p>显然，从这里看，区分度最高的指标就是 "UserAgent" , "WebGL指纹" 和 "Canvas 指纹" 。那这些指纹是怎么 work 的呢？例如Canvas指纹，其实就是由于Web 浏览器使用的图像处理引擎、图像导出选项、压缩级别、操作系统的字体，抗锯齿和亚像素渲染算法等的不同，导致画出来的图片在像素级别存在的微小但稳定的差距。这样我们就可以拿这个生成图片的校验和或算一个Hash作为他的指纹。</p><h1>指纹检测</h1><p>目前业内较为知名的浏览器指纹检测网站大概有下面这三个：</p><ol><li><a href="https://fingerprintjs.github.io/fingerprintjs/">https://fingerprintjs.github.io/fingerprintjs/</a></li><li><a href="http://f.vision/">http://f.vision/</a></li><li><a href="https://coveryourtracks.eff.org/">https://coveryourtracks.eff.org/</a></li></ol><p>第一个是最常见的浏览器指纹生成项目，之前某手用过一段时间，后来好像不用了，可以作为入门项目来看。</p><p>第二个是一个进阶的在线检测指纹的网站，检测点更全，也有一定的反伪造能力。</p><p>第三个是 <a href="https://pde.is/">Peter Eckersley</a> 实验用的检测指纹的网站，教育意义更明显，也有一定的反伪造能力。</p><p>有矛就有盾，作为（爬虫）普通用户，我们显然不想让服务端跟踪我们的操作路径，但又不想用 Tor 浏览器。因此我们就要想一些对付这些指纹检测的办法。下面就是我做的一些尝试。</p><h1>Fingerpintjs指纹</h1><h2>指纹生成算法</h2><p>首先我们先看下 fingerprintjs 检测 Canvas 指纹的核心代码，作为我们首先需要绕过的目标：</p><pre><code class="lang-javascript">function makeTextImage(canvas, context) {
    // Resizing the canvas cleans it
    canvas.width = 240;
    canvas.height = 60;
    context.textBaseline = &#039;alphabetic&#039;;
    context.fillStyle = &#039;#f60&#039;;
    context.fillRect(100, 1, 62, 20);
    context.fillStyle = &#039;#069&#039;;
    // It&#039;s important to use explicit built-in fonts in order to exclude the affect of font preferences
    // (there is a separate entropy source for them).
    context.font = &#039;11pt &quot;Times New Roman&quot;&#039;;
    // The choice of emojis has a gigantic impact on rendering performance (especially in FF).
    // Some newer emojis cause it to slow down 50-200 times.
    // There must be no text to the right of the emoji, see https://github.com/fingerprintjs/fingerprintjs/issues/574
    // A bare emoji shouldn&#039;t be used because the canvas will change depending on the script encoding:
    // https://github.com/fingerprintjs/fingerprintjs/issues/66
    // Escape sequence shouldn&#039;t be used too because Terser will turn it into a bare unicode.
    var printedText = &quot;Cwm fjordbank gly &quot; + String.fromCharCode(55357, 56835) /* */;
    context.fillText(printedText, 2, 15);
    context.fillStyle = &#039;rgba(102, 204, 0, 0.2)&#039;;
    context.font = &#039;18pt Arial&#039;;
    context.fillText(printedText, 4, 45);
    return save(canvas);
}
function makeGeometryImage(canvas, context) {
    // Resizing the canvas cleans it
    canvas.width = 122;
    canvas.height = 110;
    // Canvas blending
    // https://web.archive.org/web/20170826194121/http://blogs.adobe.com/webplatform/2013/01/28/blending-features-in-canvas/
    // http://jsfiddle.net/NDYV8/16/
    context.globalCompositeOperation = &#039;multiply&#039;;
    for (var _i = 0, _a = [
        [&#039;#f2f&#039;, 40, 40],
        [&#039;#2ff&#039;, 80, 40],
        [&#039;#ff2&#039;, 60, 80],
    ]; _i &lt; _a.length; _i++) {
        var _b = _a[_i], color = _b[0], x = _b[1], y = _b[2];
        context.fillStyle = color;
        context.beginPath();
        context.arc(x, y, 40, 0, Math.PI * 2, true);
        context.closePath();
        context.fill();
    }
    // Canvas winding
    // http://blogs.adobe.com/webplatform/2013/01/30/winding-rules-in-canvas/
    // http://jsfiddle.net/NDYV8/19/
    context.fillStyle = &#039;#f9c&#039;;
    context.arc(60, 60, 60, 0, Math.PI * 2, true);
    context.arc(60, 60, 20, 0, Math.PI * 2, true);
    context.fill(&#039;evenodd&#039;);
    return save(canvas);
}</code></pre><p>这里主要生成了下面两个东西：</p><p>指定形状、字体、文字、颜色等，画了一个图。<br><img src="https://pic.8oh.com.cn/cos/2022/01/26/7b990a6bdf944_1643211116.png" alt="162.png" title="162.png"></p><p>指定了颜色、位置，画了一些奇奇怪怪的圆弧。<br><img src="https://pic.8oh.com.cn/cos/2022/01/26/9f6b2bc6ff704_1643211122.png" alt="163.png" title="163.png"></p><p>然后 fingerprintjs 会用这两个图，根据 murmurHash3 算一个指纹。</p><h2>Canvas Fingerprint Defender</h2><p>Chrome有一个声称能防止 canvas 指纹泄露的插件 <a href="https://chrome.google.com/webstore/detail/canvas-fingerprint-defend/lanfdkkpgfjfdikkncbnojekcppdebfp">Canvas Fingerprint Defender</a> ，我们姑且拿他来检测绕过 fingerprintjs 看看。</p><p>安装后，找到 crx 安装目录，发现他的逻辑主要是在 <code>data/content_script/inject.js</code> 中，核心逻辑如下：</p><pre><code class="lang-javascript">var inject = function () {
  const toBlob = HTMLCanvasElement.prototype.toBlob;
  const toDataURL = HTMLCanvasElement.prototype.toDataURL;
  const getImageData = CanvasRenderingContext2D.prototype.getImageData;
  //
  var noisify = function (canvas, context) {
    if (context) {
      const shift = {
        &#039;r&#039;: Math.floor(Math.random() * 10) - 5,
        &#039;g&#039;: Math.floor(Math.random() * 10) - 5,
        &#039;b&#039;: Math.floor(Math.random() * 10) - 5,
        &#039;a&#039;: Math.floor(Math.random() * 10) - 5
      };
      //
      const width = canvas.width;
      const height = canvas.height;
      if (width &amp;&amp; height) {
        const imageData = getImageData.apply(context, [0, 0, width, height]);
        for (let i = 0; i &lt; height; i++) {
          for (let j = 0; j &lt; width; j++) {
            const n = ((i * (width * 4)) + (j * 4));
            imageData.data[n + 0] = imageData.data[n + 0] + shift.r;
            imageData.data[n + 1] = imageData.data[n + 1] + shift.g;
            imageData.data[n + 2] = imageData.data[n + 2] + shift.b;
            imageData.data[n + 3] = imageData.data[n + 3] + shift.a;
          }
        }
        //
        window.top.postMessage(&quot;canvas-fingerprint-defender-alert&quot;, &#039;*&#039;);
        context.putImageData(imageData, 0, 0);
      }
    }
  };
  //
  Object.defineProperty(HTMLCanvasElement.prototype, &quot;toBlob&quot;, {
    &quot;value&quot;: function () {
      noisify(this, this.getContext(&quot;2d&quot;));
      return toBlob.apply(this, arguments);
    }
  });
  //
  Object.defineProperty(HTMLCanvasElement.prototype, &quot;toDataURL&quot;, {
    &quot;value&quot;: function () {
      noisify(this, this.getContext(&quot;2d&quot;));
      return toDataURL.apply(this, arguments);
    }
  });
  //
  Object.defineProperty(CanvasRenderingContext2D.prototype, &quot;getImageData&quot;, {
    &quot;value&quot;: function () {
      noisify(this.canvas, this);
      return getImageData.apply(this, arguments);
    }
  });
  //
  document.documentElement.dataset.cbscriptallow = true;
};
inject();</code></pre><p>代码基本不用解释，主要就做了一件事情：重写 Canvas 的 toBlob，toDataURL 方法和 Context 的 getImageData 方法，使他们在生成图片时加一些噪点。</p><p>通过下面的 selenium 代码注入上述脚本：</p><pre><code>from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
options = Options()
 
driver = webdriver.Chrome(options=options, executable_path=&quot;/path/to/chromedriver&quot;)
 
f = open(&#039;./inject.js&#039;)
js = f.read()
driver.execute_cdp_cmd(&quot;Page.addScriptToEvaluateOnNewDocument&quot;, {&quot;source&quot;: js})
 
driver.get(&#039;https://fingerprintjs.github.io/fingerprintjs/&#039;)</code></pre><p>连续跑两次，发现指纹的确并不相同：<br><img src="https://pic.8oh.com.cn/cos/2022/01/26/5b3c127c3a044_1643211182.png" alt="164.png" title="164.png"></p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/d567988bc7d6c_1643211198.png" alt="165.png" title="165.png"></p><h1>f.vision指纹</h1><p>但是上面的脚本如果应用在 f.vision 上（或者 <a href="https://webbrowsertools.com/canvas-fingerprint/">canvas-fingerprint</a> ），虽然也会生成不同的指纹，但是会发现有下面的结果：</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/10918bd88a923_1643211231.png" alt="166.png" title="166.png"></p><p>有意思，果然是做了一些检测，让我们摘下面具看看他做了啥。一番搜索后发现了如下检测点。</p><h2>检测点一</h2><pre><code>CanvasRenderingContext2D.prototype.getImageData.length !== 4 
|| !CanvasRenderingContext2D.prototype.getImageData.toString().match(/^\s*function getImageData\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/) 
|| (CanvasRenderingContext2D.prototype.getImageData.name !== &quot;getImageData&quot; &amp;&amp; !ie)</code></pre><p>原来他check了一下关键对象的 prototype 的属性，我们来看下当前实际的结果：</p><pre><code>CanvasRenderingContext2D.prototype.getImageData.length
0
 
CanvasRenderingContext2D.prototype.getImageData.toString()
&quot;function () {\n      noisify(this.canvas, this);\n      return getImageData.apply(this, arguments);\n    }&quot;
 
CanvasRenderingContext2D.prototype.getImageData.name
&quot;value&quot;</code></pre><p>这里果然和没有改过的不一样。</p><h2>检测点二</h2><pre><code>function cKnownPixels(size) {
    &quot;use strict&quot;;
 
    var canvas = document.createElement(&quot;canvas&quot;);
    canvas.height = size;
    canvas.width = size;
    var context = canvas.getContext(&quot;2d&quot;);
    if (!context)
        return false;
 
    context.fillStyle = &quot;rgba(0, 127, 255, 1)&quot;;
    var pixelValues = [0, 127, 255, 255];
    context.fillRect(0, 0, canvas.width, canvas.height);
    var p = context.getImageData(0, 0, canvas.width, canvas.height).data;
    for (var i = 0; i &lt; p.length; i += 1) {
        if (p[i] !== pixelValues[i % 4]) {
            return false;
        }
    }
    return true;
}</code></pre><p>这个是个很容易想到的检测方法，输入一个已知的稳定像素图（由于不存在字体、复杂计算等，因此不同机器的渲染几乎不会有差异），如果输出的结果和已知输入不一样，那肯定是人为加了噪点。</p><h2>检测点三</h2><pre><code class="lang-javascript">function cReadOut() {
 
    &quot;use strict&quot;;
 
    var canvas = document.createElement(&quot;canvas&quot;);
    var context = canvas.getContext(&quot;2d&quot;);
 
    if (!context)
        return false;
 
    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    for (let i = 0; i &lt; imageData.data.length; i += 1) {
        if (i % 4 !== 3) {
            imageData.data[i] = Math.floor(256 * Math.random());
        } else {
            imageData.data[i] = 255;
        }
    }
    context.putImageData(imageData, 0, 0);
 
    var imageData1 = context.getImageData(0, 0, canvas.width, canvas.height);
    var imageData2 = context.getImageData(0, 0, canvas.width, canvas.height);
    for (let i = 0; i &lt; imageData2.data.length; i += 1) {
        if (imageData1.data[i] !== imageData2.data[i]) {
            return false;
        }
    }
    return true;
}</code></pre><p>这也是个比较容易想到的检测方法，将同一份像素点连续输出两次，如果这两次的结果不一样，那肯定是加了某些随机的噪点。</p><h2>检测点四</h2><pre><code class="lang-javascript">function cDoubleReadOut() {
 
    &quot;use strict&quot;;
 
    var canvas = document.createElement(&quot;canvas&quot;);
    var context = canvas.getContext(&quot;2d&quot;);
 
    if (!context)
        return false;
 
    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    for (let i = 0; i &lt; imageData.data.length; i += 1) {
        if (i % 4 !== 3) {
            imageData.data[i] = Math.floor(256 * Math.random());
        } else {
            imageData.data[i] = 255;
        }
    }
 
    var imageData1 = context.getImageData(0, 0, canvas.width, canvas.height);
 
    var canvas2 = document.createElement(&quot;canvas&quot;);
    var context2 = canvas2.getContext(&quot;2d&quot;);
    context2.putImageData(imageData1, 0, 0);
    var imageData2 = context2.getImageData(0, 0, canvas.width, canvas.height);
 
    for (let i = 0; i &lt; imageData2.data.length; i += 1) {
        if (imageData1.data[i] !== imageData2.data[i]) {
            return false;
        }
    }
    return true;
}</code></pre><p>和上面的检测挺像，但是检测出来的结果比较trick，没太搞懂这是检测的啥，不过实践中发现如果 a 通道随机到的噪点偏移值不太好，很容易检测不通过。</p><h2>各个击破</h2><p>道高一尺，魔高一丈，知道他是怎么检测的，我们就可以有针对性的处理了。解决思路如下：</p><ol><li>再次 mock 出对应prototype 的正确 length、toString、name 属性。</li><li>对于没有添加文字、没有复杂位图变换的图，不进行随机填充。</li><li>随机参数在启动时就生成且只生成一次，保证相同的图多次处理结果也一样。</li></ol><p>具体操作可以参考下面的代码：</p><pre><code class="lang-js">function random(list) {
    let min = 0;
    let max = list.length
    return list[Math.floor(Math.random() * (max - min)) + min];
}
 
let rsalt = random([...Array(7).keys()].map(a =&gt; a - 3))
let gsalt = random([...Array(7).keys()].map(a =&gt; a - 3))
let bsalt = random([...Array(7).keys()].map(a =&gt; a - 3))
let asalt = random([...Array(7).keys()].map(a =&gt; a - 3))
 
const rawGetImageData = CanvasRenderingContext2D.prototype.getImageData;
 
let noisify = function (canvas, context) {
    let ctxIdx = ctxArr.indexOf(context);
    let info = ctxInf[ctxIdx];
    const width = canvas.width, height = canvas.height;
    const imageData = rawGetImageData.apply(context, [0, 0, width, height]);
    if (info.useArc || info.useFillText) {
        for (let i = 0; i &lt; height; i++) {
            for (let j = 0; j &lt; width; j++) {
                const n = ((i * (width * 4)) + (j * 4));
                imageData.data[n + 0] = imageData.data[n + 0] + rsalt;
                imageData.data[n + 1] = imageData.data[n + 1] + gsalt;
                imageData.data[n + 2] = imageData.data[n + 2] + bsalt;
                imageData.data[n + 3] = imageData.data[n + 3] + asalt;
            }
        }
    }
    context.putImageData(imageData, 0, 0);
};
 
let ctxArr = [];
let ctxInf = [];
 
(function mockGetContext() {
    let rawGetContext = HTMLCanvasElement.prototype.getContext
 
    Object.defineProperty(HTMLCanvasElement.prototype, &quot;getContext&quot;, {
        &quot;value&quot;: function () {
            let result = rawGetContext.apply(this, arguments);
            if (arguments[0] === &#039;2d&#039;) {
                ctxArr.push(result)
                ctxInf.push({})
            }
            return result;
        }
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.constructor, &quot;length&quot;, {
        &quot;value&quot;: 1
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.constructor, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function getContext() { [native code] }&quot;
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.constructor, &quot;name&quot;, {
        &quot;value&quot;: &quot;getContext&quot;
    });
})();
 
(function mockArc() {
    let rawArc = CanvasRenderingContext2D.prototype.arc
    Object.defineProperty(CanvasRenderingContext2D.prototype, &quot;arc&quot;, {
        &quot;value&quot;: function () {
            let ctxIdx = ctxArr.indexOf(this);
            ctxInf[ctxIdx].useArc = true;
            return rawArc.apply(this, arguments);
        }
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.arc, &quot;length&quot;, {
        &quot;value&quot;: 5
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.arc, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function arc() { [native code] }&quot;
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.arc, &quot;name&quot;, {
        &quot;value&quot;: &quot;arc&quot;
    });
})();
 
(function mockFillText() {
    const rawFillText = CanvasRenderingContext2D.prototype.fillText;
    Object.defineProperty(CanvasRenderingContext2D.prototype, &quot;fillText&quot;, {
        &quot;value&quot;: function () {
            let ctxIdx = ctxArr.indexOf(this);
            ctxInf[ctxIdx].useFillText = true;
            return rawFillText.apply(this, arguments);
        }
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.fillText, &quot;length&quot;, {
        &quot;value&quot;: 4
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.fillText, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function fillText() { [native code] }&quot;
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.fillText, &quot;name&quot;, {
        &quot;value&quot;: &quot;fillText&quot;
    });
})();
 
 
(function mockToBlob() {
    const toBlob = HTMLCanvasElement.prototype.toBlob;
 
    Object.defineProperty(HTMLCanvasElement.prototype, &quot;toBlob&quot;, {
        &quot;value&quot;: function () {
            noisify(this, this.getContext(&quot;2d&quot;));
            return toBlob.apply(this, arguments);
        }
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toBlob, &quot;length&quot;, {
        &quot;value&quot;: 1
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toBlob, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function toBlob() { [native code] }&quot;
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toBlob, &quot;name&quot;, {
        &quot;value&quot;: &quot;toBlob&quot;
    });
})();
 
(function mockToDataURL() {
    const toDataURL = HTMLCanvasElement.prototype.toDataURL;
    Object.defineProperty(HTMLCanvasElement.prototype, &quot;toDataURL&quot;, {
        &quot;value&quot;: function () {
            noisify(this, this.getContext(&quot;2d&quot;));
            return toDataURL.apply(this, arguments);
        }
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toDataURL, &quot;length&quot;, {
        &quot;value&quot;: 0
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toDataURL, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function toDataURL() { [native code] }&quot;
    });
 
    Object.defineProperty(HTMLCanvasElement.prototype.toDataURL, &quot;name&quot;, {
        &quot;value&quot;: &quot;toDataURL&quot;
    });
})();
 
 
(function mockGetImageData() {
    Object.defineProperty(CanvasRenderingContext2D.prototype, &quot;getImageData&quot;, {
        &quot;value&quot;: function () {
            noisify(this.canvas, this);
            return rawGetImageData.apply(this, arguments);
        }
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.getImageData, &quot;length&quot;, {
        &quot;value&quot;: 4
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.getImageData, &quot;toString&quot;, {
        &quot;value&quot;: () =&gt; &quot;function getImageData() { [native code] }&quot;
    });
 
    Object.defineProperty(CanvasRenderingContext2D.prototype.getImageData, &quot;name&quot;, {
        &quot;value&quot;: &quot;getImageData&quot;
    });
})();</code></pre><p>再用 selenium 跑两下 f.vision，结果如下：</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/7ac0a5406fe20_1643211325.png" alt="167.png" title="167.png"></p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/5187edc5502a8_1643211337.png" alt="168.png" title="168.png"></p><p>这下老实了。</p><h1>实战-某音</h1><p>有了上面的能力，理论上我们就可以找个小白鼠来实战试验一下。</p><p>下图是某音网页版的推荐页 <a href="https://douyin.com/recommend">https://douyin.com/recommend</a> , 指纹计算的逻辑在他的 <strong>webmssdk.js</strong> 中，具体逻辑没仔细看，姑且当成黑盒测试一下。可以看到他给我计算了一个Canvas指纹(即 canvasHash 变量)。多次打开、清理cookie、使用不同显示器后指纹均能够保持稳定。</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/39a898df39956_1643211371.png" alt="169.png" title="169.png"></p><p>然后上一下我们的代码，可以发现指纹的确发生了变化，且每次打开均不同：</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/f139a9f1d6592_1643211379.png" alt="170.png" title="170.png"></p><h1>coveryourtracks指纹</h1><p>本来以为上面的代码足够应对绝大多数场景了，但是回头跑了一下 <a href="https://coveryourtracks.eff.org/">https://coveryourtracks.eff.org/</a> 这个（检测时可能要翻墙），忽然发现他竟然还是能检测出来我做了伪造:</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/5adce19267da9_1643211393.png" alt="171.png" title="171.png"></p><p>回想到他在检测时似乎有刷新页面的操作，因此猜测他也是通过刷新页面的方式，比较两次生成的指纹是否相同来检测伪造。于是我尝试将随机性从 js 脚本中提取到 python 代码里，保证相同会话无论刷新多少次都是用的同一套随机数。结果果然印证了我的猜想。</p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/7a0cfc5831e34_1643211402.png" alt="172.png" title="172.png"></p><p><img src="https://pic.8oh.com.cn/cos/2022/01/26/74a042369f6c8_1643211408.png" alt="173.png" title="173.png"></p><h1>小结</h1><p>虽然看起来上述操作能解决已知的 Canvas 检测问题，但像所有的攻守对抗一样，只要检测方愿意，他们可以十分轻松的想出很多种办法检测出我们检测出了他们的检测代码，问题只在于他们是否有闲情做这件事、以及这件事情是否必要。据我所知目前被搞的比较多的网站大都也都没有做这些检测，毕竟除了Canvas指纹之外，他们也有太多的工具可以使用。</p><p>来源：<a href="https://blog.mythsman.com/post/60d1a161e8493b525875166c/">https://blog.mythsman.com/post/60d1a161e8493b525875166c/</a></p>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/177/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/%E8%AE%BE%E5%A4%87%E6%8C%87%E7%BA%B9/</wfw:commentRss>
</item>
</channel>
</rss>