-
Notifications
You must be signed in to change notification settings - Fork 5
/
dump_plaintext_messages.js
155 lines (138 loc) · 4.87 KB
/
dump_plaintext_messages.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* This Frida script hooks functions that are responsible for encrypting and decrypting
* network plaintext messages. In particular, we hook:
*
* MMProtocalJni.unpack
* UtilsJni.HybridEcdhEncrypt
*
* Native functions in libMMProtocalJni.so:
* * AesEncrypt at address 0x0013d108
*
* the calculated function offsets and class definitions are from 8.0.23 / 2160 APK:
* https://dldir1.qq.com/weixin/android/weixin8023android2160_arm64_1.apk
*/
const GHIDRA_BASE = 0x00100000;
const MODULE_NAME = "libMMProtocalJni.so";
function timelog(message) {
console.log("[" + new Date().toISOString() + "] "
+ message);
}
function errorlog(message) {
timelog("[ERROR] " + message);
}
function inspectAES(args) {
timelog("----AESEncrypt----")
timelog("----REQUEST PLAINTEXT----");
var datalen = parseInt(String(args[5]), 16);
console.log(hexdump(args[4], {length: datalen, header: false, ansi: false}));
timelog("----END REQUEST PLAINTEXT----");
}
// offsets from 8023 / 2160 version. link:
// https://dldir1.qq.com/weixin/android/weixin8023android2160_arm64_1.apk
var target_funcs = [
{addr: 0x0013d108, name: "AES", onEnterFn: inspectAES},
];
function hookFuncs() {
var module = Process.findModuleByName(MODULE_NAME);
if (module == null) {
errorlog("module was null");
return;
}
timelog("Module found: " + MODULE_NAME);
var moduleBaseAddress = Module.findBaseAddress(MODULE_NAME);
target_funcs.map( ({name, addr, onEnterFn}) => {
const realAddr = moduleBaseAddress.add(addr - GHIDRA_BASE);
if (onEnterFn == null) {
onEnterFn = (args) => timelog('Called ' + name);
}
Interceptor.attach(realAddr, {
onEnter: onEnterFn,
});
timelog('Hooked '+name+ " at " + realAddr);
});
}
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
Interceptor.attach(Module.findExportByName(null, "open"), {
onEnter: function (args) {
var arg = args[0].readUtf8String();
this.foun = null;
var modules = [MODULE_NAME];
for (var i = 0; i < modules.length; i++) {
if (arg.includes(modules[i])) {
this.found = modules[i];
}
}
if (arg.includes(MODULE_NAME)) {
setTimeout(hookFuncs, 100);
}
},
onLeave: function(retval) {
if (this.found != null) {
delay(10).then(() => bindAllModuleFunctions(this.found));
}
}
});
function bindFunction(module, name) {
Interceptor.attach(Module.findExportByName(module, name), {
onEnter: function(args) {
timelog(module+": " + name + " called");
},
});
}
function bindAllModuleFunctions(module) {
var module = Process.findModuleByName(module);
if (module != null) {
timelog(module.name+" library loaded");
var exports = module.enumerateExports();
for (var i = 0; i < exports.length; i++) {
if (!exports[i].name.includes("Log") && !exports[i].includes("logger")) {
bindFunction(module.name, exports[i].name);
}
}
} else {
timelog(module+" library not loaded");
}
}
Java.perform(function(){
var UtilsJni = Java.use("com.tencent.mm.jni.utils.UtilsJni");
var toHexString = function(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};
UtilsJni.HybridEcdhEncrypt.overload(
'long', '[B'
).implementation = function(crypto_engine_id, plaintext) {
timelog("----HybridEcdhEncrypt----");
timelog("----REQUEST PLAINTEXT----");
console.log(toHexString(plaintext));
timelog("----END REQUEST PLAINTEXT----");
const HexClass = Java.use('org.apache.commons.codec.binary.Hex');
const StringClass = Java.use('java.lang.String');
const hexChars = HexClass.encodeHex(plaintext);
var ciphertext = this.HybridEcdhEncrypt(crypto_engine_id, plaintext);
return ciphertext;
};
var MMProtocalJni = Java.use("com.tencent.mm.protocal.MMProtocalJni");
var PByteArray = Java.use('com.tencent.mm.pointers.PByteArray');
MMProtocalJni.unpack.overload(
'com.tencent.mm.pointers.PByteArray',
'[B',
'[B',
'com.tencent.mm.pointers.PByteArray',
'com.tencent.mm.pointers.PInt',
'com.tencent.mm.pointers.PInt',
'com.tencent.mm.pointers.PInt',
'com.tencent.mm.pointers.PInt',
).implementation = function(p1, p2, p3, p4, p5, p6, p7, p8) {
var result = this.unpack(p1, p2, p3, p4, p5, p6, p7, p8);
var plaintext = p1.value.value;
timelog("----unpack----");
timelog("----RESPONSE PLAINTEXT----");
console.log(toHexString(plaintext));
timelog("----END RESPONSE PLAINTEXT----");
return result;
};
})