【Android 逆向】ART 函数抽取加壳 ③ ( 禁用 dex2oat 操作 HOOK 点介绍 | 集成 InLineHook )


两篇博客中 , 简单介绍了 禁用 dex2oat 机制 的原理 , 下面开始 实现 dex2oat 禁用功能 ;





一、禁用 dex2oat 操作 HOOK 点介绍



dex2oat 机制 的实现流程中 , 其中需要调用 exec_utils.cc 源码中的 ExecAndReturnCode 函数 , 在 ExecAndReturnCode 函数 中调用了 execve 函数 , 源码片段如下 :

int ExecAndReturnCode(std::vector<std::string>& arg_vector, std::string* error_msg) {
    
    
    if (envp == nullptr) {
    
    
      execv(program, &args[0]);
    } else {
    
    
      execve(program, &args[0], envp);
    }
    return -1;
}

源码地址 : http://aospxref.com/android-8.0.0_r36/xref/art/runtime/exec_utils.cc#ExecAndReturnCode ;





二、集成 InLineHook



在上个章节介绍的 exec_utils.cc 源码 编译后的二进制代码被封装到了 libc 库 中 ;


禁用 dex2oat 机制 , 需要 HOOK libc 库中的 execve 函数 , HOOK Java 代码 , 使用静态代理 或 动态代理 模式即可 , HOOK C 语言中的代码需要使用 InLineHook , 这里先集成 InLineHook ;

将 相关源码拷贝到 app\src\main\cpp\ 目录下 , 主要源码结构如下 :

CMakeLists.txt
│  native-lib.cpp
│
└─hook
    │  inlineHook.c
    │  relocate.c
    │  relocate.h
    │  TKHooklib.h
    │
    ├─dlfcn
    │      dlfcn_compat.c
    │      dlfcn_compat.h
    │      dlfcn_nougat.c
    │      dlfcn_nougat.h
    │
    └─include
            inlineHook.h

在 CMakeLists.txt 构建脚本中 , 进行如下配置 :

add_library( 
		# 设置函数库名称
        native-lib
        # 设置函数库类型 , 动态库
        SHARED
        # 指定 InLineHook 涉及到的 .c 源码
        hook/relocate.c
        hook/inlineHook.c
        hook/dlfcn/dlfcn_nougat.c
        hook/dlfcn/dlfcn_compat.c
        # 核心功能源码
        native-lib.cpp)

集成了 InLineHook 库之后 , 在 native-lib.cpp 中导入 inlineHook.h 头文件 , 就可以调用如下 内联钩子 InLineHook 函数 , 如 :

enum ele7en_status registerInlineHook(uint32_t target_addr, uint32_t new_addr, uint32_t **proto_addr);
enum ele7en_status inlineUnHook(uint32_t target_addr);
void inlineUnHookAll();
enum ele7en_status inlineHook(uint32_t target_addr);
void inlineHookAll();

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/127434894