iOS底层基础知识iOS逆向学习笔记

mach-o 介绍

2019-01-05  本文已影响0人  司空123

一、简介

Mach-O是一种文件格式,是mac上可执行文件的格式,类似于windows上的PE格式 (Portable Executable ), linux上的elf格式 (Executable and Linking Format)。我们编写的C、C++、swift、OC,最终编译链接生成Mach-O可执行文件

二、mach-o文件类型分为:

image

总共有11种。

  1. MH_OBJECT (0x1 )
  1. MH_EXECUTE (0x2) 可执行文件,
  1. MH_DYLIB 动态库文件
  1. MH_DYLINKER (0x7) 动态链接编辑器
  1. MH_DYSM 符号文件

三、Mach-O格式

Mach-O是一个以数据块分组的二进制字节流,这些数据块包含元信息,比如字节顺序、CPU类型、数据块大小等等。
典型的Mach-O文件包含三个区域:
1.Header:保存Mach-O的一些基本信息,包括平台、文件类型、指令数、指令总大小,dyld标记Flags等等。
2.Load Commands:紧跟Header,加载Mach-O文件时会使用这部分数据确定内存分布,对系统内核加载器和动态连接器起指导作用。
3.Data:每个segment的具体数据保存在这里,包含具体的代码、数据等等。

用一张图表示Mach-O

image

(一)、Header
数据结构

/*
 * The 32-bit mach header appears at the very beginning of the object file for
 * 32-bit architectures.
 */
struct mach_header {
    uint32_t    magic;      /* mach magic number identifier */
    cpu_type_t  cputype;    /* cpu specifier */
    cpu_subtype_t   cpusubtype; /* machine specifier */
    uint32_t    filetype;   /* type of file */
    uint32_t    ncmds;      /* number of load commands */
    uint32_t    sizeofcmds; /* the size of all the load commands */
    uint32_t    flags;      /* flags */
};

/* Constant for the magic field of the mach_header (32-bit architectures) */
#define MH_MAGIC    0xfeedface  /* the mach magic number */
#define MH_CIGAM    0xcefaedfe  /* NXSwapInt(MH_MAGIC) */

/*
 * The 64-bit mach header appears at the very beginning of object files for
 * 64-bit architectures.
 */
struct mach_header_64 {
    uint32_t    magic;      /* mach magic number identifier */
    cpu_type_t  cputype;    /* cpu specifier */
    cpu_subtype_t   cpusubtype; /* machine specifier */
    uint32_t    filetype;   /* type of file */
    uint32_t    ncmds;      /* number of load commands */
    uint32_t    sizeofcmds; /* the size of all the load commands */
    uint32_t    flags;      /* flags */
    uint32_t    reserved;   /* reserved */
};

/* Constant for the magic field of the mach_header_64 (64-bit architectures) */
#define MH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
#define MH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */

根据定义与注释,得到以下解释

名称 含义
magic Mach-O魔数,FAT:0xcafebabeARMv7:0xfeedface,ARM64:0xfeedfacf
cputype、cpusubtype CPU架构及子版本
filetype MH_EXECUTABLE(可执行二进制文件)、MH_OBJECT(目标文件)、MH_DYLIB(动态库),有11种宏定义类型,具体可查看源码
ncmds 加载命令的数量
sizeofcmds 所有加载命令的大小
flags dyld加载需要的一些标记,有28种宏定义,具体看源码,其中MH_PIE表示启用ASLR地址空间布局随机化
reserved 64位保留字段

使用MachOView查看某可执行文件:

屏幕快照 2019-01-13 下午10.30.39.gif

(二)、Load Commands
数据结构:

/*
 * The load commands directly follow the mach_header.  The total size of all
 * of the commands is given by the sizeofcmds field in the mach_header.  All
 * load commands must have as their first two fields cmd and cmdsize.  The cmd
 * field is filled in with a constant for that command type.  Each command type
 * has a structure specifically for it.  The cmdsize field is the size in bytes
 * of the particular load command structure plus anything that follows it that
 * is a part of the load command (i.e. section structures, strings, etc.).  To
 * advance to the next load command the cmdsize can be added to the offset or
 * pointer of the current load command.  The cmdsize for 32-bit architectures
 * MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple
 * of 8 bytes (these are forever the maximum alignment of any load commands).
 * The padded bytes must be zero.  All tables in the object file must also
 * follow these rules so the file can be memory mapped.  Otherwise the pointers
 * to these tables will not work well or at all on some machines.  With all
 * padding zeroed like objects will compare byte for byte.
 */
struct load_command {
    uint32_t cmd;       /* type of load command */
    uint32_t cmdsize;   /* total size of command in bytes */
};

注释的大概意思:
load_commands紧跟mach_header,load_commands展开后的数目与总大小已经在mach_header有记录,所有加载指令都是以cmd、cmdsize起头。cmd字段用该命令类型的常量表示,有专门的结构;cmdsize字段以字节为单位,主要记录偏移量让load command指针进入下一条加载指令,32位架构的cmdsize是以4字节的倍数,64位结构的cmdsize是以8字节的倍数(加载指令永远是这样对齐),不够用0填充字节。文件中的所有表都遵循这样的规则,这样就可以被映射到内存,否则的话指针不能很好地指向。

