[iOS]分析Mach-O文件
创始人
2024-01-31 05:02:26
0

一、Mach-O文件介绍

 我们拿到IPA文件后,解压后就能拿到一个与APP同名的可执行文件。

Mach-O为Mach Object文件格式的缩写,它是一种用于可执行文件,目标代码,动态库,内核转储的文件格式。

每个Mach-O文件包括一个Mach-O头,然后是一系列的载入命令,再是一个或多个块,每个块包括0到255个段。

Header (头部)
用于快速确认该文件的CPU类型、文件类型等信息

LoadCommands (加载命令)
用于告诉loader如何设置并加载二进制数据

Data (数据段 segment)
存放数据:代码、字符常量、类、方法等
可以拥有多个segment,每个segment可以有零到多个section。每个段都有一段虚拟地址映射到进程的地址空间

Loader Info (链接信息)
一个完整的用户级MachO文件的末端是一系列链接信息。其中包含了动态加载器用来链接可执行文件或者依赖所需使用的符号表、字符串表等

 了解更多可以去看MachO文件详解,MachO文件结构详解。
 

二、用otool命令分析

1.所有命令

终端输入otool,然后回车,查看所有命令。

-f print the fat headers
-a print the archive header
-h print the mach header
-l print the load commands
-L print shared libraries used
-D print shared library id name
-t print the text section (disassemble with -v)
-x print all text sections (disassemble with -v)
-p   start dissassemble from routine name
-s print contents of section
-d print the data section
-o print the Objective-C segment
-r print the relocation entries
-S print the table of contents of a library (obsolete)
-T print the table of contents of a dynamic shared library (obsolete)
-M print the module table of a dynamic shared library (obsolete)
-R print the reference table of a dynamic shared library (obsolete)
-I print the indirect symbol table
-H print the two-level hints table (obsolete)
-G print the data in code table
-v print verbosely (symbolically) when possible
-V print disassembled operands symbolically
-c print argument strings of a core file
-X print no leading addresses or headers
-m don't use archive(member) syntax
-B force Thumb disassembly (ARM objects only)
-q use llvm's disassembler (the default)
-Q use otool(1)'s disassembler
-mcpu=arg use `arg' as the cpu for disassembly
-j print opcode bytes
-P print the info plist section as strings
-C print linker optimization hints
--version print the version of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool

2.常用命令

1.查看App所使用的动态库

otool -L 可执行文件

2.查看ipa是否已经砸壳

otool -l 可执行文件 | grep crypt

3.打印Mach header

otool -h 可执行文件

4.打印整个ARM的汇编码

otool -tV 可执行文件

5.类的继承结构和引用关系

otool -ov 可执行文件

三、用class-dump分析

class-dump主要用于提取Mach-O文件中的头文件信息,生成.h文件导出到指定文件夹。通过对头文件的分析,了解App实现的基本思路。

1.安装

class dum下载:
class-dump - Steve Nygard
http://stevenygard.com/download/class-dump-3.5.tar.gz
上面下载class-dump,使用时会报错Error: Cannot find offset for address 0xb000000001028576 in stringAtAddress:
这时需要换个来源下载:
http://stevenygard.com/download/class-dump-3.5.tar.gzMonkeyDev/class-dump at master · AloneMonkey/MonkeyDev · GitHub
打开终端输入open /usr/local/bin,把dmg文件中的class-dump文件复制到进去。

open /usr/local/bin

 修改改权限,终端输入:

sudo chmod 777 /usr/local/bin/class-dump

2.所有命令

终端输入class-dump,拿到所有命令。

-a             show instance variable offsets
-A             show implementation addresses
--arch   choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64, armv6, armv7, armv7s, arm64)
-C      only display classes matching regular expression
-f        find string in method name
-H             generate header files in current directory, or directory specified with -o
-I             sort classes, categories, and protocols by inheritance (overrides -s)
-o

       output directory used for -H
-r             recursively expand frameworks and fixed VM shared libraries
-s             sort classes and categories by name
-S             sort methods by name
-t             suppress header in output, for testing
--list-arches  list the arches in the file, then exit
--sdk-ios      specify iOS SDK version (will look in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
--sdk-mac      specify Mac OS X version (will look in /Developer/SDKs/MacOSX.sdk
--sdk-root     specify the full SDK root path (or use --sdk-ios/--sdk-mac for a shortcut)

2.使用

class-dump -H 可执行文件 -o 头文件保存保存目录

例如:class-dump -H /Users/gamin/Desktop/APP/Payload/Reading.app/Reading -o /Users/gamin/Desktop/test

四、用MachOView分析

MachOView软件下载:
MachOView download | SourceForge.net
https://github.com/fangshufeng/MachOView
MachOView源码:
https://github.com/gdbinit/MachOView

安装时,将MacOView拖动到应用程序就可以。
使用时,选择可执行文件File - Open - MachO。

点击Mach Header,可以看到cpu架构信息、load commands数量 、load commandssize 、file type等信息。

 

查看数据段中的汇编指令

 

五、用Hopper Disassembler分析(推荐)

官网地址:https://www.hopperapp.com
打开软件页面就这样

 

更详细的内容可以去查看官方教程,或其他文章Hopper Disassembler的基本使用功能整理,hopper disassembler

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...