frida备忘录
zsk Lv4

frida hook某个类下所有方法

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
Java.perform(function () {
travel('com.xxx.xxx')
})

function printstack() {
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
}

function travel(className) {
var NetContent = Java.use(className)
//1.得到这个类下面的所有方法
var Methods = NetContent.class.getDeclaredMethods()
//2.遍历所有的方法
Methods.forEach(function (Method) {
//得到这个方法的所有重载 ovelroads
var methodName = Method.getName()
var overloads = NetContent[methodName].overloads
//遍历重载
overloads.forEach(function (overload) {

var prot = '('
overload.argumentTypes.forEach(function (argumentType) {
prot += argumentType.className + ','
})
prot += ')'

overload.implementation = function () {
console.log('----------------------------------')
console.log(className + '.' + methodName + prot)
printstack()
for (var i = 0; i < arguments.length; i++) {
console.log('argument:' + JSON.stringify(arguments[i]))
}
var ret = this[methodName].apply(this, arguments)
console.log(methodName + 'return:' + JSON.stringify(ret))
return ret
}
})
})
}

hookEvent

trace所有的mOnClickListener,hook它们的onClick函数,实现点到哪里,定位到哪个类的功能。

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
var jclazz = null;
var jobj = null;

function getObjClassName(obj) {
if (!jclazz) {
var jclazz = Java.use("java.lang.Class");
}
if (!jobj) {
var jobj = Java.use("java.lang.Object");
}
return jclazz.getName.call(jobj.getClass.call(obj));
}

function watch(obj, mtdName) {
var listener_name = getObjClassName(obj);
var target = Java.use(listener_name);
if (!target || !mtdName in target) {
return;
}
// send("[WatchEvent] hooking " + mtdName + ": " + listener_name);
target[mtdName].overloads.forEach(function (overload) {
overload.implementation = function () {
//send("[WatchEvent] " + mtdName + ": " + getObjClassName(this));
console.log("[WatchEvent] " + mtdName + ": " + getObjClassName(this))
return this[mtdName].apply(this, arguments);
};
})
}

function OnClickListener() {
Java.perform(function () {

//以spawn启动进程的模式来attach的话
Java.use("android.view.View").setOnClickListener.implementation = function (listener) {
if (listener != null) {
watch(listener, 'onClick');
}
return this.setOnClickListener(listener);
};

//如果frida以attach的模式进行attch的话
Java.choose("android.view.View$ListenerInfo", {
onMatch: function (instance) {
instance = instance.mOnClickListener.value;
if (instance) {
console.log("mOnClickListener name is :" + getObjClassName(instance));
watch(instance, 'onClick');
}
},
onComplete: function () {
}
})
})
}
setImmediate(OnClickListener);

系统库常用算法hook

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
function printstack() {
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
}

Java.perform(function () {
console.log('enter')
var Base64 = Java.use('android.util.Base64')
var ByteString = Java.use("com.android.okhttp.okio.ByteString");
var secretKeySpec = Java.use('javax.crypto.spec.SecretKeySpec');
secretKeySpec.$init.overload('[B', 'java.lang.String').implementation = function (a, b) {
console.log("======================================");
printstack();
var result = this.$init(a, b);
console.log("算法名:" + b + "|str密钥:" + Java.use('java.lang.String').$new(a));
console.log("算法名:" + b + "|Hex密钥:" + ByteString.of(a).hex());
return result;
}

var DESKeySpec = Java.use('javax.crypto.spec.DESKeySpec');
DESKeySpec.$init.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.$init(a);
var bytes_key_des = this.getKey();
console.log("des密钥 |str " + Java.use('java.lang.String').$new(bytes_key_des));
console.log("des密钥 |hex " + ByteString.of(bytes_key_des).hex());
return result;
}

DESKeySpec.$init.overload('[B', 'int').implementation = function (a, b) {
console.log("======================================");
printstack();
var result = this.$init(a, b);

var bytes_key_des = this.getKey();
console.log("des密钥 |str " + Java.use('java.lang.String').$new(bytes_key_des));
console.log("des密钥 |hex " + ByteString.of(bytes_key_des).hex());
return result;
}

var mac = Java.use('javax.crypto.Mac');
mac.getInstance.overload('java.lang.String').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.getInstance(a);

console.log("算法名:" + a);
return result;
}
mac.update.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
this.update(a);

console.log("update:" + Java.use('java.lang.String').$new(a))
}
mac.update.overload('[B', 'int', 'int').implementation = function (a, b, c) {
console.log("======================================");
printstack();
this.update(a, b, c)

console.log("update:" + Java.use('java.lang.String').$new(a) + "|" + b + "|" + c);
}
mac.doFinal.overload().implementation = function () {
console.log("======================================");
printstack();
var result = this.doFinal();

console.log("mac doFinal结果: |str :" + Java.use('java.lang.String').$new(result));
console.log("mac doFinal结果: |hex :" + ByteString.of(result).hex());
console.log("mac doFinal结果: |base64 :" + Base64.encodeToString(result, 0));
return result;
}
mac.doFinal.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.doFinal(a);

console.log("doFinal参数: |str :" + Java.use('java.lang.String').$new(a));
console.log("doFinal结果: |str :" + Java.use('java.lang.String').$new(result));
console.log("doFinal结果: |hex :" + ByteString.of(result).hex());
console.log("doFinal结果: |base64 :" + Base64.encodeToString(result, 0));
return result;
}

