JS逆向hook通用脚本合集
1. cookie 通用hook
Cookie Hook 用于定位 Cookie 中关键参数生成位置,以下代码演示了当 Cookie 中匹配到了 v 关键字, 则插入断点
(function () {var cookieTemp = '';Object.defineproperty(document, 'cookie', {set: function (val) {if (val.indexOf('v') != -1) {debugger}console.log('Hook捕获到cookie设置->', val);cookieTemp = val;return val;},get: function () {return cookieTemp;},});
})();
2. header 参数通用hook
(function () {// 头部参数 请求对象当中的 设胃请求头部参数var org = window.XMLHttpRequest.prototype.setRequestHeader;window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {// 关键字 在请求当中发现有键是Authorization 断点if (key == 'Authorization') {debugger;}return org.apply(this, arguments);}
})();
3. hook过debugger
// 先保留原 constructor
// 1. 比如判断是否有该debugger词语,替换为同等长度的空格(避免判断长度)
Function.prototype.constructor_ = Function.prototype.constructor;
Function.prototype.constructor = function (a) {// 如果参数为 debugger,就返回空方法if(a == "debugger") {return function (){};}// 如果参数不为 debugger,还是返回原方法return Function.prototype.constructor_(a);
};2. 如果是定时器的debugger采用以下语句
// 先保留原定时器
var setInterval_ = setInterval
setInterval = function (func, time){// 如果时间参数为 0x7d0,就返回空方法// 当然也可以不判断,直接返回空,有很多种写法if(time == 0x7d0){return function () {};}// 如果时间参数不为 0x7d0,还是返回原方法return setInterval_(func, time)
}// eval("debugger;");
4. hook URL
URL Hook 用于定位请求 URL 中关键参数生成位置,以下代码演示了当请求的 URL 里包含 login 关键字时,则插入断点:
(function () {var open = window.XMLHttpRequest.prototype.open;window.XMLHttpRequest.prototype.open = function (method, url, async) {if (url.indexOf("login") != -1) {debugger;}return open.apply(this, arguments);};
})();
5. hook JSON.stringify
JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串,在某些站点的加密过程中可能会遇到,以下代码演示了遇到 JSON.stringify() 时,则插入断点:
(function() {var stringify = JSON.stringify;JSON.stringify = function(params) {console.log("Hook JSON.stringify ——> ", params);debugger;return stringify(params);}
})();
6. hook JSON.parse
JSON.parse() 方法用于将一个 JSON 字符串转换为对象,在某些站点的加密过程中可能会遇到,以下代码演示了遇到 JSON.parse() 时,则插入断点:
(function() {var parse = JSON.parse;JSON.parse = function(params) {console.log("Hook JSON.parse ——> ", params);debugger;return parse(params);}
})();
7. hook eval
JavaScript eval() 函数的作用是计算 JavaScript 字符串,并把它作为脚本代码来执行。如果参数是一个表达式,eval() 函数将执行表达式。如果参数是 Javascript 语句,eval() 将执行 Javascript 语句,经常被用来动态执行 JS。以下代码执行后,之后所有的 eval() 操作都会在控制台打印输出将要执行的 JS 源码:
(function() {// 保存原始方法window.__cr_eval = window.eval;// 重写 evalvar myeval = function(src) {console.log(src);console.log("=============== eval end ===============");debugger;return window.__cr_eval(src);}// 屏蔽 JS 中对原生函数 native 属性的检测var _myeval = myeval.bind(null);_myeval.toString = window.__cr_eval.toString;Object.defineProperty(window, 'eval', {value: _myeval});
})();
8. hook Function
以下代码执行后,所有的函数操作都会在控制台打印输出将要执行的 JS 源码:
(function() {// 保存原始方法window.__cr_fun = window.Function;// 重写 functionvar myfun = function() {var args = Array.prototype.slice.call(arguments, 0, -1).join(","),src = arguments[arguments.length - 1];console.log(src);console.log("=============== Function end ===============");debugger;return window.__cr_fun.apply(this, arguments);}// 屏蔽js中对原生函数native属性的检测myfun.toString = function() {return window.__cr_fun + ""}Object.defineProperty(window, 'Function', {value: myfun});
})();
9. 通用反调试
(function() {var _constructor = unsafeWindow.Function.prototype.constructor;// Hook Function.prototype.constructorunsafeWindow.Function.prototype.constructor = function() {var fnContent = arguments[0];if (fnContent) {if (fnContent.includes('debugger')) { // An anti-debugger is attempting to stop debuggingvar caller = Function.prototype.constructor.caller; // Non-standard hack to get the function callervar callerContent = caller.toString();if (callerContent.includes(/\bdebugger\b/gi)) { // Eliminate all debugger statements from the caller, if anycallerContent = callerContent.replace(/\bdebugger\b/gi, ''); // Remove all debugger expressionseval('caller = ' + callerContent); // Replace the function}return (function () {});}}// Execute the normal function constructor if nothing unusual is going onreturn _constructor.apply(this, arguments);};
})();