[iOS]分析Mach-O文件
创始人
2024-01-31 00:59:33
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

相关内容

热门资讯

西藏:拉鲁湿地获“世界海拔最高...   12月20日,斑头雁飞翔在拉鲁湿地上空。  12月20日,“世界海拔最高的城市天然湿地”WRCA...
文化中国行·人间好时节|冬至阳...   冬至  二十四节气中一个重要的节气  古代民间有“冬至大如年”的说法  古人认为  此时阳气初萌...
“海南封关120多万卡宴只要6... 近日,话题“海南封关120多万卡宴只要60万”冲上热搜,引发公众热议。 媒体报道,12月18日,随...
齐鲁守艺人|王永训:放飞“中华...   “老手艺要活,不能光摆着看。得让它动起来,亮起来!”山东手造工匠王永训深耕风筝手艺40 多年,坚...
理响中国·烽火丹心丨金融密战中...   (高捷成家书)  “我所欠挂百川银庄二万多元的债  时刻记念在心  本利至今当在三万余  国家得...
财经聚焦|破解科技企业融资痛点...   “科技之花”绽放,离不开金融活水精心浇灌。如何进一步提升金融服务质效、破解科技企业融资难题?  ...
【理响中国】怎样健全扩大内需的...   扩大内需,既需要宏观政策积极有为,也需要形成长效机制。在《求是》杂志新近刊发的习近平总书记重要文...
冬至,福至!   长夜漫漫,寒意正浓  梅枝探雪,灯火向荣  水饺是冬夜弯曲的银月  汤圆是心中思念的玉盘  羊肉...
多地育儿补贴已到账 还没申领的...   临近年末  有人惊喜地发现  银行账户上多出了一笔钱  原来是育儿补贴到账了  2025年11月...
西藏拉鲁湿地获“世界海拔最高的...   12月20日,拉鲁湿地国家级自然保护区通过世界纪录认证(WRCA)获“世界海拔最高的城市天然湿地...