常見的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中被保護的代碼,在手機上效率是一個問題。
對應的脫殼思路
1.1 針對破壞Elf Header和刪除Section Header
裏面有個知識點,需要說明下:
從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:
- Dynamic section at offset 0x2e8c contains 32 entries:
- Tag Type Name/Value
- 0x00000003 (PLTGOT) 0x3fd4
- 0x00000002 (PLTRELSZ) 64 (bytes)
- 0x00000017 (JMPREL) 0xc74
- 0x00000014 (PLTREL) REL
- 0x00000011 (REL) 0xc24
- 0x00000012 (RELSZ) 80 (bytes)
- 0x00000013 (RELENT) 8 (bytes)
- 0x6ffffffa (RELCOUNT) 7
- 0x00000006 (SYMTAB) 0x18c
- 0x0000000b (SYMENT) 16 (bytes)
- 0x00000005 (STRTAB) 0x51c
- 0x0000000a (STRSZ) 1239 (bytes)
- 0x00000004 (HASH) 0x9f4
- 0x00000001 (NEEDED) Shared library: [liblog.so]
- 0x00000001 (NEEDED) Shared library: [libdl.so]
- 0x00000001 (NEEDED) Shared library: [libstdc++.so]
- 0x00000001 (NEEDED) Shared library: [libm.so]
- 0x00000001 (NEEDED) Shared library: [libc.so]
- 0x0000000e (SONAME) Library soname: [libPLTUtils.so]
- 0x0000001a (FINI_ARRAY) 0x3e80
- 0x0000001c (FINI_ARRAYSZ) 8 (bytes)
- 0x00000019 (INIT_ARRAY) 0x3e88
- 0x0000001b (INIT_ARRAYSZ) 4 (bytes)
- 0x00000010 (SYMBOLIC) 0x0
- 0x0000001e (FLAGS) SYMBOLIC BIND_NOW
- 0x6ffffffb (FLAGS_1) Flags: NOW
- 0x6ffffff0 (VERSYM) 0xb74
- 0x6ffffffc (VERDEF) 0xbe8
- 0x6ffffffd (VERDEFNUM) 1
- 0x6ffffffe (VERNEED) 0xc04
- 0x6fffffff (VERNEEDNUM) 1
- 0x00000000 (NULL) 0x0
然後arm-linux-androideabi-readelf -r ~/Public/libPLTUtils.so:
- Relocation section '.rel.dyn' at offset 0xc24 contains 10 entries:
- Offset Info Type Sym.Value Sym. Name
- 00003e80 00000017 R_ARM_RELATIVE
- 00003fb4 00000017 R_ARM_RELATIVE
- 00003fb8 00000017 R_ARM_RELATIVE
- 00003fbc 00000017 R_ARM_RELATIVE
- 00003fc0 00000017 R_ARM_RELATIVE
- 00003fc8 00000017 R_ARM_RELATIVE
- 00003fcc 00000017 R_ARM_RELATIVE
- 00004004 00000402 R_ARM_ABS32 00000000 puts
- 00003fc4 00000915 R_ARM_GLOB_DAT 00000000 __gnu_Unwind_Find_exid
- 00003fd0 00001f15 R_ARM_GLOB_DAT 00000000 __cxa_call_unexpected
- Relocation section '.rel.plt' at offset 0xc74 contains 8 entries:
- Offset Info Type Sym.Value Sym. Name
- 00003fe0 00000216 R_ARM_JUMP_SLOT 00000000 __cxa_atexit
- 00003fe4 00000116 R_ARM_JUMP_SLOT 00000000 __cxa_finalize
- 00003fe8 00000416 R_ARM_JUMP_SLOT 00000000 puts
- 00003fec 00000916 R_ARM_JUMP_SLOT 00000000 __gnu_Unwind_Find_exid
- 00003ff0 00000f16 R_ARM_JUMP_SLOT 00000000 abort
- 00003ff4 00001116 R_ARM_JUMP_SLOT 00000000 memcpy
- 00003ff8 00001c16 R_ARM_JUMP_SLOT 00000000 __cxa_begin_cleanup
- 00003ffc 00001d16 R_ARM_JUMP_SLOT 00000000 __cxa_type_match
在ida中,是這樣的。
- .got:00003FB4 ; ===========================================================================
- .got:00003FB4
- .got:00003FB4 ; Segment type: Pure data
- .got:00003FB4 AREA .got, DATA
- .got:00003FB4 ; ORG 0x3FB4
- .got:00003FB4 global_fun_ptr DCD global_fun ; DATA XREF: Java_com_example_ndkplt_PLTUtils_pltTest+16o
- .got:00003FB4 ; Java_com_example_ndkplt_PLTUtils_pltTest+18r ...
- .got:00003FB8 __aeabi_unwind_cpp_pr0_ptr DCD __aeabi_unwind_cpp_pr0 ; DATA XREF: sub_E54+1Cr
- .got:00003FB8 ; .text:off_E98o
- .got:00003FBC __aeabi_unwind_cpp_pr1_ptr DCD __aeabi_unwind_cpp_pr1 ; DATA XREF: sub_E54+28r
- .got:00003FBC ; .text:off_E9Co
- .got:00003FC0 __aeabi_unwind_cpp_pr2_ptr DCD __aeabi_unwind_cpp_pr2 ; DATA XREF: sub_E54+34r
- .got:00003FC0 ; .text:off_EA0o
- .got:00003FC4 __gnu_Unwind_Find_exidx_ptr DCD __imp___gnu_Unwind_Find_exidx
- .got:00003FC4 ; DATA XREF: sub_EA4+8r
- .got:00003FC4 ; .text:off_F9Co
- .got:00003FC8 off_3FC8 DCD aCallTheMethod ; DATA XREF: sub_EA4+48r
- .got:00003FC8 ; .text:off_FA0o
- .got:00003FC8 ; "call the method"
- .got:00003FCC off_3FCC DCD 0x2304 ; DATA XREF: sub_EA4+4Cr
- .got:00003FCC ; .text:off_FA4o
- .got:00003FD0 __cxa_call_unexpected_ptr DCD __cxa_call_unexpected ; DATA XREF: sub_150C+3A8r
- .got:00003FD0 ; .text:off_18F8o
- .got:00003FD4 _GLOBAL_OFFSET_TABLE_ DCD 0 ; DATA XREF: .plt:00000CBCo
- .got:00003FD4 ; .plt:off_CC4o
- .got:00003FD8 DCD 0
- .got:00003FDC DCD 0
- .got:00003FE0 __cxa_atexit_ptr DCD __imp___cxa_atexit ; DATA XREF: __cxa_atexit+8r
- .got:00003FE4 __cxa_finalize_ptr DCD __imp___cxa_finalize ; DATA XREF: __cxa_finalize+8r
- .got:00003FE8 puts_ptr DCD __imp_puts ; DATA XREF: puts+8r
- .got:00003FEC __gnu_Unwind_Find_exidx_ptr_0 DCD __imp___gnu_Unwind_Find_exidx
- .got:00003FEC ; DATA XREF: __gnu_Unwind_Find_exidx+8r
- .got:00003FF0 abort_ptr DCD __imp_abort ; DATA XREF: abort+8r
- .got:00003FF4 memcpy_ptr DCD __imp_memcpy ; DATA XREF: memcpy+8r
- .got:00003FF8 __cxa_begin_cleanup_ptr DCD __imp___cxa_begin_cleanup
- .got:00003FF8 ; DATA XREF: __cxa_begin_cleanup+8r
- .got:00003FFC __cxa_type_match_ptr DCD __imp___cxa_type_match
- .got:00003FFC ; DATA XREF: __cxa_type_match+8r
- .got:00003FFC ; .got ends
- 00004004 00000402 R_ARM_ABS32 00000000 puts
- 00003fc4 00000915 R_ARM_GLOB_DAT 00000000 __gnu_Unwind_Find_exid
- 00003fd0 00001f15 R_ARM_GLOB_DAT 00000000 __cxa_call_unexpected
- 00003fe0 00000216 R_ARM_JUMP_SLOT 00000000 __cxa_atexit
- 00003fe4 00000116 R_ARM_JUMP_SLOT 00000000 __cxa_finalize
- 00003fe8 00000416 R_ARM_JUMP_SLOT 00000000 puts
- 00003fec 00000916 R_ARM_JUMP_SLOT 00000000 __gnu_Unwind_Find_exid
- 00003ff0 00000f16 R_ARM_JUMP_SLOT 00000000 abort
- 00003ff4 00001116 R_ARM_JUMP_SLOT 00000000 memcpy
- 00003ff8 00001c16 R_ARM_JUMP_SLOT 00000000 __cxa_begin_cleanup
- 00003ffc 00001d16 R_ARM_JUMP_SLOT 00000000 __cxa_type_match
使用dlopen來加載so,返回一個soinfo結構體如下:
- struct soinfo {
- const char name[SOINFO_NAME_LEN]; Elf32_Phdr *phdr; //Elf32_Phdr 實際內存地址 int phnum;
- unsigned entry;
- unsigned base; //SO 起始
- unsigned size; //內存對齊後佔用大小
- int unused; // DO NOT USE, maintained for compatibility. unsigned *dynamic; //.dynamic 實際內存地址
- unsigned wrprotect_start; //mprotect 調用 unsigned wrprotect_end;
- soinfo *next; //下一個 soinfo unsigned flags;
- const char *strtab; //.strtab 實際內存地址 Elf32_Sym *symtab; //. symtab 實際內存地址
- //hash 起始位置:bucket – 2 * sizeof(int) unsigned nbucket; //size = nbucket * sizeof(int) unsigned nchain; //size = nchain * sizeof(int) unsigned *bucket;
- unsigned *chain;
- unsigned *plt_got; //對應.dynamic: DT_PLTGOT Elf32_Rel *plt_rel; //函數重定位表
- unsigned plt_rel_count;
- Elf32_Rel *rel; //符號重定位表 unsigned rel_count;
- ....
- };
得到這個結構體時,已經執行了init_array,已經實現瞭解密。剩下的工作就是如何恢復原so了,這部分參考ELF section修復的一些思考和從零打造簡單的SODUMP工具。
1.3 針對無源碼加密Section或者函數和自定義loader來加載SO,即從內存加載SO
和針對有源碼加密Section或者函數類似,但不像原來那樣,只要在ndk開發中調用dlopen即可。從soinfo結構體恢復so文件的時機,要選擇在Android源碼中,具體時機,如果日後有需要,再做研究。