成熟的App会Hook自己
2022-04-19 11:20:26

一、目标

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

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

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

  • so hook
  • Dobby

二、步骤

先把so调用起来

成熟的App会Hook自己_Android

把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

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

git clone下来。

成熟的App会Hook自己_#include_02

整个文件夹放到 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自己也是很有意义的。

成熟的App会Hook自己_Android_03

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

TIP: 本文的目的只有一个就是学习更多的逆向技巧和思路,如果有人利用本文技术去进行非法商业获取利益带来的法律责任都是操作者自己承担,和本文以及作者没关系。

关注微信公众号: 奋飞安全,最新技术干货实时推送

本文摘自 :https://blog.51cto.com/u


更多科技新闻 ......