Advanced Anti-Fingerprinting Script

Advanced measures to reduce browser fingerprinting

  1. // ==UserScript==
  2. // @name Advanced Anti-Fingerprinting Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @author saiHy
  6. // @description Advanced measures to reduce browser fingerprinting
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 扩展 Canvas 防护
  17. function advancedCanvasProtection() {
  18. const originalGetContext = HTMLCanvasElement.prototype.getContext;
  19. HTMLCanvasElement.prototype.getContext = function(type, contextAttributes) {
  20. const ctx = originalGetContext.call(this, type, contextAttributes);
  21. if (ctx) {
  22. const randomOffset = Math.random() * 100;
  23. const randomFactor = Math.random();
  24. ['save', 'restore', 'fillRect', 'strokeRect', 'clearRect', 'fill', 'stroke', 'fillText', 'strokeText'].forEach(method => {
  25. const originalMethod = ctx[method];
  26. ctx[method] = function(...args) {
  27. if (method.includes('Text')) {
  28. args[1] += randomOffset; // 改变文本位置
  29. args[2] += randomOffset;
  30. } else if (method.includes('Rect')) {
  31. args[1] += randomOffset * randomFactor; // 改变矩形位置
  32. args[2] += randomOffset * randomFactor;
  33. args[3] *= randomFactor; // 改变大小
  34. args[4] *= randomFactor;
  35. }
  36. return originalMethod.apply(this, args);
  37. };
  38. });
  39. }
  40. return ctx;
  41. };
  42. }
  43.  
  44. // 增强 WebGL 防护
  45. function advancedWebGLProtection() {
  46. try {
  47. const originalGetParameter = WebGLRenderingContext.prototype.getParameter;
  48. WebGLRenderingContext.prototype.getParameter = function(param) {
  49. // 提供通用或随机化的信息
  50. switch(param) {
  51. case 0x9245: // WebGL_VERSION
  52. return 'WebGL 1.0';
  53. case 0x8B31: // RENDERER
  54. return 'Generic GPU';
  55. case 0x8B30: // VENDOR
  56. return 'Anonymous Vendor';
  57. case 0x9242: // SHADING_LANGUAGE_VERSION
  58. return 'WebGL GLSL ES 1.0';
  59. case 0x9246: // UNMASKED_VENDOR_WEBGL
  60. case 0x9247: // UNMASKED_RENDERER_WEBGL
  61. return 'Anonymous';
  62. default:
  63. return originalGetParameter.call(this, param);
  64. }
  65. };
  66.  
  67. // 屏蔽更多 WebGL 扩展
  68. const originalGetExtension = WebGLRenderingContext.prototype.getExtension;
  69. WebGLRenderingContext.prototype.getExtension = function(name) {
  70. if (['WEBGL_debug_renderer_info', 'WEBGL_compressed_texture_s3tc', 'WEBGL_depth_texture'].includes(name)) return null;
  71. return originalGetExtension.call(this, name);
  72. };
  73. } catch (e) {} // 如果不支持 WebGL 则忽略错误
  74. }
  75.  
  76. // 进一步混淆浏览器信息
  77. function furtherObscureBrowserInfo() {
  78. // 语言信息
  79. Object.defineProperty(navigator, 'language', { get: () => 'en-US' });
  80. Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
  81.  
  82. // 时区
  83. const offset = -new Date().getTimezoneOffset() / 60;
  84. Object.defineProperty(Intl, 'DateTimeFormat', {
  85. value: function() {
  86. return {
  87. resolvedOptions: () => ({
  88. timeZone: 'UTC'
  89. })
  90. };
  91. }
  92. });
  93.  
  94. // 插件信息
  95. Object.defineProperty(navigator, 'plugins', { get: () => [{ name: 'Generic Plugin', description: 'Generic Plugin' }] });
  96.  
  97. // 字体
  98. const originalMeasureText = CanvasRenderingContext2D.prototype.measureText;
  99. CanvasRenderingContext2D.prototype.measureText = function() {
  100. const result = originalMeasureText.apply(this, arguments);
  101. result.width = 100 * Math.random(); // 随机化文本宽度
  102. return result;
  103. };
  104.  
  105. // 屏幕尺寸
  106. const screenWidth = window.screen.width;
  107. const screenHeight = window.screen.height;
  108. Object.defineProperty(window.screen, 'width', { get: () => Math.floor(screenWidth / 100) * 100 });
  109. Object.defineProperty(window.screen, 'height', { get: () => Math.floor(screenHeight / 100) * 100 });
  110.  
  111. // 硬件并发数
  112. Object.defineProperty(navigator, 'hardwareConcurrency', { get: () => 4 }); // 假设所有人都有4核
  113.  
  114. // 浏览器特性
  115. Object.defineProperty(navigator, 'platform', { get: () => 'Win32' });
  116. Object.defineProperty(navigator, 'appVersion', { get: () => '5.0 (Windows)' });
  117. Object.defineProperty(navigator, 'userAgent', { get: () => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36' });
  118. }
  119.  
  120. // 执行所有防护措施
  121. advancedCanvasProtection();
  122. advancedWebGLProtection();
  123. furtherObscureBrowserInfo();
  124.  
  125. })();