iOS逆向之五 MACH O檔案解析

2021-08-09 19:24:18 字數 4020 閱讀 5653

macho檔案是蘋果可執行二進位制檔案的格式

lc_segment_64

將可執行檔案(64位)對映到程序位址空間

32位系統的是lc_segment

是載入的主要命令,負責指導核心來設定程序的記憶體空間

lc_dyld_info_only

動態鏈結相關資訊

lc_symtab

符號表位址

lc_dysymtab

動態符號位址表

lc_load_dylinker

載入乙個動態鏈結器,路徑「/usr/lib/dyld」

lc_uuid

二進位制檔案的標識id,dsym檔案、crash中都存在這個值,確定兩個檔案是否匹配,分析出對應的崩潰位置

lc_version_min_macosx

二進位制檔案要求的最低系統版本,和xcode中配置的target有關

lc_main

設定程式的入口,編譯型的語言需要指定入口位址,直譯器語言對此沒有邀請

lc_source_version

構建二進位制檔案使用的源**版本

lc_function_starts

定義函式的起始位址表,使我們的偵錯程式很容易看到位址

lc_data_in_code

定義在**段內的非指令資料

#import struct segment_command ;
struct segment_command_64 ;
** 段結構圖(**段__text): ** 

段(segment)和節(section)

"__text"代表的是segment,  

「__text」代表的是section

__pagezero段

乙個全用0填充的段,使用者抓取空指標引用(非法記憶體訪問)。通常不會占用物理記憶體

__text段

**段:只有可執行**和其他唯讀資料

__text		主程式的**

__stubs 動態庫鏈結的樁

__stub_helper 動態庫鏈結的樁的輔助

__cstring 字串常量(**中寫固定的字元)立即數(**中直接寫的數字,成為模數)

常量字串表的描述資訊,通過該資訊,可以獲取常量字元符號的表位址

__unwind_info 用於異常處理

__data段

用於讀取和寫入資料的乙個段

__nl_symbol_ptr	非延時匯入符號表指標

_la_symbol_ptr 延時匯入符號表指標

__linkedit

包含給動態鏈結庫鏈結器(ldyd)的原始資料段,包含符號表和字串表,壓縮動態鏈結資訊,以及動態符號表

struct section ;
struct section_64 ;
節結構圖(**段):

char		sectname[16];	/* section名字 */

char segname[16]; /* section所在的segment的名字 */

uint32_t addr; /* 記憶體中起始位置 */

uint32_t size; /* section大小 */

uint32_t offset; /* section的檔案偏移 */

uint32_t align; /* 對其 */

uint32_t reloff; /* file offset of relocation entries */

uint32_t nreloc; /* number of relocation entries */

uint32_t flags; /* flags (section type and attributes)*/

uint32_t reserved1; /* reserved (for offset or index) */

uint32_t reserved2; /* reserved (for count or sizeof) */

__text:__cstring 分析 源**:

static int pi = 3.1415;

int main(int argc, const char * argv)

return 0;

}

對應的_cstring 節內容如下:

可以看到所有的字串儲存在一張字串表中,對應的內容是可以被修改的,執行的時候執行的結果也會變成修改之後的內容。

lc_dyld_info_only

資料結構:

** 重定向資料rebase(命令碼:高四位、低四位) ** 11:rebase_opcode_set_type_imm 高四位0x10表示設定立即數型別 低四0x01位表示立即數型別為指標

22:rebase_opcode_set_segment_and_offset_uleb + 2 重定向到資料段的第2個section

action 為翻譯出來的內容,指向的是資料段的第2個節(load commands中的所以) 

nslog是庫中的函式,所以需要重定向,其他的幾個函式也是類似的。

繫結資料bind: 進行動態繫結的dyld的函式(dyld_stub_binder)

弱繫結資料 weak_bind

延時繫結 lazy_bind

對於需要從動態庫載入的函式符號(_printf)

export info

可以使用nm工具檢視二進位制檔案的匯出符號 【 nm - display name list (symbol table)】

使用工具檢視可以看到匯出符號有兩個

動態鏈結庫的執行方式

todo

IOS安全 逆向 反編譯5 Mach O

介面分析 cycript reveal 分析 對mach o檔案的靜態分析 machoview class dump hopper disassembler ida等 動態除錯 debugserver lldb 4.編寫 必要時還可能需要重新簽名 打包ip class dump 命令格式為 clas...

08 安全攻防之Mach O檔案

mach o是一種檔案格式,是mac上可執行檔案的格式,類似於windows上的pe格式 portable executable linux上的elf格式 executable and linking format 我們編寫的c c swift oc,最終編譯鏈結生成mach o可執行檔案 總共有1...

iOS逆向之安裝簽名工具ldid

1.安裝brew,並安裝ldid usr bin ruby e curl fssl brew install ldid 2.修改使用者的環境配置 vim bash profile 這個只是其中的一種方法 export theos theos export path theos bin path so...