Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / web / src / main / assets / shields / farble.js
farble.js252 lines · 9.7 KiBRaw
| 1 | // Brave-style fingerprint farbling. |
| 2 | // |
| 3 | // Injected at document start with two globals already defined by the browser: |
| 4 | // __shieldsSeed 32-bit int, stable per (session, eTLD+1) and different across both |
| 5 | // __shieldsAggressive true to also farble timezone and remove WebRTC |
| 6 | // |
| 7 | // The goal is Brave's: make the values a site reads slightly wrong in a way that is |
| 8 | // consistent within the page (so nothing visibly breaks or flickers) but uncorrelated |
| 9 | // across sites and across restarts (so the readings cannot be joined into an identifier). |
| 10 | (function () { |
| 11 | 'use strict'; |
| 12 | |
| 13 | var seed = window.__shieldsSeed >>> 0; |
| 14 | var aggressive = window.__shieldsAggressive === true; |
| 15 | try { |
| 16 | delete window.__shieldsSeed; |
| 17 | delete window.__shieldsAggressive; |
| 18 | } catch (e) {} |
| 19 | |
| 20 | // mulberry32 — small, fast, and deterministic from the session/origin seed. |
| 21 | var state = seed; |
| 22 | function rnd() { |
| 23 | state |= 0; |
| 24 | state = (state + 0x6d2b79f5) | 0; |
| 25 | var t = Math.imul(state ^ (state >>> 15), 1 | state); |
| 26 | t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; |
| 27 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296; |
| 28 | } |
| 29 | |
| 30 | // Farbling must be a pure function of the input, not of how many times it has been |
| 31 | // called: a page that hashes the same canvas twice has to get the same answer, both |
| 32 | // because real code does that and because a mismatch would announce that we are here. |
| 33 | function reseed(extra) { |
| 34 | state = (seed ^ (extra | 0)) | 0; |
| 35 | } |
| 36 | |
| 37 | function hashString(s) { |
| 38 | var h = 0; |
| 39 | for (var i = 0; i < s.length; i++) h = (Math.imul(h, 31) + s.charCodeAt(i)) | 0; |
| 40 | return h; |
| 41 | } |
| 42 | |
| 43 | function define(target, name, value) { |
| 44 | try { |
| 45 | Object.defineProperty(target, name, { get: function () { return value; }, configurable: true }); |
| 46 | } catch (e) {} |
| 47 | } |
| 48 | |
| 49 | function wrap(proto, name, make) { |
| 50 | try { |
| 51 | var original = proto[name]; |
| 52 | if (typeof original !== 'function') return; |
| 53 | var replacement = make(original); |
| 54 | // Keep toString() looking native so detection scripts do not trip on the wrapper. |
| 55 | try { |
| 56 | Object.defineProperty(replacement, 'name', { value: name, configurable: true }); |
| 57 | } catch (e) {} |
| 58 | replacement.toString = function () { return 'function ' + name + '() { [native code] }'; }; |
| 59 | proto[name] = replacement; |
| 60 | } catch (e) {} |
| 61 | } |
| 62 | |
| 63 | // --- Canvas ------------------------------------------------------------- |
| 64 | // Perturb the low bit of a sparse scattering of subpixels. Invisible on screen, |
| 65 | // but enough to change every hash a canvas fingerprinter computes. |
| 66 | function farbleImageData(data) { |
| 67 | reseed(data.length); |
| 68 | var step = audioAndPixelStep(data.length); |
| 69 | for (var i = 0; i < data.length; i += step) { |
| 70 | data[i] = data[i] ^ (rnd() < 0.5 ? 0 : 1); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | function audioAndPixelStep(length) { |
| 75 | return Math.max(4, ((length / 512) | 0) * 4 || 4); |
| 76 | } |
| 77 | |
| 78 | wrap(CanvasRenderingContext2D.prototype, 'getImageData', function (original) { |
| 79 | return function () { |
| 80 | var result = original.apply(this, arguments); |
| 81 | try { farbleImageData(result.data); } catch (e) {} |
| 82 | return result; |
| 83 | }; |
| 84 | }); |
| 85 | |
| 86 | function farbleCanvas(canvas) { |
| 87 | try { |
| 88 | var ctx = canvas.getContext('2d'); |
| 89 | if (!ctx || canvas.width === 0 || canvas.height === 0) return; |
| 90 | var image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 91 | ctx.putImageData(image, 0, 0); |
| 92 | } catch (e) {} |
| 93 | } |
| 94 | |
| 95 | wrap(HTMLCanvasElement.prototype, 'toDataURL', function (original) { |
| 96 | return function () { |
| 97 | farbleCanvas(this); |
| 98 | return original.apply(this, arguments); |
| 99 | }; |
| 100 | }); |
| 101 | |
| 102 | wrap(HTMLCanvasElement.prototype, 'toBlob', function (original) { |
| 103 | return function () { |
| 104 | farbleCanvas(this); |
| 105 | return original.apply(this, arguments); |
| 106 | }; |
| 107 | }); |
| 108 | |
| 109 | // --- WebGL -------------------------------------------------------------- |
| 110 | // The GPU strings are the highest-entropy bits available, so report the generic |
| 111 | // values Brave reports rather than noise, and add noise to pixel readback. |
| 112 | var UNMASKED_VENDOR = 0x9245; |
| 113 | var UNMASKED_RENDERER = 0x9246; |
| 114 | |
| 115 | ['WebGLRenderingContext', 'WebGL2RenderingContext'].forEach(function (name) { |
| 116 | var ctor = window[name]; |
| 117 | if (!ctor) return; |
| 118 | wrap(ctor.prototype, 'getParameter', function (original) { |
| 119 | return function (parameter) { |
| 120 | if (parameter === UNMASKED_VENDOR) return 'Google Inc.'; |
| 121 | if (parameter === UNMASKED_RENDERER) return 'ANGLE (Google, Vulkan 1.3.0 (SwiftShader Device))'; |
| 122 | return original.apply(this, arguments); |
| 123 | }; |
| 124 | }); |
| 125 | wrap(ctor.prototype, 'readPixels', function (original) { |
| 126 | return function () { |
| 127 | var result = original.apply(this, arguments); |
| 128 | try { |
| 129 | var pixels = arguments[6]; |
| 130 | if (pixels && pixels.length) farbleImageData(pixels); |
| 131 | } catch (e) {} |
| 132 | return result; |
| 133 | }; |
| 134 | }); |
| 135 | wrap(ctor.prototype, 'getSupportedExtensions', function (original) { |
| 136 | return function () { |
| 137 | var extensions = original.apply(this, arguments); |
| 138 | try { |
| 139 | // Drop one extension so the reported set is not a stable signature. Chosen from |
| 140 | // the seed alone so every call in this page returns the same list. |
| 141 | if (extensions && extensions.length > 1) { |
| 142 | reseed(extensions.length); |
| 143 | extensions.splice((rnd() * extensions.length) | 0, 1); |
| 144 | } |
| 145 | } catch (e) {} |
| 146 | return extensions; |
| 147 | }; |
| 148 | }); |
| 149 | }); |
| 150 | |
| 151 | // --- Audio -------------------------------------------------------------- |
| 152 | // A fixed oscillator rendered offline produces an identical buffer on identical |
| 153 | // hardware; scaling each sample by ~1e-7 defeats the hash without being audible. |
| 154 | function farbleSamples(samples) { |
| 155 | reseed(samples.length); |
| 156 | var step = Math.max(1, (samples.length / 256) | 0); |
| 157 | for (var i = 0; i < samples.length; i += step) { |
| 158 | samples[i] = samples[i] * (1 + (rnd() - 0.5) * 1e-6); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (window.AnalyserNode) { |
| 163 | ['getFloatFrequencyData', 'getByteFrequencyData', 'getFloatTimeDomainData'].forEach(function (name) { |
| 164 | wrap(AnalyserNode.prototype, name, function (original) { |
| 165 | return function (array) { |
| 166 | original.apply(this, arguments); |
| 167 | try { farbleSamples(array); } catch (e) {} |
| 168 | }; |
| 169 | }); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | if (window.AudioBuffer) { |
| 174 | wrap(AudioBuffer.prototype, 'getChannelData', function (original) { |
| 175 | return function () { |
| 176 | var data = original.apply(this, arguments); |
| 177 | try { farbleSamples(data); } catch (e) {} |
| 178 | return data; |
| 179 | }; |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | // --- Fonts -------------------------------------------------------------- |
| 184 | // Font enumeration works by measuring text width per family. Perturbing by a factor |
| 185 | // derived from the font and the string keeps every repeat measurement identical while |
| 186 | // making the set of widths useless for identifying the installed font list. |
| 187 | if (window.TextMetrics) { |
| 188 | wrap(CanvasRenderingContext2D.prototype, 'measureText', function (original) { |
| 189 | return function (text) { |
| 190 | var metrics = original.apply(this, arguments); |
| 191 | try { |
| 192 | reseed(hashString(String(this.font) + '|' + String(text))); |
| 193 | define(metrics, 'width', metrics.width * (1 + (rnd() - 0.5) * 1e-4)); |
| 194 | } catch (e) {} |
| 195 | return metrics; |
| 196 | }; |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | // --- navigator ---------------------------------------------------------- |
| 201 | // Brave reports a plausible-but-quantised core count and no plugins at all. |
| 202 | reseed(0); |
| 203 | define(navigator, 'hardwareConcurrency', 2 + (((rnd() * 3) | 0) * 2)); |
| 204 | define(navigator, 'deviceMemory', [2, 4, 8][(rnd() * 3) | 0]); |
| 205 | define(navigator, 'languages', Object.freeze(['en-US', 'en'])); |
| 206 | try { |
| 207 | var emptyPlugins = Object.create(PluginArray.prototype); |
| 208 | define(emptyPlugins, 'length', 0); |
| 209 | define(navigator, 'plugins', emptyPlugins); |
| 210 | var emptyMimeTypes = Object.create(MimeTypeArray.prototype); |
| 211 | define(emptyMimeTypes, 'length', 0); |
| 212 | define(navigator, 'mimeTypes', emptyMimeTypes); |
| 213 | } catch (e) {} |
| 214 | |
| 215 | // --- Aggressive only ---------------------------------------------------- |
| 216 | if (aggressive) { |
| 217 | // Timezone is stable, high-entropy and rarely load-bearing outside of calendars. |
| 218 | // Every reader has to agree, otherwise the disagreement is itself a signal: a page |
| 219 | // that sees resolvedOptions() say UTC but Date say -0700 knows it is being farbled. |
| 220 | try { |
| 221 | var DateTimeFormat = Intl.DateTimeFormat; |
| 222 | var patched = function (locales, options) { |
| 223 | options = Object.assign({}, options); |
| 224 | if (!options.timeZone) options.timeZone = 'UTC'; |
| 225 | return new DateTimeFormat(locales, options); |
| 226 | }; |
| 227 | patched.prototype = DateTimeFormat.prototype; |
| 228 | patched.supportedLocalesOf = DateTimeFormat.supportedLocalesOf.bind(DateTimeFormat); |
| 229 | Intl.DateTimeFormat = patched; |
| 230 | |
| 231 | Date.prototype.getTimezoneOffset = function () { return 0; }; |
| 232 | // Re-point every local-time reader at its UTC twin. |
| 233 | [ |
| 234 | ['getDate', 'getUTCDate'], ['getDay', 'getUTCDay'], |
| 235 | ['getFullYear', 'getUTCFullYear'], ['getHours', 'getUTCHours'], |
| 236 | ['getMilliseconds', 'getUTCMilliseconds'], ['getMinutes', 'getUTCMinutes'], |
| 237 | ['getMonth', 'getUTCMonth'], ['getSeconds', 'getUTCSeconds'], |
| 238 | ['toString', 'toUTCString'], ['toDateString', 'toUTCString'], |
| 239 | ['toTimeString', 'toUTCString'], ['toLocaleString', 'toUTCString'], |
| 240 | ['toLocaleDateString', 'toUTCString'], ['toLocaleTimeString', 'toUTCString'], |
| 241 | ].forEach(function (pair) { |
| 242 | var utc = Date.prototype[pair[1]]; |
| 243 | Date.prototype[pair[0]] = function () { return utc.call(this); }; |
| 244 | }); |
| 245 | } catch (e) {} |
| 246 | |
| 247 | // WebRTC leaks local interface addresses even behind a proxy. |
| 248 | ['RTCPeerConnection', 'webkitRTCPeerConnection', 'RTCDataChannel'].forEach(function (name) { |
| 249 | try { delete window[name]; } catch (e) {} |
| 250 | }); |
| 251 | } |
| 252 | })(); |