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

请问安卓9的open函数hook不到是什么原因。同样代码安卓10是没有问题的。 #16

Closed
LeeKasm opened this issue Nov 23, 2021 · 13 comments
Labels
help wanted Extra attention is needed invalid This doesn't seem right

Comments

@LeeKasm
Copy link

LeeKasm commented Nov 23, 2021

Android层测试代码:
File file = new File("/storage/emulated/0/1.txt");
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buf = new byte[(int)file.length()];
fileInputStream.read(buf);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

c层hook代码:
bytehook_hook_single("libjavacore.so", NULL, "open", open_proxy, open_hooked_callback, NULL);
bytehook_hook_single("libjavacore.so", NULL, "stat", stat_proxy, stat_hooked_callback, NULL);
bytehook_hook_single("libjavacore.so", NULL, "read", read_proxy_auto, read_hooked_callback, NULL);

安卓9的回调显示hook成功了。
bytehook_tag: >>>>> hooked. stub: c6b93ac0, status: 0, caller_path_name: /system/lib/libjavacore.so, sym_name: open, new_func: c5e08ebd, prev_func: e756aefd, arg: 0
安卓10的回调也类似。
bytehook_tag: >>>>> hooked. stub: ba5d3480, status: 0, caller_path_name: /apex/com.android.runtime/lib/libjavacore.so, sym_name: open, new_func: c04afebd, prev_func: edaa2299, arg: 0
但是实际测试中在open入口打印,只有安卓10的设备有响应。
LOG("open hooked path("%s")", pathname);

@caikelun
Copy link
Member

如果要处理兼容性,需要hook:open,__open_real,__open_2。
可以参考bytehook的sample app:https://github.com/bytedance/bhook/blob/main/bytehook_sample/src/main/cpp/hacker.c#L152

@caikelun caikelun added the help wanted Extra attention is needed label Nov 23, 2021
@LeeKasm
Copy link
Author

LeeKasm commented Nov 23, 2021

您误会了,在此之前我都尝试过了,是不行的。
这可能是个bug,上一条是为了简化现象说的不够具体。

__open_real在API17之后已经不用了,在安卓9上应该不用_open_real。
#if ANDROID_API >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
return __open_2(pathname, flags);
#else
return __open_real(pathname, flags);
#endif

除了open,stat等函数在安卓9上也hook不到,意外的是read却可以。

@LeeKasm
Copy link
Author

LeeKasm commented Nov 23, 2021

另外需要明确的是hook自己写的so库是没有这个问题的。目前发现的仅仅是安卓9 libjavacore.so库下libcore_io_Linux.cpp的open hook不到。

@caikelun
Copy link
Member

  1. 可以反编译一下被hook的so,看看源码中对应位置最后调用了什么函数,以及怎么调用的。
  2. 可以动态调试一下,确认下程序流程确实走到了你预期的位置。

@LeeKasm
Copy link
Author

LeeKasm commented Nov 23, 2021

您好,下面说的可能不完全正确但是应该方向大差不差,根据您的建议我反编译了libjavacore.so,确实存在open函数。

在安卓9中Java层的FileInputStream类实例化时对文件打开,
调用 libcore.io.IoBridge.open(String path, int flags),
进而调用了native方法libcore.io.Linux.open(String path, int flags, int mode),
随后进入底层libjavacore.so中libcore_io_Linux.cpp的Linux_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode)。

我用xhook验证,hook libjavacore.so的open可以打印出信息,也说明open函数是在这个so中。
但是用bhook虽然hook到了但是无打印结果,应该是没hook上。

@caikelun
Copy link
Member

您好,下面说的可能不完全正确但是应该方向大差不差,根据您的建议我反编译了libjavacore.so,确实存在open函数。

在安卓9中Java层的FileInputStream类实例化时对文件打开, 调用 libcore.io.IoBridge.open(String path, int flags), 进而调用了native方法libcore.io.Linux.open(String path, int flags, int mode), 随后进入底层libjavacore.so中libcore_io_Linux.cpp的Linux_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode)。

我用xhook验证,hook libjavacore.so的open可以打印出信息,也说明open函数是在这个so中。 但是用bhook虽然hook到了但是无打印结果,应该是没hook上。

