-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatch_communication.cpp
More file actions
638 lines (535 loc) · 22.7 KB
/
Copy pathpatch_communication.cpp
File metadata and controls
638 lines (535 loc) · 22.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include "patch_communication.h"
#include "analyze/base_func.h"
#include "3rdparty/aarch64_asm_helper.h"
#include "3rdparty/aarch64_reg_protect_guard.h"
using namespace asmjit;
using namespace asmjit::a64;
using namespace asmjit::a64::Predicate;
PatchCommunication::PatchCommunication(const PatchBase66& patch_base)
: PatchBase66(patch_base) {}
PatchCommunication::~PatchCommunication() {}
// 辅助:解析十六进制参数
// 从字符串指针str_ptr开始解析hex数字到result
void PatchCommunication::emit_parse_hex_param(Assembler* a, GpX str_ptr, GpX result) {
Label loop_start = a->newLabel();
Label loop_end = a->newLabel();
Label is_digit = a->newLabel();
Label is_upper = a->newLabel();
Label is_lower = a->newLabel();
Label add_digit = a->newLabel();
// result = 0
a->mov(result, xzr);
a->bind(loop_start);
// 读取一个字符
a->ldrb(w16, ptr(str_ptr).post(1));
// 检查是否为'/'或'\0'(参数结束)
a->cmp(w16, Imm('/'));
a->b(CondCode::kEQ, loop_end);
a->cbz(w16, loop_end);
// result = result << 4
a->lsl(result, result, Imm(4));
// 判断字符类型并转换
// '0'-'9' (0x30-0x39)
a->cmp(w16, Imm('0'));
a->b(CondCode::kLT, loop_start);
a->cmp(w16, Imm('9'));
a->b(CondCode::kLE, is_digit);
// 'A'-'F' (0x41-0x46)
a->cmp(w16, Imm('A'));
a->b(CondCode::kLT, loop_start);
a->cmp(w16, Imm('F'));
a->b(CondCode::kLE, is_upper);
// 'a'-'f' (0x61-0x66)
a->cmp(w16, Imm('a'));
a->b(CondCode::kLT, loop_start);
a->cmp(w16, Imm('f'));
a->b(CondCode::kLE, is_lower);
a->b(loop_start);
a->bind(is_digit);
a->sub(w16, w16, Imm('0'));
// 修复:零扩展w16到x16,防止高32位有垃圾数据
// 使用 mov(x16, x16) 配合 uxtw 操作数修饰,或使用 and 清零高位
// AArch64: 对w寄存器的写操作会自动将高32位清零
// 所以 sub(w16, ...) 已经保证x16的高32位为0
a->b(add_digit);
a->bind(is_upper);
a->sub(w16, w16, Imm('A' - 10));
// w16操作自动零扩展到x16
a->b(add_digit);
a->bind(is_lower);
a->sub(w16, w16, Imm('a' - 10));
// w16操作自动零扩展到x16
a->bind(add_digit);
a->orr(result, result, x16);
a->b(loop_start);
a->bind(loop_end);
}
size_t PatchCommunication::patch_comm_handler(
const SymbolRegion& hook_func_start_region,
size_t root_key_addr,
size_t hook_area_start,
size_t kallsyms_lookup_name_addr,
size_t module_alloc_addr,
size_t module_memfree_addr,
std::vector<patch_bytes_data>& vec_out_patch_bytes_data)
{
size_t hook_func_start_addr = hook_func_start_region.offset;
if (hook_func_start_addr == 0) { return 0; }
std::cout << "Generating communication handler at: 0x" << std::hex << hook_func_start_addr << std::endl;
std::cout << " kallsyms_lookup_name: 0x" << kallsyms_lookup_name_addr << std::endl;
std::cout << " module_alloc: 0x" << module_alloc_addr << std::endl;
std::cout << " module_memfree: 0x" << module_memfree_addr << std::endl << std::dec << std::endl;
aarch64_asm_ctx asm_ctx = init_aarch64_asm();
auto a = asm_ctx.assembler();
// 通信协议格式:
// "/root_key_xxx/<cmd>/<param1>/<param2>/..."
// x1 = struct filename* (filename->name指向字符串)
//
// 命令码:
// 1 = 读取内核内存: /key/1/<kaddr>/<size>/<user_buf>
// 2 = 写入内核内存: /key/2/<kaddr>/<size>/<user_buf>
// 3 = 执行shellcode: /key/3/<code_addr>/<result_buf>
// 4 = 符号查找: /key/4/<name_ptr>/<result_buf>
// 5 = 安装Hook: /key/5/<target_addr>/<hook_handler>/<result_buf>
// 6 = 分配内核内存: /key/6/<size>/<result_buf>
// 7 = 释放内核内存: /key/7/<kaddr>
// 8 = 获取Hook区信息: /key/8/<result_buf>
// 奥创版Phase 2:
// 9 = SELinux切换: /key/9/<state>/<result_buf> (0=permissive, 1=enforcing)
Label label_end = a->newLabel();
Label label_restore = a->newLabel(); // 统一恢复寄存器点
Label label_cmd_1 = a->newLabel();
Label label_cmd_2 = a->newLabel();
Label label_cmd_3 = a->newLabel();
Label label_cmd_4 = a->newLabel();
Label label_cmd_5 = a->newLabel();
Label label_cmd_6 = a->newLabel();
Label label_cmd_7 = a->newLabel();
Label label_cmd_8 = a->newLabel();
Label label_cmd_9 = a->newLabel(); // 奥创版: SELinux切换
Label label_invalid_cmd = a->newLabel();
// ========== 统一保存寄存器 ==========
// 保存x29/x30和callee-saved寄存器x19-x28
a->stp(x29, x30, ptr(sp).pre(-16));
a->stp(x19, x20, ptr(sp).pre(-16));
a->stp(x21, x22, ptr(sp).pre(-16));
a->stp(x23, x24, ptr(sp).pre(-16));
a->stp(x25, x26, ptr(sp).pre(-16));
a->stp(x27, x28, ptr(sp).pre(-16));
// ========== 运行时地址计算(KASLR支持)==========
// ADR x28, . 获取当前PC的运行时地址
// 编码: ADR Xd, #0 -> 0x10000000 | Rd
uint32_t adr_instr = 0x10000000 | 28; // ADR x28, #0
a->embed((const uint8_t*)&adr_instr, sizeof(adr_instr));
// 计算此ADR指令在文件中的偏移
size_t adr_file_offset = hook_func_start_addr + a->offset() - 4;
// kernel_base = runtime_PC - file_offset
// x27 = kernel_base (保存到callee-saved寄存器)
aarch64_asm_mov_x(a, x26, adr_file_offset); // x26 = ADR指令的文件偏移
a->sub(x27, x28, x26); // x27 = kernel_base
// x1 = filename结构体指针
// 获取filename->name字符串指针
a->ldr(x19, ptr(x1)); // x19 = filename->name
// 跳过root_key部分 (48字节 + '/')
a->add(x19, x19, Imm(ROOT_KEY_LEN + 1));
// 解析命令码 (单个字符)
a->ldrb(w20, ptr(x19).post(1));
a->sub(w20, w20, Imm('0')); // 转换为数字
// 跳过'/'
a->ldrb(w21, ptr(x19).post(1));
// 命令分发
a->cmp(w20, Imm(1));
a->b(CondCode::kEQ, label_cmd_1);
a->cmp(w20, Imm(2));
a->b(CondCode::kEQ, label_cmd_2);
a->cmp(w20, Imm(3));
a->b(CondCode::kEQ, label_cmd_3);
a->cmp(w20, Imm(4));
a->b(CondCode::kEQ, label_cmd_4);
a->cmp(w20, Imm(5));
a->b(CondCode::kEQ, label_cmd_5);
a->cmp(w20, Imm(6));
a->b(CondCode::kEQ, label_cmd_6);
a->cmp(w20, Imm(7));
a->b(CondCode::kEQ, label_cmd_7);
a->cmp(w20, Imm(8));
a->b(CondCode::kEQ, label_cmd_8);
a->cmp(w20, Imm(9));
a->b(CondCode::kEQ, label_cmd_9);
a->b(label_invalid_cmd);
// ========== CMD 1: 读取内核内存 ==========
// 格式: /key/1/<kaddr>/<size>/<user_buf>
// 奥创版增强:内核地址边界检查 + 8字节批量复制
a->bind(label_cmd_1);
{
Label label_cmd1_error = a->newLabel();
Label copy_8byte_loop = a->newLabel();
Label copy_1byte_start = a->newLabel();
Label copy_1byte_loop = a->newLabel();
Label copy_done = a->newLabel();
// 解析kaddr
emit_parse_hex_param(a, x19, x20); // x20 = kaddr
// 解析size
emit_parse_hex_param(a, x19, x21); // x21 = size
// 解析user_buf
emit_parse_hex_param(a, x19, x22); // x22 = user_buf
// ========== 安全检查:验证kaddr在内核空间 ==========
// ARM64内核空间: 高16位为0xFFFF (地址 >= 0xFFFF000000000000)
// 检查方法: kaddr >> 48 应该等于 0xFFFF
a->lsr(x23, x20, Imm(48));
a->mov(x24, Imm(0xFFFF));
a->cmp(x23, x24);
a->b(CondCode::kNE, label_cmd1_error); // 非内核地址,返回错误
// ========== 安全检查:验证size不超过合理范围 ==========
// 限制单次操作最大1MB,防止意外读取大量数据
a->mov(x24, Imm(0x100000)); // 1MB
a->cmp(x21, x24);
a->b(CondCode::kGT, label_cmd1_error); // 超过1MB,返回错误
// ========== 安全检查:验证kaddr + size不溢出 ==========
a->adds(x23, x20, x21);
a->b(CondCode::kCS, label_cmd1_error); // 加法溢出,返回错误
// 检查是否有数据需要复制
a->cbz(x21, copy_done);
// ========== 8字节批量复制(主循环)==========
// x25 = size / 8 (8字节块数)
a->lsr(x25, x21, Imm(3));
a->cbz(x25, copy_1byte_start); // 不足8字节,跳到单字节复制
a->bind(copy_8byte_loop);
a->ldr(x23, ptr(x20).post(8)); // 从kaddr读取8字节
a->str(x23, ptr(x22).post(8)); // 写入user_buf
a->subs(x25, x25, Imm(1));
a->b(CondCode::kNE, copy_8byte_loop);
// ========== 剩余字节单字节复制 ==========
a->bind(copy_1byte_start);
a->and_(x21, x21, Imm(7)); // size % 8
a->cbz(x21, copy_done);
a->bind(copy_1byte_loop);
a->ldrb(w23, ptr(x20).post(1));
a->strb(w23, ptr(x22).post(1));
a->subs(x21, x21, Imm(1));
a->b(CondCode::kNE, copy_1byte_loop);
a->bind(copy_done);
a->b(label_end);
// ========== 错误处理 ==========
a->bind(label_cmd1_error);
a->mov(x0, Imm(-2)); // 返回错误码 -2 表示地址/大小无效
a->b(label_restore);
}
// ========== CMD 2: 写入内核内存 ==========
// 格式: /key/2/<kaddr>/<size>/<user_buf>
// 奥创版增强:内核地址边界检查 + 8字节批量复制
a->bind(label_cmd_2);
{
Label label_cmd2_error = a->newLabel();
Label copy_8byte_loop = a->newLabel();
Label copy_1byte_start = a->newLabel();
Label copy_1byte_loop = a->newLabel();
Label copy_done = a->newLabel();
// 解析kaddr
emit_parse_hex_param(a, x19, x20); // x20 = kaddr
// 解析size
emit_parse_hex_param(a, x19, x21); // x21 = size
// 解析user_buf
emit_parse_hex_param(a, x19, x22); // x22 = user_buf
// ========== 安全检查:验证kaddr在内核空间 ==========
// ARM64内核空间: 高16位为0xFFFF (地址 >= 0xFFFF000000000000)
a->lsr(x23, x20, Imm(48));
a->mov(x24, Imm(0xFFFF));
a->cmp(x23, x24);
a->b(CondCode::kNE, label_cmd2_error); // 非内核地址,返回错误
// ========== 安全检查:验证size不超过合理范围 ==========
// 限制单次操作最大1MB
a->mov(x24, Imm(0x100000)); // 1MB
a->cmp(x21, x24);
a->b(CondCode::kGT, label_cmd2_error); // 超过1MB,返回错误
// ========== 安全检查:验证kaddr + size不溢出 ==========
a->adds(x23, x20, x21);
a->b(CondCode::kCS, label_cmd2_error); // 加法溢出,返回错误
// 检查是否有数据需要复制
a->cbz(x21, copy_done);
// ========== 8字节批量复制(主循环)==========
// x25 = size / 8 (8字节块数)
a->lsr(x25, x21, Imm(3));
a->cbz(x25, copy_1byte_start); // 不足8字节,跳到单字节复制
a->bind(copy_8byte_loop);
a->ldr(x23, ptr(x22).post(8)); // 从user_buf读取8字节
a->str(x23, ptr(x20).post(8)); // 写入kaddr
a->subs(x25, x25, Imm(1));
a->b(CondCode::kNE, copy_8byte_loop);
// ========== 剩余字节单字节复制 ==========
a->bind(copy_1byte_start);
a->and_(x21, x21, Imm(7)); // size % 8
a->cbz(x21, copy_done);
a->bind(copy_1byte_loop);
a->ldrb(w23, ptr(x22).post(1));
a->strb(w23, ptr(x20).post(1));
a->subs(x21, x21, Imm(1));
a->b(CondCode::kNE, copy_1byte_loop);
a->bind(copy_done);
a->b(label_end);
// ========== 错误处理 ==========
a->bind(label_cmd2_error);
a->mov(x0, Imm(-2)); // 返回错误码 -2 表示地址/大小无效
a->b(label_restore);
}
// ========== CMD 3: 执行shellcode ==========
// 格式: /key/3/<code_addr>/<result_buf>
a->bind(label_cmd_3);
{
// 解析code_addr
emit_parse_hex_param(a, x19, x20); // x20 = code_addr
// 解析result_buf
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
// 修复:调用shellcode前x30已保存在栈上,直接调用即可
// blr会覆盖x30,但我们已经在函数开头保存了
a->blr(x20);
// 将返回值(x0)写入result_buf
a->str(x0, ptr(x21));
}
a->b(label_end);
// ========== CMD 4: 符号查找 (kallsyms_lookup_name) ==========
// 格式: /key/4/<name_ptr>/<result_buf>
// name_ptr: 指向符号名字符串的用户空间指针
// result_buf: 用于存放结果的用户空间地址
a->bind(label_cmd_4);
if (kallsyms_lookup_name_addr != 0) {
// 解析name_ptr
emit_parse_hex_param(a, x19, x20); // x20 = name_ptr
// 解析result_buf
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
// 修复:使用运行时地址 = kernel_base + 符号偏移
// x27 = kernel_base (已在函数开头计算)
// kallsyms_lookup_name的运行时地址 = x27 + kallsyms_lookup_name_addr
aarch64_asm_mov_x(a, x10, kallsyms_lookup_name_addr);
a->add(x10, x27, x10); // x10 = runtime kallsyms_lookup_name address
// 调用 kallsyms_lookup_name(name)
// x0 = name
a->mov(x0, x20);
a->blr(x10);
// 将结果写入result_buf
a->str(x0, ptr(x21));
} else {
// 符号查找不可用,返回0
emit_parse_hex_param(a, x19, x20); // 跳过name_ptr
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
a->str(xzr, ptr(x21));
}
a->b(label_end);
// ========== CMD 5: 安装Hook ==========
// 格式: /key/5/<target_addr>/<hook_handler>/<result_buf>
// target_addr: 要Hook的目标地址
// hook_handler: Hook处理函数地址
// result_buf: 存放原指令的缓冲区
// 奥创版增强:添加范围检查 + 缓存刷新确保Hook立即生效
a->bind(label_cmd_5);
{
Label label_cmd5_error = a->newLabel();
Label label_cmd5_range_ok = a->newLabel();
// 解析target_addr
emit_parse_hex_param(a, x19, x20); // x20 = target_addr
// 解析hook_handler
emit_parse_hex_param(a, x19, x21); // x21 = hook_handler
// 解析result_buf
emit_parse_hex_param(a, x19, x22); // x22 = result_buf
// ========== 安全检查:验证target_addr在内核空间 ==========
a->lsr(x23, x20, Imm(48));
a->mov(x24, Imm(0xFFFF));
a->cmp(x23, x24);
a->b(CondCode::kNE, label_cmd5_error);
// ========== 安全检查:验证跳转范围 ==========
// B指令范围: ±128MB (±0x8000000)
// offset = handler - target
a->sub(x23, x21, x20);
// 检查offset是否在范围内: -0x8000000 <= offset < 0x8000000
// 方法: 将offset加上0x8000000,结果应该 < 0x10000000
a->mov(x24, Imm(0x8000000));
a->add(x25, x23, x24); // x25 = offset + 0x8000000
a->mov(x24, Imm(0x10000000));
a->cmp(x25, x24);
a->b(CondCode::kCS, label_cmd5_error); // 超出范围
a->bind(label_cmd5_range_ok);
// 读取并备份原指令
a->ldr(w23, ptr(x20));
a->str(w23, ptr(x22));
// 计算跳转偏移: offset = (handler - target) >> 2
a->sub(x23, x21, x20);
a->asr(x23, x23, Imm(2));
// 构造B指令: 0x14000000 | (offset & 0x3FFFFFF)
a->and_(x23, x23, Imm(0x3FFFFFF));
a->mov(w24, Imm(0x14000000));
a->orr(w23, w23, w24);
// 写入跳转指令到target地址
a->str(w23, ptr(x20));
// ========== 奥创版:缓存刷新序列 ==========
// 确保其他CPU能立即看到修改后的指令
// ARM64缓存一致性要求:
// 1. DC CVAU - 清理数据缓存到统一点 (Clean by VA to Point of Unification)
// 2. DSB ISH - 数据同步屏障 (内部共享域)
// 3. IC IVAU - 使指令缓存无效 (Invalidate by VA to Point of Unification)
// 4. DSB ISH - 数据同步屏障
// 5. ISB - 指令同步屏障
// DC CVAU, x20 编码: 0xD50B7B20 | (Rt & 0x1F)
// sys #3, c7, c11, #1, x20
uint32_t dc_cvau = 0xD50B7B20 | (20 & 0x1F); // x20
a->embed((const uint8_t*)&dc_cvau, sizeof(dc_cvau));
// DSB ISH 编码: 0xD5033B9F
uint32_t dsb_ish = 0xD5033B9F;
a->embed((const uint8_t*)&dsb_ish, sizeof(dsb_ish));
// IC IVAU, x20 编码: 0xD50B7520 | (Rt & 0x1F)
// sys #3, c7, c5, #1, x20
uint32_t ic_ivau = 0xD50B7520 | (20 & 0x1F); // x20
a->embed((const uint8_t*)&ic_ivau, sizeof(ic_ivau));
// DSB ISH
a->embed((const uint8_t*)&dsb_ish, sizeof(dsb_ish));
// ISB 编码: 0xD5033FDF
uint32_t isb = 0xD5033FDF;
a->embed((const uint8_t*)&isb, sizeof(isb));
// 返回成功标志
a->mov(x0, Imm(1));
a->str(x0, ptr(x22, 8));
a->b(label_end);
// ========== CMD 5 错误处理 ==========
a->bind(label_cmd5_error);
a->mov(x0, Imm(-6)); // ERR_HOOK_RANGE: 地址无效或跳转范围超出
a->b(label_restore);
}
// ========== CMD 6: 分配内核内存 ==========
// 格式: /key/6/<size>/<result_buf>
// 调用 module_alloc(size) 分配可执行内存
a->bind(label_cmd_6);
if (module_alloc_addr != 0) {
// 解析size
emit_parse_hex_param(a, x19, x20); // x20 = size
// 解析result_buf
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
// 修复:使用运行时地址 = kernel_base + 符号偏移
aarch64_asm_mov_x(a, x10, module_alloc_addr);
a->add(x10, x27, x10); // x10 = runtime module_alloc address
// 调用 module_alloc(size)
a->mov(x0, x20);
a->blr(x10);
// 将分配的地址写入result_buf
a->str(x0, ptr(x21));
} else {
// module_alloc不可用,返回0
emit_parse_hex_param(a, x19, x20); // 跳过size
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
a->str(xzr, ptr(x21));
}
a->b(label_end);
// ========== CMD 7: 释放内核内存 ==========
// 格式: /key/7/<kaddr>
// 调用 module_memfree(kaddr) 或 vfree(kaddr)
a->bind(label_cmd_7);
if (module_memfree_addr != 0) {
// 解析kaddr
emit_parse_hex_param(a, x19, x20); // x20 = kaddr
// 修复:使用运行时地址 = kernel_base + 符号偏移
aarch64_asm_mov_x(a, x10, module_memfree_addr);
a->add(x10, x27, x10); // x10 = runtime module_memfree address
// 调用 module_memfree(kaddr)
a->mov(x0, x20);
a->blr(x10);
} else {
// module_memfree不可用,跳过
emit_parse_hex_param(a, x19, x20); // 跳过kaddr
}
a->b(label_end);
// ========== CMD 8: 获取Hook跳板区信息 ==========
// 格式: /key/8/<result_buf>
// 返回: [hook_area_start, hook_area_used] 共16字节
a->bind(label_cmd_8);
{
// 解析result_buf
emit_parse_hex_param(a, x19, x20); // x20 = result_buf
// 写入hook_area_start (硬编码在patch时)
aarch64_asm_mov_x(a, x21, hook_area_start);
a->str(x21, ptr(x20));
// 写入hook_area_used = 0 (初始值)
a->str(xzr, ptr(x20, 8));
}
a->b(label_end);
// ========== 奥创版CMD 9: SELinux状态切换 ==========
// 格式: /key/9/<state>/<result_buf>
// state: 0=permissive, 1=enforcing
// 返回: result_buf[0] = 旧状态
// 注意: 需要selinux_enforcing符号地址,通过运行时kallsyms查找
a->bind(label_cmd_9);
if (kallsyms_lookup_name_addr != 0) {
Label label_cmd9_error = a->newLabel();
// 解析state
emit_parse_hex_param(a, x19, x20); // x20 = new state
// 解析result_buf
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
// 使用kallsyms_lookup_name查找selinux_enforcing
// 构造符号名 "selinux_enforcing" 在栈上
a->sub(sp, sp, Imm(32));
// "selinux_enforcing\0" = 17字节
// 小端存储
aarch64_asm_mov_x(a, x22, 0x6E696C6573); // "selin"
a->str(x22, ptr(sp));
aarch64_asm_mov_x(a, x22, 0x666E655F7875); // "ux_enf"
a->str(x22, ptr(sp, 5));
aarch64_asm_mov_x(a, x22, 0x676E6963726F); // "orcing"
a->str(x22, ptr(sp, 11));
a->strb(wzr, ptr(sp, 17)); // null terminator
// 调用kallsyms_lookup_name("selinux_enforcing")
aarch64_asm_mov_x(a, x10, kallsyms_lookup_name_addr);
a->add(x10, x27, x10);
a->mov(x0, sp);
a->blr(x10);
// 恢复栈
a->add(sp, sp, Imm(32));
// x0 = selinux_enforcing地址
a->cbz(x0, label_cmd9_error);
// 读取旧状态
a->ldr(w22, ptr(x0));
// 写入新状态
a->str(w20, ptr(x0));
// 返回旧状态
a->str(x22, ptr(x21));
a->b(label_end);
a->bind(label_cmd9_error);
// selinux_enforcing未找到
a->mov(x0, Imm(-7)); // ERR_NOT_AVAILABLE
a->b(label_restore);
} else {
// kallsyms_lookup_name不可用
emit_parse_hex_param(a, x19, x20); // 跳过state
emit_parse_hex_param(a, x19, x21); // x21 = result_buf
a->mov(x0, Imm(-7)); // ERR_NOT_AVAILABLE
a->b(label_restore);
}
// ========== 无效命令 ==========
a->bind(label_invalid_cmd);
// 返回错误码-1,直接跳到恢复寄存器,不经过label_end
a->mov(x0, Imm(-1));
a->b(label_restore);
// ========== 结束(成功)==========
a->bind(label_end);
// 设置返回值为0表示成功处理
a->mov(x0, xzr);
// ========== 统一恢复寄存器 ==========
a->bind(label_restore);
// 恢复顺序与保存顺序相反
a->ldp(x27, x28, ptr(sp).post(16));
a->ldp(x25, x26, ptr(sp).post(16));
a->ldp(x23, x24, ptr(sp).post(16));
a->ldp(x21, x22, ptr(sp).post(16));
a->ldp(x19, x20, ptr(sp).post(16));
a->ldp(x29, x30, ptr(sp).post(16));
a->ret(x30);
std::cout << print_aarch64_asm(a) << std::endl;
std::vector<uint8_t> bytes = aarch64_asm_to_bytes(a);
if (bytes.size() == 0) return 0;
std::string str_bytes = bytes2hex((const unsigned char*)bytes.data(), bytes.size());
size_t shellcode_size = str_bytes.length() / 2;
if (shellcode_size > hook_func_start_region.size) {
std::cout << "[发生错误] patch_comm_handler failed: not enough kernel space. Need "
<< shellcode_size << " bytes, have " << hook_func_start_region.size << std::endl;
return 0;
}
vec_out_patch_bytes_data.push_back({ str_bytes, hook_func_start_addr });
std::cout << "Communication handler size: " << shellcode_size << " bytes" << std::endl;
return shellcode_size;
}