Skip to content

Commit 1ecf776

Browse files
修复一些bug
1 parent cca6637 commit 1ecf776

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
**set_target_file**函数用于设置要hook so的路径(必须是完整路径)。
1313

14-
**set_fun_info**函数第一个参数是要设置函数的uprobe偏移,计算方法见文末,第二个参数为函数偏移,第三个参数为自定义函数名。
14+
**set_fun_info**函数第一个参数是要设置函数的uprobe偏移,计算方法见文末,第二个参数为函数偏移,第三个参数为自定义函数名,
15+
第四个参数是对应函数地址处的第一条汇编指令(即在函数地址处读取的4个字节),为空即不修正uprobe在该函数地址处的备份指令。
1516

1617
**clear_all_uprobes**函数用于清除所有的uprobe挂载点。
1718

1819
上述函数的返回结果有SET_TRACE_SUCCESS、SET_TRACE_ERROR两种,分别表示设置成功和失败。
1920

2021
# 使用示例
21-
编程思路可以参考[示例](https://github.com/AndroidReverser-Test/Il2cppTraceModule/blob/main/app/src/main/cpp/il2cpp_trace.cpp)
22+
编程思路可以大概参考[示例](https://github.com/AndroidReverser-Test/Il2cppTraceModule/blob/main/app/src/main/cpp/il2cpp_trace.cpp)
2223

2324
# 支持的内核版本
2425
目前只在5.10以及5.15两个版本通过测试,理论上5.10以上版本都能正常使用。
@@ -30,3 +31,13 @@
3031
以下图片展示了一个so文件在maps文件中的区域段
3132
![计算示例](./pic/偏移计算.JPG)
3233
假如一个函数的地址为0x7626323000,那要hook这个函数那传给uprobe_register函数的偏移值应该为0x7626323000-0x7626322000+0x90000=0x91000.
34+
35+
## uprobe无法对加固过的so进行hook?
36+
在对应so的代码段没有被加密的情况下,uprobe是能正确获取到将要被**brk #5**覆盖的原始汇编指令,但当so的代码段是在运行时才解密时,
37+
uprobe获取到的汇编指令往往都是没有解密前的密文汇编指令,这些密文汇编指令当然是无法被执行的,这就会引发app的直接崩溃。至于为什么
38+
uprobe不是实时获取内存中的汇编指令,原因尚不清楚。
39+
40+
## 解决方案
41+
将为用户提供的set_fun_info函数增加一个fix_insn参数用于让用户自行设置指定函数地址的第一条原始汇编指令,
42+
再通过用户提供的这些正确的汇编指令来为uprobe直接设置其要备份的汇编指令。这样就能直接解决uprobe不能实时获取
43+
内存中的汇编指令的问题。

kernel_trace.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "kernel_trace.h"
1515

1616
KPM_NAME("kernel_trace");
17-
KPM_VERSION("2.3.0");
17+
KPM_VERSION("3.5.0");
1818
KPM_LICENSE("GPL v2");
1919
KPM_AUTHOR("Test");
2020
KPM_DESCRIPTION("use uprobe trace some fun in kpm");
@@ -29,18 +29,30 @@ void (*rcu_read_unlock)(void) = 0;
2929
int (*trace_printk)(unsigned long ip, const char *fmt, ...) = 0;
3030

3131
void *show_map_vma_addr;
32+
void *copy_insn_addr;
3233

3334

3435
char file_name[MAX_PATH_LEN];
3536
uid_t target_uid = -1;
3637
unsigned long fun_offsets[MAX_HOOK_NUM];
3738
int hook_num = 0;
3839
struct rb_root fun_info_tree = RB_ROOT;
40+
struct rb_root fix_ins_tree = RB_ROOT;
3941
static struct inode *inode;
4042
unsigned long module_base = 0;
4143
static struct uprobe_consumer trace_uc;
4244

4345

46+
void before_copy_insn(hook_fargs5_t *args, void *udata){
47+
struct my_key_value *ins_info;
48+
loff_t offset = (loff_t)args->arg4;
49+
ins_info = search_key_value(&fix_ins_tree,offset);
50+
if(ins_info){
51+
memcpy((void *)args->arg2,ins_info->value,INS_LEN);
52+
args->ret = 0;
53+
args->skip_origin = 1;
54+
}
55+
}
4456

4557
void before_show_map_vma(hook_fargs2_t *args, void *udata)
4658
{
@@ -89,6 +101,20 @@ void before_mincore(hook_fargs3_t *args, void *udata){
89101
goto success_out;
90102
}
91103

104+
if(trace_info==FIX_ORI_INS){
105+
unsigned long rfun_offset = (unsigned long)syscall_argn(args, 0);
106+
const char __user *ufix_ins = (typeof(ufix_ins))syscall_argn(args, 2);
107+
char fix_ins[INS_LEN*2];
108+
compat_strncpy_from_user(fix_ins,ufix_ins,INS_LEN*2);
109+
// logkd("+Test-Log3+ insn:%lx %lx %lx %lx\n",fix_ins[0],fix_ins[1],fix_ins[2],fix_ins[3]);
110+
int insert_ins_ret = insert_key_value(&fix_ins_tree,rfun_offset,fix_ins);
111+
if(insert_ins_ret==-1){
112+
logke("+Test-Log+ set insn for same fun 0x%llx\n",rfun_offset);
113+
goto error_out;
114+
}
115+
goto success_out;
116+
}
117+
92118
if(trace_info==SET_TARGET_UPROBE){
93119
unsigned long rfun_offset = (unsigned long)syscall_argn(args, 0);
94120
int hret = uprobe_register(inode,rfun_offset,&trace_uc);
@@ -138,6 +164,7 @@ void before_mincore(hook_fargs3_t *args, void *udata){
138164
}
139165
hook_num = 0;
140166
destroy_entire_tree(&fun_info_tree);
167+
destroy_entire_tree(&fix_ins_tree);
141168
logkd("+Test-Log+ success clear all uprobes\n");
142169
goto success_out;
143170
}
@@ -205,6 +232,8 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
205232

206233
show_map_vma_addr = (void *)kallsyms_lookup_name("show_map_vma");
207234

235+
copy_insn_addr = (void *)kallsyms_lookup_name("__copy_insn");
236+
208237
logkd("+Test-Log+ mtask_pid_nr_ns:%llx\n",mtask_pid_nr_ns);
209238
logkd("+Test-Log+ uprobe_register:%llx\n",uprobe_register);
210239
logkd("+Test-Log+ uprobe_unregister:%llx\n",uprobe_unregister);
@@ -223,10 +252,12 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
223252

224253
logkd("+Test-Log+ show_map_vma_addr:%llx\n",show_map_vma_addr);
225254

255+
logkd("+Test-Log+ copy_insn_addr:%llx\n",copy_insn_addr);
256+
226257
if(!(mtask_pid_nr_ns && uprobe_register && uprobe_unregister
227258
&& kern_path && igrab && path_put && rcu_read_unlock
228259
&& rb_erase && rb_insert_color && rb_first && trace_printk
229-
&& show_map_vma_addr)){
260+
&& show_map_vma_addr && copy_insn_addr)){
230261
logke("+Test-Log+ can not find some fun addr\n");
231262
return -1;
232263
}
@@ -245,6 +276,11 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
245276
return -1;
246277
}
247278

279+
err = hook_wrap5(copy_insn_addr, before_copy_insn, NULL, 0);
280+
if(err){
281+
logke("+Test-Log+ hook copy_insn error\n");
282+
return -1;
283+
}
248284

249285
logkd("+Test-Log+ success init\n");
250286
return 0;
@@ -261,12 +297,14 @@ static long kernel_trace_exit(void *__user reserved)
261297
{
262298
inline_unhook_syscall(__NR_mincore, before_mincore, 0);
263299
unhook(show_map_vma_addr);
300+
unhook(copy_insn_addr);
264301
rcu_read_unlock();//解锁,不然内核会崩
265302
for (int i = 0; i < hook_num; ++i) {
266303
uprobe_unregister(inode,fun_offsets[i],&trace_uc);
267304
}
268305
logkd("+Test-Log+ success clear all uprobes\n");
269306
destroy_entire_tree(&fun_info_tree);
307+
destroy_entire_tree(&fix_ins_tree);
270308
logkd("kpm kernel_trace exit\n");
271309
}
272310

kernel_trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
77
#define MAX_PATH_LEN 300
88
#define MAX_FUN_NAME 150
9+
#define INS_LEN 4
910
#define LOOKUP_FOLLOW 0x0001
1011
#define HASH_LEN_DECLARE u32 hash; u32 len
1112

uprobe_trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum trace_info {
77
SET_TARGET_FILE,
88
SET_MODULE_BASE,
99
SET_FUN_INFO,
10+
FIX_ORI_INS,
1011
SET_TARGET_UPROBE,
1112
SET_TARGET_UID,
1213
CLEAR_UPROBE,

user/uprobe_trace_user.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum trace_info {
1111
SET_TARGET_FILE,
1212
SET_MODULE_BASE,
1313
SET_FUN_INFO,
14+
FIX_ORI_INS,
1415
SET_TARGET_UPROBE,
1516
SET_TARGET_UID,
1617
CLEAR_UPROBE,
@@ -33,9 +34,15 @@ int set_target_file(char* file_name){
3334
return ret;
3435
}
3536

36-
int set_fun_info(unsigned long uprobe_offset,unsigned long fun_offset,char *fun_name){
37+
int set_fun_info(unsigned long uprobe_offset,unsigned long fun_offset,char *fun_name,char *fix_insn){
3738
int insert_key_ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
3839
if(insert_key_ret==SET_TRACE_SUCCESS){
40+
if(fix_insn){
41+
int fix_insn_ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+FIX_ORI_INS,fix_insn);
42+
if(fix_insn_ret == SET_TRACE_ERROR){
43+
return SET_TRACE_ERROR;
44+
}
45+
}
3946
int ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+SET_TARGET_UPROBE,"");
4047
return ret;
4148
}

0 commit comments

Comments
 (0)