最近在学习用ptrace注入so到用户进程。昨天写了一个原生c程序用来当做被trace的目标,没有问题。
今天自己写了一个app(有一个libnative.so),想把这个libnative.so的内存中代码区的属性改为可写。但是发现注入原生c程序的代码一注入app就有问题了。
尝试了几次之后发现就连调用目标进程的mmap函数也不稳定,有时ptrace函数会失败,提示No such process
输出如下:
root@ido:/data/local # ./hook_s 5328
params num:2
get_remote_fun_addr:fun[0xf72395ed], local[0xf7223000], remote[0xf7220000]
get_remote_fun_addr:fun[0xf728ef4d], local[0xf728e000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf728ef55], local[0xf728e000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf728f065], local[0xf728e000], remote[0xf7396000]
get remote fun addr,mmap :[0xf72365ed] dlopen :[0xf7396f4d], dlsym:[0xf7396f55], dlclose:[0xf7397065]
ptrace call ret status is 0
mmap return address :f02a5000
hook finish
root@ido:/data/local # ./hook_s 5328
params num:2
get_remote_fun_addr:fun[0xf75795ed], local[0xf7563000], remote[0xf7220000]
get_remote_fun_addr:fun[0xf75cef4d], local[0xf75ce000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf75cef55], local[0xf75ce000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf75cf065], local[0xf75ce000], remote[0xf7396000]
get remote fun addr,mmap :[0xf72365ed] dlopen :[0xf7396f4d], dlsym:[0xf7396f55], dlclose:[0xf7397065]
ptrace call ret status is 0
mmap return address :f02a4000
hook finish
root@ido:/data/local # ./hook_s 5328
params num:2
get_remote_fun_addr:fun[0xf73675ed], local[0xf7351000], remote[0xf7220000]
get_remote_fun_addr:fun[0xf73bcf4d], local[0xf73bc000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf73bcf55], local[0xf73bc000], remote[0xf7396000]
get_remote_fun_addr:fun[0xf73bd065], local[0xf73bc000], remote[0xf7396000]
get remote fun addr,mmap :[0xf72365ed] dlopen :[0xf7396f4d], dlsym:[0xf7396f55], dlclose:[0xf7397065]
ptrace call ret status is 0
ptrace_getregs: No such process
hook finish
If the parent process would like the child to stop (for example, after user input in a debugger), it can simply send it a SIGSTOP through the usual methods. Again, technically any unused signal besides SIGKILL would do the job, but its best to avoid ambiguity. It is important to ensure that the child task is stopped before doing anything to it; otherwise ptrace will return an ESRCH error: “no such process”. 目标进程必须是stopping的。 不知道有没有帮助