-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsilkhook_kmod.c
More file actions
500 lines (405 loc) · 19.7 KB
/
silkhook_kmod.c
File metadata and controls
500 lines (405 loc) · 19.7 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* silkhook - miniature arm hooking lib
* silkhook_kmod - kernel module test
*
* SPDX-License-Identifier: MIT
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/workqueue.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <asm/unistd.h>
#include "include/silkhook.h"
#include "platform/kernel/ksyms.h"
#include "platform/kernel/hide.h"
#include "platform/kernel/memory.h"
#include "platform/kernel/svc.h"
#include "platform/kernel/shadow.h"
#include "platform/kernel/elb.h"
#include "platform/kernel/ich.h"
#include "platform/kernel/apr.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("vmsplit");
MODULE_DESCRIPTION("silkhook test");
static struct delayed_work setup_work;
static struct silkhook_svc_hook __svc_hook;
static struct silkhook_hidden_mod __hidden;
static struct silkhook_elb_hook __elb_hook;
static struct silkhook_ich_hook __ich_hook;
static struct silkhook_apr_hook __apr_hook;
static unsigned int __trigger_count;
static uint32_t apr_shellcode[] = {
0xd280a720, /* mov x0, #0x539 (1337) */
0xd65f03c0, /* ret */
};
/* ─────────────────────────────────────────────────────────────────────────────
* svc hook test (shadow tbl)
* ───────────────────────────────────────────────────────────────────────────── */
asmlinkage long hooked_getuid(void);
asmlinkage long hooked_getuid(void)
{
__trigger_count++;
if (__trigger_count % 10 == 1)
pr_info("silkhook: [svc] getuid called !! count=%u\n", __trigger_count);
return current_uid().val;
}
/* ─────────────────────────────────────────────────────────────────────────────
* elb hook test (exception bounce)
* ───────────────────────────────────────────────────────────────────────────── */
static unsigned int __elb_count;
static int __elb_stack_dumped;
static void elb_test_handler(struct pt_regs *regs, struct silkhook_elb_hook *ctx)
{
__elb_count++;
if (!__elb_stack_dumped)
{
__elb_stack_dumped = 1;
pr_info("silkhook: --------------------------------------------\n");
pr_info("silkhook: [elb] callstack proof\n");
pr_info("silkhook: [elb] proving hook runs in exception path:\n");
pr_info("silkhook: --------------------------------------------\n");
dump_stack();
pr_info("silkhook: --------------------------------------------\n");
}
if (__elb_count % 100 == 1)
pr_info("silkhook: [elb] exception bounce !! pc=%lx count=%u\n",
instruction_pointer(regs), __elb_count);
}
/* ─────────────────────────────────────────────────────────────────────────────
* ich hook test (interrupt coalescing hijack)
*
* runs from hardirq ctx on timer interrupt
* ───────────────────────────────────────────────────────────────────────────── */
static unsigned int __ich_exec_count;
static void ich_test_payload(struct pt_regs *regs, struct silkhook_ich_hook *ctx)
{
struct task_struct *task = current;
__ich_exec_count++;
if (__ich_exec_count % 10 == 1)
{
pr_info("silkhook: [ich] tick !! cpu=%u pid=%d comm=%s\n",
smp_processor_id(),
task->pid,
task->comm);
}
if (strncmp(task->comm, "ssh", 3) == 0)
{
if (__ich_exec_count % 50 == 1)
pr_info("silkhook: [ich] spotted ssh !! pid=%d\n", task->pid);
}
}
/* ─────────────────────────────────────────────────────────────────────────────
* apr hook test (asynchronous page remap)
*
* replaces getppid with shadow page version
* ───────────────────────────────────────────────────────────────────────────── */
// static unsigned int __apr_count = 0;
/*
* replacement getppid
* lives in shadow page, executes instead of real getppid
*/
// static asmlinkage long apr_hooked_getppid(const struct pt_regs *regs)
// {
// return 1337;
// }
/* ─────────────────────────────────────────────────────────────────────────────
* /proc/silkhook_debug - elb proof
* ───────────────────────────────────────────────────────────────────────────── */
static int silkhook__debug_show(struct seq_file *m, void *v)
{
uint32_t *hook_site;
uint32_t instr;
int i;
int has_movz = 0, has_br = 0;
seq_puts(m, "+---------------------+\n");
seq_puts(m, "| silkhook elb test |\n");
seq_puts(m, "+---------------------+\n\n");
if (!__elb_hook.installed)
{
seq_puts(m, "elb hook NOT installed\n");
return 0;
}
hook_site = (uint32_t *)__elb_hook.targ;
instr = hook_site[0];
seq_puts(m, "[1] single instr patch\n");
seq_puts(m, " -------------------------------------------------------\n");
seq_printf(m, " hook addr: %px\n", hook_site);
seq_printf(m, " curr instr: %08x\n", instr);
seq_printf(m, " expected brk: %08x\n", SILKHOOK_BRK_INSTR);
seq_printf(m, " orig instr: %08x\n", __elb_hook.orig_instr);
seq_printf(m, " bytes changed: %d\n", 4);
if (instr == SILKHOOK_BRK_INSTR)
seq_puts(m, " result: GOOD - only 4 bytes modified!!\n\n");
else
seq_puts(m, " result: BAD\n\n");
seq_puts(m, "[2] no jump sequences\n");
seq_puts(m, " -------------------------------------------------------\n");
seq_puts(m, " scanning for inline hook sigs...\n");
for (i = 0; i < 5; i++)
{
uint32_t ins = hook_site[i];
if ((ins & 0xFF800000) == 0xD2800000)
has_movz++;
if ((ins >> 23) == (0xF28 >> 1) ||
(ins >> 23) == (0xF2A >> 1) ||
(ins >> 23) == (0xF2C >> 1) ||
(ins >> 23) == (0xF2E >> 1))
has_movz++;
if ((ins & 0xFFFFFC1F) == 0xD61F0000)
has_br++;
}
seq_printf(m, " movz/movk spotted: %d\n", has_movz);
seq_printf(m, " br found: %d\n", has_br);
if (has_movz < 2 && has_br == 0)
seq_puts(m, " result: GOOD - no shellcode pattern!!\n\n");
else
seq_puts(m, " result: BAD - jump seq detected\n\n");
seq_puts(m, "[3] brk instr analysis\n");
seq_puts(m, " -------------------------------------------------------\n");
if ((instr & 0xFFE0001F) == 0xD4200000)
{
uint16_t imm = (instr >> 5) & 0xFFFF;
seq_printf(m, " encoding: brk #0x%04x\n", imm);
seq_printf(m, " opcode mask: %08x & FFE0001F = D4200000 [GOOD]\n", instr);
seq_puts(m, " result: GOOD - valid brk instr\n\n");
}
else {
seq_puts(m, " result: BAD\n\n");
}
seq_puts(m, "[4] mem dump @ hook-site\n");
seq_puts(m, " -------------------------------------------------------\n");
for (i = 0; i < 8; i++)
{
seq_printf(m, " %px: %08x", &hook_site[i], hook_site[i]);
if (i == 0)
seq_printf(m, " <-- brk #0x%04x (hook)\n", SILKHOOK_BRK_IMM);
else
seq_puts(m, "\n");
}
return 0;
}
static int silkhook__debug_open(struct inode *inode, struct file *file)
{
return single_open(file, silkhook__debug_show, NULL);
}
static const struct proc_ops silkhook_debug_ops = {
.proc_open = silkhook__debug_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
/* ─────────────────────────────────────────────────────────────────────────────
* /proc/silkhook_ich - ich stats
* ───────────────────────────────────────────────────────────────────────────── */
static int silkhook__ich_debug_show(struct seq_file *m, void *v)
{
uint64_t exec, skip, cycles;
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ silkhook ich stats │\n");
seq_puts(m, "└─────────────────────────────────────────┘\n\n");
if (!__ich_hook.installed)
{
seq_puts(m, "[!] ich hook NOT installed\n");
return 0;
}
silkhook__ich_get_stats(&__ich_hook, &exec, &skip, &cycles);
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ config │\n");
seq_puts(m, "├─────────────────────────────────────────┤\n");
seq_printf(m, "│ targ: %px │\n", __ich_hook.targ);
seq_printf(m, "│ orig instr: %08x │\n", __ich_hook.orig_instr);
seq_printf(m, "│ coalesce: every %4u irqs │\n", __ich_hook.coalesce_n);
seq_printf(m, "│ jitter: %u-%u cycles │\n",
__ich_hook.jitter_min, __ich_hook.jitter_max);
seq_puts(m, "└─────────────────────────────────────────┘\n\n");
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ runtime stats │\n");
seq_puts(m, "├─────────────────────────────────────────┤\n");
seq_printf(m, "│ execs: %8llu │\n", exec);
seq_printf(m, "│ skipped: %8llu │\n", skip);
seq_printf(m, "│ total cyc: %8llu │\n", cycles);
if (exec > 0)
seq_printf(m, "│ avg cycles: %8llu │\n", cycles / exec);
seq_puts(m, "└─────────────────────────────────────────┘\n");
return 0;
}
static int silkhook__ich_debug_open(struct inode *inode, struct file *file)
{
return single_open(file, silkhook__ich_debug_show, NULL);
}
static const struct proc_ops silkhook_ich_debug_ops = {
.proc_open = silkhook__ich_debug_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
/* ─────────────────────────────────────────────────────────────────────────────
* /proc/silkhook_apr - apr stats
* ───────────────────────────────────────────────────────────────────────────── */
static int silkhook__apr_debug_show(struct seq_file *m, void *v)
{
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ silkhook apr stats │\n");
seq_puts(m, "└─────────────────────────────────────────┘\n\n");
if (!__apr_hook.installed)
{
seq_puts(m, "[!] apr hook NOT installed\n");
return 0;
}
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ config │\n");
seq_puts(m, "├─────────────────────────────────────────┤\n");
seq_printf(m, "│ targ: %px │\n", __apr_hook.targ);
seq_printf(m, "│ shadow: %px │\n", __apr_hook.shadow);
seq_printf(m, "│ orig_pfn: %lx │\n", __apr_hook.pfn_orig);
seq_printf(m, "│ hook_pfn: %lx │\n", __apr_hook.pfn_hook);
seq_printf(m, "│ active: %s │\n",
__apr_hook.active ? "yes" : "no");
seq_puts(m, "└─────────────────────────────────────────┘\n\n");
// seq_puts(m, "┌─────────────────────────────────────────┐\n");
// seq_puts(m, "│ runtime stats │\n");
// seq_puts(m, "├─────────────────────────────────────────┤\n");
// seq_printf(m, "│ executions: %8u │\n", __apr_count);
// seq_puts(m, "└─────────────────────────────────────────┘\n\n");
seq_puts(m, "┌─────────────────────────────────────────┐\n");
seq_puts(m, "│ technique │\n");
seq_puts(m, "├─────────────────────────────────────────┤\n");
seq_puts(m, "│ • two physical pages, one VA │\n");
seq_puts(m, "│ • swap via PTE pfn manipulation │\n");
seq_puts(m, "│ • atomic enable/disable │\n");
seq_puts(m, "│ • no code patching │\n");
seq_puts(m, "│ • forensics: can show clean page │\n");
seq_puts(m, "└─────────────────────────────────────────┘\n");
return 0;
}
static int silkhook__apr_debug_open(struct inode *inode, struct file *file)
{
return single_open(file, silkhook__apr_debug_show, NULL);
}
static const struct proc_ops silkhook_apr_debug_ops = {
.proc_open = silkhook__apr_debug_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
/* ─────────────────────────────────────────────────────────────────────────────
* setup
* ───────────────────────────────────────────────────────────────────────────── */
static void do_silkhook_setup(struct work_struct *work)
{
int r;
void *targ;
pr_info("silkhook: setup started !!!\n");
if (silkhook_ksyms_init() != 0)
return;
if (silkhook_mem_init() != 0)
return;
/* svc hook */
r = silkhook__svc_init(&__svc_hook);
if (r != SILKHOOK_OK)
{
pr_err("silkhook: svc init failure: %d\n", r);
return;
}
r = silkhook__svc_install(&__svc_hook, __NR_getuid, hooked_getuid);
if (r != SILKHOOK_OK)
{
pr_err("silkhook: svc install failure: %d\n", r);
return;
}
/* elb hook */
r = silkhook__elb_init();
if (r != SILKHOOK_OK)
{
pr_err("silkhook: elb init failure: %d\n", r);
goto skip_elb;
}
targ = silkhook_ksym("__arm64_sys_getpid");
if (targ)
{
r = silkhook__elb_install(&__elb_hook, targ, elb_test_handler, NULL);
if (r == SILKHOOK_OK)
proc_create("silkhook_debug", 0444, NULL, &silkhook_debug_ops);
else
pr_err("silkhook: elb install failure: %d\n", r);
}
skip_elb:
/* ich hook */
r = silkhook__ich_init();
if (r != SILKHOOK_OK)
{
pr_err("silkhook: ich init failure: %d\n", r);
goto skip_ich;
}
{
struct silkhook_ich_cfg ich_cfg = {
.coalesce_n = 16,
.jitter_min = 10,
.jitter_max = 50,
.payload = ich_test_payload,
.payload_ctx = NULL,
};
r = silkhook__ich_install(&__ich_hook, &ich_cfg);
if (r == SILKHOOK_OK)
{
proc_create("silkhook_ich", 0444, NULL, &silkhook_ich_debug_ops);
pr_info("silkhook: ich ready !!!\n");
}
else {
pr_err("silkhook: ich install failure: %d\n", r);
}
}
skip_ich:
/* apr hook */
r = silkhook__apr_init();
if (r != SILKHOOK_OK)
{
pr_err("silkhook: apr init failure: %d\n", r);
goto skip_apr;
}
targ = silkhook_ksym("__arm64_sys_getppid");
if (targ) {
r = silkhook__apr_install(&__apr_hook, targ,
apr_shellcode, sizeof(apr_shellcode));
if (r == SILKHOOK_OK) {
silkhook__apr_enable(&__apr_hook);
proc_create("silkhook_apr", 0444, NULL, &silkhook_apr_debug_ops);
pr_info("silkhook: apr ready !!!\n");
} else {
pr_err("silkhook: apr install failure: %d\n", r);
}
}
skip_apr:
silkhook__hide_mod(THIS_MODULE, &__hidden);
pr_info("silkhook: setup complete !!!\n");
}
static int __init silkhook_mod_init(void)
{
pr_info("silkhook: module loaded !!!\n");
INIT_DELAYED_WORK(&setup_work, do_silkhook_setup);
schedule_delayed_work(&setup_work, msecs_to_jiffies(100));
return 0;
}
static void __exit silkhook_mod_exit(void)
{
remove_proc_entry("silkhook_apr", NULL);
remove_proc_entry("silkhook_ich", NULL);
remove_proc_entry("silkhook_debug", NULL);
cancel_delayed_work_sync(&setup_work);
silkhook__unhide_mod(&__hidden);
silkhook__apr_disable(&__apr_hook);
silkhook__apr_remove(&__apr_hook);
silkhook__apr_exit();
silkhook__ich_remove(&__ich_hook);
silkhook__ich_exit();
silkhook__elb_remove(&__elb_hook);
silkhook__elb_exit();
silkhook__svc_remove(&__svc_hook);
pr_info("silkhook: unloaded !!!\n");
}
module_init(silkhook_mod_init);
module_exit(silkhook_mod_exit);