今天看啥  ›  专栏  ›  紫电清霜Owenli

iOS逆向与安全(六):class-dump

紫电清霜Owenli  · 简书  ·  · 2018-09-21 10:44

前言

本节学习如何使用工具导出第三方App的头文件,学习前需要补充点基础知识。

Mach-O文件

想让程序在设备上运行起来,需要将写好的代码生成可执行文件这样才能被操作系统所理解。比如,在Linux下可执行文件的格式是ELF,在Window中可执行文件是PE32/PE32+,而在MACiOS中是Mach-O格式。

Mach-O的组成结构如下图:

图片来自《Mach-O 文件格式探索》

可以看的出 Mach-O 主要由 3 部分组成:

  • Mach-O 头(Mach Header):这里描述了 Mach-O 的 CPU 架构、文件类型以及加载命令等信息;
  • 加载命令(Load Command):描述了文件中数据的具体组织结构,不同的数据类型使用不同的加载命令表示;
  • 数据区(Data):Data 中每一个段(Segment)的数据都保存在此,段的概念和 ELF 文件中段的概念类似,都拥有一个或多个 Section ,用来存放数据和代码。

如何获取Mach-O文件,在App Store中下载应用.ipa改成.zip格式,之后解压缩会在里面发现一个应用同名的文件。
下面是简书的Mach-O文件:

Class-dump

class-dump的作用就是把Mach-O文件的class信息导出来生成头文件。

下载后将其中的class-dump拷贝到/usr/local/bin目录下,就可以在终端使用class-dump命令。

终端输入class-dump

class-dump 3.5 (64 bit) (Debug version compiled Sep 17 2017 16:24:48)
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, armv6, armv7, armv7s, arm64)
        -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 for /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS<version>.sdk
                       or /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS<version>.sdk)
        --sdk-mac      specify Mac OS X version (will look for /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX<version>.sdk
                       or /Developer/SDKs/MacOSX<version>.sdk)
        --sdk-root     specify the full SDK root path (or use --sdk-ios/--sdk-mac for a shortcut)

安装成功,可以解析头文件方法很简单,常用指令。

class-dump -H mach-o路径 -o 头文件存储路径

以简书App为例,使用之前砸壳的ipa文件,找到里面的Mach-O文件。

class-dump -H Hugo -o ./Header

在当前目录中会生成Header文件夹,里面包含所有的头文件。

参考文章




原文地址:访问原文地址
快照地址: 访问文章快照