关于android:成熟的App会Hook自己

7次阅读

共计 2098 个字符,预计需要花费 6 分钟才能阅读完成。

一、指标

李老板:奋飞呀,我都是本人了,还不是想怎么玩就怎么玩,还用 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. 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 库,反正很牛就对了。

git clone 下来。

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

而后编辑 CMakeLists.txt 文件

# 这里指定动态链接,生成一个 so; 默认为 ON,生成两个 so
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 本人也是很有意义的。

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

正文完
 0