一、目标

一、目标

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

奋飞:男人要对自己狠一点。

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

  • so hook
  • Dobby

二、步骤

先把so调用起来

loadso
1:loadso

把so放在cpp的同级目录 jniLibs下面。 然后跑起来,输出:

2021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call directly. 1
2021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call from global ptr. 1
2021-06-11 09:45:11.185 17916-18002/com.fenfei.dobbydemo D/mytest: call from local ptr. 1
2021-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库,反正很牛X就对了。

git clone下来。

dobby
1:dobby

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

然后编辑 CMakeLists.txt 文件

# so; ONso
set(GENERATE_SHARED OFF)
#  dobby 
set(DOBBY_SOURCE_DIR Dobby)
add_subdirectory(${DOBBY_SOURCE_DIR} dobby.out)
#end

......

# target_link_libraries  dobby
target_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. 1
2021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call from global ptr. 1
2021-06-11 10:23:12.175 30447-30493/com.fenfei.dobbydemo D/Dobby_libtest: [mytest] call from local ptr. 1
2021-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自己也是很有意义的。

ffshow
1:ffshow

有的东西吧,外行人看起来很厉害,但是我们内行人看起来吧,那真xxx不是一般的厉害

100

关注微信公众号,最新技术干货实时推送

100