代码改变世界

iOS安全攻防(十六)看懂mach-o(1)

2014-12-25 18:01  16字节  阅读(3210)  评论(3编辑  收藏  举报

个人原创,转帖请注明来源:cnblogs.com/jailbreaker

在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:

NewImage

然后选择第一行,到达mach-o header区域:

NewImage

hopper中间显示如下:

NewImage

hopper的注释已经比较详细了,在xcode下,打开mach-o/loader.h ,看下header结构的定义:

NewImage

 

根据以上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_DYLDLINK0x4/* the object file is input for the

  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区域分析完毕了。