使用MachOView查看Load Commands区:

image

Load Commands下常见的加载指令:

指令 含义
LC_SEGMENT_64 定义一段(Segment),加载后被映射到进程的内存空间中,包括里面的节(Section)
LC_DYLD_INFO_ONLY 记录有关链接的信息,包括在__LINKEDIT中动态链接的相关信息的具体偏移大小(重定位,绑定,弱绑定,懒加载绑定,导出信息等),ONLY表示该指令是程序运行所必需的。
LC_SYMTAB 定义符号表和字符串表,链接文件时被dyld使用,也用于调试器映射符号到源文件。符号表定义的本地符号仅用于调试,而已定义和未定义的external符号被链接器使用
LC_DYSYMTAB 将符号表中给出符号的额外信息提供给dyld
LC_LOAD_DYLINKER dyld的默认路径
LC_UUID Mach-O唯一ID
LC_VERSION_MIN_IPHONES 系统要求的最低版本
LC_SOURCE_VERSION 构建二进制文件的源代码版本号
LC_MAIN 应用程序入口,dyld的_main函数获取该地址,然后跳转
LC_ENCRYPTION_INFO_64 文件加密标志,加密内容偏移和大小
LC_LOAD_DYLIB 依赖的动态库,含动态库名,版本号等信息
LC_RPATH @rpath搜索路径
LC_DATA_IN_CODE 定义在代码段内的非指令的表
LC_CODE_SIGNATURE 代码签名信息

LC_SEGMENT_64段数据结构(说明附在注释部分):

/*
 * The 64-bit segment load command indicates that a part of this file is to be
 * mapped into a 64-bit task's address space.  If the 64-bit segment has
 * sections then section_64 structures directly follow the 64-bit segment
 * command and their size is reflected in cmdsize.
 */
struct segment_command_64 { /* for 64-bit architectures */
    uint32_t    cmd;        /* Load Command类型 */
    uint32_t    cmdsize;    /*包含的所有section结构体的大小 */
    char        segname[16];    /* 段名 */
    uint64_t    vmaddr;     /* 映射到虚拟地址的偏移 */
    uint64_t    vmsize;     /* 映射到虚拟地址的大小 */
    uint64_t    fileoff;    /* 相对于当前架构文件的偏移 */
    uint64_t    filesize;   /* 文件大小 */
    vm_prot_t   maxprot;    /* 段页面的最高内存保护 */
    vm_prot_t   initprot;   /* 初始内存保护 */
    uint32_t    nsects;     /* 包含的section数 */
    uint32_t    flags;      /* 段页面标志 */
};

该数据结构的段主要有以下4种:

含义
_PAGEZERO 空指针陷阱段,映射到虚拟内存空间第一页,捕捉对NULL指针的引用
_TEXT 代码段、只读数据段
_DATA 读取和写入数据段
_LINKEDIT dyld需要使用的信息,包括重定位、绑定、懒加载信息等

(三)、Data
Load Commands区域下来接着就是DATA区域,展开Load Commands下的LC_SEGMENT_64可以看到多个Section64,各个Section的具体信息可以在Load Commands紧接着的部分查看,它们是一一对应的:

image

section的数据结构如下:

struct section_64 { /* for 64-bit architectures */
    char        sectname[16];   /* 节名 */
    char        segname[16];    /* 所属段名 */
    uint64_t    addr;       /* 映射到虚拟地址的偏移 */
    uint64_t    size;       /* 节的大小 */
    uint32_t    offset;     /* 节在当前架构文件中的偏移 */
    uint32_t    align;      /* 节的字节对齐大小n,2^n */
    uint32_t    reloff;     /* 重定位入口的文件偏移 */
    uint32_t    nreloc;     /* 重定位入口个数 */
    uint32_t    flags;      /* 节的类型和属性*/
    uint32_t    reserved1;  /* reserved (for offset or index) */
    uint32_t    reserved2;  /* reserved (for count or sizeof) */
    uint32_t    reserved3;  /* 保留位,以上两同理 */
};

section节已经是最小的分类,大部分内容集中在__TEXT,__DATA这两段中,部分内容如下:

__TEXT节 含义
__text 程序可执行代码区域
__stubs 间接符号存根,用于跳转到懒加载指针表
__stubs_helper 懒加载符号加载辅助函数
__cstring 只读的C字符串,包含OC的部分字符串和属性名
...... ......
__DATA 含义
__nl_symbol_ptr 非懒加载指针表,dyld加载时立即绑定值
__la_symbol_ptr 懒加载指针表,第1次调用才绑定值
__got 非懒加载全局指针表
__mod_init_func constructor函数
__cfstring OC字符串
...... ......

四、小结

了解Mach-O可以帮助我们理解dyld的加载Mach-O的过程以及与Mach-O相关的读取或操作,如fishhook、文件内偏移地址等。

五、参考

Dynamic Linking of Imported Functions in Mach-O
https://www.jianshu.com/p/7040dd1396f7

上一篇下一篇

猜你喜欢

热点阅读