Android中的so注入(inject)和挂钩(hook) - For both x86 and arm

来源:互联网 发布:软件升级方案 编辑:程序博客网 时间:2024/05/21 10:34

http://blog.csdn.net/jinzhuojun/article/details/9900105

对于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:

 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <asm/user.h>
  4. #include <asm/ptrace.h>
  5. #include <sys/ptrace.h>
  6. #include <sys/wait.h>
  7. #include <sys/mman.h>
  8. #include <dlfcn.h>
  9. #include <dirent.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <elf.h>
  13. #include <android/log.h>
  14. #if defined(__i386__)
  15. #define pt_regs user_regs_struct
  16. #endif
  17. #define ENABLE_DEBUG 1
  18. #if ENABLE_DEBUG
  19. #define LOG_TAG "INJECT"
  20. #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, fmt, ##args)
  21. #define DEBUG_PRINT(format,args...) \
  22. LOGD(format, ##args)
  23. #else
  24. #define DEBUG_PRINT(format,args...)
  25. #endif
  26. #define CPSR_T_MASK ( 1u << 5 )
  27. const char *libc_path ="/system/lib/libc.so";
  28. const char *linker_path ="/system/bin/linker";
  29. int ptrace_readdata(pid_t pid, uint8_t *src, uint8_t *buf,size_t size)
  30. {
  31. uint32_t i, j, remain;
  32. uint8_t *laddr;
  33. union u {
  34. long val;
  35. char chars[sizeof(long)];
  36. } d;
  37. j = size / 4;
  38. remain = size % 4;
  39. laddr = buf;
  40. for (i = 0; i < j; i ++) {
  41. d.val = ptrace(PTRACE_PEEKTEXT, pid, src, 0);
  42. memcpy(laddr, d.chars, 4);
  43. src += 4;
  44. laddr += 4;
  45. }
  46. if (remain > 0) {
  47. d.val = ptrace(PTRACE_PEEKTEXT, pid, src, 0);
  48. memcpy(laddr, d.chars, remain);
  49. }
  50. return 0;
  51. }
  52. int ptrace_writedata(pid_t pid, uint8_t *dest, uint8_t *data,size_t size)
  53. {
  54. uint32_t i, j, remain;
  55. uint8_t *laddr;
  56. union u {
  57. long val;
  58. char chars[sizeof(long)];
  59. } d;
  60. j = size / 4;
  61. remain = size % 4;
  62. laddr = data;
  63. for (i = 0; i < j; i ++) {
  64. memcpy(d.chars, laddr, 4);
  65. ptrace(PTRACE_POKETEXT, pid, dest, d.val);
  66. dest += 4;
  67. laddr += 4;
  68. }
  69. if (remain > 0) {
  70. d.val = ptrace(PTRACE_PEEKTEXT, pid, dest, 0);
  71. for (i = 0; i < remain; i ++) {
  72. d.chars[i] = *laddr ++;
  73. }
  74. ptrace(PTRACE_POKETEXT, pid, dest, d.val);
  75. }
  76. return 0;
  77. }
  78. #if defined(__arm__)
  79. int ptrace_call(pid_t pid, uint32_t addr,long *params, uint32_t num_params, struct pt_regs* regs)
  80. {
  81. uint32_t i;
  82. for (i = 0; i < num_params && i < 4; i ++) {
  83. regs->uregs[i] = params[i];
  84. }
  85. //
  86. // push remained params onto stack
  87. //
  88. if (i < num_params) {
  89. regs->ARM_sp -= (num_params - i) * sizeof(long) ;
  90. ptrace_writedata(pid, (void *)regs->ARM_sp, (uint8_t *)&params[i], (num_params - i) *sizeof(long));
  91. }
  92. regs->ARM_pc = addr;
  93. if (regs->ARM_pc & 1) {
  94. /* thumb */
  95. regs->ARM_pc &= (~1u);
  96. regs->ARM_cpsr |= CPSR_T_MASK;
  97. } else {
  98. /* arm */
  99. regs->ARM_cpsr &= ~CPSR_T_MASK;
  100. }
  101. regs->ARM_lr = 0;
  102. if (ptrace_setregs(pid, regs) == -1
  103. || ptrace_continue(pid) == -1) {
  104. printf("error\n");
  105. return -1;
  106. }
  107. int stat = 0;
  108. waitpid(pid, &stat, WUNTRACED);
  109. while (stat != 0xb7f) {
  110. if (ptrace_continue(pid) == -1) {
  111. printf("error\n");
  112. return -1;
  113. }
  114. waitpid(pid, &stat, WUNTRACED);
  115. }
  116. return 0;
  117. }
  118. #elif defined(__i386__)
  119. long ptrace_call(pid_t pid, uint32_t addr,long *params, uint32_t num_params, struct user_regs_struct * regs)
  120. {
  121. regs->esp -= (num_params) * sizeof(long) ;
  122. ptrace_writedata(pid, (void *)regs->esp, (uint8_t *)params, (num_params) *sizeof(long));
  123. long tmp_addr = 0x00;
  124. regs->esp -= sizeof(long);
  125. ptrace_writedata(pid, regs->esp, (char *)&tmp_addr,sizeof(tmp_addr));
  126. regs->eip = addr;
  127. if (ptrace_setregs(pid, regs) == -1
  128. || ptrace_continue( pid) == -1) {
  129. printf("error\n");
  130. return -1;
  131. }
  132. int stat = 0;
  133. waitpid(pid, &stat, WUNTRACED);
  134. while (stat != 0xb7f) {
  135. if (ptrace_continue(pid) == -1) {
  136. printf("error\n");
  137. return -1;
  138. }
  139. waitpid(pid, &stat, WUNTRACED);
  140. }
  141. return 0;
  142. }
  143. #else
  144. #error "Not supported"
  145. #endif
  146. int ptrace_getregs(pid_t pid,struct pt_regs * regs)
  147. {
  148. if (ptrace(PTRACE_GETREGS, pid, NULL, regs) < 0) {
  149. perror("ptrace_getregs: Can not get register values");
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. int ptrace_setregs(pid_t pid,struct pt_regs * regs)
  155. {
  156. if (ptrace(PTRACE_SETREGS, pid, NULL, regs) < 0) {
  157. perror("ptrace_setregs: Can not set register values");
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. int ptrace_continue(pid_t pid)
  163. {
  164. if (ptrace(PTRACE_CONT, pid, NULL, 0) < 0) {
  165. perror("ptrace_cont");
  166. return -1;
  167. }
  168. return 0;
  169. }
  170. int ptrace_attach(pid_t pid)
  171. {
  172. if (ptrace(PTRACE_ATTACH, pid, NULL, 0) < 0) {
  173. perror("ptrace_attach");
  174. return -1;
  175. }
  176. int status = 0;
  177. waitpid(pid, &status , WUNTRACED);
  178. return 0;
  179. }
  180. int ptrace_detach(pid_t pid)
  181. {
  182. if (ptrace(PTRACE_DETACH, pid, NULL, 0) < 0) {
  183. perror("ptrace_detach");
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. void* get_module_base(pid_t pid,const char* module_name)
  189. {
  190. FILE *fp;
  191. long addr = 0;
  192. char *pch;
  193. char filename[32];
  194. char line[1024];
  195. if (pid < 0) {
  196. /* self process */
  197. snprintf(filename, sizeof(filename),"/proc/self/maps", pid);
  198. } else {
  199. snprintf(filename, sizeof(filename),"/proc/%d/maps", pid);
  200. }
  201. fp = fopen(filename, "r");
  202. if (fp != NULL) {
  203. while (fgets(line, sizeof(line), fp)) {
  204. if (strstr(line, module_name)) {
  205. pch = strtok( line, "-" );
  206. addr = strtoul( pch, NULL, 16 );
  207. if (addr == 0x8000)
  208. addr = 0;
  209. break;
  210. }
  211. }
  212. fclose(fp) ;
  213. }
  214. return (void *)addr;
  215. }
  216. void* get_remote_addr(pid_t target_pid,const char* module_name,void* local_addr)
  217. {
  218. void* local_handle, *remote_handle;
  219. local_handle = get_module_base(-1, module_name);
  220. remote_handle = get_module_base(target_pid, module_name);
  221. DEBUG_PRINT("[+] get_remote_addr: local[%x], remote[%x]\n", local_handle, remote_handle);
  222. void * ret_addr = (void *)((uint32_t)local_addr + (uint32_t)remote_handle - (uint32_t)local_handle);
  223. #if defined(__i386__)
  224. if (!strcmp(module_name, libc_path)) {
  225. ret_addr += 2;
  226. }
  227. #endif
  228. return ret_addr;
  229. }
  230. int find_pid_of(constchar *process_name)
  231. {
  232. int id;
  233. pid_t pid = -1;
  234. DIR* dir;
  235. FILE *fp;
  236. char filename[32];
  237. char cmdline[256];
  238. struct dirent * entry;
  239. if (process_name == NULL)
  240. return -1;
  241. dir = opendir("/proc");
  242. if (dir == NULL)
  243. return -1;
  244. while((entry = readdir(dir)) != NULL) {
  245. id = atoi(entry->d_name);
  246. if (id != 0) {
  247. sprintf(filename, "/proc/%d/cmdline", id);
  248. fp = fopen(filename, "r");
  249. if (fp) {
  250. fgets(cmdline, sizeof(cmdline), fp);
  251. fclose(fp);
  252. if (strcmp(process_name, cmdline) == 0) {
  253. /* process found */
  254. pid = id;
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. closedir(dir);
  261. return pid;
  262. }
  263. long ptrace_retval(struct pt_regs * regs)
  264. {
  265. #if defined(__arm__)
  266. return regs->ARM_r0;
  267. #elif defined(__i386__)
  268. return regs->eax;
  269. #else
  270. #error "Not supported"
  271. #endif
  272. }
  273. long ptrace_ip(struct pt_regs * regs)
  274. {
  275. #if defined(__arm__)
  276. return regs->ARM_pc;
  277. #elif defined(__i386__)
  278. return regs->eip;
  279. #else
  280. #error "Not supported"
  281. #endif
  282. }
  283. int ptrace_call_wrapper(pid_t target_pid,const char * func_name,void * func_addr, long * parameters,int param_num, struct pt_regs * regs)
  284. {
  285. DEBUG_PRINT("[+] Calling %s in target process.\n", func_name);
  286. if (ptrace_call(target_pid, (uint32_t)func_addr, parameters, param_num, regs) == -1)
  287. return -1;
  288. if (ptrace_getregs(target_pid, regs) == -1)
  289. return -1;
  290. DEBUG_PRINT("[+] Target process returned from %s, return value=%x, pc=%x \n",
  291. func_name, ptrace_retval(regs), ptrace_ip(regs));
  292. return 0;
  293. }
  294. int inject_remote_process(pid_t target_pid,const char *library_path,const char *function_name,const char *param,size_t param_size)
  295. {
  296. int ret = -1;
  297. void *mmap_addr, *dlopen_addr, *dlsym_addr, *dlclose_addr, *dlerror_addr;
  298. void *local_handle, *remote_handle, *dlhandle;
  299. uint8_t *map_base = 0;
  300. uint8_t *dlopen_param1_ptr, *dlsym_param2_ptr, *saved_r0_pc_ptr, *inject_param_ptr, *remote_code_ptr, *local_code_ptr;
  301. struct pt_regs regs, original_regs;
  302. extern uint32_t _dlopen_addr_s, _dlopen_param1_s, _dlopen_param2_s, _dlsym_addr_s, \
  303. _dlsym_param2_s, _dlclose_addr_s, _inject_start_s, _inject_end_s, _inject_function_param_s, \
  304. _saved_cpsr_s, _saved_r0_pc_s;
  305. uint32_t code_length;
  306. long parameters[10];
  307. DEBUG_PRINT("[+] Injecting process: %d\n", target_pid);
  308. if (ptrace_attach(target_pid) == -1)
  309. goto exit;
  310. if (ptrace_getregs(target_pid, &regs) == -1)
  311. goto exit;
  312. /* save original registers */
  313. memcpy(&original_regs, &regs, sizeof(regs));
  314. mmap_addr = get_remote_addr(target_pid, libc_path, (void *)mmap);
  315. DEBUG_PRINT("[+] Remote mmap address: %x\n", mmap_addr);
  316. /* call mmap */
  317. parameters[0] = 0; // addr
  318. parameters[1] = 0x4000; // size
  319. parameters[2] = PROT_READ | PROT_WRITE | PROT_EXEC; // prot
  320. parameters[3] = MAP_ANONYMOUS | MAP_PRIVATE; // flags
  321. parameters[4] = 0; //fd
  322. parameters[5] = 0; //offset
  323. if (ptrace_call_wrapper(target_pid,"mmap", mmap_addr, parameters, 6, &regs) == -1)
  324. goto exit;
  325. map_base = ptrace_retval(&regs);
  326. dlopen_addr = get_remote_addr( target_pid, linker_path, (void *)dlopen );
  327. dlsym_addr = get_remote_addr( target_pid, linker_path, (void *)dlsym );
  328. dlclose_addr = get_remote_addr( target_pid, linker_path, (void *)dlclose );
  329. dlerror_addr = get_remote_addr( target_pid, linker_path, (void *)dlerror );
  330. DEBUG_PRINT("[+] Get imports: dlopen: %x, dlsym: %x, dlclose: %x, dlerror: %x\n",
  331. dlopen_addr, dlsym_addr, dlclose_addr, dlerror_addr);
  332. printf("library path = %s\n", library_path);
  333. ptrace_writedata(target_pid, map_base, library_path, strlen(library_path) + 1);
  334. parameters[0] = map_base;
  335. parameters[1] = RTLD_NOW| RTLD_GLOBAL;
  336. if (ptrace_call_wrapper(target_pid,"dlopen", dlopen_addr, parameters, 2, &regs) == -1)
  337. goto exit;
  338. void * sohandle = ptrace_retval(&regs);
  339. #define FUNCTION_NAME_ADDR_OFFSET 0x100
  340. ptrace_writedata(target_pid, map_base + FUNCTION_NAME_ADDR_OFFSET, function_name, strlen(function_name) + 1);
  341. parameters[0] = sohandle;
  342. parameters[1] = map_base + FUNCTION_NAME_ADDR_OFFSET;
  343. if (ptrace_call_wrapper(target_pid,"dlsym", dlsym_addr, parameters, 2, &regs) == -1)
  344. goto exit;
  345. void * hook_entry_addr = ptrace_retval(&regs);
  346. DEBUG_PRINT("hook_entry_addr = %p\n", hook_entry_addr);
  347. #define FUNCTION_PARAM_ADDR_OFFSET 0x200
  348. ptrace_writedata(target_pid, map_base + FUNCTION_PARAM_ADDR_OFFSET, param, strlen(param) + 1);
  349. parameters[0] = map_base + FUNCTION_PARAM_ADDR_OFFSET;
  350. if (ptrace_call_wrapper(target_pid,"hook_entry", hook_entry_addr, parameters, 1, &regs) == -1)
  351. goto exit;
  352. printf("Press enter to dlclose and detach\n");
  353. getchar();
  354. parameters[0] = sohandle;
  355. if (ptrace_call_wrapper(target_pid,"dlclose", dlclose, parameters, 1, &regs) == -1)
  356. goto exit;
  357. /* restore */
  358. ptrace_setregs(target_pid, &original_regs);
  359. ptrace_detach(target_pid);
  360. ret = 0;
  361. exit:
  362. return ret;
  363. }
  364. int main(int argc,char** argv) {
  365. pid_t target_pid;
  366. target_pid = find_pid_of("/system/bin/surfaceflinger");
  367. if (-1 == target_pid) {
  368. printf("Can't find the process\n");
  369. return -1;
  370. }
  371. //target_pid = find_pid_of("/data/test");
  372. inject_remote_process(target_pid, "/data/libhello.so","hook_entry", "I'm parameter!", strlen("I'm parameter!"));
  373. return 0;
  374. }

注意上面的/system/bin/surfaceflinger进程我随手写的,你的设备上不一定有。没有的话挑其它的也行,前提是ps命令里能找到。

Android.mk:

 
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE := inject
  4. LOCAL_SRC_FILES := inject.c
  5. #shellcode.s
  6. LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
  7. #LOCAL_FORCE_STATIC_EXECUTABLE := true
  8. include $(BUILD_EXECUTABLE)

 

Application.mk:

 
  1. APP_ABI := x86 armeabi-v7a

运行nkd-build编译成生x86和arm平台下的可执行文件:

 
  1. jzj@jzj-laptop:~/workspace/inject_hook/inject_jni$ ndk-build
  2. Install : inject => libs/x86/inject
  3. Install : inject => libs/armeabi-v7a/inject


再来生成要注入的so,创建目录及文件:

jni
hello.c
Android.mk
Application.mk

hello.c:

 
 
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <android/log.h>
  5. #include <elf.h>
  6. #include <fcntl.h>
  7. #define LOG_TAG "DEBUG"
  8. #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
  9. int hook_entry(char * a){
  10. LOGD("Hook success, pid = %d\n", getpid());
  11. LOGD("Hello %s\n", a);
  12. return 0;
  13. }

Android.mk:

 
 
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
  4. #LOCAL_ARM_MODE := arm
  5. LOCAL_MODULE := hello
  6. LOCAL_SRC_FILES := hello.c
  7. include $(BUILD_SHARED_LIBRARY)


Application.mk:

 
  1. APP_ABI := x86 armeabi-v7a

运行nkd-build编译成生x86和arm平台下的so:

 
  1. jzj@jzj-laptop:~/workspace/inject_hook/hook_so_simple$ ndk-build
  2. Install : libhello.so => libs/x86/libhello.so
  3. Install : libhello.so => libs/armeabi-v7a/libhello.so


然后就可以跑起来试试了,连接root过的Android设备或者打开模拟器。将inject和libhello.so拷入设备,设执行权限,执行:


先看看被注入进程(surfaceflinger)的mmap,可以看到我们的so已经被加载了,紧接着的那一块就是我们mmap出来的:

从logcat中也可以看到so注入成功,并且以被注入进程的身份执行了so中的代码:


简单的注入成功,现在我们再来做一个实验,就是应用这套机制来截获surfaceflinger中的eglSwapBuffers调用,然后用我们自己的函数来替换掉原来的eglSwapBuffers调用。关于截系统中的函数调用网上有例子http://bbs.pediy.com/showthread.php?t=157419,这里依葫芦画瓢。首先将hello.c改下:

 
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <android/log.h>
  5. #include <EGL/egl.h>
  6. #include <GLES/gl.h>
  7. #include <elf.h>
  8. #include <fcntl.h>
  9. #include <sys/mman.h>
  10. #define LOG_TAG "DEBUG"
  11. #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
  12. EGLBoolean (*old_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surf) = -1;
  13. EGLBoolean new_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
  14. {
  15. LOGD("New eglSwapBuffers\n");
  16. if (old_eglSwapBuffers == -1)
  17. LOGD("error\n");
  18. return old_eglSwapBuffers(dpy, surface);
  19. }
  20. void* get_module_base(pid_t pid,const char* module_name)
  21. {
  22. FILE *fp;
  23. long addr = 0;
  24. char *pch;
  25. char filename[32];
  26. char line[1024];
  27. if (pid < 0) {
  28. /* self process */
  29. snprintf(filename, sizeof(filename),"/proc/self/maps", pid);
  30. } else {
  31. snprintf(filename, sizeof(filename),"/proc/%d/maps", pid);
  32. }
  33. fp = fopen(filename, "r");
  34. if (fp != NULL) {
  35. while (fgets(line, sizeof(line), fp)) {
  36. if (strstr(line, module_name)) {
  37. pch = strtok( line, "-" );
  38. addr = strtoul( pch, NULL, 16 );
  39. if (addr == 0x8000)
  40. addr = 0;
  41. break;
  42. }
  43. }
  44. fclose(fp) ;
  45. }
  46. return (void *)addr;
  47. }
  48. #define LIBSF_PATH "/system/lib/libsurfaceflinger.so"
  49. int hook_eglSwapBuffers()
  50. {
  51. old_eglSwapBuffers = eglSwapBuffers;
  52. LOGD("Orig eglSwapBuffers = %p\n", old_eglSwapBuffers);
  53. void * base_addr = get_module_base(getpid(), LIBSF_PATH);
  54. LOGD("libsurfaceflinger.so address = %p\n", base_addr);
  55. int fd;
  56. fd = open(LIBSF_PATH, O_RDONLY);
  57. if (-1 == fd) {
  58. LOGD("error\n");
  59. return -1;
  60. }
  61. Elf32_Ehdr ehdr;
  62. read(fd, &ehdr, sizeof(Elf32_Ehdr));
  63. unsigned long shdr_addr = ehdr.e_shoff;
  64. int shnum = ehdr.e_shnum;
  65. int shent_size = ehdr.e_shentsize;
  66. unsigned long stridx = ehdr.e_shstrndx;
  67. Elf32_Shdr shdr;
  68. lseek(fd, shdr_addr + stridx * shent_size, SEEK_SET);
  69. read(fd, &shdr, shent_size);
  70. char * string_table = (char *)malloc(shdr.sh_size);
  71. lseek(fd, shdr.sh_offset, SEEK_SET);
  72. read(fd, string_table, shdr.sh_size);
  73. lseek(fd, shdr_addr, SEEK_SET);
  74. int i;
  75. uint32_t out_addr = 0;
  76. uint32_t out_size = 0;
  77. uint32_t got_item = 0;
  78. int32_t got_found = 0;
  79. for (i = 0; i < shnum; i++) {
  80. read(fd, &shdr, shent_size);
  81. if (shdr.sh_type == SHT_PROGBITS) {
  82. int name_idx = shdr.sh_name;
  83. if (strcmp(&(string_table[name_idx]),".got.plt") == 0
  84. || strcmp(&(string_table[name_idx]), ".got") == 0) {
  85. out_addr = base_addr + shdr.sh_addr;
  86. out_size = shdr.sh_size;
  87. LOGD("out_addr = %lx, out_size = %lx\n", out_addr, out_size);
  88. for (i = 0; i < out_size; i += 4) {
  89. got_item = *(uint32_t *)(out_addr + i);
  90. if (got_item == old_eglSwapBuffers) {
  91. LOGD("Found eglSwapBuffers in got\n");
  92. got_found = 1;
  93. uint32_t page_size = getpagesize();
  94. uint32_t entry_page_start = (out_addr + i) & (~(page_size - 1));
  95. mprotect((uint32_t *)entry_page_start, page_size, PROT_READ | PROT_WRITE);
  96. *(uint32_t *)(out_addr + i) = new_eglSwapBuffers;
  97. break;
  98. } else if (got_item == new_eglSwapBuffers) {
  99. LOGD("Already hooked\n");
  100. break;
  101. }
  102. }
  103. if (got_found)
  104. break;
  105. }
  106. }
  107. }
  108. free(string_table);
  109. close(fd);
  110. }
  111. int hook_entry(char * a){
  112. LOGD("Hook success\n");
  113. LOGD("Start hooking\n");
  114. hook_eglSwapBuffers();
  115. return 0;
  116. }

其实这种查找方法有点简单粗暴,要是正式应用的话可以在动态符号表中查找这个符号的got地址。

接着Android.mk改为:

 
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog -lEGL
  4. #LOCAL_ARM_MODE := arm
  5. LOCAL_MODULE := hello
  6. LOCAL_SRC_FILES := hello.c
  7. include $(BUILD_SHARED_LIBRARY)

Application.mk :

 
  1. APP_ABI := x86 armeabi-v7a
  2. APP_PLATFORM := android-14

运行ndk-build编译:

 
  1. jzj@jzj-laptop:~/workspace/inject_hook/hook_so$ ndk-build
  2. Install : libhello.so => libs/x86/libhello.so
  3. Install : libhello.so => libs/armeabi-v7a/libhello.so

和上面一样运行,查看logcat,我们可以看到surfaceflinger中调用eglSwapBuffers的地址已被替换成我们的版本:

 
  1. D/INJECT ( 2231): [+] Injecting process: 1728
  2. D/INJECT ( 2231): [+] get_remote_addr: local[b7e4f000], remote[b7e73000]
  3. D/INJECT ( 2231): [+] Remote mmap address: b7e9fe32
  4. D/INJECT ( 2231): [+] Calling mmap in target process.
  5. D/INJECT ( 2231): [+] Target process returned from mmap, return value=b4f1c000, pc=0
  6. D/INJECT ( 2231): [+] get_remote_addr: local[b7f00000], remote[b7f7e000]
  7. D/INJECT ( 2231): [+] get_remote_addr: local[b7f00000], remote[b7f7e000]
  8. D/INJECT ( 2231): [+] get_remote_addr: local[b7f00000], remote[b7f7e000]
  9. D/INJECT ( 2231): [+] get_remote_addr: local[b7f00000], remote[b7f7e000]
  10. D/INJECT ( 2231): [+] Get imports: dlopen: b7f84f50, dlsym: b7f84fd0, dlclose: b7f84de0, dlerror: b7f84dc0
  11. D/INJECT ( 2231): [+] Calling dlopen in target process.
  12. D/INJECT ( 2231): [+] Target process returned from dlopen, return value=b7f995ec, pc=0
  13. D/INJECT ( 2231): [+] Calling dlsym in target process.
  14. D/INJECT ( 2231): [+] Target process returned from dlsym, return value=b4f17e10, pc=0
  15. D/INJECT ( 2231): hook_entry_addr = 0xb4f17e10
  16. D/DEBUG ( 1728): Hook success
  17. D/DEBUG ( 1728): Start hooking
  18. D/DEBUG ( 1728): Orig eglSwapBuffers = 0xb7d4a9c0
  19. D/DEBUG ( 1728): libsurfaceflinger.so address = 0xb7f22000
  20. D/DEBUG ( 1728): out_addr = b7f7aff4, out_size = 624
  21. D/DEBUG ( 1728): Found eglSwapBuffers in got
  22. ...
  23. D/DEBUG ( 1728): New eglSwapBuffers
  24. D/DEBUG ( 1728): New eglSwapBuffers
  25. D/DEBUG ( 1728): New eglSwapBuffers
  26. ...

 

0 0
原创粉丝点击