1. 程式人生 > >class-dump 和 iOSOpenDev 的使用

class-dump 和 iOSOpenDev 的使用

eat doc trac profiling weixin rar conn aar 手機

class-dump 官網地址:這裏

我這裏下載的是 class-dump-3.5.dmg 版本號的。

雙擊.dmg 文件,將?技術分享圖片?拉倒 /usr / local / bin 文件夾下,這樣就能夠在終端使用 class-dump 命令了。

這裏我演示dump系統自帶的計算器,導出它的頭文件。

命令例如以下:

class-dump -H /Applications/Calculator.app -o /Users/Rio/Desktop/calculate\ heads

解釋:

/Applications/Calculator.app 是計算器app的路徑,註意,這裏dump的是應用後綴是.app而不是.ipa,不要傻傻的去dump .ipa後綴了。會提示“

class-dump: Input file (xxx.ipa) is neither a Mach-O file nor a fat archive.”錯誤的!

/Users/Rio/Desktop/calculate\ heads 是存放dump出來頭文件的文件夾路徑

class-dump 3.5 (64 bit)
Usage: class-dump [options] <mach-o-file>

  where options are:
        -a             show instance variable offsets
        -A             show implementation addresses
        --arch <arch>  choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64)
        -C <regex>     only display classes matching regular expression
        -f <str>       find string in method name
        -H             generate header files in current directory, or directory specified with -o
        -I             sort classes, categories, and protocols by inheritance (overrides -s)
        -o <dir>       output directory used for -H
        -r             recursively expand frameworks and fixed VM shared libraries
        -s             sort classes and categories by name
        -S             sort methods by name
        -t             suppress header in output, for testing
        --list-arches  list the arches in the file, then exit
        --sdk-ios      specify iOS SDK version (will look in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS<version>.sdk
        --sdk-mac      specify Mac OS X version (will look in /Developer/SDKs/MacOSX<version>.sdk
        --sdk-root     specify the full SDK root path (or use --sdk-ios/--sdk-mac for a shortcut)

執行命令。能夠看到已經dump出頭文件了,例如以下所看到的:

技術分享圖片

? ?class-dump 盡管非常實用,但有時我們會發現 class-dump 執行失敗,無法得到我們想要的 .h 文件,或者 .h 文件的內容是加密的密文。

出現這種現象的原因是:class-dump 額作用對象必須是未經加密的可執行文件,而App Store 下載的 App 都是經過簽名加密的。可執行文件被加上了一層“殼”。能夠使用 AppCrackr 來自己主動砸殼。


---------- 2016.01.07 更新 ----------------

從App Store 上下載的 App 我這裏是通過 Clutch 這個來砸殼的。

先 ssh 到 iPhone 上。可能會遇到這種問題:

MacBook:~ aaron$ ssh [email protected]

ssh: connect to host 172.17.24.70 port 22: Connection refused

不急,去 Cydia 安裝 OpenSSH 就可以,安裝完後再次 ssh 到 iPhone 發現已經能夠了。

去網上下載 Clutch 這個軟件,改名為 clutch (不改也能夠。這裏改名僅僅是為了以後敲命令的時候老是要第一個字母大寫,麻煩!

),然後 copy 到 iPhone 的 /usr/bin/ 文件夾以下。

MacBook:~ aaron$ scp /Users/aaron/Documents/kugou/doc/越獄開發/hack/clutch [email protected]:/usr/bin/

[email protected]‘s password:?

clutch? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100%? 915KB 914.8KB/s ? 00:00?

吶。已經 ok 了,接下來改動 clutch 為最高權限 777

iPhone:/usr/bin root# chmod 777 ./clutch

查看是否改動成功。

iPhone:/usr/bin root# ls -al | grep clutch

-rwxrwxrwx 1 root ? wheel? 936752 Jan? 7 16:02 clutch

能夠看到已經是最高權限了。

接下來是使用 clutch 了,網上說還要安裝 Mobile Terminal 啥啥啥的一律不用管它,直接開幹。

iPhone:/usr/bin root# clutch -i?

輸入上面的命令就能夠看到一大堆能夠砸殼的應用:

Installed apps:

1) <AlipayWallet bundleID: com.alipay.iphoneclient>


2) <WeChat bundleID: com.tencent.xin>


