-
Notifications
You must be signed in to change notification settings - Fork 32
/
android-aesinfo-and-hashing-decryptor.js
379 lines (329 loc) · 14.4 KB
/
android-aesinfo-and-hashing-decryptor.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
/*
Description: Android AES and Hashing observer
Usage: frida -U -f XXX -l android-aesinfo-and-hashing-decryptor.js
Credit: masihyeganeh
Links:
https://developer.android.com/reference/java/lang/RuntimeException
https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec
https://developer.android.com/reference/javax/crypto/spec/IvParameterSpec
https://developer.android.com/reference/javax/crypto/Cipher
https://developer.android.com/reference/java/security/MessageDigest
https://developer.android.com/reference/java/util/Base64
*/
Java.perform(function()
{
var use_single_byte = false;
var complete_bytes = new Array();
var index = 0;
var RuntimeException = Java.use('java.lang.RuntimeException');
var secretKeySpecDef = Java.use('javax.crypto.spec.SecretKeySpec');
var ivParameterSpecDef = Java.use('javax.crypto.spec.IvParameterSpec');
var cipherDef = Java.use('javax.crypto.Cipher');
var messageDigestDef = Java.use('java.security.MessageDigest');
var b64Def = Java.use('android.util.Base64');
var cipherDoFinal_1 = cipherDef.doFinal.overload();
var cipherDoFinal_2 = cipherDef.doFinal.overload('[B');
var cipherDoFinal_3 = cipherDef.doFinal.overload('[B', 'int');
var cipherDoFinal_4 = cipherDef.doFinal.overload('[B', 'int', 'int');
var cipherDoFinal_5 = cipherDef.doFinal.overload('[B', 'int', 'int', '[B');
var cipherDoFinal_6 = cipherDef.doFinal.overload('[B', 'int', 'int', '[B', 'int');
var cipherUpdate_1 = cipherDef.update.overload('[B');
var cipherUpdate_2 = cipherDef.update.overload('[B', 'int', 'int');
var cipherUpdate_3 = cipherDef.update.overload('[B', 'int', 'int', '[B');
var cipherUpdate_4 = cipherDef.update.overload('[B', 'int', 'int', '[B', 'int');
var cipherGetInstance = cipherDef.getInstance.overload('java.lang.String');
var messageDigestGetInstance = messageDigestDef.getInstance.overload('java.lang.String');
var messageDigestDigest_1 = messageDigestDef.digest.overload();
var messageDigestDigest_2 = messageDigestDef.digest.overload('[B');
var messageDigestDigest_3 = messageDigestDef.digest.overload('[B', 'int', 'int');
var messageDigestUpdate_1 = messageDigestDef.update.overload('[B');
var messageDigestUpdate_2 = messageDigestDef.update.overload('[B', 'int', 'int');
var b64DefEncode_2 = b64Def.encode.overload('[B', 'int');
var b64DefEncode_3 = b64Def.encode.overload('[B', 'int', 'int', 'int');
var b64DefEncodeToString_2 = b64Def.encodeToString.overload('[B', 'int');
var b64DefEncodeToString_3 = b64Def.encodeToString.overload('[B', 'int', 'int', 'int');
var b64DefDecode_1 = b64Def.decode.overload('java.lang.String', 'int');
var b64DefDecode_2 = b64Def.decode.overload('[B', 'int');
var b64DefDecode_3 = b64Def.decode.overload('[B', 'int', 'int', 'int');
var secretKeySpecDef_init_1 = secretKeySpecDef.$init.overload('[B', 'java.lang.String');
var secretKeySpecDef_init_2 = secretKeySpecDef.$init.overload('[B', 'int', 'int', 'java.lang.String');
var ivParameterSpecDef_init_1 = ivParameterSpecDef.$init.overload('[B');
var ivParameterSpecDef_init_2 = ivParameterSpecDef.$init.overload('[B', 'int', 'int');
/*
secretKeySpecDef_init_1.implementation = function(arr, alg) {
send("Creating " + alg + " secret key, plaintext:", new Uint8Array(arr));
return secretKeySpecDef_init_1.call(this, arr, alg);
}
secretKeySpecDef_init_2.implementation = function(arr, off, len, alg) {
send("Creating " + alg + " secret key, plaintext:", new Uint8Array(arr));
return secretKeySpecDef_init_2.call(this, arr, off, len, alg);
}
cipherGetInstance.implementation = function(alg) {
send("Creating " + alg + " cipher");
return cipherGetInstance.call(this, alg);
}
*/
/*
messageDigestGetInstance.implementation = function(alg) {
send("Creating " + alg + " hash");
return messageDigestGetInstance.call(this, alg);
}
messageDigestDigest_1.implementation = function() {
send("Hashing");
return messageDigestDigest_1.call(this);
}
messageDigestDigest_2.implementation = function(arr) {
send("Hashing: ~ " + this.getAlgorithm(), new Uint8Array(arr));
return messageDigestDigest_2.call(this, arr);
}
*/
messageDigestDigest_3.implementation = function(arr, off, len)
{
send("Hashing: [" + off + "," + len + "] ~ " + this.getAlgorithm(), new Uint8Array(arr));
return messageDigestDigest_3.call(this, arr, off, len);
}
messageDigestUpdate_1.implementation = function(arr)
{
send("Updating hash: " + this.getAlgorithm(), new Uint8Array(arr));
return messageDigestUpdate_1.call(this, arr);
}
/*
messageDigestUpdate_2.implementation = function(arr, off, len)
{
send("Updating hash: [" + off + "," + len + "] ~ " + this.getAlgorithm(), new Uint8Array(arr));
return messageDigestUpdate_2.call(this, arr, off, len);
}
ivParameterSpecDef_init_1.implementation = function(arr)
{
send("Creating IV:\\n", new Uint8Array(arr));
return ivParameterSpecDef_init_1.call(this, arr);
}
ivParameterSpecDef_init_2.implementation = function(arr, off, len)
{
send("Creating IV, plaintext:\\n", new Uint8Array(arr));
return ivParameterSpecDef_init_2.call(this, arr, off, len);
}
*/
b64DefEncode_2.implementation = function(arr, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 Encode: ", new Uint8Array(arr));
return b64DefEncode_2.call(this, arr, flag);
}
b64DefEncode_3.implementation = function(arr, off, len, flag)
{
Java.perform(function() {
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 Encode: [" + off + "," + len + "]", new Uint8Array(arr));
return b64DefEncode_3.call(this, arr, off, len, flag);
}
b64DefEncodeToString_2.implementation = function(arr, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 EncodeToString: ", new Uint8Array(arr));
return b64DefEncodeToString_2.call(this, arr, flag);
}
b64DefEncodeToString_3.implementation = function(arr, off, len, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 EncodeToString: [" + off + "," + len + "]", new Uint8Array(arr));
return b64DefEncodeToString_3.call(this, arr, off, len, flag);
}
b64DefDecode_1.implementation = function(str, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 Decode: " + str);
return b64DefDecode_1.call(this, str, flag);
}
b64DefDecode_2.implementation = function(arr, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 Decode: ", new Uint8Array(arr));
return b64DefDecode_2.call(this, arr, flag);
}
b64DefDecode_3.implementation = function(arr, off, len, flag)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("Base64 Decode: [" + off + "," + len + "]", new Uint8Array(arr));
return b64DefDecode_3.call(this, arr, off, len, flag);
}
cipherDoFinal_1.implementation = function()
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_1 ~ " + this.getAlgorithm() + " ~ " + this.getIV());
var ret = cipherDoFinal_1.call(this);
send("- cipherDoFinal_1 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, ret);
return ret;
}
cipherDoFinal_2.implementation = function(arr)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_2: ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
var ret = cipherDoFinal_2.call(this, arr);
send("- cipherDoFinal_2 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, ret);
return ret;
}
cipherDoFinal_3.implementation = function(arr, a)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_3: output ~ " + this.getAlgorithm() + " ~ " + this.getIV());
addtoarray(arr);
var ret = cipherDoFinal_3.call(this, arr, a);
send("- cipherDoFinal_3 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, ret);
return ret;
}
cipherDoFinal_4.implementation = function(arr, a, b)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_4: [" + b + "," + a + "] ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
var ret = cipherDoFinal_4.call(this, arr, a, b);
send("- cipherDoFinal_4 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, ret);
return ret;
}
cipherDoFinal_5.implementation = function(arr, a, b, c)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_5: [" + b + "," + a + "] ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
var ret = cipherDoFinal_5.call(this, arr, a, b, c);
send("- cipherDoFinal_5 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, ret);
return ret;
}
cipherDoFinal_6.implementation = function(arr, a, b, c, d)
{
Java.perform(function()
{
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
send("- cipherDoFinal_6: [" + b + "," + a + "] -> " + d + " ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
var ret = cipherDoFinal_6.call(this, arr, a, b, c, d);
send("- cipherDoFinal_6 ret :", new Uint8Array(ret));
info(this.getIV(), this.getAlgorithm(), complete_bytes, c);
return ret;
}
cipherUpdate_1.implementation = function(arr)
{
send("- cipherUpdate_1: ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
return cipherUpdate_1.call(this, arr);
}
cipherUpdate_2.implementation = function(arr, a, b)
{
send("- cipherUpdate_2: [" + b + "," + a + "] ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
return cipherUpdate_2.call(this, arr, a, b);
}
cipherUpdate_3.implementation = function(arr, a, b, c)
{
send("- cipherUpdate_3: [" + b + "," + a + "] ~ " + this.getAlgorithm() + " ~ " + this.getIV(), new Uint8Array(arr));
addtoarray(arr);
return cipherUpdate_3.call(this, arr, a, b, c);
}
cipherUpdate_4.implementation = function(arr, a, b, c, d)
{
send("- cipherUpdate_4: [" + b + "," + a + "] -> " + d + " ~ " + this.getAlgorithm() + " ~ " + this.getParameters(), new Uint8Array(arr));
addtoarray(arr);
return cipherUpdate_4.call(this, arr, a, b, c, d);
}
function info(iv, alg, plain, encoded)
{
send("Performing encryption/decryption");
if (iv)
{
send("Initialization Vector: \\n" + hexdump(b2s(iv)));
}
else
{
send("Initialization Vector: " + iv);
}
send("Algorithm: " + alg);
send("In: \\n" + hexdump(b2s(plain)));
send("Out: \\n" + hexdump(b2s(encoded)));
complete_bytes = [];
index = 0;
}
function hexdump(buffer, blockSize)
{
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
for (var b = 0; b < buffer.length; b += blockSize)
{
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.split('').map(function(ch)
{
var code = ch.charCodeAt(0);
return " " + hex[(0xF0 & code) >> 4] + hex[0x0F & code];
}).join("");
codes += " ".repeat(blockSize - block.length);
var chars = block.replace(/[\\x00-\\x1F\\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
lines.push(addr + " " + codes + " " + chars);
}
return lines.join("\\n");
}
function b2s(array)
{
var result = "";
for (var i = 0; i < array.length; i++)
{
result += String.fromCharCode(modulus(array[i], 256));
}
return result;
}
function modulus(x, n)
{
return ((x % n) + n) % n;
}
function addtoarray(arr)
{
for (var i = 0; i < arr.length; i++)
{
complete_bytes[index] = arr[i];
index = index + 1;
}
}
});