frida Hook一个类下的所有方法

代码来自于ZenTracer这个项目,获取类的地方做了修改。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function log(text) {
    var packet = {
        'cmd': 'log',
        'data': text
    };
    send("ZenTracer:::" + JSON.stringify(packet))
}

function enter(tid, tname, cls, method, args) {
    var packet = {
        'cmd': 'enter',
        'data': [tid, tname, cls, method, args]
    };
    send("ZenTracer:::" + JSON.stringify(packet))
}

function exit(tid, retval) {
    var packet = {
        'cmd': 'exit',
        'data': [tid, retval]
    };
    send("ZenTracer:::" + JSON.stringify(packet))
}

function getTid() {
    var Thread = Java.use("java.lang.Thread")
    return Thread.currentThread().getId();
}

function getTName() {
    var Thread = Java.use("java.lang.Thread")
    return Thread.currentThread().getName();
}

function traceClass(target) {
    try {
        var clsname = target.class.getName();
        var methods = target.class.getDeclaredMethods();
        // log(methods);
        methods.forEach(function (method) {
            var methodName = method.getName();
            var overloads = target[methodName].overloads;
            overloads.forEach(function (overload) {
                var proto = "(";
                overload.argumentTypes.forEach(function (type) {
                    proto += type.className + ", ";
                });
                if (proto.length > 1) {
                    proto = proto.substr(0, proto.length - 2);
                }
                proto += ")";
                log("hooking: " + clsname + "." + methodName + proto);
                overload.implementation = function () {
                    var args = [];
                    var tid = getTid();
                    var tName = getTName();
                    for (var j = 0; j < arguments.length; j++) {
                        args[j] = arguments[j] + ""
                    }
                    enter(tid, tName, clsname, methodName + proto, args);
                    var retval = this[methodName].apply(this, arguments);
                    exit(tid, "" + retval);
                    return retval;
                }
            });
        });
    } catch (e) {
        console.error(e)
        // var stackTrace = Java.use('android.util.Log').getStackTraceString(Java.use('java.lang.Exception').$new());
        // console.log(stackTrace);
        log("'" + clsname + "' hook fail: " + e)
    }
}

Java.perform(function(){
    Java.choose("dalvik.system.PathClassLoader",{
        onMatch: function(instance){
            var loader = Java.ClassFactory.get(instance)
            try {
                var clazz = loader.use("com.douyu.sdk.ad.douyu.video.AdVideoPlayerPresenter")
                console.log("-------------clazz", clazz)
                traceClass(clazz)
                return "stop"
            }catch(e){
                //console.log("next")
                //console.log(e)
            }
        },
        onComplete:function(){
            console.log("Done")
        }
    })
})
updatedupdated2024-12-222024-12-22