iOS逆向开发之工具介绍及简单运用

2019-04-08  本文已影响0人  荒漠现甘泉

进行iOS逆向开发首先需要下载相关的工具。

准备工作

  • 越狱手机(例如我的:iPhone 4s,系统8.4.1)
  • 越狱手机上的Cydia中安装CycriptOpenSSH
  • Mac上安装iFunBoxXCodeHopper DisassemblerSublime Text
  • Mac终端命令行中安装Class DumpTheosDumpdecrypted

工具作用介绍

OpenSSH

Mac连接手机,通过Mac的终端命令操作手机。

Cycript

获取手机中某一个运行的进程,并可以获取App进程后用OC代码获取反编译App的Documents目录路径。

iFunBox

Mac端的连接手机后的图形界面,比较方便的把砸壳后的文件直接导出到Mac上。下载地址:iFunBox

Xcode

这个就不用多说了,iOS开发工具。

Hopper Disassembler

这个是进行反汇编的工具,可以反汇编、反编译并且调试你的应用,我们可以通过这个工具查看伪代码的方法名和变量,还原函数体的具体逻辑。

下载破解版地址:hopper disassembler for mac破解版 v4.0.8

破解方法如下:http://www.sdifen.com/hopperdisassembler408.html

更多使用方法可以参考文章:iOS 逆向工程工具集之Hopper Disassembler破解版的安装与使用

Class Dump

对App可执行文件获取其头文件,这样我们可以根据头文件中暴露的方法名去猜测其App的编写逻辑。然后利用上述的反编译工具对某一文件进行函数的具体编写逻辑。

安装方法:
点击下载后解压后会有class-dump和源码文件。

  • 如果是OS X 10.11之前,可以直接将class-dump 复制到/usr/bin/class-dump目录下即可。
  • 如果是OS X 10.11,因为没有/usr/bin文件夹的写权限,所以我们得用另外的方法。

更多class-dump的使用可以参考文章iOS逆向之class-dump

Theos

创建一个Tweak工程,通过定制工程文件,再通过指定的bundle id,编译+打包+安装,从而向手机上的App注入代码,进而实现对某个App中的功能的破解(例如会员去广告、抢红包等功能)。

安装方法:

更多关于theos的安装与使用参考文章:

Dumpdecrypted

俗称:砸壳dumpdecrypted是个出色的app脱壳开源工具,它的原理是:将应用程序运行起来(iOS系统会先解密程序再启动),然后将内存中的解密结果dump写入文件中,得到一个新的可执行程序文件。

下载及生成砸壳用到dumpdecrypted.dylib工具,如下命令:

