forked from WithSecureLabs/android-keystore-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer-cipher.js
449 lines (395 loc) · 16.3 KB
/
tracer-cipher.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Cipher class hooks + utilities.
Hooks will attempt to trace calls to Cipher class and hexdump buffer passed/returned during encryption/decryption.
All instances of Cipher class are captured by hooking any getInstance() call. You can them it in the cipherList variable.
Utilities:
ListCiphers()
* List Cipher instances collected in cipherList
GetCipher(cipherName)
* Get Cipher instance from cipherList using it's name
* example: GetCipher("javax.crypto.Cipher@b6859ee")
doUpdate(cipherName, bytes)
* Call doUpdate on Cipher instance from cipherList using it's name
* you can pass buffer into it which will be processed.
* The bytes buffer must be Java [B
* Example: doUpdate('javax.crypto.Cipher@b6859ee', buffer)
doFinal(cipherName)
* Call doFinal on Cipher instance from cipherList using it's name
* Example: doFinal('javax.crypto.Cipher@b6859ee')
*/
console.log("Cipher hooks loaded!");
Java.perform(function () {
hookCipherGetInstance();
hookCipherGetInstance2();
hookCipherGetInstance3();
hookCipherInit();
hookCipherInit2();
hookCipherInit3();
hookCipherInit4();
hookCipherInit5();
hookCipherInit6();
hookCipherInit7();
hookCipherInit8();
hookDoFinal();
hookDoFinal2();
hookDoFinal3();
hookDoFinal4();
hookDoFinal5();
hookDoFinal6();
hookDoFinal7();
hookUpdate();
hookUpdate2();
hookUpdate3();
hookUpdate4();
hookUpdate5();
});
var cipherList = [];
var StringCls = null;
Java.perform(function () {
StringCls = Java.use('java.lang.String');
});
/*
.overload('java.lang.String')
.overload('java.lang.String', 'java.security.Provider')
.overload('java.lang.String', 'java.lang.String')
*/
function hookCipherGetInstance() {
var cipherGetInstance = Java.use('javax.crypto.Cipher')['getInstance'].overload("java.lang.String");
cipherGetInstance.implementation = function (type) {
console.log("[Cipher.getInstance()]: type: " + type);
var tmp = this.getInstance(type);
console.log("[Cipher.getInstance()]: cipherObj: " + tmp);
cipherList.push(tmp);
return tmp;
}
}
function hookCipherGetInstance2() {
var cipherGetInstance = Java.use('javax.crypto.Cipher')['getInstance'].overload('java.lang.String', 'java.security.Provider');
cipherGetInstance.implementation = function (transforamtion, provider) {
console.log("[Cipher.getInstance2()]: transforamtion: " + transforamtion + ", provider: " + provider);
var tmp = this.getInstance(transforamtion, provider);
console.log("[Cipher.getInstance2()]: cipherObj: " + tmp);
cipherList.push(tmp);
return tmp;
}
}
function hookCipherGetInstance3() {
var cipherGetInstance = Java.use('javax.crypto.Cipher')['getInstance'].overload('java.lang.String', 'java.lang.String');
cipherGetInstance.implementation = function (transforamtion, provider) {
console.log("[Cipher.getInstance3()]: transforamtion: " + transforamtion + ", provider: " + provider);
var tmp = this.getInstance(transforamtion, provider);
console.log("[Cipher.getInstance3()]: cipherObj: " + tmp);
cipherList.push(tmp);
return tmp;
}
}
/*
.overload('int', 'java.security.cert.Certificate')
.overload('int', 'java.security.Key')
.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters')
//.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec')
.overload('int', 'java.security.cert.Certificate', 'java.security.SecureRandom')
.overload('int', 'java.security.Key', 'java.security.SecureRandom')
.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec', 'java.security.SecureRandom')
.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters', 'java.security.SecureRandom')
*/
function hookCipherInit() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.cert.Certificate');
cipherInit.implementation = function (mode, cert) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", cert: " + cert + " , cipherObj: " + this);
var tmp = this.init(mode, cert);
}
}
function hookCipherInit2() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key');
cipherInit.implementation = function (mode, secretKey) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey);
}
}
function hookCipherInit3() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key', 'java.security.AlgorithmParameters');
cipherInit.implementation = function (mode, secretKey, alParam) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " alParam:" + alParam + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey, alParam);
}
}
function hookCipherInit4() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec');
cipherInit.implementation = function (mode, secretKey, spec) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " spec:" + spec + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey, spec);
}
}
function hookCipherInit5() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.cert.Certificate', 'java.security.SecureRandom');
cipherInit.implementation = function (mode, cert, secureRandom) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", cert: " + cert + " secureRandom:" + secureRandom + " , cipherObj: " + this);
var tmp = this.init(mode, cert, secureRandom);
}
}
function hookCipherInit6() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key', 'java.security.SecureRandom');
cipherInit.implementation = function (mode, secretKey, secureRandom) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " secureRandom:" + secureRandom + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey, secureRandom);
}
}
function hookCipherInit7() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec', 'java.security.SecureRandom');
cipherInit.implementation = function (mode, secretKey, spec, secureRandom) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " spec:" + spec + " secureRandom: " + secureRandom + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey, spec, secureRandom);
}
}
function hookCipherInit8() {
var cipherInit = Java.use('javax.crypto.Cipher')['init'].overload('int', 'java.security.Key', 'java.security.AlgorithmParameters', 'java.security.SecureRandom');
cipherInit.implementation = function (mode, secretKey, alParam, secureRandom) {
console.log("[Cipher.init()]: mode: " + decodeMode(mode) + ", secretKey: " + secretKey.$className + " alParam:" + alParam + " secureRandom: " + secureRandom + " , cipherObj: " + this);
var tmp = this.init(mode, secretKey, alParam, secureRandom);
}
}
/*
.overload()
.overload('[B')
.overload('[B', 'int')
.overload('java.nio.ByteBuffer', 'java.nio.ByteBuffer')
.overload('[B', 'int', 'int')
.overload('[B', 'int', 'int', '[B')
.overload('[B', 'int', 'int', '[B', 'int')
*/
function hookDoFinal() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload();
cipherInit.implementation = function () {
console.log("[Cipher.doFinal()]: " + " cipherObj: " + this);
var tmp = this.doFinal();
dumpByteArray('Result', tmp);
return tmp;
}
}
function hookDoFinal2() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('[B');
cipherInit.implementation = function (byteArr) {
console.log("[Cipher.doFinal2()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.doFinal(byteArr);
dumpByteArray('Result', tmp);
return tmp;
}
}
function hookDoFinal3() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('[B', 'int');
cipherInit.implementation = function (byteArr, a1) {
console.log("[Cipher.doFinal3()]: " + " cipherObj: " + this);
dumpByteArray('Out buffer', byteArr);
var tmp = this.doFinal(byteArr, a1);
dumpByteArray('Out buffer', byteArr);
return tmp;
}
}
function hookDoFinal4() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('java.nio.ByteBuffer', 'java.nio.ByteBuffer');
cipherInit.implementation = function (a1, a2) {
console.log("[Cipher.doFinal4()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', a1.array());
var tmp = this.doFinal(a1, a2);
dumpByteArray('Out buffer', a2.array());
return tmp;
}
}
function hookDoFinal5() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('[B', 'int', 'int');
cipherInit.implementation = function (byteArr, a1, a2) {
console.log("[Cipher.doFinal5()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.doFinal(byteArr, a1, a2);
dumpByteArray('Out buffer', tmp);
return tmp;
}
}
function hookDoFinal6() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('[B', 'int', 'int', '[B');
cipherInit.implementation = function (byteArr, a1, a2, outputArr) {
console.log("[Cipher.doFinal6()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.doFinal(byteArr, a1, a2, outputArr);
dumpByteArray('Out buffer', outputArr);
return tmp;
}
}
function hookDoFinal7() {
var cipherInit = Java.use('javax.crypto.Cipher')['doFinal'].overload('[B', 'int', 'int', '[B', 'int');
cipherInit.implementation = function (byteArr, a1, a2, outputArr, a4) {
console.log("[Cipher.doFinal7()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.doFinal(byteArr, a1, a2, outputArr, a4);
dumpByteArray('Out buffer', outputArr);
return tmp;
}
}
/*
.overload('[B')
.overload('java.nio.ByteBuffer', 'java.nio.ByteBuffer')
.overload('[B', 'int', 'int')
.overload('[B', 'int', 'int', '[B')
.overload('[B', 'int', 'int', '[B', 'int')
*/
function hookUpdate() {
var cipherInit = Java.use('javax.crypto.Cipher')['update'].overload('[B');
cipherInit.implementation = function (byteArr) {
console.log("[Cipher.update()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.update(byteArr);
dumpByteArray('Out buffer', tmp);
return tmp;
}
}
function hookUpdate2() {
var cipherInit = Java.use('javax.crypto.Cipher')['update'].overload('java.nio.ByteBuffer', 'java.nio.ByteBuffer');
cipherInit.implementation = function (byteArr, outputArr) {
console.log("[Cipher.update2()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr.array());
var tmp = this.update(byteArr, outputArr);
dumpByteArray('Out buffer', outputArr.array());
return tmp;
}
}
function hookUpdate3() {
var cipherInit = Java.use('javax.crypto.Cipher')['update'].overload('[B', 'int', 'int');
cipherInit.implementation = function (byteArr, a1, a2) {
console.log("[Cipher.update3()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.update(byteArr, a1, a2);
dumpByteArray('Out buffer', tmp);
return tmp;
}
}
function hookUpdate4() {
var cipherInit = Java.use('javax.crypto.Cipher')['update'].overload('[B', 'int', 'int', '[B');
cipherInit.implementation = function (byteArr, a1, a2, outputArr) {
console.log("[Cipher.update4()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.update(byteArr, a1, a2, outputArr);
dumpByteArray('Out buffer', outputArr);
return tmp;
}
}
function hookUpdate5() {
var cipherInit = Java.use('javax.crypto.Cipher')['update'].overload('[B', 'int', 'int', '[B', 'int');
cipherInit.implementation = function (byteArr, a1, a2, outputArr, a4) {
console.log("[Cipher.update5()]: " + " cipherObj: " + this);
dumpByteArray('In buffer', byteArr);
var tmp = this.update(byteArr, a1, a2, outputArr, a4);
dumpByteArray('Out buffer', outputArr);
return tmp;
}
}
/*
* List Cipher instances collected in cipherList
*/
function ListCiphers() {
Java.perform(function () {
for (var i = 0; i < cipherList.length; i++) {
console.log("[" + i + "] " + cipherList[i]);
}
});
return "[done]";
}
/*
* Get Cipher instance from cipherList using it's name e.g. Cipger.toString() (like: Cipher@a0b1c2)
* Example: GetCipher('Cipher@a0b1c2')
*/
function GetCipher(cipherName) {
var result = null;
Java.perform(function () {
for (var i = 0; i < cipherList.length; i++) {
if (cipherName.localeCompare("" + cipherList[i]) == 0)
result = cipherList[i];
}
});
return result;
}
/*
* Call doUpdate on Cipher instance from cipherList using it's name e.g. Cipger.toString() (like: Cipher@a0b1c2), you can pass buffer into it which will be processed.
* The bytes buffer must be Java [B
* Example: doUpdate('Cipher@a0b1c2', buffer)
*/
function doUpdate(cipherName, bytes) {
Java.perform(function () {
var cipher = GetCipher(cipherName);
cipher.update(bytes);
//cipher.doFinal();
});
}
/*
* Call doFinal on Cipher instance from cipherList using it's name e.g. Cipger.toString() (like: Cipher@a0b1c2)
* Example: doFinal('Cipher@a0b1c2')
*/
function doFinal(cipherName) {
Java.perform(function () {
var cipher = GetCipher(cipherName);
cipher.final(bytes);
//cipher.doFinal();
});
}
function decodeMode(mode) {
if (mode == 1)
return "Encrypt mode";
else if (mode == 2)
return "Decrypt mode";
else if (mode == 3)
return "Wrap mode";
else if (mode == 4)
return "Unwrap mode";
}
/* All below is hexdump implementation*/
function dumpByteArray(title, byteArr) {
if (byteArr != null) {
try {
var buff = new ArrayBuffer(byteArr.length)
var dtv = new DataView(buff)
for (var i = 0; i < byteArr.length; i++) {
dtv.setUint8(i, byteArr[i]); // Frida sucks sometimes and returns different byteArr.length between ArrayBuffer(byteArr.length) and for(..; i < byteArr.length;..). It occured even when Array.copyOf was done to work on copy.
}
console.log(title + ":\n");
console.log(hexdumpJS(dtv.buffer, 0, byteArr.length))
} catch (error) { console.log("Exception has occured in hexdump") }
}
else {
console.log("byteArr is null!");
}
}
function _fillUp(value, count, fillWith) {
var l = count - value.length;
var ret = "";
while (--l > -1)
ret += fillWith;
return ret + value;
}
function hexdumpJS(arrayBuffer, offset, length) {
var view = new DataView(arrayBuffer);
offset = offset || 0;
length = length || arrayBuffer.byteLength;
var out = _fillUp("Offset", 8, " ") + " 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n";
var row = "";
for (var i = 0; i < length; i += 16) {
row += _fillUp(offset.toString(16).toUpperCase(), 8, "0") + " ";
var n = Math.min(16, length - offset);
var string = "";
for (var j = 0; j < 16; ++j) {
if (j < n) {
var value = view.getUint8(offset);
string += (value >= 32 && value < 128) ? String.fromCharCode(value) : ".";
row += _fillUp(value.toString(16).toUpperCase(), 2, "0") + " ";
offset++;
}
else {
row += " ";
string += " ";
}
}
row += " " + string + "\n";
}
out += row;
return out;
};