收到,感谢反馈,我调试一下。。。
能问下你的android 9设备的机型吗?

@LeeKasm
Copy link
Author

LeeKasm commented Nov 29, 2021

您好,我用的两个设备分别是:
HUAWEI Mate9,型号:MHA-AL00,版本号:9.1.0.231;
HUAWEI MediaPad M5 lite 10,型号:BAH2-AL10,版本号:9.1.0.170;

@caikelun
Copy link
Member

@LeeKasm 我在“HUAWEI Mate9,型号:MHA-AL00”上试了下,bhook和xhook hook libjavacore.so 的open都是成功的,但是java层执行你贴的FileInputStream逻辑,都无法走到proxy函数中。你可以再用xhook确认下。

另外,你可以动态调试确认下FileInputStream流程确实会走到open?可以用inlinehook确认下,或者直接用frida之类的工具。

@LeeKasm
Copy link
Author

LeeKasm commented Nov 30, 2021

我重新测试了下,您说的对,确实是我把几个设备搞混了。
安卓9走的libopenjdk.so,安卓10是走的libjavacore.so。
抱歉因为我的调试失误给您强行加了波班😀

@zhbzhbzhbz
Copy link

Android层测试代码: File file = new File("/storage/emulated/0/1.txt"); try { FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[(int)file.length()]; fileInputStream.read(buf); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); }

c层hook代码: bytehook_hook_single("libjavacore.so", NULL, "open", open_proxy, open_hooked_callback, NULL); bytehook_hook_single("libjavacore.so", NULL, "stat", stat_proxy, stat_hooked_callback, NULL); bytehook_hook_single("libjavacore.so", NULL, "read", read_proxy_auto, read_hooked_callback, NULL);

安卓9的回调显示hook成功了。 bytehook_tag: >>>>> hooked. stub: c6b93ac0, status: 0, caller_path_name: /system/lib/libjavacore.so, sym_name: open, new_func: c5e08ebd, prev_func: e756aefd, arg: 0 安卓10的回调也类似。 bytehook_tag: >>>>> hooked. stub: ba5d3480, status: 0, caller_path_name: /apex/com.android.runtime/lib/libjavacore.so, sym_name: open, new_func: c04afebd, prev_func: edaa2299, arg: 0 但是实际测试中在open入口打印,只有安卓10的设备有响应。 LOG("open hooked path("%s")", pathname);

@LeeKasm 求问您方便分享下您hook read和write的代码么~ C语言小白想参考下

@LeeKasm
Copy link
Author

LeeKasm commented Jun 22, 2022

@zhbzhbzhbz

bytehook_hook_single("libjavacore.so", NULL, "read", (void *) my_read, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "pread64", (void *) my_pread64, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "readv", (void *) my_readv, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "write", (void *) my_write, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "pwrite64", (void *) my_pwrite64, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "writev", (void *) my_writev, NULL, NULL);

@zhbzhbzhbz
Copy link

@zhbzhbzhbz

bytehook_hook_single("libjavacore.so", NULL, "read", (void *) my_read, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "pread64", (void *) my_pread64, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "readv", (void *) my_readv, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "write", (void *) my_write, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "pwrite64", (void *) my_pwrite64, NULL, NULL);
bytehook_hook_single("libjavacore.so", NULL, "writev", (void *) my_writev, NULL, NULL);

谢谢!!!

@zhbzhbzhbz
Copy link

如果要处理兼容性,需要hook:open,__open_real,__open_2。 可以参考bytehook的sample app:https://github.com/bytedance/bhook/blob/main/bytehook_sample/src/main/cpp/hacker.c#L152

最近发现,c语言下的open(path, O_RDONLY);会触发open2或者open_real,只使用open确实会拦截不到

