先來看看效果:

最近在看 《ios應(yīng)用逆向工程(第二版)》剛好看到利用theos創(chuàng)建tweak工程于是就上手試了一下。
1.安裝THEOS
1.1
打開終端,設(shè)置目錄
export THEOS=/opt/theos
安裝 THEOS
sudo svn co http://svn.howett.net/svn/theos/trunk $THEOS
1.2 下載ldid
下載到tmp
sudo curl -s http://dl.dropbox.com/u/3157793/ldid > /tmp/ldid
復(fù)制
sudo cp /tmp/ldid $THEOS/bin/
權(quán)限
sudo chmod +x $THEOS/bin/ldid
刪除
rm /tmp/ldid
1.3dpkg,打包用
安裝Xcode Command Tools
xcode-select --install
安裝Macport 選擇自己的系統(tǒng)版本
https://guide.macports.org/chunked/installing.macports.html
設(shè)置下Port的環(huán)境 不然一直報(bào)command not found: port
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
安裝dpkg
sudo port install dpkg
2.1創(chuàng)建Theos工程
如果之前的command已經(jīng)關(guān)閉,重新輸入運(yùn)行 export THEOS=/opt/theos 設(shè)置環(huán)境變量
$THEOS/bin/nic.pl
2.2 選擇個(gè)工程模板

創(chuàng)建tweak工程選擇11

輸入相應(yīng)的參數(shù)
2.3 創(chuàng)建好后會(huì)生成4個(gè)文件
Makefile、Tweak.xm、control、iOSREProject.plist 下面進(jìn)行簡單說明。
2.3.1Makefile
Makefile 文件指定工程用到的文件、框架、庫等信息,將整個(gè)過程自動(dòng)化,下圖為里面內(nèi)容說明。

2.3.2Tweak.xm
用 Theos 創(chuàng)建 tweak 工程, 認(rèn)生成的源文件是 Tweak.xm。
如果后綴名是單獨(dú)一個(gè)“ x”,說明源文件支持 Logos 和 C 語法;
如果后綴名是“ xm ”,說明源文件支持 Logos 和 C/C++ 語法,與“ m ”和“ mm ”的區(qū)別類似。
Tweak.xm 語法眾多,在這里簡單介紹一些,詳細(xì)語法大家可以去看書籍。
● %hook
指定需要 hook 的 class, 必須以 %end 結(jié)尾
%hook SpringBoard
- (void)_menuButtonDown:(id)down {
NSLog(@"You've pressed home button.");
%orig; // call the original _menuButtonDown:
}
%end
● %orig該指 在 %hook 內(nèi)部使用,執(zhí)行被 (hook)的 數(shù)的原始代碼,如下:
%hook SpringBoard
- (void)_menuButtonDown:(id)down {
NSLog(@"You've pressed home button.");
%orig; // call the original _menuButtonDown: }
%end
●還可以利用 %orig 更改原始 數(shù)的參數(shù),例如:
%hook SBLockScreenDateViewController
- (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2 {
%orig(@"li lei", arg2); }
%end
2.3.3control文件
control文件 錄了deb包管理系統(tǒng)所需的基本信息,會(huì)被打包進(jìn)deb包里。
iOSREProject 里 control 文件的內(nèi)容如下:

2.3.4iOSREProject.plist文件
簡單來說就是里面描述了tweak 的作用范圍,也就是需要作用的APP的bundle identifier。
也就是在創(chuàng)建項(xiàng)目的時(shí)候填寫的這個(gè)地方:
3.1 工程配置完成后,開始打包編譯安裝
cd theos工程目錄
make package install
再次過程中需要輸入兩次密碼
**補(bǔ)充
如果在打包安裝的時(shí)候出現(xiàn)如下錯(cuò)誤:

找到:

把原來的打包方式改成gzip即可:
