Repository navigation
Expand file tree
/
Copy pathjava_guard.ts
More file actions
295 lines (266 loc) · 10.6 KB
/
Copy pathjava_guard.ts
File metadata and controls
295 lines (266 loc) · 10.6 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
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
import Java from "frida-java-bridge";
const SHIELD_PACKAGE = "pkbpqnzc.";
const protectedPid = Process.id;
let nativeGuard: CModule | null = null;
let sentinelFaultCount = 0;
let reportGuardInstalled = false;
let reportGuardInstalling = false;
const guardStatus = {
installed: false,
directGuardInstalled: false,
directAttempts: 0,
suppressed: false,
className: null as string | null,
message: null as string | null,
detail: null as string | null,
};
function installReportGuardFor(loader: Java.Wrapper): void {
if (reportGuardInstalled || reportGuardInstalling) return;
reportGuardInstalling = true;
try {
const factory = Java.ClassFactory.get(loader);
const Report = factory.use("pkbpqnzc.aG");
const report = Report.a.overload("java.lang.String");
report.implementation = function (code: Java.Wrapper) {
guardStatus.suppressed = true;
guardStatus.className = "pkbpqnzc.aG";
guardStatus.message = code === null ? "" : code.toString();
guardStatus.detail = "direct protection report suppressed";
send(`[JAVA-GUARD] protection report suppressed: ${code}`);
try {
Java.vm.getEnv().exceptionClear();
} catch (_) {}
};
reportGuardInstalled = true;
guardStatus.directGuardInstalled = true;
send("[JAVA-GUARD] direct report guard ready");
} catch (error) {
guardStatus.detail = `direct report guard pending: ${error}`;
} finally {
reportGuardInstalling = false;
}
}
function discoverReportGuard(): void {
if (reportGuardInstalled || !Java.available) return;
Java.performNow(() => {
guardStatus.directAttempts++;
const loaders = Java.enumerateClassLoadersSync();
for (const loader of loaders) {
installReportGuardFor(loader);
if (reportGuardInstalled) return;
}
});
}
function installSentinelFaultGuard(): void {
Process.setExceptionHandler((details) => {
try {
const memory = details.memory;
if (
details.type !== "access-violation" ||
memory === undefined ||
memory.address.compare(ptr("0x7fff0000")) < 0 ||
memory.address.compare(ptr("0x80010000")) > 0 ||
sentinelFaultCount >= 64
) {
return false;
}
sentinelFaultCount++;
const context = details.context as CpuContext & {
pc: NativePointer;
sp: NativePointer;
rax?: NativePointer;
};
const faultPc = context.pc;
const instruction = Instruction.parse(faultPc);
const module = Process.findModuleByAddress(faultPc);
const location = module === null
? "unknown"
: `${module.name}+${faultPc.sub(module.base)}`;
send(
`[FAULT-GUARD] sentinel #${sentinelFaultCount} ` +
`address=${memory.address} pc=${faultPc} ` +
`op=${memory.operation} at=${location} insn=${instruction}`
);
// A branch to the sentinel has to be unwound like a normal return.
// A load/store touching it can safely be skipped instruction-wise.
if (faultPc.equals(memory.address) && Process.arch === "x64") {
context.pc = context.sp.readPointer();
context.sp = context.sp.add(Process.pointerSize);
} else {
if (Process.arch === "x64" && context.rax !== undefined) {
context.rax = ptr(0);
}
context.pc = instruction.next;
}
return true;
} catch (error) {
send(`[FAULT-GUARD] recovery failed: ${error}`);
return false;
}
});
send("[FAULT-GUARD] ready");
}
function findLibcExport(name: string): NativePointer | null {
try {
return Process.getModuleByName("libc.so").getExportByName(name);
} catch (_) {
return null;
}
}
function installNativeTerminationGuard(): void {
const killAddress = findLibcExport("kill");
const tgkillAddress = findLibcExport("tgkill");
const ptraceAddress = findLibcExport("ptrace");
if (killAddress === null || tgkillAddress === null || ptraceAddress === null) {
send("[NATIVE-GUARD] required libc exports are unavailable");
return;
}
const source = `
#include <stdint.h>
extern int original_kill(int pid, int sig);
extern int original_tgkill(int tgid, int tid, int sig);
extern long original_ptrace(long request, long pid, void *addr, void *data);
#define PROTECTED_PID ${protectedPid}
#define PTRACE_CONT 7
#define PTRACE_KILL 8
#define PTRACE_SINGLESTEP 9
static int is_fatal_signal(long sig) {
return sig == 6 || sig == 9 || sig == 11;
}
int guarded_kill(int pid, int sig) {
if ((pid == PROTECTED_PID || pid == 0) && is_fatal_signal(sig))
return 0;
return original_kill(pid, sig);
}
int guarded_tgkill(int tgid, int tid, int sig) {
if (tgid == PROTECTED_PID && is_fatal_signal(sig))
return 0;
return original_tgkill(tgid, tid, sig);
}
long guarded_ptrace(long request, long pid, void *addr, void *data) {
long sig = (long) data;
if (pid == PROTECTED_PID) {
if (request == PTRACE_KILL)
return 0;
if ((request == PTRACE_CONT || request == PTRACE_SINGLESTEP) &&
is_fatal_signal(sig))
data = 0;
}
return original_ptrace(request, pid, addr, data);
}
`;
try {
nativeGuard = new CModule(source, {
original_kill: killAddress,
original_tgkill: tgkillAddress,
original_ptrace: ptraceAddress,
});
Interceptor.replace(killAddress, nativeGuard.guarded_kill);
Interceptor.replace(tgkillAddress, nativeGuard.guarded_tgkill);
Interceptor.replace(ptraceAddress, nativeGuard.guarded_ptrace);
send(`[NATIVE-GUARD] ready for PID ${protectedPid}`);
} catch (error) {
send(`[NATIVE-GUARD] setup failed: ${error}`);
}
}
function installShieldExceptionGuard(): void {
if (!Java.available) {
send("[JAVA-GUARD] ART is not available");
return;
}
// This only touches a boot-class method, so it can be installed while the
// spawned app is still suspended. Java.perform() waits for the app class
// loader and would lose the race with the protection's JNI_OnLoad path.
Java.performNow(() => {
try {
const ClassLoader = Java.use("java.lang.ClassLoader");
const loadClass = ClassLoader.loadClass.overload(
"java.lang.String",
"boolean"
);
loadClass.implementation = function (name: Java.Wrapper, resolve: boolean) {
const loaded = loadClass.call(this, name, resolve);
if (name.toString() === "pkbpqnzc.aG") {
installReportGuardFor(this);
}
return loaded;
};
const loadClassSimple = ClassLoader.loadClass.overload("java.lang.String");
loadClassSimple.implementation = function (name: Java.Wrapper) {
const loaded = loadClassSimple.call(this, name);
if (name.toString() === "pkbpqnzc.aG") {
installReportGuardFor(this);
}
return loaded;
};
const BaseDexClassLoader = Java.use("dalvik.system.BaseDexClassLoader");
const findClass = BaseDexClassLoader.findClass.overload("java.lang.String");
findClass.implementation = function (name: Java.Wrapper) {
const loaded = findClass.call(this, name);
if (name.toString() === "pkbpqnzc.aG") {
installReportGuardFor(this);
}
return loaded;
};
const KillApplicationHandler = Java.use(
"com.android.internal.os.RuntimeInit$KillApplicationHandler"
);
const uncaughtException = KillApplicationHandler.uncaughtException.overload(
"java.lang.Thread",
"java.lang.Throwable"
);
uncaughtException.implementation = function (thread: Java.Wrapper, error: Java.Wrapper) {
let className = "";
let message = "";
try {
className = error.getClass().getName().toString();
} catch (_) {}
try {
const value = error.getMessage();
message = value === null ? "" : value.toString();
} catch (_) {}
send(`[JAVA-GUARD] uncaught ${className}: ${message}`);
if (className.startsWith(SHIELD_PACKAGE)) {
guardStatus.suppressed = true;
guardStatus.className = className;
guardStatus.message = message;
guardStatus.detail =
"protection exception returned without invoking app kill";
try {
Java.vm.getEnv().exceptionClear();
} catch (_) {}
send("[JAVA-GUARD] protection exception suppressed");
return;
}
return uncaughtException.call(this, thread, error);
};
guardStatus.installed = true;
guardStatus.detail = "selective Java report guard installed";
send("[JAVA-GUARD] ready");
} catch (error) {
guardStatus.detail = String(error);
send(`[JAVA-GUARD] setup failed: ${error}`);
}
});
}
installShieldExceptionGuard();
installSentinelFaultGuard();
discoverReportGuard();
const directGuardStartedAt = Date.now();
const directGuardTimer = setInterval(() => {
try {
discoverReportGuard();
} catch (error) {
guardStatus.detail = `direct report guard retry failed: ${error}`;
}
if (reportGuardInstalled || Date.now() - directGuardStartedAt > 10000) {
clearInterval(directGuardTimer);
if (!reportGuardInstalled)
send(`[JAVA-GUARD] direct report guard timed out: ${guardStatus.detail}`);
}
}, 5);
rpc.exports = {
status() {
return guardStatus;
},
};