var md = Java.use('java.security.MessageDigest');
md.getInstance.overload('java.lang.String', 'java.lang.String').implementation = function (a, b) {
console.log("======================================");
printstack();

console.log("算法名:" + a);
return this.getInstance(a, b);
}
md.getInstance.overload('java.lang.String').implementation = function (a) {
console.log("======================================");
printstack();

console.log("算法名:" + a);
return this.getInstance(a);
}
md.update.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();

console.log("update:" + Java.use('java.lang.String').$new(a))
return this.update(a);
}
md.update.overload('[B', 'int', 'int').implementation = function (a, b, c) {
console.log("======================================");
printstack();
console.log("update:" + Java.use('java.lang.String').$new(a) + "|" + b + "|" + c);
return this.update(a, b, c);
}
md.digest.overload().implementation = function () {
console.log("======================================");
printstack();
var result = this.digest();
console.log("digest11结果:" + ByteString.of(result).hex());
console.log("digest结果:" + Base64.encodeToString(result, 0));
return result;
}
md.digest.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
console.log("digest参数:" + Java.use('java.lang.String').$new(a));
var result = this.digest(a);
console.log("digest结果:" + ByteString.of(result).hex());
console.log("digest结果:" + Base64.encodeToString(result, 0));
return result;
}

var ivParameterSpec = Java.use('javax.crypto.spec.IvParameterSpec');
ivParameterSpec.$init.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.$init(a);
console.log("iv向量: |str:" + Java.use('java.lang.String').$new(a));
console.log("iv向量: |hex:" + ByteString.of(a).hex());
return result;
}

var cipher = Java.use('javax.crypto.Cipher');
cipher.getInstance.overload('java.lang.String').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.getInstance(a);

console.log("模式填充:" + a);
return result;
}
cipher.init.overload('int', 'java.security.Key').implementation = function (a, b) {
console.log("======================================");
printstack();
var result = this.init(a, b);

if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());
return result;
}
cipher.init.overload('int', 'java.security.cert.Certificate').implementation = function (a, b) {
console.log("======================================");
printstack();
var result = this.init(a, b);


if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

return result;
}
cipher.init.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec').implementation = function (a, b, c) {
console.log("======================================");
printstack();
var result = this.init(a, b, c);


if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());

return result;
}
cipher.init.overload('int', 'java.security.cert.Certificate', 'java.security.SecureRandom').implementation = function (a, b, c) {
console.log("======================================");
printstack();
var result = this.init(a, b, c);
if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}
return result;
}
cipher.init.overload('int', 'java.security.Key', 'java.security.SecureRandom').implementation = function (a, b, c) {
console.log("======================================");
printstack();
var result = this.init(a, b, c);
if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());
return result;
}
cipher.init.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters').implementation = function (a, b, c) {
console.log("======================================");
printstack();
var result = this.init(a, b, c);
if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());
return result;
}
cipher.init.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters', 'java.security.SecureRandom').implementation = function (a, b, c, d) {
console.log("======================================");
printstack();
var result = this.init(a, b, c, d);
if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());
return result;
}
cipher.init.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec', 'java.security.SecureRandom').implementation = function (a, b, c, d) {
console.log("======================================");
printstack();
var result = this.update(a, b, c, d);
if (N_ENCRYPT_MODE == a) {
console.log("init | 加密模式");
} else if (N_DECRYPT_MODE == a) {
console.log("init | 解密模式");
}

var bytes_key = b.getEncoded();
console.log("init key:" + "|str密钥:" + Java.use('java.lang.String').$new(bytes_key));
console.log("init key: |base64 :" + Base64.encodeToString(bytes_key, 0));
console.log("init key:" + "|Hex密钥:" + ByteString.of(bytes_key).hex());
return result;
}

cipher.update.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.update(a);
console.log("======================================");
console.log("update:" + Java.use('java.lang.String').$new(a));
return result;
}
cipher.update.overload('[B', 'int', 'int').implementation = function (a, b, c) {
console.log("======================================");
printstack();
var result = this.update(a, b, c);

console.log("update:" + Java.use('java.lang.String').$new(a) + "|" + b + "|" + c);
return result;
}
cipher.doFinal.overload().implementation = function () {
console.log("======================================");
printstack();
var result = this.doFinal();

console.log("doFinal结果: |str :" + Java.use('java.lang.String').$new(result));
console.log("doFinal结果: |hex :" + ByteString.of(result).hex());
console.log("doFinal结果: |base64 :" + Base64.encodeToString(result, 0));
return result;
}
cipher.doFinal.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.doFinal(a);

console.log("doFinal参数: |str :" + Java.use('java.lang.String').$new(a));
console.log("doFinal参数: |base64 :" + Base64.encodeToString(a, 0));
console.log("doFinal结果: |str :" + Java.use('java.lang.String').$new(result));
console.log("doFinal结果: |hex :" + ByteString.of(result).hex());
console.log("doFinal结果: |base64 :" + Base64.encodeToString(result, 0));
return result;
}

var x509EncodedKeySpec = Java.use('java.security.spec.X509EncodedKeySpec');
x509EncodedKeySpec.$init.overload('[B').implementation = function (a) {
console.log("======================================");
printstack();
var result = this.$init(a);

console.log("RSA密钥:" + Base64.encodeToString(a, 0));
return result;
}

var rSAPublicKeySpec = Java.use('java.security.spec.RSAPublicKeySpec');
rSAPublicKeySpec.$init.overload('java.math.BigInteger', 'java.math.BigInteger').implementation = function (a, b) {
console.log("======================================");
printstack();
var result = this.$init(a, b);

console.log("RSA密钥N:" + a.toString(16));
console.log("RSA密钥E:" + b.toString(16));
return result;
}
})
 评论