cmzy pushed a commit to cmzy/bhook that referenced this issue Mar 28, 2023
Crash Thread-> [pid:15662]:[pname:com.example_for_hidden.ph] [tid:16061]:[tname:sps-core]
    x0  00000071217f7d90  x1  000000710b01e350  x2  0000000000000000  x3  0000000000000000
    x4  8080808080000000  x5  0000000000000000  x6  0000008080808080  x7  fefefefeff6e722d
    x8  726569727261626f  x9  00000071ddf30280  x10 0000000a30203020  x11 0000000000000000
    x12 000000000000018c  x13 98e1752cb5d3e1ab  x14 007491a877137aec  x15 ffffffffffffffff
    x16 00000071637dbf20  x17 000000726dc6087c  x18 000000711de9e000  x19 0000000000000000
    x20 0000000000000000  x21 00000071d66640e0  x22 726569727261626f  x23 00000071d6664108
    x24 00000071217fc000  x25 00000071d66640e8  x26 0000000000000001  x27 0000000000000000
    x28 00000000655785c7  x29 00000071217f7dc0
    sp  00000071217f7d90  lr  00000071637d450c  pc  00000071637d4530

stack:
  #00 pc 000000000000a530  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!liblubanhook.so (offset 0x1b17000) (bh_elf_manager_refresh+1436) (BuildId: 8bf4f411698f5d0194eb5f99234231ec40b3f469)
  bytedance#1 pc 0000000000008560  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!liblubanhook.so (offset 0x1b17000) (BuildId: 8bf4f411698f5d0194eb5f99234231ec40b3f469)
  bytedance#2 pc 000000000000108c  /apex/com.android.runtime/lib64/bionic/libdl.so (dlclose+8) (BuildId: 0ef8b9fd3ba84892809321b735317a50)
  #03 pc 0000000000155264  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#4 pc 00000000001577e8  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  #05 pc 00000000000dd730  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#6 pc 000000000011fa70  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  #07 pc 000000000005bedc  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#8 pc 000000000002ffc4  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#9 pc 000000000002fbb4  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#10 pc 000000000002f4e4  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#11 pc 00000000001b2978  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/split_config.arm64_v8a.apk!libexample_for_hidden.so (offset 0x2299000) (BuildId: 606487eb6a92f9fcc2f957ccd85c2943a22edc26)
  bytedance#12 pc 00000000002daf18  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/oat/arm64/base.odex (art_jni_trampoline+152)
  bytedance#13 pc 0000000000913f54  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/oat/arm64/base.odex (com.example_for_hidden.example_for_hidden.wvvvuwwu.vwvvvuvuv+84)
  bytedance#14 pc 00000000008f018c  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/oat/arm64/base.odex (com.example_for_hidden.example_for_hidden.uvuuwwuww.vuwuwuuuw.vvwvwwwwu+1084)
  bytedance#15 pc 00000000008f0ffc  /data/app/~~4OPpN4vZ38SuvHU2NPnRnA==/com.example_for_hidden.ph-SPA1I_lJ-Zc2SPOE9_ucRQ==/oat/arm64/base.odex (com.example_for_hidden.example_for_hidden.uvuuwwuww.vuwuwuuuw.handleMessage+620)
  bytedance#16 pc 00000000006a4cf8  /system/framework/arm64/boot-framework.oat (android.os.Handler.dispatchMessage+136) (BuildId: adacda98a7a45bd33ea7f02316d4c011be2906a6)
  bytedance#17 pc 000000000074044c  /system/framework/arm64/boot-framework.oat (android.os.Looper.loop+2220) (BuildId: adacda98a7a45bd33ea7f02316d4c011be2906a6)
  bytedance#18 pc 00000000006a6ea0  /system/framework/arm64/boot-framework.oat (android.os.HandlerThread.run+544) (BuildId: adacda98a7a45bd33ea7f02316d4c011be2906a6)
  bytedance#19 pc 0000000000133564  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+548) (BuildId: 2cc47e90cab939f919f347ffb2e8950a)
  bytedance#20 pc 00000000001a8a78  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+200) (BuildId: 2cc47e90cab939f919f347ffb2e8950a)
  bytedance#21 pc 0000000000555830  /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+460) (BuildId: 2cc47e90cab939f919f347ffb2e8950a)
  bytedance#22 pc 00000000005a3fb8  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1308) (BuildId: 2cc47e90cab939f919f347ffb2e8950a)
  bytedance#23 pc 00000000000da278  /apex/com.android.runtime/lib64/bionic/libc.so!libc.so (offset 0xd2000) (__pthread_start(void*)+64) (BuildId: 1ca28d785d6567d2b225cf978ef04de5)
  bytedance#24 pc 000000000007a448  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 1ca28d785d6567d2b225cf978ef04de5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants