我爱编程

iOS 逆向之砸壳

2018-05-28  本文已影响121人  善舞ice
背景

#### 前言

从 AppStore 下载的 App 是被苹果使用 FairPlay 技术加密过的, 可执行文件被套上了一层保护壳, 而 class-dump Hopper 等工具无法作用于加密过的 App。 在这种情况下, 想要获取头文件, 需要先解密 App 的可执行文件, 俗称“砸壳”。 砸壳的工具有好多种,其核心原理就是将内存中已解密的镜像 “dump” 出来,再生成新的镜像文件,从而达到解密的效果。可执行程序要想运行起来,必须由操作系统把可执行文件加载进内存,iOS 中用的是 DYLD。

#### 一、dumpdecrypted

dumpdecrypted 就是由越狱社区的知名人士 Stefan Esser( @i0n1c) 出品的一款砸壳工具, 被越狱社区广泛运用在 iOS 逆向工程研究中。下面介绍怎么用 dumpdecrypted 给 app 砸壳。

下载 dumpdecrypted 地址为 https://github.com/stefanesser/dumpdecrypted/

下载完成后直接 make (如果安装了不同的 iOS SDK,可以调整 Makefile 文件),一切顺利的话会生成名为 dumpdecrypted.dylib 的动态库,先暂时放在这里留着备用。

#### 二、SSH 连接越狱 iDivice

为了书写和描述,我们把 iPhone、iPad、iPod 等 统称为 iDivice 。iDivice 越狱后有 root 和 mobile 两个账户,默认密码为 alpine ,建议马上修改这个默认的密码,防止其他人通过这个默认密码登录 iDivice 。修改命令如下:

``` bash

$ su root

$ passwd

Changing password for root.

New password:

Retype new password:

```

越狱 iDivice 使用 Cydia 搜索 openSSH ,安装即可。连接方式有两种

##### 2.1 WiFi 连接

首先确保 iDivice 和 电脑在统一局域网,连接操作如下

``` bash

$ ssh root@x.x.x.x

```

其中 x.x.x.x 为 iDivice 的 ip 地址。

##### 2.2 USB连接

##### 2.2.1 安装 usbmuxd

``` bash

$ brew install usbmuxd

```

##### 2.2.2 端口映射

``` bash

$ iproxy 2345 22

```

相当于把 iDivice 的 22 端口映射到电脑的 2345 端口

##### 2.2.3 连接

新开一个终端窗口,并且保持 2.2 中的终端窗口没有被关闭

``` bash

$ ssh -p 2345 root@127.0.0.1

```

#### 三、获取两个路径

查看 app 进程,这里以 WeChat 为例 ,这里要保证 WeChat 在前台运行

``` bash

$ ps -ef | grep /var/mobile/

  501  506    1  0  0:00.00 ??        1:19.23 /var/mobile/Containers/Bundle/Application/F14174A6-AEC8-4AD4-A09A-9049FDCBB55E/WeChat.app/WeChat

    0  1529  1482  0  0:00.00 ttys001    0:00.01 grep /var/mobile/

```

先记下这个路径备用

查看 app Document 路径

这个需要用到 cycript 这个工具。cycript 是混合了 oc 与 js 语法的一个工具, 让开发者在命令行下和应用交互,在运行时查看和修改应用,挂钩相关的进程后,我们就可以用 oc 的方法来查看 app Document 路径了。

挂钩进程

``` bash

$ cycript -p WeChat

cy# [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]

@[#"file:///var/mobile/Containers/Data/Application/6475E209-3EF7-4264-ADC5-FBE0264516BF/Documents/"]

cy#

```

退出 cy# 模式的快捷键为 ctl + d,把上面输出的路径也记下来备用。

#### 四、把第一步中生成的 dumpdecripted.dylib 动态库拷贝到该 app 的 Document 目录下

该操作要用到 scp 命令。scp 是 secure copy 的缩写, 基于 ssh 登陆进行安全的远程文件拷贝命令。例子:两台机器IP分别为:A 192.168.11.10,B 192.168.11.11,如果想把 A 根目录上的 a.jpg 文件拷到 B 上,命令如下:在A服务器上操作 命令为:

``` bash

$ scp /a.jpg root@192.168.11.11:/

```

所以此时把生成的 dumpdecripted.dylib 动态库拷贝到该 app 的 Document 目录,命令如下:

在 mac的终端上操作

``` bash

$ scp dumpdecrypted.dylib root@x.x.x.x:/var/mobile/Containers/Data/Application/6475E209-3EF7-4264-ADC5-FBE0264516BF/Documents/

```

#### 五、开始砸壳

进入 Document 目录

``` bash

$ cd /var/mobile/Containers/Data/Application/6475E209-3EF7-4264-ADC5-FBE0264516BF/Documents/

```

砸壳

``` bash

$ DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Containers/Bundle/Application/F14174A6-AEC8-4AD4-A09A-9049FDCBB55E/WeChat.app/WeChat

mach-o decryption dumper

DISCLAIMER: This tool is only meant for security research purposes, not for application crackers.

[+] detected 64bit ARM binary in memory.

[+] offset to cryptid found: @0x1000fcca8(from 0x1000fc000) = ca8

[+] Found encrypted data at address 00004000 of length 58818560 bytes - type 1.

[+] Opening /private/var/mobile/Containers/Bundle/Application/F14174A6-AEC8-4AD4-A09A-9049FDCBB55E/WeChat.app/WeChat for reading.

[+] Reading header

[+] Detecting header type

[+] Executable is a plain MACH-O image

[+] Opening WeChat.decrypted for writing.

[+] Copying the not encrypted start of the file

[+] Dumping the decrypted data into the file

[+] Copying the not encrypted remainder of the file

[+] Setting the LC_ENCRYPTION_INFO->cryptid to 0 at offset ca8

[+] Closing original file

[+] Closing dump file

$ ls

00000000000000000000000000000000  MMResourceMgr  OpenImResource    db.globalconfig

CrashReport   MMappedKV SafeMode.dat   dumpdecrypted.dylib

LocalInfo.lst   MemoryStat WeChat.decrypted  heavy_user_id_mapping.dat

```

此时就可以在当前目录下看到被砸壳后的 WeChat.decrypted。

#### 六、获取可执行文件的所有头文件

从 iDvice 上把解密后的文件拷到电脑的桌面上,在电脑终端上操作

``` bash

$ scp root@x.x.x.x:/var/mobile/Containers/Data/Application/6475E209-3EF7-4264-ADC5-FBE0264516BF/Documents/WeChat.decrypted ~/Desktop/

```

获取 .h 文件需要用到 class-dump 工具,下载地址为 http://stevenygard.com/projects/class-dump/。把砸壳后的可执行文件的所有 .h 文件输出到 wechat_test_h 文件夹下

``` bash

class-dump -H WeChat.decrypted -o wechat_test_h

```

这时候就能看到所有 WeChat 的头文件了。

#### 七、相关链接

FairPlay https://en.wikipedia.org/wiki/FairPlay

dumpdecrypted https://github.com/stefanesser/dumpdecrypted/

class-dump http://stevenygard.com/projects/class-dump/

上一篇 下一篇

猜你喜欢

热点阅读