qingfengdeMacBook-Pro:练习 qingfeng$ git clone https://github.com/stefanesser/dumpdecrypted.git
Cloning into 'dumpdecrypted'...
remote: Enumerating objects: 31, done.
remote: Total 31 (delta 0), reused 0 (delta 0), pack-reused 31
Unpacking objects: 100% (31/31), done.
qingfengdeMacBook-Pro:练习 qingfeng$ ls
ArchitechureExample SwiftExample        架构学习
CoreAnimationExample    TouchEventExample   函数式编程
RuntimeExample      dumpdecrypted       工厂方法一
qingfengdeMacBook-Pro:练习 qingfeng$ cd dumpdecrypted
qingfengdeMacBook-Pro:dumpdecrypted qingfeng$ make
`xcrun --sdk iphoneos --find gcc` -Os  -Wimplicit -isysroot `xcrun --sdk iphoneos
qingfengdeMacBook-Pro:dumpdecrypted qingfeng$ ls
Makefile        dumpdecrypted.c     dumpdecrypted.o
README          dumpdecrypted.dylib

更多相关使用参考文章:dumpdecrypted介绍及其使用

Sublime Text

打开后面要用到的Makefile文件。
下载地址:Sublime Text

实现目标

我们来破解一款“传奇来了”的游戏,在我们打开“传奇来了”APP之后,自动弹出一个警告框。先放一张结果图:

逆向截屏.PNG

实现流程

1、利用dumpdecrypted.dylib砸壳

第一步:利用OpenSSH命令通过mac终端来操作手机,openSSH密码是alpine。退出登录的命令是exit。手机和电脑需要在同一个网段,连接同一个wifi,然后点开设置查看当前手机分配的IP地址。如下图:

IP地址.png

然后输入如下命令(密码是alpine):

qingfengdeMacBook-Pro:~ qingfeng$ ssh root@192.168.11.130
root@192.168.11.130's password:
iPhone:~ root#

第二步:查找要反编译app的路径

ps -e 获取手机中所有的当前运行的进程
ps -A|grep mobile 抓取手机上运行的APP进程

运行命令如下:

iPhone:~ root# ps -e 
  PID TTY           TIME CMD
    1 ??         0:15.59 /sbin/launchd
   32 ??         0:11.16 /usr/libexec/UserEventAgent (System)
   36 ??         0:18.43 /System/Library/Frameworks/CoreTelephony.framework/Sup
...
iPhone:~ root# ps -A|grep mobile
  382 ??         0:14.73 /usr/libexec/mobileassetd
  940 ??         0:36.71 /var/mobile/Containers/Bundle/Application/CE59CF92-3C59-46B5-B3ED-81F1F7CC0440/CQClient.app/CQClient
  951 ttys000    0:00.01 grep mobile
iPhone:~ root#

其中的/var/mobile/Containers/Bundle/Application/CE59CF92-3C59-46B5-B3ED-81F1F7CC0440/CQClient.app/CQClient是我们要破解的app的路径。

第三步:使用Cycript找出反编译App的Documents目录路径

这里有两个路径,/var/mobile/Containers/Bundle/Application/下的是app的路径,/var/mobile/Containers/Data/Application则是我们要找的Documents路径。
我们通过如下命令来获取App的Documents路径。

iPhone:~ root# cycript -p CQClient
cy# [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomans:NSUserDomainMask][0]
#"file:///var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents/"
cy#

其中的/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents/即是我们要找的Documents目录路径。

退出cyscript命令环境control+z。如下:

cy# ^Z
[1]+  Stopped(SIGTSTP)        cycript -p CQClient
iPhone:~ root#

退出OpenSSH的命令是exit。但在这之前需要输入命令jobs,否则无法退出。具体可以查看这篇文章Linux中 “there are stopped jobs”问题的解决方案。命令如下:

iPhone:~ root# exit
logout
There are stopped jobs.
iPhone:~ root# jobs
[1]+  Stopped(SIGTSTP)        cycript -p CQClient
iPhone:~ root# exit
logout
Connection to 192.168.11.130 closed.

第四步:将dumpdecrypted.dylib拷贝到Documents目录下,此处是使用的scp dumpdecrypted.dylib路径 root@ip:Documents路径方式。也可以使用iFunBox或者PP助手进行文件操作(我试了不行,不知道是不是方法不对,还是在iFunBox或者PP助手展示的文件路径不对)。此处的dumpdecrypted.dylib可以通过上面Dumpdecrypted工具介绍的方法生成。
命令如下:

qingfengdeMacBook-Pro:~ qingfeng$ scp /Users/qingfeng/Documents/WorkSpace/Exercise/dumpdecrypted/dumpdecrypted.dylib root@192.168.11.130:/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents/
root@192.168.11.130's password: 
dumpdecrypted.dylib                           100%  193KB   1.8MB/s   00:00    
qingfengdeMacBook-Pro:~ qingfeng$

第五步:使用dumpdecrypted砸壳
重新用mac连接上手机,首先cd到Documents路径下,然后执行命令DYLD_INSERT_LIBRARIES= dumpdecrypted.dylib app路径。命令如下:

qingfengdeMacBook-Pro:~ qingfeng$ ssh root@192.168.11.130
root@192.168.11.130's password: 
iPhone:~ root# cd /var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents/
iPhone:/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Containers/Bundle/Application/CE59CF92-3C59-46B5-B3ED-81F1F7CC0440/CQClient.app/CQClient
mach-o decryption dumper

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

[+] detected 32bit ARM binary in memory.
[+] offset to cryptid found: @0x9980(from 0x9000) = 980
[+] Found encrypted data at address 00004000 of length 655360 bytes - type 1.
[+] Opening /private/var/mobile/Containers/Bundle/Application/CE59CF92-3C59-46B5-B3ED-81F1F7CC0440/CQClient.app/CQClient for reading.
[+] Reading header
[+] Detecting header type
[+] Executable is a FAT image - searching for right architecture
[+] Correct arch is at offset 16384 in the file
[+] Opening CQClient.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 4980
[+] Closing original file
[+] Closing dump file
iPhone:/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents root# 

利用ls命令查看后,其中QQ.decrypted就是我们要的破解文件。

iPhone:/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents root# ls
CQClient.decrypted  dumpdecrypted.dylib  jrdb

2、使用class-dump将文件解析

我们同样用scp命令将CQClient.decrypted拷贝到Mac桌面文件夹。

qingfengdeMacBook-Pro:~ qingfeng$ scp root@192.168.11.130:/var/mobile/Containers/Data/Application/A1BA8E41-6A42-4D1F-AAA5-7F1262622D25/Documents/CQClient.decrypted /Users/qingfeng/Documents/WorkSpace/Exercise
root@192.168.11.130's password: 
CQClient.decrypted                            100% 1918KB   2.5MB/s   00:00    
qingfengdeMacBook-Pro:~ qingfeng$

我们再使用class-dump进行解析。

qingfengdeMacBook-Pro:QQHeader qingfeng$ class-dump --arch armv7 /Users/qingfeng/Documents/WorkSpace/Exercise/CQClient.decrypted -H -o /Users/qingfeng/Documents/WorkSpace/Exercise/CQClientHeader 
qingfengdeMacBook-Pro:QQHeader qingfeng$

这样即可获取头文件。因为我用的手机是iPhone4s,所以使用armv7,其他型号使用相对应的4(armv7),4s(armv7),5(armv7),5s(arm64),6(arm64),6s(arm64),7(arm64)等

如果是用的frida-ios-dump砸壳,砸壳出来的ipa文件,可以从里面获取.app文件,这个时候我们可以用class-dump -H /Users/xxxx/ProductName.app -o /Users/xxxx/HearderFile这种形式的命令来获取头文件

3、反编译静态分析 Hopper Disassembler

Hopper是一款运行在Mac、Windows和Linux下的调试(os x only)、反汇编和反编译的交互式工具。
可以对32、64位的MAC程序、Windows程序和IOS程序(arm)进行调试、反编译等。功能如下:

我们把CQClient.decrypted拖进去,就能进行代码分析了。具体怎么分析可以参考这篇文章[Mac OSX 之自己动手初步学习破解软件入门](Mac OSX 之自己动手初步学习破解软件入门)

4、Tweak与Theos

tweak的实质就是iOS平台的动态库。iOS平台上有两种形式的动态库,dylibframeworkFramework这种开发者用的比较多,而dylib这种就相对比较少一点,比如libsqlite.dyliblibz.dylib等。而tweak用的正是dylib这种形式的动态库。我们可以在/Library/MobileSubstrate/DynamicLibraries目录下查看手机上存在着的所有tweak。这个目录下除dylib还存在着plistbundle两种格式的文件,plist文件是用来标识tweak的作用范围,而bundletweak所用到的资源文件。

与正常的App开发使用Xcode不同,Tweak的开发环境是theos或者iosopendeviosopendev是在theos的基础上实现的基于Xcode的开发环境。而theos是一种命令式的开发编译环境,与c/c++的命令行编译形式很相像。

详情参考这篇文章:Tweak及TheOS简介

安装参考工具介绍Theos部分。
使用步骤及文件介绍如下:

1、Tweak工程的创建

cd到要创建tweak的目录下,执行如下指令:

nic.pl

执行完nic.pl指令后,会出现如下选择模板的界面:

qingfengdeMacBook-Pro:CQClientTweak qingfeng$ nic.pl
NIC 2.0 - New Instance Creator
------------------------------
  [1.] iphone/activator_event
  [2.] iphone/application_modern
  [3.] iphone/application_swift
  [4.] iphone/cydget
  [5.] iphone/flipswitch_switch
  [6.] iphone/framework
  [7.] iphone/ios7_notification_center_widget
  [8.] iphone/library
  [9.] iphone/notification_center_widget
  [10.] iphone/preference_bundle_modern
  [11.] iphone/tool
  [12.] iphone/tool_swift
  [13.] iphone/tweak
  [14.] iphone/xpc_service
Choose a Template (required): 13

在这里我们要开发的是tweak插件,所以输入13选择iphone/tweak,按下Enter继续。接着出现如下需要我们填写信息的步骤:

Project Name (required): AlertTest
Package Name [com.yourcompany.alerttest]: com.tianfu.alert
Author/Maintainer Name [清风]: tianfu
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.cqll.90
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: com.cqll.90
Instantiating iphone/tweak in alerttest/...
Done.

2、工程文件说明

Tweak工程新建完成后,其工程目录的文件如下所示:

tweak文件目录.png

2.1 control文件

该文件记录了工程的基本信息,会被打包进deb包中,字段内容如下:

Package: com.tianfu.alert
Name: AlertTest
Depends: mobilesubstrate
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: tianfu
Author: tianfu
Section: Tweaks

control文件中,可以自定义的字段还有很多,一般上面的信息就已经足够了。更全面的可以查看官方网站。值得注意的是:Theos在打包deb时会对control文件做进一步的处理。比如更改Version字段为:0.0.1-2,标识Theos的打包次数,方便管理;增加Installed-Size字段,用于描述deb包安装后的估算大小,与实际大小可能有偏差,不要更改。

2.2 Makefile

该文件用来指定工程编译和链接要用到的文件、框架、库等信息,将整个过程自动化。字段内容如下:

THEOS_DEVICE_IP = 192.168.11.130
THEOS = /usr/local/opt/theos
include $(THEOS)/makefiles/common.mk

TWEAK_NAME = AlertTest
AlertTest_FILES = Tweak.xm

include $(THEOS_MAKE_PATH)/tweak.mk

after-install::
    install.exec "killall -9 CQClient"

Makefile文件中除了自动生成的这些字段外,还可以根据功能手动添加其他字段:

更多的可以参考这篇文章Theos

2.3 AlertTest.plist

plist文件记录工程的配置信息,主要作用是指定程序的作用范围,内容如下:

alert.plist.png

从上面可以看到,该文件的内容是一系列的Dictionary,最外层为RootFilter键,Filter键下得array即为要设置的部分。该部分分为三类:

这三类Array可以根据需要来设定。但按照混合配置方式,一个文件只有满足Filter中所有Array下的至少一个条件,tweak才能生效。这样显然不合理。所以额外有一个Mode键,将其值设置为Any,那么文件满足Filter中的任一条件就能成为tweak的作用对象。

当Filter下的array只有一类时,不需要添加Mode和Any键值对。

2.4 Tweak.xm

该文件是实现具体功能的关键所在,是实现具体功能的源文件,这个文件支持Logos和C、C++语法。文件内容如下:

/* How to Hook with Logos
Hooks are written with syntax similar to that of an Objective-C @implementation.
You don't need to #include <substrate.h>, it will be done automatically, as will
the generation of a class list and an automatic constructor.

%hook ClassName

// Hooking a class method
+ (id)sharedInstance {
    return %orig;
}

// Hooking an instance method with an argument.
- (void)messageName:(int)argument {
    %log; // Write a message about this call, including its class, name and arguments, to the system log.

    %orig; // Call through to the original function with its original arguments.
    %orig(nil); // Call through to the original function with a custom argument.

    // If you use %orig(), you MUST supply all arguments (except for self and _cmd, the automatically generated ones.)
}

// Hooking an instance method with no arguments.
- (id)noArguments {
    %log;
    id awesome = %orig;
    [awesome doSomethingElse];

    return awesome;
}

// Always make sure you clean up after yourself; Not doing so could have grave consequences!
%end
*/

以上的注释展示了基本的Logos语法,具体可以分为三类:

除了注释中展示的三种语法外,Logos还支持%group%init%ctor等语法,更多的Logos语法参考这篇文章

这里我们输入代码来实现个弹出提示框的简单功能,代码如下:

%hook AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功注入代码" message:@"IOS 逆向工程" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alertView show];
#pragma clang diagnostic pop
return %orig;

}
%end

3、编译&打包&安装

3.1 编译

Theos采用与debian相同的make命令来编译Theos工程。执行如下指令:

qingfengdeMacBook-Pro:alerttest qingfeng$ make

输出结果如下:

qingfengdeMacBook-Pro:alerttest qingfeng$ make
> Making all for tweak AlertTest…
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (armv7)…
==> Linking tweak AlertTest (armv7)…
==> Generating debug symbols for AlertTest…
rm /Users/qingfeng/Documents/WorkSpace/Exercise/CQClientTweak/alerttest/.theos/obj/debug/armv7/Tweak.xm.mm
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (arm64)…
==> Linking tweak AlertTest (arm64)…
==> Generating debug symbols for AlertTest…
rm /Users/qingfeng/Documents/WorkSpace/Exercise/CQClientTweak/alerttest/.theos/obj/debug/arm64/Tweak.xm.mm
==> Merging tweak AlertTest…
==> Signing AlertTest…

从上图的输出结果可以看到,Theos完成了预处理、编译、签名等一系列的操作,此时目录下多了一个.theos文件夹。

3.2 打包

使用如下指令打包:

qingfengdeMacBook-Pro:alerttest qingfeng$ make package

输出结果如下:

qingfengdeMacBook-Pro:alerttest qingfeng$ make package
> Making all for tweak AlertTest…
make[2]: Nothing to be done for `internal-library-compile'.
> Making stage for tweak AlertTest…
dm.pl: building package `com.tianfu.alert:iphoneos-arm' in `./packages/com.tianfu.alert_0.0.1-1+debug_iphoneos-arm.deb'