3) <yidian bundleID: com.yidian.zixun>


4) <YoukuiPhone bundleID: com.youku.YouKu>


5) <嘀嘀打車 bundleID: com.xiaojukeji.didi>


6) <Taobao4iPhone bundleID: com.taobao.taobao4iphone>


7) <NewsBoard bundleID: com.netease.news>


8) <iLady bundleID: cn.com.modernmedia.imodernlady>


9) <netdisk_iPhone bundleID: com.baidu.netdisk>

..........


輸入你想要砸殼的應用 bundle identifier 進行砸殼

iPhone:/usr/bin root# clutch -d com.yidian.zixun

成功後你就能夠看到已經破解完後的路徑:

DONE: /private/var/mobile/Documents/Dumped/com.yidian.zixun-iOS7.0-(Clutch-2.0 RC2).ipa

之後。copy .ipa 文件到你的電腦上,

MacBook:~ aaron$ scp root:172.17.24.70:/private/var/mobile/Documents/Dumped/com.yidian.zixun-iOS7.0-(Clutch-2.0 RC2).ipa ~/Desktop/

-bash: syntax error near unexpected token `(‘

誒,有錯誤,識別不了`(‘,那麽改一下.ipa 名字後在 scp 吧

iPhone:/private/var/mobile/Documents/Dumped root# mv com.yidian.zixun-iOS7.0-\(Clutch-2.0\ RC2\).ipa yidian.ipa

再 copy 到電腦桌面:

MacBook:~ aaron$ scp [email protected]:/private/var/mobile/Documents/Dumped/yidian.ipa ~/Desktop/yidian.ipa

[email protected]‘s password:?

yidian.ipa? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% ? 25MB ? 1.5MB/s ? 00:16

至此,砸殼就已經完畢了,如今能夠用 class-dump 把頭文件 dump 出來了。cheer!


-------------------------------- 切割線 -----------------------------------

安裝?apt-get :

打開cydia –管理設置選擇開發人員”—完畢,?搜索apt。安裝APT 0.6 Transitional,它會安裝四五個其他依賴包,都不大.

安裝後就能用apt-get了。比如apt-get install netstat, apt-get install ps等。




-------------------------------- 切割線 -----------------------------------


在新建project之前另一個非常重要的事情要做。安裝打印日誌用的 log。

# 看log
apt-get install socat

安裝完後,輸入以下命令看log

socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock
>watch


安裝 socat 的時候出現這個錯誤提示的話:

E: Could not get lock /var/lib/dpkg/lock - open (35: Resource temporarily unavailable)?

E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

能夠嘗試用這裏的辦法解決。

(先試一下這個帖子提到的其他辦法,最後不得已再使用例如以下辦法)

技術分享圖片


為了在使用 iosOpenDev?開發時調試更加便利,機子還須要安裝這樣一些環境:

# 這個是使用iosOpenDev開發時,xcode把deb包安裝到手機時。須要手機先具備的環境條件
apt-get install coreutils diskdev-cmds file-cmds system-cmds com.saurik.substrate.safemode mobilesubstrate preferenceloader


若出現“Package diskdev-cmds is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source”這種安裝錯誤的時候。執行例如以下命令後再次安裝過,
sudo apt-get update && && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get install packagename
(or)
apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y && apt-get install packagename
參考來源:http://www.blackmoreops.com/2014/12/13/fixing-error-package-packagename-not-available-referred-another-package-may-mean-package-missing-obsoleted-available-another-source-e-pa/


有些機子環境,安裝不了netstat、socat等環境,於是就不能通過iosOpenDev來拷貝dylib。也不能用socat來查看log。這時,解決的方法是:
1. 在iosOpenDev編譯完後,手動把dylib復制到手機:
scp hookKTV.dylib [email protected]:/Library/MobileSubstrate/DynamicLibraries/
2. 把原本的NSLog換成寫文件。把hook信息寫到文件,之後用手機助手之類的工具導出文件。
3. 重新啟動springboard:
killall SpringBoard


-------------------------------- 切割線 -----------------------------------

以上全部環境搞定之後就能夠新建一個project了:

技術分享圖片

選擇Logos Tweak 。然後一路next 到創建好新project。

新建好的 .xm 文件裏會有一個一個提示,要你去?/opt/iOSOpenDev/lib/ 文件夾下拉一個libsubstrate.dylib 動態鏈接庫到project,由於我們這裏是要hook SpringBoard,要彈出一個彈出框。所以須要再導入 UIKit.framework.

#error iOSOpenDev post-project creation from template requirements (remove these lines after completed) -- \

Link to libsubstrate.dylib: \

(1) go to TARGETS > Build Phases > Link Binary With Libraries and add /opt/iOSOpenDev/lib/libsubstrate.dylib \

(2) remove these lines from *.xm files (not *.mm files as they‘re automatically generated from *.xm files)


然後在 .xm 裏面編寫代碼例如以下:

// Logos by Dustin Howett
// See http://iphonedevwiki.net/index.php/Logos
#import <UIKit/UIKit.h>

%hook SpringBoard

- (void)applicationDidFinishLaunching:(id)application {
    %orig;
    
    NSLog(@"hook SpringBoard:這是測試log輸出。。

。。

"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"welcome" message:@"hellowrold" delegate:nil cancelButtonTitle:@"thanks" otherButtonTitles:nil]; [alert show]; [alert release]; } %end %hook AppDelegate - (void)applicationDidEnterBackground:(UIApplication *)application{ %orig; NSLog(@"hook AppDelegate:這是測試log輸出。。。

"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hook" message:@"hellowrold" delegate:nil cancelButtonTitle:@"thanks" otherButtonTitles:nil]; [alert show]; [alert release]; } %end

編譯的時候有可能會遇到例如以下的編譯錯誤:

技術分享圖片

解決的方法是:改動 TARGETS - Build Settings - Code Signing - Provisioning Profile 為 iOS Team Provisioning Profile: * 就好了。


ssl 到手機,執行

socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock

>watch

這個命令。準備查看輸出 log。


電腦和越獄設備連接到同一個網絡下,然後查看設備的 wifi 地址,記下來,然後填寫到 TARGETS - Build Settings - User-Defined - iOSOpenDevDevice 裏面去。比方我這裏是 192.168.2.42 ,為的是等下 Profiling 的時候把動態鏈接庫 HookTest.dylb copy 到[email protected]:/Library/MobileSubstrate/DynamicLibraries/ 文件夾下。

假如你以上環境都能成功搭建的話,那接下來就簡單了。點擊 XCode -> Product -> Build For -> Profiling ,XCode 就會幫你把動態鏈接庫 HookTest.dylib (我新建的project叫HookTest,所以這裏是HookTest.dylib)和 .plist(這裏是 HookTest.plist) 文件copy 到[email protected]:/Library/MobileSubstrate/DynamicLibraries/ 這個路徑下了,然後重新啟動springboard:
執行命令 killall SpringBoard,就能夠看到手機在啟動的時候就會去執行剛剛的 HookTest.dylib 然後彈出一個彈出框了。

copy 進去的 .plist 的作用是在hook的時候系統會依據 .plist 去過濾 hook 那個應用。假設 .plist 裏面啥過濾條件都不寫的話是全部應用都會去載入剛剛的copy進去的動態庫的,默認是hook SpringBoard。
。比方說你如今hook 應用啟動時的?

<del>- (void)applicationDidEnterBackground:(UIApplication *)application</del>

這種方法的話。假設.plist裏面不指定是哪個應用去載入HookTest.dylib 的話,默認全部應用啟動的時候都會去載入 HookTest.dylib 動態庫的。


Profiling 完後查看了一下。Library/MobileSubstrate/DynamicLibraries 這個文件夾並沒有生成,而是把project打包成 .deb 文件然後 copy 到越獄設備的 iOSOpenDevPackages 文件夾下,比方我生成的是?

com.aaron.hooksb_1.0-1_iphoneos-arm.deb 是這個,Profiling 完之後。iphone 會自己主動重新啟動(kill SpringBoard)然後執行hook代碼。


用 scp 命令能夠將設備裏面的文件復制到電腦裏查看:

scp [email protected]:/var/root/iOSOpenDevPackages/com.aaron.hooksb_1.0-1_iphoneos-arm.deb ~/Desktop/



NOTE:在 Profiling 可能遇到的問題:?

1、Permission denied (publickey,password,keyboard-interactive)?

相關的解決方法是:

第一步:拷貝公鑰到 iPhone 的 ~/home/userName 文件夾(沒有該文件夾新建就可以,事實上我認為應該是iPhone的不論什麽文件夾都行的。其目的都是為了第二步)

scp ~/.ssh/id_rsa.pub [email protected]:/home/serverUsername

第二步:把公鑰的key寫到?~/.ssh/authorized_keys (相同沒有該文件夾新建)

cat id_rsa.pub >> ~/.ssh/authorized_keys

然後再次 Profiling 就編譯通過了。

參考鏈接:這裏


2、Permission denied,Failed to create directory /var/root/iOSOpenDevPackages on device 192.168.2.42 Command /bin/sh failed with exit code 255。

問題看起來應該是跟 1 一樣的。網上找到了一個更優雅的解決方案:直接在 MacBook 命令行上執行命令:iosod sshkey -h 192.168.2.42 。然後再次 Profiling 就好了。

參考鏈接:這裏


假如你以上環境安裝出錯的話,我最好建議你Google一下。當然你也能夠選擇每次改動之後都手動copy到[email protected]:/Library/MobileSubstrate/DynamicLibraries/ 文件夾下,明明能夠僅僅是點擊一下button就能夠搞定的事為什麽每次都要手動去copy一下呢,那樣不認為非常蛋疼嗎?


ok,就到這裏了。


-------------------------- 2017.01.16 改動 ----------------------

?今天在用 clutch 砸殼的時候發現出錯了:

com.tencent.xin contains watchOS 2 compatible application. It‘s not possible to dump watchOS 2 apps with Clutch 2.0.3 at this moment.

Zipping WeChat.app

Killed: 9


微信6.5.3的版本號如今用 clutch 是砸不了了,那麽就直接改用 dumpdecrypted 來給砸:

dumpdecryptd 的砸殼步驟基本參照這一篇文章來做的:?iOS逆向project(簡單利用"dumpdecrypted"給ipa砸殼)?,

如今想記錄的是砸完殼後得到的?WeChat.decrypted 用這個命令僅僅能 dump 出一個?CDStructures.h 文件

class-dump -H WeChat.decrypted --arch arm64 -o WeChatHead/

?這說明解密是不完整的。能夠用 otool 命令查看 cryptid 這個字段驗證解密的完整性,1 代表未解密,0 代碼已解密。


?? Desktop otool -l WeChat.decrypted | grep crypt

WeChat.decrypted (architecture armv7):

?? ? cryptoff 16384

? ? cryptsize 48513024

? ? ? cryptid 0

WeChat.decrypted (architecture arm64):

?? ? cryptoff 16384

? ? cryptsize 52133888

? ? ? cryptid 1


能夠看到 crypid 為 1 ,確實是還沒完整解密的。看來用 arm64 架構解密是不行的。換個架構試試:

class-dump --arch?armv7? WeChat.decrypted -H -o?WeChatHead/


armv7 OK,稍等一會兒。已經成功將頭文件 dump 出來了。



拓展鏈接:

1、手把手教你制作一款iOS越獄App,偽裝微信位置

2、一步一步實現iOS微信自己主動搶紅包(非越獄)

3、移動App入侵與逆向破解技術 - iOS篇

class-dump 和 iOSOpenDev 的使用