一、指标

李老板: 奋飞呀,我都是本人了,还不是想怎么玩就怎么玩,还用Hook这么麻烦吗?

奋飞:男人要对本人狠一点。

我有一个 libtest.so,我调用它后,它会应用 android_log_print 输入一些信息,我想让它输入的内容加点私货。入手吧。

  • so hook
  • Dobby

二、步骤

先把so调用起来

把so放在cpp的同级目录 jniLibs上面。
而后跑起来,输入:

2021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call directly. 12021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call from global ptr. 12021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call from local ptr. 12021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call from local ptr2. 1 (definitely failed when compiled with -O0)

咱们的指标就是在这些输入外面加点私货。

Dobby

https://github.com/jmpews/Dobby 是一个多平台的Hook库,反正很牛就对了。

git clone下来。

整个文件夹放到 CMakeLists.txtnative-lib.cpp 同级目录上面。

而后编辑 CMakeLists.txt 文件

# 这里指定动态链接,生成一个so;默认为 ON,生成两个soset(GENERATE_SHARED OFF)# 指定 dobby 库目录set(DOBBY_SOURCE_DIR Dobby)add_subdirectory(${DOBBY_SOURCE_DIR} dobby.out)#end......# target_link_libraries 局部减少 dobbytarget_link_libraries( # Specifies the target library.                       native-lib                       dobby                       # Links the target library to the log library                       # included in the NDK.                       ${log-lib} )

而后加上Hook代码

#include <android/log.h>#include "Dobby/include/dobby.h"static int (*orig_log_print)(int prio, const char* tag, const char* fmt, ...);static int my_libtest_log_print(int prio, const char* tag, const char* fmt, ...){    va_list ap;    char buf[1024];    int r;    snprintf(buf, sizeof(buf), "[%s] %s", (NULL == tag ? "" : tag), (NULL == fmt ? "" : fmt));    va_start(ap, fmt);    r = __android_log_vprint(prio, "Dobby_libtest", buf, ap);    va_end(ap);    return r;}__attribute__((constructor)) static void ctor() {    DobbyHook((void *) DobbySymbolResolver(NULL, "__android_log_print"), (void *) my_libtest_log_print,(void **) &orig_log_print);}

跑起来,体验一下。

2021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call directly. 12021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call from global ptr. 12021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call from local ptr. 12021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call from local ptr2. 1 (definitely failed when compiled with -O0)

私货整进去了, mytest: 整成了 Dobby_libtest: [mytest]

三、总结

Hook是经久不衰的话题,除了Hook他人,Hook本人也是很有意义的。

有的货色吧,外行人看起来很厉害,然而咱们内行人看起来吧,那真xxx不是个别的厉害