这时候目录下多了一个packages文件夹,里面有一个.deb文件。

3.3安装

执行指令:

make package install

输出结果如下:

qingfengdeMacBook-Pro:alerttest qingfeng$ make package install
> Making all for tweak AlertTest…
make[2]: Nothing to be done for `internal-library-compile'.
> Making stage for tweak AlertTest…
dm.pl: building package `com.tianfu.alert:iphoneos-arm' in `./packages/com.tianfu.alert_0.0.1-4+debug_iphoneos-arm.deb'
==> Installing…
root@192.168.11.130's password: 
(Reading database ... 3518 files and directories currently installed.)
Preparing to unpack /tmp/_theos_install.deb ...
Unpacking com.tianfu.alert (0.0.1-4+debug) over (0.0.1-2+debug) ...
Setting up com.tianfu.alert (0.0.1-4+debug) ...
install.exec "killall -9 CQClient"
root@192.168.11.130's password: 
qingfengdeMacBook-Pro:alerttest qingfeng$

可以看到已经成功安装。打开app,看到如下结果:

逆向截屏.PNG

更多的介绍参考文章:
1、iOS逆向之TheOS
2、iOS逆向教程 2.1 Theos的安装及用法

参考文章

1、iOS 逆向工程--基础工具的运用
2、iOS逆向工程
3、ios逆向傻瓜入门教程(一)
4、IOS APP破解完整流程(逆向工程完整详解)
5、iOS 逆向工程-浅析

上一篇 下一篇

猜你喜欢

热点阅读