侧边栏壁纸
  • 累计撰写 10 篇文章
  • 累计创建 6 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

JS逆向Hook命令

BeiFeng
2024-05-08 / 0 评论 / 0 点赞 / 35 阅读 / 6898 字

JS逆向Hook命令

利用tampermonkey进行hook
Js逆向hook:headers,cookies,url,JSON.stringify,eval

headers:

// ==UserScript==
// @name         Hook Headers
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function () {
    var org = window.XMLHttpRequest.prototype.setRequestHeader;
    window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {
        console.log(key, value)
        if(key=="headers key"){//headers key,需要hook的headers
        debugger;
        }
        return org.apply(this, arguments);
    };
})();

cookies

// ==UserScript==
// @name         Hook cookies
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function () {
  'use strict';
  var cookieTemp = '';
  Object.defineProperty(document, 'cookie', {
    set: function (val) {
      if (val.indexOf('cookiesname') != -1) {//cookiesname需要hook的cookie
        debugger;
      }
      console.log('Hook捕获到cookie设置->', val);
      cookieTemp = val;
      return val;
    },
    get: function () {
      return cookieTemp;
    },
  });
})();

url

// ==UserScript==
// @name         Hook url
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function () {
    'use strict';
	 var open = window.XMLHttpRequest.prototype.open;
	  window.XMLHttpRequest.prototype.open = function (method, url, async) {
        if (url.indexOf("url") > -1) {
            debugger;
		}
        return open.apply(this, arguments);
	  }
})();

JSON.stringify

// ==UserScript==
// @name         Hook JSON.stringify
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
    var stringify = JSON.stringify;
    JSON.stringify = function(params) {
        console.log("Hook JSON.stringify ——> ", params);
        debugger;
        return stringify(params);
    }
})();

JSON.parse

// ==UserScript==
// @name         Hook JSON.parse
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
    var parse = JSON.parse;
    JSON.parse = function(params) {
        console.log("Hook JSON.parse ——> ", params);
        debugger;
        return parse(params);
    }
})();

eval

// ==UserScript==
// @name         Hook eval
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
    // 保存原始方法
    window.__eval = window.eval;
    // 重写 eval
    var myeval = function(src) {
        console.log(src);
        console.log("=== eval end ===");
        debugger;
        return window.__eval(src);
    }
    // 屏蔽 JS 中对原生函数 native 属性的检测
    var _myeval = myeval.bind(null);
    _myeval.toString = window.__eval.toString;
    Object.defineProperty(window, 'eval', {
        value: _myeval
    });
})();

Function

// ==UserScript==
// @name         Hook Function
// @namespace    BeiFeng
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(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 constructor构造器实现的

// ==UserScript==
// @name         Hook debugger-constructor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       BeiFeng
// @match        *
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
    'use strict';

    var _constructor = constructor;
    Function.prototype.constructor = function(s) {
    if (s == "debugger") {
        console.log(s);
        return null;
    }
    return _constructor(s);
}
})();
0

评论区