油猴Tampermonkey脚本hook案例

油猴脚本编写介绍

  • 注释内容解释
  • @match这个注释的内容最为重要,它决定了你的脚本应用到哪个具体的网页,还是应用到所有的网页
  • @run-at确定了脚本的注入时机,在js逆向中也很重要
  • 油猴脚本所有注释介绍
属性名作用
@name脚本的名字
@namespace区分相同名称的脚本,一般写成作者名字或者网址就可以了
@version版本号
@description描述
@author作者名字
@match匹配的网址才会执行对应的脚本
@grant指定脚本运行所需权限,如果脚本拥有相应的权限,就可以调用油猴扩展提供的API与浏览器进行交互。如果设置为none的话,则不使用沙箱环境,脚本会直接运行在网页的环境中,这时候无法使用大部分油猴扩展的API。如果不指定的话,油猴会默认添加几个最常用的API
@require导入外部的脚本
@run-at脚本注入时机,document-start:网页开始时;document-body:body出现时;document-end:载入时或者之后执行;document-idle:载入完成后执行,默认选项
@connect当用户使用GM_xmlhttpRequest请求远程数据的时候,需要使用connect指定允许访问的域名,支持域名、子域名、IP地址以及*通配符
@updateURL脚本更新网址

hook的脚本合集

  1. hook之window属性案例
// ==UserScript==
// @name         定位pre加密参数
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       十三
// @run-at       document-start
// @match        https://flight.qunar.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var pre = "";
    Object.defineProperty(window, '_pt_', {
        get: function() {
            console.log('Getting window.属性');
            return pre
        },
        set: function(val) {
            console.log('Setting window.属性', val);
            debugger ;
            pre = val;
        }
    })
})();

  1. hook所有cookie,注意修改@match 所匹配的网址,否则可能hook不到
// ==UserScript==
// @name         定位cookie
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       十三
// @match        http://entp.yjj.gxzf.gov.cn/appnet/appEntpList.action?entpType=004
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
   'use strict';
    var _cookie = ""; // hook cookie
    Object.defineProperty(document, 'cookie', {
        set: function(val) {
            console.log('cookie set->', new Date().getTime(), val);
            debugger;
            _cookie = val;
            return val;
        },
        get: function() {
            return _cookie;
        }
   });
})()

  1. hook之split
String.prototype.split_bk = String.prototype.split;
String.prototype.split = function(val){
    str =  this.toString();
    debugger;
    return str.split_bk(val)
}
a = 'dsdfasdf sdsasd'
a.split(' ')

  1. hook之Header
(function () {
    var org = window.XMLHttpRequest.prototype.setRequestHeader;
    window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {
        if (key == 'Authorization') {
            debugger;
        }
        return org.apply(this, arguments);
    };
})();

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


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

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

  1. hook canvas (定位图片生成的地方)
(function() {
    'use strict';
    let create_element = document.createElement.bind(doument);

    document.createElement = function (_element) {
        console.log("create_element:",_element);
        if (_element === "canvas") {
            debugger;
        }
        return create_element(_element);
    }
})();

  1. setInterval 定时器
(function() {
    setInterval_ = setInterval;
    console.log("原函数已被重命名为setInterval_")
    setInterval = function() {}
    ;
    setInterval.toString = function() {
        console.log("有函数正在检测setInterval是否被hook");
        return setInterval_.toString();
    }
    ;
}
)();

  1. hook 正则
(function () {
    var _RegExp = RegExp;
    RegExp = function (pattern, modifiers) {
        console.log("Some codes are setting regexp");
        debugger;
        if (modifiers) {
            return _RegExp(pattern, modifiers);
        } else {
            return _RegExp(pattern);
        }
    };
    RegExp.toString = function () {
        return "function setInterval() { [native code] }"
    };
})();

  1. 过debugger—1 constructor 构造器构造出来的
var _constructor = constructor;
Function.prototype.constructor = function(s) {
    if (s == "debugger") {
        console.log(s);
        return null;
    }
    return _constructor(s);
}

  1. 过debugger—2 eval的
(function() {
    'use strict';
    var eval_ = window.eval;
    window.eval = function(x) {
        eval_(x.replace("debugger;", "  ; "));
    }
    ;
    window.eval.toString = eval_.toString;
}
)();

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༒࿈十三༙྇࿈༒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值