Hook中文译为钩子,在程序中可理解为“劫持”。通过Hook技术能劫持对象,替换代码片段、修改参数或返回值,控制其与其他对象的交互。
方法一:
(function () {
let cookieCache = "";
Object.defineProperty(document, "cookie", {
set: function (val) {
console.log("Hook set cookie => ", val);
if (val.indexOf("baidu.com")!== -1) {
debugger;
}
cookieCache = val;
return val;
},
get: function () {
return cookieCache;
}
});
})();
方法二(不推荐):
(function () {
let cookieCache = document.cookie.__lookupSetter__("cookie");
document.__defineSetter__("cookie", function (val) {
console.log("Hook set cookie => ", val);
if (val.indexOf("baidu.com")!== -1) {
debugger;
}
cookieCache = val;
});
document.__defineGetter__("cookie", function () {
return cookieCache;
});
})();
(function () {
let headerCache = window.XMLHttpRequest.prototype.setRequestHeader;
window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {
console.log("Hook set header %s => %s", key, value);
if (key === "baidu.com") {
debugger;
}
return headerCache.apply(this, arguments);
};
})();
Firefox、Firefox Developer Edition浏览器从121版本开始,取消“Pause on debugger statement”勾选可绕过无限debugger。
常见绕过方法:
构造函数:
(function () {
let constructorCache = Function.prototype.constructor;
Function.prototype.constructor = function (string) {
if (string === "debugger") {
console.log("Hook constructor debugger!");
return function () {};
}
return constructorCache(string);
};
})();
(function () {
let openCache = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, url) {
console.log("Hook xhr method => %s, url => %s", method, url);
if (url.indexOf("baidu.com")!== -1) {
debugger;
}
return openCache.apply(this, arguments);
};
})();
(function () {
let fetchCache = Object.getOwnPropertyDescriptor(window, "fetch");
Object.defineProperty(window, "fetch", {
value: function (url) {
console.log("Hook fetch url => ", url);
debugger;
return fetchCache.value.apply(this, arguments);
}
});
})();
(function () {
let stringifyCache = JSON.stringify;
JSON.stringify = function (params) {
console.log("Hook JSON.stringify => ", params);
debugger;
return stringifyCache(params);
};
})();
(function () {
let parseCache = JSON.parse;
JSON.parse = function (params) {
console.log("Hook JSON.parse => ", params);
debugger;
return parseCache(params);
};
})();
(function () {
let FunctionCache = window.Function;
let newFunction = function () {
let src = arguments[arguments.length - 1];
console.log("Hook Function => ", src);
debugger;
return FunctionCache.apply(this, arguments);
};
newFunction.toString = function () {
return FunctionCache.toString();
};
})();
(function () {
let sendCache = WebSocket.prototype.send;
WebSocket.prototype.send = function (data) {
console.info("Hook WebSocket send => ", data);
return sendCache(data);
};
})();
(function () {
String.prototype.splitCache = String.prototype.split;
String.prototype.split = function (separator, limit) {
console.log("Hook String.prototype.split separator => %s, limit => %s", separator, limit);
let str = this.toString();
if(str.includes("baidu.com")) {
debugger;
}
return str.splitCache(separator, limit)
};
})();
(function () {
let consoleCache = console.log;
console.log = function (msg) {
consoleCache("Hook console.log =>", msg);
if(msg === "baidu.com") {
debugger;
}
consoleCache(msg);
};
})();
(function () {
let evalCache = window.eval;
window.eval = function (string) {
console.log("Hook eval =>", string);
debugger;
return evalCache(string);
};
window.eval.toString = function () {
return evalCache.toString();
};
})();
(function () {
window.onbeforeunload = function () {
console.log("Hook window.onbeforeunload.");
debugger;
return false;
};
})();
(function () {
let RegExpCache = RegExp;
RegExp = function (pattern, flags) {
console.log("Hook RegExp pattern => %s, flags => %s", pattern, flags);
debugger;
return RegExpCache(pattern, flags);
};
})();
(function () {
let createElementCache = document.createElement;
document.createElement = function (tagName) {
console.info("Hook createElement tagName => ", tagName);
if(tagName === "canvas") {
debugger;
}
return createElementCache(tagName);
};
})();
(function () {
let createElementCache = document.createElement;
document.createElement = function (tagName) {
console.info("Hook createElement tagName => ", tagName);
if(tagName === "div") {
debugger;
}
return createElementCache(tagName);
};
})();
(function () {
let getElementByIdCache = document.getElementById;
document.getElementById = function (id) {
console.info("Hook getElementById id => ", id);
if (id === "spiderapi") {
debugger;
}
return getElementByIdCache(id);
};
})();
(function () {
let setAttributeCache = window.Element.prototype.setAttribute;
window.Element.prototype.setAttribute = function (name, value) {
console.info("Hook setAttribute name => %s, value => %s", name, value);
if (name === "spiderapi") {
debugger;
}
return setAttributeCache(name, value);
};
})();
(function () {
let setIntervalCache = setInterval;
setInterval = function (func, delay) {
console.log("Hook setInterval func => %s, delay => %s", func, delay);
debugger;
return setIntervalCache(func, delay);
};
})();
(function () {
let setTimeoutCache = setTimeout;
setTimeout = function (func, delay) {
console.log("Hook setTimeout func => %s, delay => %s", func, delay);
debugger;
return setTimeoutCache(func, delay);
};
})();
for (let i = 1; i < 99999; i++) window.clearInterval(i);
(function () {
Date.now = function now() { return 1661986251253 };
Date.parse = function () { return 1661986251253 };
Date.prototype.valueOf = function () { return 1661986251253 };
Date.prototype.getTime = function () { return 1661986251253 };
Date.prototype.toString = function () { return 1661986251253 };
Performance.prototype.now = function now(){ return Number('1661986251253'.slice(8))}
Math.random = function random() { return 0.08636862211354912 };
window.crypto.getRandomValues = function getRandomValues(array32, ...args){
return array32;
}
})();
let height = 660; // 固定的高度值
let width = 1366; // 固定的宽度值
let innerHeight_property_accessor = Object.getOwnPropertyDescriptor(window, "innerHeight"); // 获取innerHeight属性访问器描述符
let innerHeight_set_accessor = innerHeight_property_accessor.set; // 获取setter
Object.defineProperty(window, "innerHeight", {
get: function () {
return height;
},
set: function () {
innerHeight_set_accessor.call(window, height);
}
});
let innerWidth_property_accessor = Object.getOwnPropertyDescriptor(window, "innerWidth"); // 获取innerWidth属性访问器描述符
let innerWidth_set_accessor = innerWidth_property_accessor.set; // 获取setter
Object.defineProperty(window, "innerWidth", {
get: function () {
return width;
},
set: function () {
innerWidth_set_accessor.call(window, width);
}
});
(function () {
let oldFunc = func;
func = function (arguments) {
console.log(arguments);
return oldFunc.apply(arguments);
};
})();