• [iOS]分析Mach-O文件


    一、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

  • 相关阅读:
    k8s--基础--10.1--命令--kubectl--介绍.md
    MQTT搭建(Windows和Linux)版
    dubbo~全局异常拦截器的使用与设计缺陷
    Redis之主从复制、哨兵模式、集群模式
    【gazebo要素10】制作移动的机器人
    链接脚本(Linker Script)解析
    视频亮度太低了,如何调高
    【2022-11-07】 转-代码质量保证
    CSS 3之美化表格样式
    C#实现在企业微信内创建微信群发送群消息
  • 原文地址:https://blog.csdn.net/u012881779/article/details/127930630