iOS逆向 - 运行时分析(一)class-dump

2,073 阅读2分钟

欢迎关注微信公众号:FSA全栈行动 👋

概述

众所周知,OC 是一门面向对象的动态编程语言,在程序运行时才会确定对象的真实类型,并调用类和对象的相应方法。所以在 iOS 逆向领域便出现了一些工具,利用 Runtime 帮助我们开发者在程序运行时去动态修改类和对象中的属性和方法。

class-dump

class-dump 是一个命令行工具,可对存储在 Mach-O 文件中的 OC Runtime 信息进行检测,生成类、分类和协议的属性和方法的声明,即 .h 头文件。

1、下载安装

官网下载链接:

下载后解压,将 class-dump 拖到 /usr/local/bin 目录下即可在终端内使用

官网下载的 class-dump 由于很久没有维护了,不支持 SwiftOC 混编的二进制文件,会报如下错误

class-dump[36848:11809395] Error: Cannot find offset for address 0x800000001001586 in stringAtAddress:

所以推荐从 github.com/AloneMonkey… 下载 class-dump

下载后拖到 /usr/local/bin 目录下,再赋予执行权限方可在终端内使用

chmod 755 /usr/local/bin/class-dump

2、使用

使用参数说明:

class-dump 3.5 (64 bit)
Usage: class-dump [options] <mach-o-file>

  where options are:
        -a             show instance variable offsets
        -A             show implementation addresses
        --arch <arch>  choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64)
        -C <regex>     only display classes matching regular expression
        -f <str>       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 <dir>       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<version>.sdk
        --sdk-mac      specify Mac OS X version (will look in /Developer/SDKs/MacOSX<version>.sdk
        --sdk-root     specify the full SDK root path (or use --sdk-ios/--sdk-mac for a shortcut)

举个例子:我们对 Twitter 的二进制文件导出头文件

class-dump --arch arm64 -a -A -H Twitter -o ./Headers

各参数说明:

参数作用
a显示实例变量的偏移地址
A显示方法实现地址
H生成头文件
arch指定架构(如果只有一个架构,可以不加该参数)
o指定保存生成的头文件的目录位置

执行完成后,可以看到 Headers 目录下一共有 343 个文件

Headers

任意打开一个头文件,可以看到变量名、方法名、偏移地址等信息