Android SO 加壳(加密)与脱壳思路

原帖地址:http://blog.csdn.net/jltxgcy/article/details/52205210


0x01 常见的Android SO加壳(加密)思路

    1.1 破坏Elf Header

    将Elf32_Ehdr 中的e_shoff, e_shnum, e_shstrndx, e_shentsize字段处理,变为无效值。由于在链接过程中,这些字段是无用的,所以可以随意修改,这会导致ida打不开这个so文件。


    1.2 删除Section Header

    同样的道理,在链接过程中,Section Header是没有用到的,可以随意删除,这会导致ida打不开这个so文件。


    1.3 有源码加密Section或者函数

    一是对section加壳,一个是对函数加壳。参考Android逆向之旅---基于对so中的section加密技术实现so加固Android逆向之旅---基于对so中的函数加密技术实现so加固


    1.4 无源码加密Section或者函数

    将解密函数放在另一个so中,只需保证解密函数在被加密函数执行前执行即可。和其他so一样,解密so的加载也放在类的初始化static{}中。我们可以在解密so加载过程中执行解密函数,就能保证被加密函数执行前解密。执行时机可以选在linker执行.init_array时,也可以选在OnLoad函数中。当然,解密so一定要放在被解密so后加载。否则,搜索进程空间找不到被解密的so。

    详细介绍和代码:无源码加解密实现 && NDK Native Hook 


    1.5 自定义loader来加载SO,即从内存加载SO

    我们可以DIY SO,然后使用我们自定义的loader来加载,破解难度又加大了。详解的介绍参考SO文件格式及linker机制学习总结(1)SO文件格式及linker机制学习总结(2)


    1.6 在原so外面加一层壳,如下图

    

     packed so相当于把loader的代码插入到原so的init_array或者jni_onload处,然后重新打包成packed so,加载这个so,首先执行init_array或者jni_onload,在这里完成对原so的解密,从内存加载,并形成soinfo结构,然后替换原packed so的soinfo结构。


    1.7 llvm源码级混淆

    

    Clang ( 发音为 /klæŋ/) 是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程语言。Clang 对源程序进行词法分析和语义分析,并将分析结果转换为 Abstract Syntax Tree ( 抽象语法树 ) ,最后使用 LLVM 作为后端代码的生成器。

    在Android llvm源码级混淆比较成熟的是Safengine。

    如果想自己研究llvm源码级混淆,可以参考Android LLVM-Obfuscator C/C++ 混淆编译的深入研究,通过修改NDK的编译工具,来实现编译混淆。


    1.8 花指令

    通过在C语言中,内嵌arm汇编的方式,可以加入arm花指令,迷惑IDA。

 

    1.9 so vmp保护

    写一个arm虚拟机虚拟执行so中被保护的代码,在手机上效率是一个问题。    


    0x02 对应的脱壳思路

    1.1 针对破坏Elf Header和删除Section Header

    参考ELF section修复的一些思考

    里面有个知识点,需要说明下:

    从DT_PLTGOT可以得到__global_offset_table的偏移位置。由got表的结构知道,__global_offset_table前是rel.dyn重定位结构,之后为rel.plt重定位结构,都与rel一一对应。我们还是以libPLTUtils.so为例,下载地址:http://download.csdn.net/detail/jltxgcy/9602803

    arm-linux-androideabi-readelf -d ~/Public/libPLTUtils.so:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Dynamic section at offset 0x2e8c contains 32 entries:  
  2.   Tag        Type                         Name/Value  
  3.  0x00000003 (PLTGOT)                     0x3fd4  
  4.  0x00000002 (PLTRELSZ)                   64 (bytes)  
  5.  0x00000017 (JMPREL)                     0xc74  
  6.  0x00000014 (PLTREL)                     REL  
  7.  0x00000011 (REL)                        0xc24  
  8.  0x00000012 (RELSZ)                      80 (bytes)  
  9.  0x00000013 (RELENT)                     8 (bytes)  
  10.  0x6ffffffa (RELCOUNT)                   7  
  11.  0x00000006 (SYMTAB)                     0x18c  
  12.  0x0000000b (SYMENT)                     16 (bytes)  
  13.  0x00000005 (STRTAB)                     0x51c  
  14.  0x0000000a (STRSZ)                      1239 (bytes)  
  15.  0x00000004 (HASH)                       0x9f4  
  16.  0x00000001 (NEEDED)                     Shared library: [liblog.so]  
  17.  0x00000001 (NEEDED)                     Shared library: [libdl.so]  
  18.  0x00000001 (NEEDED)                     Shared library: [libstdc++.so]  
  19.  0x00000001 (NEEDED)                     Shared library: [libm.so]  
  20.  0x00000001 (NEEDED)                     Shared library: [libc.so]  
  21.  0x0000000e (SONAME)                     Library soname: [libPLTUtils.so]  
  22.  0x0000001a (FINI_ARRAY)                 0x3e80  
  23.  0x0000001c (FINI_ARRAYSZ)               8 (bytes)  
  24.  0x00000019 (INIT_ARRAY)                 0x3e88  
  25.  0x0000001b (INIT_ARRAYSZ)               4 (bytes)  
  26.  0x00000010 (SYMBOLIC)                   0x0  
  27.  0x0000001e (FLAGS)                      SYMBOLIC BIND_NOW  
  28.  0x6ffffffb (FLAGS_1)                    Flags: NOW  
  29.  0x6ffffff0 (VERSYM)                     0xb74  
  30.  0x6ffffffc (VERDEF)                     0xbe8  
  31.  0x6ffffffd (VERDEFNUM)                  1  
  32.  0x6ffffffe (VERNEED)                    0xc04  
  33.  0x6fffffff (VERNEEDNUM)                 1  
  34.  0x00000000 (NULL)                       0x0  
    我们看到PLTGOT的value为0x3fb4。

    然后arm-linux-androideabi-readelf -r ~/Public/libPLTUtils.so:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Relocation section '.rel.dyn' at offset 0xc24 contains 10 entries:  
  2.  Offset     Info    Type            Sym.Value  Sym. Name  
  3. 00003e80  00000017 R_ARM_RELATIVE     
  4. 00003fb4  00000017 R_ARM_RELATIVE     
  5. 00003fb8  00000017 R_ARM_RELATIVE     
  6. 00003fbc  00000017 R_ARM_RELATIVE     
  7. 00003fc0  00000017 R_ARM_RELATIVE     
  8. 00003fc8  00000017 R_ARM_RELATIVE     
  9. 00003fcc  00000017 R_ARM_RELATIVE     
  10. 00004004  00000402 R_ARM_ABS32       00000000   puts  
  11. 00003fc4  00000915 R_ARM_GLOB_DAT    00000000   __gnu_Unwind_Find_exid  
  12. 00003fd0  00001f15 R_ARM_GLOB_DAT    00000000   __cxa_call_unexpected  
  13.   
  14. Relocation section '.rel.plt' at offset 0xc74 contains 8 entries:  
  15.  Offset     Info    Type            Sym.Value  Sym. Name  
  16. 00003fe0  00000216 R_ARM_JUMP_SLOT   00000000   __cxa_atexit  
  17. 00003fe4  00000116 R_ARM_JUMP_SLOT   00000000   __cxa_finalize  
  18. 00003fe8  00000416 R_ARM_JUMP_SLOT   00000000   puts  
  19. 00003fec  00000916 R_ARM_JUMP_SLOT   00000000   __gnu_Unwind_Find_exid  
  20. 00003ff0  00000f16 R_ARM_JUMP_SLOT   00000000   abort  
  21. 00003ff4  00001116 R_ARM_JUMP_SLOT   00000000   memcpy  
  22. 00003ff8  00001c16 R_ARM_JUMP_SLOT   00000000   __cxa_begin_cleanup  
  23. 00003ffc  00001d16 R_ARM_JUMP_SLOT   00000000   __cxa_type_match  
    我们可以看到0x00003fb4恰好是其分界线。

    在ida中,是这样的。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. .got:00003FB4 ; ===========================================================================  
  2. .got:00003FB4  
  3. .got:00003FB4 ; Segment type: Pure data  
  4. .got:00003FB4                 AREA .got, DATA  
  5. .got:00003FB4                 ; ORG 0x3FB4  
  6. .got:00003FB4 global_fun_ptr  DCD global_fun          ; DATA XREF: Java_com_example_ndkplt_PLTUtils_pltTest+16o  
  7. .got:00003FB4                                         ; Java_com_example_ndkplt_PLTUtils_pltTest+18r ...  
  8. .got:00003FB8 __aeabi_unwind_cpp_pr0_ptr DCD __aeabi_unwind_cpp_pr0 ; DATA XREF: sub_E54+1Cr  
  9. .got:00003FB8                                         ; .text:off_E98o  
  10. .got:00003FBC __aeabi_unwind_cpp_pr1_ptr DCD __aeabi_unwind_cpp_pr1 ; DATA XREF: sub_E54+28r  
  11. .got:00003FBC                                         ; .text:off_E9Co  
  12. .got:00003FC0 __aeabi_unwind_cpp_pr2_ptr DCD __aeabi_unwind_cpp_pr2 ; DATA XREF: sub_E54+34r  
  13. .got:00003FC0                                         ; .text:off_EA0o  
  14. .got:00003FC4 __gnu_Unwind_Find_exidx_ptr DCD __imp___gnu_Unwind_Find_exidx  
  15. .got:00003FC4                                         ; DATA XREF: sub_EA4+8r  
  16. .got:00003FC4                                         ; .text:off_F9Co  
  17. .got:00003FC8 off_3FC8        DCD aCallTheMethod      ; DATA XREF: sub_EA4+48r  
  18. .got:00003FC8                                         ; .text:off_FA0o  
  19. .got:00003FC8                                         ; "call the method"  
  20. .got:00003FCC off_3FCC        DCD 0x2304              ; DATA XREF: sub_EA4+4Cr  
  21. .got:00003FCC                                         ; .text:off_FA4o  
  22. .got:00003FD0 __cxa_call_unexpected_ptr DCD __cxa_call_unexpected ; DATA XREF: sub_150C+3A8r  
  23. .got:00003FD0                                         ; .text:off_18F8o  
  24. .got:00003FD4 _GLOBAL_OFFSET_TABLE_ DCD 0             ; DATA XREF: .plt:00000CBCo  
  25. .got:00003FD4                                         ; .plt:off_CC4o  
  26. .got:00003FD8                 DCD 0  
  27. .got:00003FDC                 DCD 0  
  28. .got:00003FE0 __cxa_atexit_ptr DCD __imp___cxa_atexit ; DATA XREF: __cxa_atexit+8r  
  29. .got:00003FE4 __cxa_finalize_ptr DCD __imp___cxa_finalize ; DATA XREF: __cxa_finalize+8r  
  30. .got:00003FE8 puts_ptr        DCD __imp_puts          ; DATA XREF: puts+8r  
  31. .got:00003FEC __gnu_Unwind_Find_exidx_ptr_0 DCD __imp___gnu_Unwind_Find_exidx  
  32. .got:00003FEC                                         ; DATA XREF: __gnu_Unwind_Find_exidx+8r  
  33. .got:00003FF0 abort_ptr       DCD __imp_abort         ; DATA XREF: abort+8r  
  34. .got:00003FF4 memcpy_ptr      DCD __imp_memcpy        ; DATA XREF: memcpy+8r  
  35. .got:00003FF8 __cxa_begin_cleanup_ptr DCD __imp___cxa_begin_cleanup  
  36. .got:00003FF8                                         ; DATA XREF: __cxa_begin_cleanup+8r  
  37. .got:00003FFC __cxa_type_match_ptr DCD __imp___cxa_type_match  
  38. .got:00003FFC                                         ; DATA XREF: __cxa_type_match+8r  
  39. .got:00003FFC ; .got          ends  
    我们在 0x00003fb4地址前,看到了:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 00004004  00000402 R_ARM_ABS32       00000000   puts  
  2. 00003fc4  00000915 R_ARM_GLOB_DAT    00000000   __gnu_Unwind_Find_exid  
  3. 00003fd0  00001f15 R_ARM_GLOB_DAT    00000000   __cxa_call_unexpected  
    在这个地址后面,看到了:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 00003fe0  00000216 R_ARM_JUMP_SLOT   00000000   __cxa_atexit  
  2. 00003fe4  00000116 R_ARM_JUMP_SLOT   00000000   __cxa_finalize  
  3. 00003fe8  00000416 R_ARM_JUMP_SLOT   00000000   puts  
  4. 00003fec  00000916 R_ARM_JUMP_SLOT   00000000   __gnu_Unwind_Find_exid  
  5. 00003ff0  00000f16 R_ARM_JUMP_SLOT   00000000   abort  
  6. 00003ff4  00001116 R_ARM_JUMP_SLOT   00000000   memcpy  
  7. 00003ff8  00001c16 R_ARM_JUMP_SLOT   00000000   __cxa_begin_cleanup  
  8. 00003ffc  00001d16 R_ARM_JUMP_SLOT   00000000   __cxa_type_match  


     1.2 针对有源码加密Section或者函数
    使用dlopen来加载so,返回一个soinfo结构体如下:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. struct soinfo {  
  2.     const char name[SOINFO_NAME_LEN]; Elf32_Phdr *phdr; //Elf32_Phdr 实际内存地址 int phnum;  
  3.     unsigned entry;  
  4.     unsigned base; //SO 起始  
  5.     unsigned size; //内存对齐后占用大小  
  6.     int unused; // DO NOT USE, maintained for compatibility. unsigned *dynamic; //.dynamic 实际内存地址  
  7.     unsigned wrprotect_start; //mprotect 调用 unsigned wrprotect_end;  
  8.     soinfo *next; //下一个 soinfo unsigned flags;  
  9.     const char *strtab; //.strtab 实际内存地址 Elf32_Sym *symtab; //. symtab 实际内存地址  
  10.     //hash 起始位置:bucket – 2 * sizeof(int) unsigned nbucket; //size = nbucket * sizeof(int) unsigned nchain; //size = nchain * sizeof(int) unsigned *bucket;  
  11.     unsigned *chain;  
  12.     unsigned *plt_got; //对应.dynamic: DT_PLTGOT Elf32_Rel *plt_rel; //函数重定位表  
  13.     unsigned plt_rel_count;  
  14.     Elf32_Rel *rel; //符号重定位表 unsigned rel_count;  
  15.     ....  
  16. };  

    得到这个结构体时,已经执行了init_array,已经实现了解密。剩下的工作就是如何恢复原so了,这部分参考ELF section修复的一些思考和从零打造简单的SODUMP工具


    1.3 针对无源码加密Section或者函数和自定义loader来加载SO,即从内存加载SO

    和针对有源码加密Section或者函数类似,但不像原来那样,只要在ndk开发中调用dlopen即可。从soinfo结构体恢复so文件的时机,要选择在Android源码中,具体时机,如果日后有需要,再做研究。

发布了60 篇原创文章 · 获赞 44 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/beyond702/article/details/53786662