《iOS應用逆向工程》學習筆記(六)使用dumpdecrypted砸殼
本來是打算用AppCrackr砸殼的,結果砸殼都是失敗的,開始以為是App的加密太厲害了,後來才知道是因為AppCrackr太暴力了,引起公憤,結果被人投訴招致核心功能被迫關閉了。
幸好在RE官網搜到一個用dumpdecrypted砸殼的文章。下面是我砸殼的經曆。
一、造錘1.下載dumpdecrypted源碼
:https://github.com/stefanesser/dumpdecrypted/archive/master.zip,接著在Mac中解壓。
2.確認iOS裝置的版本
iOS 7.1.x,原帖中snakeninny略囉嗦。。。
3.Makefile
cd到dumpdecrypted目錄,看看Makefile檔案的內容:
CC_BIN=`xcrun --sdk iphoneos --find gcc`GCC_UNIVERSAL=$(GCC_BASE) -arch armv7 -arch armv7s -arch arm64SDK=`xcrun --sdk iphoneos --show-sdk-path`CFLAGS =GCC_BASE = $(GCC_BIN) -Os $(CFLAGS) -Wimplicit -isysroot $(SDK) -F$(SDK)/System$all: dumpdecrypted.dylibdumpdecrypted.dylib: dumpdecrypted.o $(GCC_UNIVERSAL) -dynamiclib -o $@ $^%.o: %.c $(GCC_UNIVERSAL) -c -o $@ $
大多數看不懂。。。接下來我們需要確認的是GCC_UNIVERSAL和SDK這兩個變數的值和iOS裝置的環境保持一致。
4.確保Makefile的配置和真機環境一致在Mac中開啟終端,輸入xcrun --sdk iphoneos --show-sdk-path命令,查看SDK版本:
/Applications/Xcode 5.1.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
Xcode的SDK版本是7.1.x,GCC_UNIVERSAL這個變數值可以略過。
5.建立動態庫檔案(1)一錯在確保Makefile中對動態庫的設定和iOS真機環境一致後,在目前的目錄下輸入:make。
但是失敗了,錯誤資訊如下:
`xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/Frameworks -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/PrivateFrameworks -arch armv7 -arch armv7s -arch arm64 -c -o dumpdecrypted.o dumpdecrypted.c/bin/sh: /Applications/Xcode: No such file or directorymake: *** [dumpdecrypted.o] Error 127
原因是找不到/Applications/Xcode來執行其中的一些指令碼。 好吧,我的Mac中有3個Xcode:/Applications/Xcode 5.0.2, /Applications/Xcode 5.1.1, /Applications/Xcode 6 Beta4,就是沒有/Applications/Xcode。
沒事,將Xcode 5.1.1重新命名為Xcode就行了:
$ sudo mv Xcode\ 5.1.1.app/ Xcode.app/
(2)再錯再make,還是報錯,錯誤資訊和上面一樣。
不怕,我們還有xcode-select這個小夥伴,通常Xcode找不到之類的錯誤都應該找它幫忙:
$ xcode-select -p/Applications/Xcode 5.1.1.app/Contents/Developer
原來xcrun尋找cmd tool時的路徑還是Xcode 5.1.1/,當然什麼都找不到了。這時候將它重設就行了(預設是/Applications/Xcode.app/):$ sudo xcode-select -r$ xcode-select -p /Applications/Xcode.app/Contents/Developer
(3)成功再make,成功,輸出如下:
$ make`xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/Frameworks -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/PrivateFrameworks -arch armv7 -arch armv7s -arch arm64 -c -o dumpdecrypted.o dumpdecrypted.c`xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/Frameworks -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/PrivateFrameworks -arch armv7 -arch armv7s -arch arm64 -dynamiclib -o dumpdecrypted.dylib dumpdecrypted.o$ lsMakefile dumpdecrypted.c dumpdecrypted.oREADME dumpdecrypted.dylib
可以看到目錄中多了兩個檔案,其中dylib尾碼的就是我們要建立的動態庫檔案,也就是用來砸殼的鎚子。
二、砸殼1.將“鎚子”放入裝置中查看iOS裝置的IP地址,然後在Mac上使用scp命令將dumpdecrypted.dylib檔案放到iOS裝置中:
$ scp dumpdecrypted.dylib root@192.168.xxx.xxx:/var/tmproot@192.168.xxx.xxx's password:dumpdecrypted.dylib 100% 81KB 81.0KB/s 00:00
2.砸選定一個讓你覺得非常不爽或非常感興趣的app,我就隨便選了一個HBGC。在iOS裝置上開啟iFile,查到它的可執行檔的路徑為:/var/mobile/Applications/EBBD26E9-DDBA-481E-9403-84D159436889/HBGC.app/HBGC
然後用SSH連到iOS裝置上,cd到剛剛動態庫的路徑:/var/tmp。
$ ssh root@192.168.xxx.xxxroot@192.168.xxx.xxx's password:root# cd /var/tmp/root# lsFlipswitchCache/ com.apple.audio.hogmode.plistL65ancd.sock= com.apple.tccd/L65d.sock= com.apple.timed.plistMediaCache/ cydia.logRestoreFromBackupLock* dumpdecrypted.dylib*SpringBoard_reboot_flag launchd/com.apple.assistant.bundleservicecache.plist mobile_assertion_agent.log
砸殼(久等了):
root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Applications/EBBD26E9-DDBA-481E-9403-84D159436889/HBGC.app/HBGCmach-o decryption dumperDISCLAIMER: This tool is only meant for security research purposes, not for application crackers.[+] detected 32bit ARM binary in memory.[+] offset to cryptid found: @0xd5a90(from 0xd5000) = a90[+] Found encrypted data at address 00004000 of length 3047424 bytes - type 1.[+] Opening /private/var/mobile/Applications/EBBD26E9-DDBA-481E-9403-84D159436889/HBGC.app/HBGC 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 HBGC.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 4a90[+] Closing original file[+] Closing dump file
成果:root# lsFlipswitchCache/ com.apple.audio.hogmode.plistHBGC.decrypted com.apple.tccd/L65ancd.sock= com.apple.timed.plistL65d.sock= cydia.logMediaCache/ dumpdecrypted.dylib*RestoreFromBackupLock* launchd/SpringBoard_reboot_flag mobile_assertion_agent.logcom.apple.assistant.bundleservicecache.plist
其中的HBGC.decrypted就是目標產物,接下來IDA各種斧頭水果刀上吧。
三、附錄1.xcrun首先簡單看看xcrun的協助資訊:
$ xcrun -hUsage: xcrun [options] ... arguments ...Find and execute the named command line tool from the active developerdirectory.The active developer directory can be set using `xcode-select`, or via theDEVELOPER_DIR environment variable. See the xcrun and xcode-select manualpages for more information.Options: -h, --help show this help message and exit --version show the xcrun version -v, --verbose show verbose logging output --sdk find the tool for the given SDK name --toolchain find the tool for the given toolchain -l, --log show commands to be executed (with --run) -f, --find only find and print the tool path -r, --run find and execute the tool (the default behavior) -n, --no-cache do not use the lookup cache -k, --kill-cache invalidate all existing cache entries --show-sdk-path show selected SDK install path --show-sdk-version show selected SDK version --show-sdk-platform-path show selected SDK platform path --show-sdk-platform-version show selected SDK platform version
xcrun的作用在於從一個啟用的開發人員目錄(active developer directory)中尋找一個command line tool,並執行這個工具。 例如上面的Makefile中: GCC_BIN=`xcrun --sdk iphoneos --find gcc`
分解來看:
(1)xcrun --find gcc
$ xcrun --find gcc/Applications/Xcode 5.1.1.app/Contents/Developer/usr/bin/gcc
這一步擷取了gcc這個tool的路徑,設為cmd_tool_path。 (2)xcrun --sdk iphoneos cmd_tool_path
這一步通過路徑名擷取到了具體的工具程式,這個工具對應iphoneos的SDK,並執行該工具。
(3)GCC_BIN是一條shell命令,對應的就是這個尋找和執行工具的過程。
再如: xcrun --sdk iphoneos --show-sdk-path
它的作用就是尋找對應於iphoneos SDK的SDK並執行。
$ xcrun --show-sdk-path/Applications/Xcode 5.1.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk $ xcrun --sdk iphoneos --show-sdk-path/Applications/Xcode 5.1.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
2.xcode-select首先看看簡單的協助資訊:
$ xcode-select -hUsage: xcode-select [options]Print or change the path to the active developer directory. This directorycontrols which tools are used for the Xcode command line tools (for example,xcodebuild) as well as the BSD development commands (such as cc and make).Options: -h, --help print this help message and exit -p, --print-path print the path of the active developer directory -s , --switch set the path for the active developer directory -v, --version print the xcode-select version -r, --reset reset to the default command line tools path
它的作用在於列印或改變active developer directory,而xcrun就是從這個directory中尋找對應的工具。 通常它的值為:/Applications/Xcode 5.1.1.app/Contents/Developer
例如,在/Applications/Xcode 5.1.1.app/Contents/Developer/usr/bin中,可以看到一些上文需要的gcc:$ lsBuildStrings gcc ndisasmCpMac gcov-4.2 opendiffDeRez git projectInfoGetFileInfo git-cvsserver resolveLinksImageUnitAnalyzer git-receive-pack scntoolMergePef git-shell sdefMvMac git-upload-archive sdpResMerger git-upload-pack svnRez gnumake svnadminRezDet hdxml2manxml svndumpfilterRezWack headerdoc2html svnlookSetFile ibtool svnrdumpSplitForks ibtool3 svnserveTextureAtlas ibtoold svnsyncUnRezWack ictool svnversionactool instruments symbolsagvtool iprofiler xcodebuildamlint ld xcrun
以上只是部分輸出。
註:以上是我個人在自己的機子上的砸殼經曆,大家要根據自己的實際情況進行,詳細請參考:用dumpdecrypted砸殼