-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfrida-java-crypto-spy.js
More file actions
139 lines (124 loc) · 4.35 KB
/
Copy pathfrida-java-crypto-spy.js
File metadata and controls
139 lines (124 loc) · 4.35 KB
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
Java.perform(() => {
const Cipher = Java.use("javax.crypto.Cipher");
const SecretKeySpec = Java.use("javax.crypto.spec.SecretKeySpec");
const IvParameterSpec = Java.use("javax.crypto.spec.IvParameterSpec");
const GCMParameterSpec = Java.use("javax.crypto.spec.GCMParameterSpec");
const Base64 = Java.use("android.util.Base64");
const Arrays = Java.use("java.util.Arrays");
function encodeBytes(bytes) {
try {
const jsStr = Java.use("java.lang.String").$new(bytes, "UTF-8") + "";
return /^[\x20-\x7E\r\n\t]*$/.test(jsStr)
? jsStr
: Base64.encodeToString(bytes, 0).toString();
} catch (_) {
return Base64.encodeToString(bytes, 0).toString();
}
}
function logObj(title, obj) {
const green = "\x1b[32m", blue = "\x1b[34m", yellow = "\x1b[33m", reset = "\x1b[0m";
console.log(`${yellow}[${title}]${reset}`);
for (const [key, val] of Object.entries(obj)) {
if (val != null)
console.log(` ${blue}${key}${reset}: ${green}${val}${reset}`);
}
console.log('');
}
function isNestedInit() {
const ExceptionCls = Java.use("java.lang.Exception");
const frames = ExceptionCls.$new().getStackTrace();
let hits = 0;
for (let i = 0; i < frames.length; i++) {
if (frames[i].getClassName() === "javax.crypto.Cipher" &&
frames[i].getMethodName() === "init") {
if (++hits > 1) return true;
}
}
return false;
}
const ctx = new WeakMap();
Cipher.init.overloads.forEach(o => {
o.implementation = function () {
// اگه کال داخلیه، لاگ نگیر
if (isNestedInit())
return o.apply(this, arguments);
// ----- لاگگیری عادی -----
const op = arguments[0].valueOf();
const mode = op === Cipher.ENCRYPT_MODE.value ? "ENCRYPT"
: op === Cipher.DECRYPT_MODE.value ? "DECRYPT" : op;
let keyStr;
try {
keyStr = encodeBytes(Java.cast(arguments[1], SecretKeySpec).getEncoded());
} catch (_) {
keyStr = arguments[1].getAlgorithm() + " (non‑SecretKeySpec)";
}
let iv = null, tagLength = null;
for (let i = 2; i < arguments.length; i++) {
const p = arguments[i];
if (!p) continue;
if (GCMParameterSpec.class.isInstance(p)) {
const g = Java.cast(p, GCMParameterSpec);
iv = encodeBytes(g.getIV());
tagLength = g.getTLen();
} else if (IvParameterSpec.class.isInstance(p)) {
iv = encodeBytes(Java.cast(p, IvParameterSpec).getIV());
}
}
logObj("Cipher.init", {
transformation: this.getAlgorithm(),
mode,
key: keyStr,
iv,
tagLength
});
return o.apply(this, arguments);
};
});
function grabBytes(args, idx) {
if (args.length <= idx || !args[idx]) return null;
if (args.length >= idx + 3 &&
typeof args[idx + 1] === "number" &&
typeof args[idx + 2] === "number") {
return encodeBytes(Arrays.copyOfRange(args[idx], args[idx + 1], args[idx + 1] + args[idx + 2]));
}
return encodeBytes(args[idx]);
}
Cipher.update.overloads.forEach(o => {
o.implementation = function () {
const out = o.apply(this, arguments);
const c = ctx.get(this) || {};
logObj("Cipher.update", {
transformation: c.transformation,
mode: c.mode,
input: grabBytes(arguments, 0)
});
return out;
};
});
Cipher.doFinal.overloads.forEach(o => {
o.implementation = function () {
const result = o.apply(this, arguments);
const c = ctx.get(this) || {};
logObj("Cipher.doFinal", {
transformation: c.transformation,
mode: c.mode,
input: grabBytes(arguments, 0),
output: encodeBytes(result)
});
return result;
};
});
if (Cipher.updateAAD) {
Cipher.updateAAD.overloads.forEach(o => {
o.implementation = function () {
const c = ctx.get(this) || {};
logObj("Cipher.updateAAD", {
transformation: c.transformation,
mode: c.mode,
aad: grabBytes(arguments, 0)
});
return o.apply(this, arguments);
};
});
}
});