hook原理:
hook是常用于js反编译的技术;翻译就是钩子,他的原理就是劫持js的函数然后进行篡改
一段简单的js代码 :这个代码是顺序执行的
function test01(){console.log('test01')test02()
}
function test02(){console.log('02')test03()}function test03(){console.log("03")test04()
}
function test04(){console.log("04")
}
test01()var a=test03
function test03 (){console.log("hook successful")
}
这个就是简单的hook原理 当然如果是web上 我们的函数重构实际上就是 篡改猴脚本在 控制台上进行的操作
实验一 使用简单的hook脚本进行锁定浏览器的宽高
我们知道 js的bom树中有个 这个是浏览器当前的宽高 我们可以使用hook技术让 这个的输出是固定的
因为我们只有把窗口缩小 就会出现数值的同步
hook前置知识 :
这里有一堆注释的东西 是什么呢
// ==UserScript==
// @name 固定百度页面窗口尺寸 //脚本的名称
// @namespace http://tampermonkey.net/ // 脚本的作用域
// @version 1.0 //脚本的版本
// @description 强制让百度页面的 innerWidth/innerHeight 返回固定值 520 //脚本的注释
// @author YourName //脚本的作者
// @match https://www.baidu.com/* // 脚本作用的url 这个建议一个每个脚本都 固定特定的url 不要使用 htttp://*
// @grant none //定义脚本所需的权限
// @run-at document-start
完整脚本 可以直接让 ai帮写
实验二 :结合ai帮我们解决 js逆向常见的debuger 问题
最简单的步骤 :
1、Title 先判断debugger函数定义的位置(一般hook 的方法就是使用 函数重构)
这种 我们点击快进 看看有几个debugfer
发现就一个
然后使用全局搜索进行 搜索debugger 函数的定义
发现之后就让ai帮我们写脚本 (上面这个需要绕过 2个地方 1、 debuger函数 2、连续的时间)
// ==UserScript==
// @name 禁用网站反调试(无限debugger)
// @namespace http://tampermonkey.net/
// @version 1.1
// @description 绕过网站通过debugger实现的无限断点反调试
// @author YourName
// @match http://www.xiaodi8.com/test.html
// @grant none
// @run-at document-start
// ==/UserScript==(function() {'use strict';// ========== 核心防御逻辑 ==========// 1. 劫持 Function 构造函数 (防止通过 new Function("debugger") 创建调试器)const nativeFunction = window.Function;window.Function = function(...args) {const code = args.slice(-1)[0];if (/debugger|setInterval|setTimeout/i.test(code)) {return function(){}; // 返回空函数}return nativeFunction.apply(this, args);};// 2. 劫持 setInterval/setTimeout (防止定时触发debugger)const originalSetInterval = window.setInterval;window.setInterval = function(fn, delay, ...args) {if (typeof fn === 'function' && /debugger/.test(fn.toString())) {return -1; // 阻止执行}return originalSetInterval(fn, delay, ...args);};// 3. 直接重写 debugger 关键字行为Object.defineProperty(window, 'debugger', {get: () => {},set: () => {},configurable: true});// 4. 防御动态脚本注入new MutationObserver(() => {// 持续重写 Function 和 debuggerwindow.Function = function(){};window.eval = function(){};}).observe(document, { subtree: true, childList: true });// ========== 附加防御层 ==========// 禁用控制台调试 (可选)if (window.console) {window.console.log = function(){};window.console.debug = function(){};}// 劫持 onbeforeunload 事件window.onbeforeunload = null;
})();
之后就能解决了