Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hook libart.so的mmap函数,跳转不到新的new_mmap函数 #3

Closed
KuNgia09 opened this issue Apr 26, 2018 · 10 comments
Closed

hook libart.so的mmap函数,跳转不到新的new_mmap函数 #3

KuNgia09 opened this issue Apr 26, 2018 · 10 comments

Comments

@KuNgia09
Copy link

环境测试:
Nexus5 CM12

测试代码如下

LOGD("[+]xhook hook register");
    xhook_enable_debug(1);
    xhook_register(LIB_ART_PATH, "open", (void *)new_open, (void **)&old_open);
    xhook_register(LIB_ART_PATH, "mmap", (void *)new_mmap, (void **)&old_mmap);
     xhook_register(LIB_ART_PATH, "munmap", (void *)new_munmap, (void **)&old_munmap);
     if (xhook_refresh(0)) {
                LOGE("[-]xhook refresh failed");
      }
     xhook_clear();

Log如下:

I/xhook   ( 9542): libxhook 1.1.4 (arm)
I/xhook   ( 9542): init OK: /system/lib/libart.so (REL ELF_HASH PLT:1736 DYN:53984 ANDROID:0
I/xhook   ( 9542): hooking open in /system/lib/libart.so
I/xhook   ( 9542): found open at symidx: 505 (ELF_HASH)
I/xhook   ( 9542): found open at .rel.plt offset: 0x304d78
I/xhook   ( 9542): XH_HK_OK 0xb47fad78: 0xb6dcf76d -> 0xaf3a26e9 open /system/lib/libart.so
I/xhook   ( 9542): hooking mmap in /system/lib/libart.so
I/xhook   ( 9542): found mmap at symidx: 522 (ELF_HASH)
I/xhook   ( 9542): found mmap at .rel.plt offset: 0x304d80
I/xhook   ( 9542): XH_HK_OK 0xb47fad80: 0xb6dd3521 -> 0xaf3a2ae9 mmap /system/lib/libart.so
I/xhook   ( 9542): hooking munmap in /system/lib/libart.so
I/xhook   ( 9542): found munmap at symidx: 525 (ELF_HASH)
I/xhook   ( 9542): found munmap at .rel.plt offset: 0x304d84

hook open函数 可以跳转到new_open函数,但是mmap和munmap函数不能跳转到新的函数

我看了下hook之后libart.so的got表 open mmap都被替换为了新的函数地址

@caikelun
Copy link
Collaborator

你是怎么判断new_mmap和new_munmap没有被执行到的?
因为hook是在so库被加载到内存之后才进行的,是否有可能在你的测试中libart.so后续没有再调用mmap了?

@KuNgia09
Copy link
Author

KuNgia09 commented Apr 27, 2018

我试了另外一个plt hook https://github.com/MelonWXD/ELFHooker,这个可以执行到新的mmap函数

ELFHooker的例子

//got hook
            ElfReader elfReader(LIB_ART_PATH, art_base);
            if (0 != elfReader.parse()) {
                LOGE("failed to parse %s in %d maps at %p", LIB_ART_PATH, getpid(), art_base);
                return;
            }
            elfReader.hook("open", (void *)new_open, (void **)&old_open);
            elfReader.hook("read", (void *)new_read, (void **)&old_read);
            elfReader.hook("mmap", (void *)new_mmap, (void **)&old_mmap);
            elfReader.hook("munmap", (void *)new_munmap, (void **)&old_munmap);
            elfReader.hook("__read_chk", (void *)new_read_chk, (void **)&old_read_chk);
            elfReader.hook("fstat", (void *)new_fstat, (void **)&old_fstat);
            elfReader.hook("fork", (void *)new_fork, (void **)&old_fork);
            

用ELFHooker可以执行到新的fake函数

用xhook只执行了new_open函数,后面几个函数都没跳过去

@caikelun
Copy link
Collaborator

@woxihuannisja

Hi,

我看了下https://github.com/MelonWXD/ELFHooker 中ELFHooker部分的代码,PLT/GOT定位的核心逻辑和xhook是一样的。而且你说open是hook成功的,对于 PLT/GOT hook来说,具体hook哪个函数不应该存在差异。

我这里目前没找到可用的Nexus5,找了一台Mi3(也是arm 32bit)试了下,是可以hook到libart.so的mmap的。

怀疑的点有两个,不知道你是否有时间,能否帮忙验证一下:

1。是否触发了 xhook 自身的段错误保护(SFP),这个可以升级到最新版本的xhook,然后在refresh之前调用一次xhook_enable_sigsegv_protection(0)来关闭SFP,再执行refresh,如果APP崩溃了,说明遇到了问题。

2。xhook 和 ELFHooker 的指令缓存刷新操作不同。xhook使用的是__builtin___clear_cache,这个是兼容 32-bit和64-bit arch的调用。ELFHooker使用的是syscall(0xf0002, start, end),这个只能适用于 32-bit arch。能够帮忙试一下,在xh_util.c的xh_util_flush_instruction_cache函数中,将:
__builtin___clear_cache((void *)PAGE_START(addr), (void *)PAGE_END(addr));
修改为:
syscall(0xf0002, (void *)PAGE_START(addr), (void *)PAGE_END(addr));

十分感谢!!

@KuNgia09
Copy link
Author

KuNgia09 commented Apr 27, 2018

@caikelun
我试了xhook_enable_sigsegv_protection(0)和syscall 还是没有效果
我是想在加载dex之前 hook函数
正常来说加载dex的操作 libart会调用open mmap 函数

使用xhook之后,
我用ida调试 在plt的mmap,open过渡函数下了断点,然后执行加载dex操作,也只有plt中的open函数断下来了

我在nexus5 红米note4x上测试都不行
感觉不是段错误问题,程序执行起来了,只是新的mmap和原mmap都不能执行

@caikelun
Copy link
Collaborator

@woxihuannisja 比较诡异。。而且根据你最初的描述,你用ida动态调试能看到got里mmap的函数地址其实是替换成功了?

如果只对libart.so hook mmap这一个函数,能被hook到正常跑吗?

@KuNgia09
Copy link
Author

KuNgia09 commented Apr 27, 2018

@caikelun
hook之后 libart.so里面的got表已经替换为new_mmap函数了
hook之后,执行加载dex的操作,libart.so也没有执行原始的mmap函数了,我在libc的mmap函数也下了断点

xhook只是改了got表吗?

如果没有hook成功我的程序是跑不起来的

@caikelun
Copy link
Collaborator

@woxihuannisja “libart.so也没有执行原始的mmap函数了”?有点糊涂了 @_@

@caikelun
Copy link
Collaborator

@woxihuannisja 方便的话能weixin或者qq吗?能把联系方式发我邮箱(caikelun@gmail.com)吗?我加你~

@KuNgia09
Copy link
Author

qq:1483943306

@caikelun
Copy link
Collaborator

caikelun commented May 9, 2018

目前的情况是hook是成功的,但是hook后app逻辑中有一个native崩溃,导致程序逻辑还没有走到hook的新函数中就崩溃退出了。app逻辑有点复杂,目前没有时间配合调试了。。暂时先关闭这个issue了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants