Skip to content

Hook 通用模板

该篇文章搬运自:虫盒

javascript
(function () {
    let oldFunc = func;
    func = function (arguments) {
        console.log(arguments);
        return oldFunc.apply(arguments);
    };
})();

javascript
(function () {
  let cookieCache = "";
  Object.defineProperty(document, "cookie", {
    set: function (val) {
      console.log("Hook set cookie => ", val);
      if (val.indexOf("pythondouluo") !== -1) {
        debugger;
      }
      cookieCache = val;
      return val;
    },
    get: function () {
      return cookieCache;
    }
  });
})();

Hook Request Header

javascript
(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 === "pythondouluo") {
            debugger;
        }
        return headerCache.apply(this, arguments);
    };
})();

Hook 无限 Debugger

提示 无限 debugger 生成的形式比较多样,常见于构造函数、定时器和 eval 里,可根据实际场景进行调整。

constructor

javascript
(function () {
    let constructorCache = Function.prototype.constructor;
    Function.prototype.constructor = function (string) {
        if (string === "debugger") {
            console.log("Hook constructor debugger!");
            return function () {};
        }
        return constructorCache(string);
    };
})();

setInterval

javascript
(function () {
    let setIntervalCache = setInterval;
    setInterval = function (func, delay) {
        if (func.toString().indexOf("debugger") !== -1) {
            console.log("Hook setInterval debugger!");
            return function () {};
        }
        return setIntervalCache(func, delay);
    };
})();

setTimeout

javascript
(function () {
    let setTimeoutCache = setTimeout;
    setTimeout = function (func, delay) {
        if (func.toString().indexOf("debugger") !== -1) {
            console.log("Hook setTimeout debugger!");
            return function () {};
        }
        return setTimeoutCache(func, delay);
    };
})();

eval

javascript
(function () {
    let evalCache = window.eval;
    window.eval = function (string) {
        if (string.includes("debugger")) {
            console.log("Hook eval debugger!");
        }
        return evalCache(string.replace(/debugger\s*;?/g, ""));
    };
    window.eval.toString = function () {
        return evalCache.toString();
    };
})();

Hook XHR

javascript
(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("pythondouluo") !== -1) {
            debugger;
        }
        return openCache.apply(this, arguments);
    };
})();

Hook fetch

javascript
(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);
        }
    });
})();

Hook JSON.stringify

javascript
(function () {
    let stringifyCache = JSON.stringify;
    JSON.stringify = function (params) {
        console.log("Hook JSON.stringify => ", params);
        debugger;
        return stringifyCache(params);
    };
})();

Hook JSON.parse

javascript
(function () {
    let parseCache = JSON.parse;
    JSON.parse = function (params) {
        console.log("Hook JSON.parse => ", params);
        debugger;
        return parseCache(params);
    };
})();

Hook Function

提示 以下代码执行后,所有的函数操作都会在控制台打印输出将要执行的 JS 源码。

javascript
(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();
    };
})();

Hook WebSocket

javascript
(function () {
    let sendCache = WebSocket.prototype.send;
    WebSocket.prototype.send = function (data) {
        console.info("Hook WebSocket send => ", data);
        return sendCache(data);
    };
})();

Hook String.prototype.split

javascript
(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("pythondouluo")) {
            debugger;
        }
        return str.splitCache(separator, limit)
    };
})();

Hook console

javascript
(function () {
    let consoleCache = console.log;
    console.log = function (msg) {
        consoleCache("Hook console.log =>", msg);
        if(msg === "pythondouluo") {
            debugger;
        }
        consoleCache(msg);
    };
})();

Hook eval

javascript
(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();
    };
})();

Hook onbeforeunload

提示 onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发。Hook 此事件可阻断跳转,使其留在当前页面,通常用来应对网站打开 F12 就跳转页面的情况。

javascript
(function () {
    window.onbeforeunload = function () {
        console.log("Hook window.onbeforeunload.");
        debugger;
        return false;
    };
})();

Hook RegExp

javascript
(function () {
    let RegExpCache = RegExp;
    RegExp = function (pattern, flags) {
        console.log("Hook RegExp pattern => %s, flags => %s", pattern, flags);
        debugger;
        return RegExpCache(pattern, flags);
    };
})();

Hook Canvas

javascript
(function () {
    let createElementCache = document.createElement;
    document.createElement = function (tagName) {
        console.info("Hook createElement tagName => ", tagName);
        if(tagName === "canvas") {
            debugger;
        }
        return createElementCache(tagName);
    };
})();

Hook createElement

(function () {
    let createElementCache = document.createElement;
    document.createElement = function (tagName) {
        console.info("Hook createElement tagName => ", tagName);
        if(tagName === "div") {
            debugger;
        }
        return createElementCache(tagName);
    };
})();

Hook getElementById

javascript
(function () {
  let getElementByIdCache = document.getElementById;
  document.getElementById = function (id) {
    console.info("Hook getElementById id => ", id);
    if (id === "pythondouluo") {
      debugger;
    }
    return getElementByIdCache(id);
  };
})();

Hook setAttribute

javascript
(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 === "pythondouluo") {
      debugger;
    }
    return setAttributeCache(name, value);
  };
})();

Hook setInterval

javascript
(function () {
    let setIntervalCache = setInterval;
    setInterval = function (func, delay) {
        console.log("Hook setInterval func => %s, delay => %s", func, delay);
        debugger;
        return setIntervalCache(func, delay);
    };
})();

Hook setTimeout

javascript
(function () {
    let setTimeoutCache = setTimeout;
    setTimeout = function (func, delay) {
        console.log("Hook setTimeout func => %s, delay => %s", func, delay);
        debugger;
        return setTimeoutCache(func, delay);
    };
})();

清除定时器

javascript
for (let i = 1; i < 99999; i++) window.clearInterval(i);

Hook 固定随机变量

提示
来源于公众号:y小白的笔记(opens new window)

javascript
(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;
  }
})();

本站收录内容源自互联网,不对其网站内容或交易负责。 | 如有内容侵犯权益,请联系站长删除相关内容!