so注入(inject)和挂钩(hook) 以及同进程动态库so文件的函数hook方法介绍 |
您所在的位置:网站首页 › 安卓注入so › so注入(inject)和挂钩(hook) 以及同进程动态库so文件的函数hook方法介绍 |
对于Android for arm上的so注入(inject)和挂钩(hook),网上已有牛人给出了代码-libinject(http://bbs.pediy.com/showthread.php?t=141355)。由于实现中的ptrace函数是依赖于平台的,所以不经改动只能用于arm平台。本文将之扩展了一下,使它能够通用于Android的x86和arm平台。Arm平台部分基本重用了libinject中的代码,其中因为汇编不好移植且容易出错,所以把shellcode.s用ptrace_call替换掉了,另外保留了mmap,用来传字符串参数,当然也可以通过栈来传,但栈里和其它东西混一起,一弄不好就会隔儿了,所以还是保险点好。最后注意设备要root。 首先创建目录及文件: jni inject.c Android.mk Application.mk inject.c: #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(__i386__) #define pt_regs user_regs_struct #endif #define ENABLE_DEBUG 1 #if ENABLE_DEBUG #define LOG_TAG "INJECT" #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, fmt, ##args) #define DEBUG_PRINT(format,args...) \ LOGD(format, ##args) #else #define DEBUG_PRINT(format,args...) #endif #define CPSR_T_MASK ( 1u ARM_pc &= (~1u); regs->ARM_cpsr |= CPSR_T_MASK; } else { /* arm */ regs->ARM_cpsr &= ~CPSR_T_MASK; } regs->ARM_lr = 0; if (ptrace_setregs(pid, regs) == -1 || ptrace_continue(pid) == -1) { printf("error\n"); return -1; } int stat = 0; waitpid(pid, &stat, WUNTRACED); while (stat != 0xb7f) { if (ptrace_continue(pid) == -1) { printf("error\n"); return -1; } waitpid(pid, &stat, WUNTRACED); } return 0; } #elif defined(__i386__) long ptrace_call(pid_t pid, uint32_t addr, long *params, uint32_t num_params, struct user_regs_struct * regs) { regs->esp -= (num_params) * sizeof(long) ; ptrace_writedata(pid, (void *)regs->esp, (uint8_t *)params, (num_params) * sizeof(long)); long tmp_addr = 0x00; regs->esp -= sizeof(long); ptrace_writedata(pid, regs->esp, (char *)&tmp_addr, sizeof(tmp_addr)); regs->eip = addr; if (ptrace_setregs(pid, regs) == -1 || ptrace_continue( pid) == -1) { printf("error\n"); return -1; } int stat = 0; waitpid(pid, &stat, WUNTRACED); while (stat != 0xb7f) { if (ptrace_continue(pid) == -1) { printf("error\n"); return -1; } waitpid(pid, &stat, WUNTRACED); } return 0; } #else #error "Not supported" #endif int ptrace_getregs(pid_t pid, struct pt_regs * regs) { if (ptrace(PTRACE_GETREGS, pid, NULL, regs) libs/x86/inject Install : inject => libs/armeabi-v7a/inject 再来生成要注入的so,创建目录及文件:jni hello.c Android.mk Application.mk hello.c: #include #include #include #include #include #include #define LOG_TAG "DEBUG" #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) int hook_entry(char * a){ LOGD("Hook success, pid = %d\n", getpid()); LOGD("Hello %s\n", a); return 0; } Android.mk: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog #LOCAL_ARM_MODE := arm LOCAL_MODULE := hello LOCAL_SRC_FILES := hello.c include $(BUILD_SHARED_LIBRARY) Application.mk: APP_ABI := x86 armeabi-v7a 运行nkd-build编译成生x86和arm平台下的so: jzj@jzj-laptop:~/workspace/inject_hook/hook_so_simple$ ndk-build Install : libhello.so => libs/x86/libhello.so Install : libhello.so => libs/armeabi-v7a/libhello.so 然后就可以跑起来试试了,连接root过的Android设备或者打开模拟器。将inject和libhello.so拷入设备,设执行权限,执行:先看看被注入进程(surfaceflinger)的mmap,可以看到我们的so已经被加载了,紧接着的那一块就是我们mmap出来的: 从logcat中也可以看到so注入成功,并且以被注入进程的身份执行了so中的代码: 同进程动态库so文件的函数hook方法 简单的注入成功,现在我们再来做一个实验,就是应用这套机制来截获surfaceflinger中的eglSwapBuffers调用,然后用我们自己的函数来替换掉原来的eglSwapBuffers调用。关于截系统中的函数调用网上有例子http://bbs.pediy.com/showthread.php?t=157419,这里依葫芦画瓢。首先将hello.c改下: #include #include #include #include #include #include #include #include #include #define LOG_TAG "DEBUG" #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) EGLBoolean (*old_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surf) = -1; EGLBoolean new_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { LOGD("New eglSwapBuffers\n"); if (old_eglSwapBuffers == -1) LOGD("error\n"); return old_eglSwapBuffers(dpy, surface); } void* get_module_base(pid_t pid, const char* module_name) { FILE *fp; long addr = 0; char *pch; char filename[32]; char line[1024]; if (pid |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |