THEOS的第一個TWeak的成功創建參考資料:成功的創建一個TWeak的彈出步驟1:安裝Xcode和Xcode command line步驟2:安裝theosa:下載theos前,設置保存的路徑:環境變量b:下載theosc:下載頭文件d:下載ldid簽名工具e:配置MoblieSubstrate環境f:安裝dpkg步驟3:開始編寫Teak應用a 創建工程b.更改Tweak.xm文件c.編寫makfiled: 編譯,安裝,運行步驟4: 證書設置(不用多次輸入密碼)總結一些坑
THEOS的第一個TWeak的成功創建
TWeak的成功創建也不是一件容易的事情.
參考資料:
- http://www.iphonedevwiki.net/index.php/Theos/Getting_Started
- http://security.ios-wiki.com/issue-3-6/
- http://security.ios-wiki.com/
- http://www.cppblog.com/AutomateProgram/archive/2013/11/11/204205.html
- http://www.tuicool.com/articles/Bf2UFb
- http://stackoverflow.com/questions/21013325/dpkg-error-contains-ununderstood-data-member
成功的創建一個TWeak的彈出
步驟1:安裝Xcode和Xcode command line
Xcode的安裝直接去Apple官網上去下載就好了.command line的話,現在的Xcode(2015-09-20,都是Xcode7了,Xcode6開始已經集成了command line到Xcode中了).
網上的教程有的是使用的Xcode4,5,等低版本.
這里埋下了一個坑.
步驟2:安裝theos
a:下載theos前,設置保存的路徑:環境變量
theos保存到哪里?這個路徑是需要設置為環境變量的
可以直接寫到~/.bash_profile中(設置環境變量的一般方法)
export THEOS=/usr/local/iOSTools/theos
網上很多的人都安裝到了/opt/theos路徑下,不過這個路徑隨意了,看你一般的開發工具放在哪里,就寫那里就好了.
設置好后可以echo $theos來查看設置的環境變量是否設置好了.
zx:iOSTools zx$ echo $THEOS
/usr/local/iOSTools/theo
b:下載theos
下載的方法很多
- svn的方法有:
svn co http://svn.howett.net/svn/theos/trunk $THEOS
- gitt途徑(推薦,方便升級)
git clone git://github.com/DHowett/theos.git $ THEOS
當然了,也可以直接去git倉庫里下載一份壓縮的,將里面的內容解壓到$THEOS
c:下載頭文件
頭文件下載下來后需要做一些更改.
- clone 頭文件倉庫
- 將頭文件放到theos/include/目錄下
- 復制系統的IOSurfaceAPI.h到**theos/include/IOSurface**目錄下
- 注釋掉IOSurfaceCreateXPCObject 和IOSurfaceLookupFromXPCObject.
# clone iphoneheaders.git git clone git://github.com/rpetrich/iphoneheaders.git mv iphoneheaders/* theos/include/ cp /System/Library/Frameworks/IOSurface.framework/Headers/IOSurfaceAPI.h theos/include/IOSurface
如果沒有拷貝和注釋這兩個步驟,最后會導致打包失敗.
d:下載ldid簽名工具
- 下載ldid可執行文件
- 放到theos/bin/目錄下
# get ldid for Mac OS X
cd ~/theos/bin
curl -O http://dl.dropbox.com/u/3157793/ldid
chmod a+x ldid
這里的文件是在dropbox上,需要什么你的懂的.
所以,這里也提供一個百度盤上下載地址:
http://pan.baidu.com/s/1kTHoIGZ
注:我把這個工具下載下來放到了http://pan.baidu.com/s/1kTHoIGZ,也可以從這里下載,然后給它添加執行權限(chmod +x ldid)並移動到$THEOS/bin/這個目錄下。(from http://security.ios-wiki.com/issue-3-6/)
PS.當然了,你可以直接從源碼編譯安裝,下面是源碼倉庫
https://github.com/rpetrich/ldid
e:配置MoblieSubstrate環境
方法一:
# get libsubstrate.dylib substrate.h cd ~/theos curl -OL http://apt.saurik.com/debs/mobilesubstrate_0.9.3366-1_iphoneos-arm.deb dpkg-deb -x mobilesubstrate_0.9.3366-1_iphoneos-arm.deb mobilesubstrate cp mobilesubstrate/Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate ~/theos/lib/libsubstrate.dylib cp mobilesubstrate/Library/Frameworks/CydiaSubstrate.framework/Headers/CydiaSubstrate.h include/substrate.h
(from http://www.tuicool.com/articles/Bf2UFb)
方法二:
- 從越獄后設備拷貝出來一份
- 重名名為: theos/lib/libsustrate.dylib
將iOS上的“/Library/Frameworks/CydiaSubstrate.framework/ CydiaSubstrate”拷貝到OSX中,將其重命名為libsubstrate.dylib后放到“/opt/theos/lib/libsubstrate. dylib”中
(from http://book.51cto.com/art/201504/472934.htm)
方法三:
- 百度盤里下載:http://pan.baidu.com/s/1o6ocwXk(from http://security.ios-wiki.com/issue-3-6/)
f:安裝dpkg
方法一:
- 通過mac port安裝.
mac port也就是一個軟件包管理工具,像weget,apt-install,brew等工具類似.
首先我嘗試安裝的是https://www.macports.org/install.php下的https://distfiles.macports.org/MacPorts/MacPorts-2.3.3-10.10-Yosemite.pkg
但是安裝卡頓,失敗.
然后,嘗試源碼編譯安裝,
“cd” into the directory where you downloaded the package and run “tar xjvf MacPorts-2.3.3.tar.bz2” or “tar xzvf MacPorts-2.3.3.tar.gz”, depending on whether you downloaded the bz2 tarball or the gz one, respectively.
Build and install the recently unpacked sources:
- cd MacPorts-2.3.3
- ./configure && make && sudo make install
Optionally:
- cd ../
- rm -rf MacPorts-2.3.3*
(from https://www.macports.org/install.php)
但是
sudo port selfupdate sudo port install dpkg
這里 時候,我的時候,根本不動了,毛網速沒有啊....
方法二:
- 通過brew來安裝
brew的作者最近不是去了apple了嗎?哈哈,以前配置號brew后,來下載就方便多了.
brew install dpkg
不到1分鍾就可以下載好了. 真是給方法一給跪了.
步驟3:開始編寫Teak應用
a 創建工程
選一個目錄,存放工程.
zx:theosCode zx$ pwd
/Users/zx/documents/theosCode
zx:theosCode zx$ $THEOS/bin/nic.pl
NIC 2.0 - New Instance Creator
------------------------------
[1.] iphone/application
[2.] iphone/library
[3.] iphone/preference_bundle
[4.] iphone/tool
[5.] iphone/tweak(選這個)
Choose a Template (required): 5
Project Name (required): zxtest
Package Name [com.yourcompany.zxtest]: com.z2xy.zxtext(可選項,按enter,[]中的作為默認值)
Author/Maintainer Name [zx]: zx(按enter,[]中的作為默認值)
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.apple.springboard(按enter,[]中的作為默認值)
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: SpringBoard(按enter,[]中的作為默認值)
Instantiating iphone/tweak in zxtest/...
Done.
生成后的文件列表如下:
x
zx:zxtest zx$ pwd
/Users/zx/documents/theosCode/zxtest
zx:zxtest zx$ tree
.
├── Makefile
├── Tweak.xm
├── control
├── theos -> /usr/local/iOSTools/theos/
└── zxtest.plist
1 directory, 4 files
其中:
- Makefiile文件用於自動化編譯
- Tweak.xm類似於我們 業務函數,main函數
- control是一些工程信息
- zxtest.plist也是項目的一些工程信息
- 還有一個符號鏈接指向我們的$THEOS的安裝目錄(最開始配置的環境變量)
b.更改Tweak.xm文件
x
#import <SpringBoard/SpringBoard.h>
%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)application {
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"__z2xy__"
message:@"from zxdev"
delegate:nil
cancelButtonTitle:@"取消" otherButtonTitles:@"好的",nil];
[alert show];
[alert release];
}
%end
上面引入了一個頭文件,然后替換了**applicationDidFinishLaunching方法的實現.
一些語法 %hook,%orig,%end,倒是很像django的模板語法.
這里不詳細講,見上的參考資料.
c.編寫makfile
xxxxxxxxxx
ARCHS = armv7
THEOS_DEVICE_IP = 192.168.0.120 //(可選)
zxtest_FRAMEWORKS = UIKit
include theos/makefiles/common.mk
TWEAK_NAME = zxtest
zxtest_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
主要是添加了頭三行.
- 第一行,指定了系統架構
- 第二行,指定了同一局域網下的iOS設備的ip地址(需要根據自己的實際情況更換)
查看iOS設備的ip地址的話,直接到手機的設置,wifi,連接 wifi下查看就可以了.
如果需要自動安裝的話,可以去掉這行
- 第三行,導入UIKit頭文件
如果不導入文件編譯出錯,找不到符號,因為我們使用了UIKit中的UIAlertView.
d: 編譯,安裝,運行
方法一:
需要指定iOS設備ip地址,需要設置好信任的證書.
zx:zxtest zx$ make package install
Making all for tweak zxtest...
make[2]: Nothing to be done for `internal-library-compile'.
Making stage for tweak zxtest...
dpkg-deb: building package `com.z2xy.zxtext' in `./com.z2xy.zxtext_0.0.1-1_iphoneos-arm.deb'.
install.exec "cat > /tmp/_theos_install.deb; dpkg -i /tmp/_theos_install.deb && rm /tmp/_theos_install.deb" < "./com.z2xy.zxtext_0.0.1-1_iphoneos-arm.deb"
Selecting previously deselected package com.z2xy.zxtext.
(Reading database ... 4989 files and directories currently installed.)
Unpacking com.z2xy.zxtext (from /tmp/_theos_install.deb) ...
Setting up com.z2xy.zxtext (0.0.1-1) ...
install.exec "killall -9 SpringBoard"
方法二:
- 編譯
zx:zxtest zx$ make
Making all for tweak zxtest...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
Linking tweak zxtest...
Stripping zxtest...
Signing zxtest...
- 打包
zx:zxtest zx$ make package
Making all for tweak zxtest...
make[2]: Nothing to be done for `internal-library-compile'.
Making stage for tweak zxtest...
dpkg-deb: building package `com.z2xy.zxtext' in `./com.z2xy.zxtext_0.0.1-2_iphoneos-arm.deb'.
- 上傳到設備安裝
x
zx:zxtest zx$ scp com.z2xy.zxtext_0.0.1-2_iphoneos-arm.deb root@192.168.0.120:/var/root/
com.z2xy.zxtext_0.0.1-2_iphoneos-arm.deb 100% 2270 2.2KB/s 00:00
當然了,可以使用各種工具將此deb文件上傳到設備中,上面使用的是scp.
第一個參數是本地文件路徑,第二參數目的路徑
root@192.168.0.120指定了同一局域下的iOS設備,/var/root/指定的設備中的路徑,中間有一個冒號連接.
- 在設備上使用iFile到/var/root/目錄下找到安裝包安裝

如果上面的步驟錯誤的話,
ununderstood data nember data.tar.xz
參考: http://stackoverflow.com/questions/21013325/dpkg-error-contains-ununderstood-data-member,打包的時候,壓縮的方式和iOS設備上解壓縮的方式不一致導致的錯誤.
方式是,替換$THEOS/makefiles/package/deb.mk文件中
$(ECHO_NOTHING)COPYFILE_DISABLE=1 $(FAKEROOT) -r dpkg-deb -b "$(THEOS_STAGING_DIR)" "$(_THEOS_DEB_PACKAGE_FILENAME)" $(STDERR_NULL_REDIRECT)$(ECHO_END)
為:
$(ECHO_NOTHING)COPYFILE_DISABLE=1 $(FAKEROOT) -r dpkg-deb -Zgzip -b "$(THEOS_STAGING_DIR)" "$(_THEOS_DEB_PACKAGE_FILENAME)" $(STDERR_NULL_REDIRECT)$(ECHO_END)
- 重啟設備,看到彈窗

可以看到第一中方法簡便了不少,只需要指定iP地址和完成證書配置就可以一行命令搞定.
步驟4: 證書設置(不用多次輸入密碼)
- 刪除iOS設備對應的ip地址所在行
從~/.ssh/known_hosts
xxxxxxxxxx
192.168.0.120 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvTS9W8/88Z+9j2D/TBft/ns173ssWhBGQgMyBnSAzIdhmfVd830aq2/78MgKyadyGxD6xhF/LG9tP5ZshaeZB9 XjBjQnEtB0gTZjiyGJpBWFdXCLX/uxV35gwZnu11Ts/HQzdC4+n+eh9pqKR7vsaJP/gIiYqfz/oDchlHrf7bdicZ0yKFlOpilTP4WLgPz2Qqr96xGcn/3dwimFTuye5zAqqfUApjTS/ idKvJNOEsFo6lVkuyOmkbItTjp6MfQliNorOY8nvV0DRFx2+lmedRjtq6UNPTyjuLPx3ymZIg/uOazvB0CHs/Ta2kMZRe1bPl8VcHI5/AbL0i7VHQB1p
- mac上生成證書
$ssh-keygen -t rsa
如果已經生成過整數,直接得到~/.ssh/id_ras.pub 公鑰就可以了.
- iOS設置生成.ssh目錄
ssh登錄到iOS設備,生成證書就可以了
xxxxxxxxxx
$ssh root@192.168.0.120
zxde-iPhone:~ root# ssh-keygen
zxde-iPhone:~ root# ls -la
drwx------ 2 root wheel 170 Sep 20 22:34 .ssh/
- 用mac上的公鑰,替換掉iOS設備上的公鑰
scp ~/.ssh/id_rsa.pub root@192.168.0.120:/var/root/.ssh/
通過上面 的證書配置和iOS設備配置,
只需要方法一的一行
$make package install
就可以實現,編譯,打包,上傳到設備,安裝deb包,運行.
總結一些坑
- 下載頭文件的時候,需要做一些替換,git倉庫克隆后都有里面居然有這個步驟的注釋!!!!(否則的話,打包就會出現找不到頭文件,知道不到符號之類的錯誤)
- ldid簽名工具下載,直接去下載就好了,不要從源碼編譯了.
- 配置MoblieSubstrate環境的話,直接從iOS設備上拷貝出來就可以了.
- makefile中指定iOS設備的IP地址,省了不少時間,同時,導入頭文件,也是解決了找不到文件的錯誤.
- 打包文件的解壓縮不一致導致的錯誤,
ununderstood data nember data.tar.xz
參考http://stackoverflow.com/questions/21013325/dpkg-error-contains-ununderstood-data-member就好了.
其實,有些問題在http://www.iphonedevwiki.net/index.php/Theos/Getting_Started 中已經給出了很好的回答
只是最開始沒有看.
—
最后,"步驟4: 證書設置(不用多次輸入密碼)”,使用后,方便了很多.
最后的最后,這里還有一些列相關的資料:
