// ==UserScript==
// @name 核心Debugger拦截器
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 核心Hook防护模块,拦截Function/eval/定时器等debugger注入
// @author doubao
// @match *://*/*
// @grant unsafeWindow
// @grant document-start
// ==/UserScript==
(function() {
'use strict';
// === 核心Hook防护 ===
// 1. Hook Function构造器(拦截Function("debugger")形式)
Function.prototype._originalConstructor = Function.prototype.constructor;
Function.prototype.constructor = function() {
const args = arguments[0];
if (typeof args === 'string' && args.includes('debugger')) {
return () => {}; // 拦截含debugger的Function构造
}
return Function.prototype._originalConstructor.apply(this, arguments);
};
// 2. Hook eval函数(拦截eval("debugger")形式)
const originalEval = eval;
unsafeWindow.eval = function(code) {
const codeStr = String(code);
if (codeStr.includes('debugger')) {
return ''; // 拦截含debugger的eval执行
}
return originalEval.call(unsafeWindow, code);
};
// 3. Hook定时器(setInterval/setTimeout)
const hookTimer = (original, name) => {
return function(callback, delay, ...args) {
let cbCode = '';
if (typeof callback === 'function') cbCode = callback.toString();
if (typeof callback === 'string') cbCode = callback;
if (cbCode.includes('debugger')) {
return null; // 拦截定时器中的debugger
}
return original.apply(this, [callback, delay, ...args]);
};
};
window.setInterval = hookTimer(window.setInterval, 'setInterval');
window.setTimeout = hookTimer(window.setTimeout, 'setTimeout');
// 4. Hook requestAnimationFrame
window.requestAnimationFrame = hookTimer(window.requestAnimationFrame, 'requestAnimationFrame');
// 5. Hook constructor属性(拦截obj.constructor("debugger")形式)
const originalConstructor = Object.prototype.constructor;
Object.defineProperty(Object.prototype, 'constructor', {
value: function() {
const args = arguments[0];
if (typeof args === 'string' && args.includes('debugger')) {
return () => {}; // 拦截constructor注入
}
return originalConstructor.apply(this, arguments);
}
});
// 6. Hook DOM操作(拦截innerHTML中的debugger)
const originalAppendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(node) {
if (node && node.innerHTML && node.innerHTML.includes('debugger')) {
node.innerHTML = node.innerHTML.replace('debugger', '// 被Debugger拦截器移除');
}
return originalAppendChild.apply(this, arguments);
};
// 7. 防御性编程:保护eval.toString不被检测
const originalToString = Function.prototype.toString;
Function.prototype.toString = function() {
if (this === eval) {
return 'function eval() { [native code] }'; // 隐藏eval的Hook
}
return originalToString.apply(this, arguments);
};
// === 启动防护 ===
console.log('[核心Debugger拦截器] 已启动,正在拦截debugger注入...');
})();