反反调试

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

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

  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.5
  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: /;\s*(?:debugger|debug(?:ger)?|breakpoint)\s*;?/g, // Use regex for efficiency
  22. consoleProps: ['log', 'warn', 'error', 'info', 'debug', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupCollapsed', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count'],
  23. maxLogHistory: 100,
  24. whitelist: /^(?:example\.com|another-site\.net)$/, // Use regex for whitelist
  25. };
  26.  
  27. const originalConsole = console; // No need for Object.fromEntries
  28.  
  29. const logHistory = [];
  30.  
  31. const safeEval = (code) => { // Removed unused context
  32. try {
  33. return Function(`return (${code})()`)(); // Simpler and potentially faster
  34. } catch (error) {
  35. originalConsole.error('Failed to evaluate code:', error, code);
  36. return null;
  37. }
  38. };
  39.  
  40. const modifyFunction = (func) => {
  41. if (typeof func !== 'function') return func;
  42.  
  43. const funcStr = func.toString();
  44. if (config.debugKeywords.test(funcStr)) {
  45. const modifiedStr = funcStr.replace(config.debugKeywords, ';/* removed */');
  46. try {
  47. return safeEval(modifiedStr);
  48. } catch (e) {
  49. originalConsole.error("Error modifying function:", e);
  50. return func;
  51. }
  52. }
  53. return func;
  54. };
  55.  
  56. const wrapConsole = () => {
  57. config.consoleProps.forEach(prop => {
  58. const original = originalConsole[prop];
  59. console[prop] = (...args) => {
  60. original.apply(originalConsole, args); // Use apply for correct context
  61. logHistory.push(`[${prop.toUpperCase()}] ${args.map(formatLogArgument).join(' ')}`);
  62. if (logHistory.length > config.maxLogHistory) logHistory.shift();
  63. };
  64. });
  65. };
  66.  
  67. const antiAntiDebugger = () => {
  68. const handler = {
  69. apply(target, thisArg, args) {
  70. return Reflect.apply(target, thisArg, args.map(modifyFunction));
  71. },
  72. construct(target, args) {
  73. return Reflect.construct(target, args.map(modifyFunction));
  74. }
  75. };
  76.  
  77. unsafeWindow.eval = new Proxy(unsafeWindow.eval, handler);
  78. unsafeWindow.Function = new Proxy(unsafeWindow.Function, handler);
  79. };
  80.  
  81. const preventDebugging = () => {
  82. if (config.whitelist.test(window.location.host)) return; // Use regex for whitelist check
  83.  
  84. const noop = () => {};
  85. ['alert', 'confirm', 'prompt'].forEach(prop => {
  86. try {
  87. Object.defineProperty(unsafeWindow, prop, { value: noop, writable: false, configurable: false });
  88. } catch (e) {
  89. originalConsole.error("Error overriding ", prop, ":", e);
  90. }
  91. });
  92.  
  93. try { // Restore video playback
  94. Object.defineProperty(HTMLMediaElement.prototype, 'play', {
  95. value: HTMLMediaElement.prototype.play,
  96. writable: true,
  97. configurable: true
  98. });
  99. } catch (error) {
  100. originalConsole.error("Error restoring HTMLMediaElement.play:", error);
  101. }
  102. };
  103.  
  104.  
  105. const formatLogArgument = (arg) => {
  106. if (arg == null) return String(arg); // Simplified null/undefined check
  107. if (typeof arg === 'object') {
  108. try {
  109. return JSON.stringify(arg, null, 2); // Removed replacer function for simplicity
  110. } catch {
  111. return '[Circular]';
  112. }
  113. }
  114. return String(arg);
  115. };
  116.  
  117.  
  118. if (!config.whitelist.test(window.location.host)) { // Moved whitelist check to top level
  119. wrapConsole();
  120. antiAntiDebugger();
  121. }
  122. preventDebugging();
  123.  
  124. console.log('%cAnti Anti-debugger is active', 'color: #00ff00; font-weight: bold;');
  125. })();