反反调试

高级反反调试保护,具有改进的性能和功能。 尝试修复视频播放问题。

当前为 2024-10-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Anti Anti-debugger
  3. // @name:vi Chống Anti-debugger
  4. // @name:zh-CN 反反调试
  5. // @namespace https://greasyfork.org/vi/users/1195312-renji-yuusei
  6. // @version 1.4
  7. // @description Advanced protection against anti-debugging with improved performance and features. Attempts to fix video playback issues.
  8. // @description:vi Bảo vệ nâng cao chống anti-debugging với hiệu suất và tính năng được cải thiện. Cố gắng sửa lỗi phát lại video.
  9. // @description:zh-CN 高级反反调试保护,具有改进的性能和功能。 尝试修复视频播放问题。
  10. // @author Yuusei
  11. // @match *://*/*
  12. // @grant unsafeWindow
  13. // @run-at document-start
  14. // @license GPL-3.0-only
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. const config = {
  21. debugKeywords: new Set(['debugger', 'debug', 'debugging', 'breakpoint']),
  22. consoleProps: ['log', 'warn', 'error', 'info', 'debug', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupCollapsed', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count'],
  23. maxLogHistory: 100,
  24. // Add a whitelist for sites where debugging should be allowed
  25. whitelist: new Set([]), // Example: ['example.com', 'another-site.net']
  26. };
  27.  
  28. const originalConsole = Object.fromEntries(
  29. Object.entries(console).map(([key, value]) => [key, value.bind(console)])
  30. );
  31.  
  32. let logHistory = [];
  33.  
  34. const safeEval = (code, context = {}) => {
  35. try {
  36. return new Function('context', `with(context) { return (${code}) }`)(context);
  37. } catch (error) {
  38. originalConsole.error('Failed to evaluate code:', error, code);
  39. return null;
  40. }
  41. };
  42.  
  43. const modifyFunction = (func) => {
  44. if (typeof func !== 'function') return func;
  45.  
  46. const funcStr = func.toString();
  47. if ([...config.debugKeywords].some(keyword => funcStr.includes(keyword))) {
  48. const modifiedStr = funcStr.replace(
  49. new RegExp(`\\b(${[...config.debugKeywords].join('|')})\\b`, 'g'),
  50. '/* removed */'
  51. );
  52. try {
  53. return safeEval(`(${modifiedStr})`);
  54. } catch (e) {
  55. originalConsole.error("Error modifying function:", e);
  56. return func; // Return original function if modification fails
  57. }
  58. }
  59. return func;
  60. };
  61.  
  62.  
  63.  
  64. const wrapConsole = () => {
  65. config.consoleProps.forEach(prop => {
  66. console[prop] = (...args) => {
  67. originalConsole[prop](...args);
  68. const formattedArgs = args.map(formatLogArgument);
  69. const logMessage = `[${prop.toUpperCase()}] ${formattedArgs.join(' ')}`;
  70. logHistory.push(logMessage);
  71. if (logHistory.length > config.maxLogHistory) logHistory.shift();
  72. };
  73. });
  74. };
  75.  
  76.  
  77. const antiAntiDebugger = () => {
  78. const proxyHandler = {
  79. apply: (target, thisArg, args) => Reflect.apply(target, thisArg, args.map(modifyFunction)),
  80. construct: (target, args) => Reflect.construct(target, args.map(modifyFunction))
  81. };
  82.  
  83. Object.defineProperties(Function.prototype, {
  84. constructor: {
  85. value: new Proxy(Function.prototype.constructor, proxyHandler),
  86. writable: false,
  87. configurable: false,
  88. }
  89. });
  90.  
  91. unsafeWindow.eval = new Proxy(unsafeWindow.eval, proxyHandler);
  92. unsafeWindow.Function = new Proxy(unsafeWindow.Function, proxyHandler);
  93. };
  94.  
  95.  
  96. const preventDebugging = () => {
  97. // Check if the current site is in the whitelist
  98. if (config.whitelist.some(site => window.location.host.includes(site))) {
  99. return; // Skip disabling debugging functions
  100. }
  101.  
  102. const noop = () => {};
  103. ['alert', 'confirm', 'prompt'].forEach(prop => {
  104. if (prop in unsafeWindow) {
  105. try { // Try/catch for robustness
  106. Object.defineProperty(unsafeWindow, prop, {
  107. value: noop,
  108. writable: false,
  109. configurable: false
  110. });
  111. } catch (e) {
  112. originalConsole.error("Error overriding ", prop, ":", e);
  113. }
  114. }
  115. });
  116. // Attempt to restore video playback by re-enabling potentially blocked APIs
  117. try {
  118. Object.defineProperty(HTMLMediaElement.prototype, 'play', {
  119. value: HTMLMediaElement.prototype.play,
  120. writable: true,
  121. configurable: true
  122. });
  123. } catch (error) {
  124. originalConsole.error("Error restoring HTMLMediaElement.play:", error);
  125. }
  126. };
  127.  
  128. const formatLogArgument = (arg) => {
  129. if (arg === null) return 'null';
  130. if (arg === undefined) return 'undefined';
  131. if (typeof arg === 'object') {
  132. try {
  133. return JSON.stringify(arg, (key, value) =>
  134. typeof value === 'function' ? value.toString() : value, 2);
  135. } catch (e) {
  136. return '[Circular]';
  137. }
  138. }
  139. return String(arg);
  140. };
  141.  
  142. const init = () => {
  143. wrapConsole();
  144.  
  145. // Check for whitelist before applying anti-debugging
  146. if (!config.whitelist.some(site => window.location.host.includes(site))) {
  147. antiAntiDebugger();
  148. }
  149. preventDebugging();
  150. console.log('%cAnti Anti-debugger is active', 'color: #00ff00; font-weight: bold;');
  151. };
  152.  
  153. init();
  154. })();