常用的hook代码
// ==UserScript==
// @name hook xxx
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @run-at document-start
// @match https://match.yuanrenxue.cn/match/5
// @icon https://www.google.com/s2/favicons?sz=64&domain=yuanrenxue.cn
// @grant none
// ==/UserScript==
// header hook 监听请求头部参数
// 头部参数 请求对象当中的 设为请求头部参数
(function () {
var org = window.XMLHttpRequest.prototype.setRequestHeader;
window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {
// 关键字 在请求当中发现有键是Authorization 断点
if (key == 'xx') {
debugger;
}
return org.apply(this, arguments);
}
})();
// hook cookie 知道具体cookie的key
(function() {
// 'use strict';
Object.defineProperty(document, 'cookie', {
set: function (val){
if(val.indexOf('RM4hZBv0dDon443M') !== -1){
debugger;
return val
}
}
})
})();
// hook cookie 不知道cookie的具体key
(function() {
var cookie_cache = document.cookie;
Object.defineProperty(document, 'cookie', {
get:function (){
console.log('Get cookie');
// debugger
return cookie_cache;
},
set:function (val){
console.log('Set cookie');
debugger
var cookie = val.split(';')[0];
var ncookie = cookie.split("=");
var flag = false;
var cacha = cookie_cache.split('; ');
cacha = cacha.map(function (a){
if(a.split('=')[0] === ncookie[0]){
flag = true;
return cookie;
}
return a;
})
cookie_cache = cacha.join('; ');
if(!flag) {
cookie_cache += '; ';
}
this. value = val;
return cookie_cache;
},
});
})();
// hook URL
(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);
};
})();
// hook eval
(function() {
// 保存原始方法
window.__cr_eval = window.eval;
// 重写 eval
var 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
});
})();
// hook JSON.parse
(function() {
var parse = JSON.parse;
JSON.parse = function(params) {
console.log("Hook JSON.parse ——> ", params);
debugger;
return parse(params);
}
})();
// hook JSON.stringify
(function() {
var stringify = JSON.stringify;
JSON.stringify = function(params) {
console.log("Hook JSON.stringify ——> ", params);
debugger;
return stringify(params);
}
})();
// hook Function
(function() {
// 保存原始方法
window.__cr_fun = window.Function;
// 重写 function
var 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
});
})();
// 去除无限debugger
Function.prototype.constructor_ = Function.prototype.constructor;
Function.prototype.constructor = function (a) {
// 如果参数为 debugger,就返回空方法
if(a == "debugger") {
return function (){};
}
// 如果参数不为 debugger,还是返回原方法
return Function.prototype.constructor_(a);
};
// 定时器debugger
var setInterval_ = setInterval
setInterval = function (func, time){
// 如果时间参数为 0x7d0,就返回空方法
// 当然也可以不判断,直接返回空,有很多种写法
if(time == 0x7d0)
{
return function () {};
}
// 如果时间参数不为 0x7d0,还是返回原方法
return setInterval_(func, time)
}
// 通用反调试
(function() {
var _constructor = unsafeWindow.Function.prototype.constructor;
// Hook Function.prototype.constructor
unsafeWindow.Function.prototype.constructor = function() {
var fnContent = arguments[0];
if (fnContent) {
if (fnContent.includes('debugger')) { // An anti-debugger is attempting to stop debugging
var caller = Function.prototype.constructor.caller; // Non-standard hack to get the function caller
var callerContent = caller.toString();
if (callerContent.includes(/\bdebugger\b/gi)) { // Eliminate all debugger statements from the caller, if any
callerContent = callerContent.replace(/\bdebugger\b/gi, ''); // Remove all debugger expressions
eval('caller = ' + callerContent); // Replace the function
}
return (function () {});
}
}
// Execute the normal function constructor if nothing unusual is going on
return _constructor.apply(this, arguments);
};
})();
// setRequestHeader
(function() {
var org = window.XMLHttpRequest.prototype.setRequestHeader;
window.XMLHttpRequest.prototype.setRequestHeader = function(key, value){
if(key = 'xxx'){
debugger;
}
retutn org.apply(this, arguments);
};
})();
本文作者:愿风带走思绪
本文链接:https://www.cnblogs.com/lsixu/p/18400829
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步