一、class-dump
簡介:顧名思義,就是用來導出目標對象的class信息的工具,私有方法聲明也能導出來。
原理:利用 Objective-C語言的 runtime 特性,將存 在Mach-O 文件中的頭文件信息提 出來,並生成對應的 .h 文件。
使用方法:
1,下載然后將class-dump 復制到“ /usr/bin”目錄下。
2,執行sudo chmod 777 /usr/bin/class-dump”命令賦予其執行權限。
3,class-dump執行:
|
1
2
|
class-dump -S -s -H /Applications/Calculator.app -o
/Users/zhangdasen/Desktop/test
|
4,上面一段代碼就是導出Mac下計算器app的頭文件,到桌面test文件夾,並且排序,class-dump的一些參數,大家可以自己在命令行輸入class-dump然后回車就有相關說明。
使用注意
從 AppStore 下 的 App 都是經過加密的,可執行文件被加上了一層”殼”。
class-dump 應付不了這樣的文件,此時使用 class-dump 看上去會“失效”。還得先用別的工具把殼砸開才行,這個后面會說到。
class-dump下載地址:http://stevenygard.com/projects/class-dump
二、Theos越獄開發工具包
簡介
Theos:是一個越獄開發工具包,由iOS越獄界知名人士Dustin Howett(@DHowett)開發並分享到 GitHub 上。
iOSOpenDev:是整合在 Xcode里的越獄開發工具, 熟悉Xcode 的朋友可能會對它更感興趣。但逆向工程接觸底層知識較多,很多東西無法自動化,因此推薦使用整合度並不算高的 Theos,當你手動完成一個又一個練習時,對逆向工程的理解一定會更深。
(1) Theos的安裝:
1,設置xcode
假設安裝了3 個 Xcode,並將它們分別命名為 Xcode1.app、Xcode2.app 和 Xcode3.app, 要指定 Xcode3 為活動 Xcode,則運行如下命令 :
|
1
|
sudo xcode-select -s /Applications/Xcode3.app/Contents/Developer
|
2, Theos下載
從 GitHub 上下 Theos, 操作如下:(在終端中執行)
|
1
2
|
export THEOS=/opt/theos
sudo git clone git://github.com/DHowett/theos.git$THEOS
|
export THEOS=/opt/theos
這句話是設置環境變量,上面兩句話指的就是git克隆theos到系統的/opt/theos目錄下.小問題:git 克隆下來的theos由於缺少文件,在后面make packget install 會提示缺少 _Prefix/NullabilityCompat.h 等文件,需要我們去https://github.com/theos/headers 把這個頭文件放到/opt/theos/include目錄中就行了。(可直接克隆到目錄中去)
1 sudo git clone <span class="hljs-symbol">https:</span>/<span class="hljs-regexp">/github.com/theos</span><span class="hljs-regexp">/headers /opt</span><span class="hljs-regexp">/theos/includ</span>不過我這邊已經有一個封好的theos,下面會提到。
3,配置ldid
ldid 是專門用來 名 iOS 可執行文件的工具,用以在越獄 iOS 中 代 Xcode 自帶的codesign。從 http://joedj.net/ldid 下 ldid,把它放在“ /opt/theos/bin/ ”下,然后用以下命令賦予它可執行權限:
|
1
|
sudo chmod 777 /opt/theos/bin/ldid
|
4,配置CydiaSubstrate
因為Theos 的一個 bug,它無法自動生成一個有效的 libsubstrate.dylib 文件,需要手動操作。
在 Cydia 中搜索安裝“CydiaSubstrate”,然后用 iFunBox 或 scp 等方式將 iOS 上的“/Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate” 到 OSX 中,將其重命名為 libsubstrate.dylib 后放到
“ /opt/theos/vendor/lib/libsubstrate.dylib”中, 替換掉無效的文件即可。
5,dpkg-deb
deb 是越獄開發安裝包的標准格式,dpkg-deb 是一個用於操作 deb 文件的工具,有了這個工具,Theos 才能正確地把工程打包成為 deb 文件。
從 https://raw.githubusercontent.com/DHowett/dm.pl/master/dm.pl 下 dm.pl,將其重命名為 dpkg-deb 后,放到“/opt/theos/bin/”目錄下,然后用以下命令賦予其可執行權限:
|
1
|
sudo chmod 777 /opt/theos/bin/dpkg-deb
|
Theos操作這些移動文件設置權限這些步驟很啰嗦。
鑒於這些,我自己這邊已經封裝了一個集成好的Theos,附帶一個自動下載的小腳本TheosScript.sh,不需要手動添加ldid、libsubstrate、dpkg-deb等。
腳本下載地址:https://github.com/DaSens/Theos-Script
進行簡單演示
(2) Theos的用法介紹:
1,更改工作目錄
更改工作目錄至常用的 iOS 工程目錄(如 “ /Users/zhangdasen/Code/”)
注:為何要做這一步呢,因為使用Theos創建的工程,是你在哪個目錄執行的nic.pl啟動的,就會在創建完成后在這個目錄生成創建的Theos項目。
2,創建工程
終端輸入“/opt/theos/bin/nic.pl”,啟動 NIC(New Instance Creator),下面進行簡單演示:
3,簡單說明
上面創建工程輸入了一大堆東西,下面用一張圖來說明:
4,工程文件說明
創建好工程就會生成四個文件:
Makefile、Tweak.xm、control、iOSREProject.plist 下面進行簡單說明。
(1) Makefile
Makefile 文件指定工程用到的文件、框架、庫等信息,將整個過程自動化,下圖為里面內容說明。
下圖是我自己修改過后的Makefile文件。
補充:
1,導入 private framework
|
1
|
iOSREProject_PRIVATE_FRAMEWORKS = private framework name
|
2,鏈接 Mach-O 對象(Mach-O object)
|
1
|
iOSREProject_LDFLAGS = -lz –lsqlite3.0 –dylib1.o
|
(2) Tweak.xm
用 Theos 創建 tweak 工程, 認生成的源文件是 Tweak.xm。
如果后綴名是單獨一個“ x”,說明源文件支持 Logos 和 C 語法;
如果后綴名是“ xm ”,說明源文件支持 Logos 和 C/C++ 語法,與“ m ”和“ mm ”的區別類似。
Tweak.xm 語法眾多,在這里簡單介紹一些,詳細語法大家可以去看書籍。
● %hook
指定需要 hook 的 class, 必須以 %end 結尾
|
1
2
3
4
5
6
7
|
%hook SpringBoard
- (void)_menuButtonDown:(id)down {
NSLog(@"You've pressed home button.");
%orig; // call the original _menuButtonDown:
}
%end
|
● %orig該指 在 %hook 內部使用,執行被 (hook)的 數的原始代碼,如下:
|
1
2
3
4
5
6
|
%hook SpringBoard
- (void)_menuButtonDown:(id)down {
NSLog(@"You've pressed home button.");
%orig; // call the original _menuButtonDown: }
%end
|
|
1
2
3
4
5
|
%hook SBLockScreenDateViewController
- (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2 {
%orig(@"iOS 8 App Reverse Engineering", arg2); }
%end
|
● %group
該指 用於將 %hook 分組,便於代碼管理及按條件初始化分組(含義 后有 細解 ),必須以 %end 結 ;一個 %group可以包含多個 %hook,所有不 於某個自定義 group 的 %hook會被隱式歸類到 %group _ungrouped 中。
用法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
%group iOS7Hook
%hook iOS7Class
- (id)iOS7Method {
id result = %orig;
NSLog(@"This class & method only exist in iOS 7.");
return result;
} %end
%end // iOS7Hook
%group iOS8Hook
%hook iOS8Class
- (id)iOS8Method {
id result = %orig;
NSLog(@"This class & method only exist in iOS 8."); return result;
}
%end
%end // iOS8Hook
|
● %init
該指令用於初始化某個 %group,必須在 %hook 或 %ctor 內調用;如果帶參數,則初始化指定的 group,如果不帶參數,則初始化 _ungrouped,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
#define kCFCoreFoundationVersionNumber_iOS_8_0 1140.10 #endif
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
%init; // Equals to %init(_ungrouped)
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0 && kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_8_0)
%init(iOS7Hook);
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0)
%init(iOS8Hook);
}%end
只有調用了
%init,對應的 %group 才能起作用,切 切 !
|
● %new
在 %hook 內部使用,給一個現有 class 加新函數,功能與 class_addMethod 相同。它的用法如下:
|
1
2
3
4
5
6
|
%hook SpringBoard %new
- (void)namespaceNewMethod {
NSLog(@"We've added a new method to SpringBoard.");
}
%end
|
● %ctor
tweak的constructor,完成初始化工作;如果不顯式定義,Theos會自動生成一個%ctor,並在其中調用 %init(_ungrouped)。因此,
%hook SpringBoard – (void)reboot {
NSLog(@”If rebooting doesn’t work then I’m screwed.”);
%orig;
}
%end
可以成功生效,因為 Theos 隱式定義了如下內容:%ctor
|
1
2
3
|
{
%init(_ungrouped);
}
|
而
|
1
2
3
4
5
6
7
|
%hook SpringBoard
- (void)reboot{
NSLog(@"If rebooting doesn't work then I'm screwed.");
%orig;
}
%end
|
里的 %hook無法生效,因為這里顯式定義了%ctor,卻沒有顯式調用 %init,
%group(_ungrouped) 不起作用。
%ctor 一般可以用來初始化 %group,以及進行 MSHookFunction 等操作,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
#define kCFCoreFoundationVersionNumber_iOS_8_0 1140.10 #endif
%ctor{
%init;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0 && kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_8_0)
%init(iOS7Hook);
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0)
%init(iOS8Hook);
MSHookFunction((void *)&AudioServicesPlaySystemSound, (void *)&replaced_AudioServicesPlaySystemSound, (void **)&original_AudioServicesPlaySystemSound);
}
|
注意,%ctor 不需要以 %end 結 。
● %c
該指 的作用等同於 objc_getClass 或 NSClassFromString,即動態獲 一個類的定義,在 %hook 或 %ctor 內使用。
(3) control文件
control文件 錄了deb包管理系統所需的基本信息,會被打包進deb包里。
iOSREProject 里 control 文件的內容如下:
control 文件中可以自定義的字段還有很多,但上面這些信息就已經足夠了。更全面的
說明可以參閱 debian 的官方網站(http://www.debian.org/doc/debian-policy/ch-controlfields.html)
(3)iOSREProject.plist文件
簡單來說就是里面描述了tweak 的作用范圍,也就是需要作用的APP的bundle identifier。
也就是在創建項目的時候填寫的這個地方:







