iOS安全攻防(十六)看懂mach-o(1)
在win下搞逆向需要看懂pe,同樣搞iOS安全攻防必須看懂mach-o格式,水果的官方mach-o文件在此:https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachORuntime/index.html
本文中實際專案分析,來達到“看懂”的目的,使用的工具還是前一篇blog所用的hopper。
先將目標檔案拖進hopper,我這裡針對的是ARMv7架構的分析,選擇顯示segment list:
然後選擇第一行,到達mach-o header區域:
hopper中間顯示如下:
hopper的註釋已經比較詳細了,在xcode下,開啟mach-o/loader.h ,看下header結構的定義:
根據以上2圖,對應分析:
1.magic,是mach-o檔案的魔數,0xfeedface代表的是32位,0xfeedfacf代表64位
2.cputype和cupsubtype代表的是cpu的型別和其子型別,有點拗口,直接看mach/machine.h中的定義,例子中分別是c和9,定義如下:
#define CPU_TYPE_ARM((cpu_type_t) 12)
#define CPU_SUBTYPE_ARM_V7((cpu_subtype_t) 9)
載入檔案的時候hopper 提示選擇架構型別,就是ARMv7
3.接著是filetype,例子中的值是2
#defineMH_EXECUTE 0x2/* demand paged executable file */
4.ncmds 指的是載入命令(load commands)的數量,例子中一共23個,編號0-22
5.sizeofcmds 表示23個load commands的總位元組大小, load commands區域是緊接著header區域的。
6.最後個flags,例子中是0x00200085,按位拆分,對應如下:
#defineMH_NOUNDEFS0x1/* the object file has no undefined references */
#define MH_DYLDLINK
dynamic linker and can't be staticly
link edited again */
#define MH_TWOLEVEL0x80/* the image is using two-level name
space bindings */
#define MH_ALLOW_STACK_EXECUTION 0x20000/* When this bit is set, all stacks
in the task will be given stack
execution privilege. Only
至此,mach-o的header區域分析完畢了。