以下流程僅供在自有裝置上進行安全研究與教學分析使用,請勿用於破解他人應用。
原本打算用 AppCrackr 砸殼,結果一直失敗。起初以為是 App 的加密太強,後來才知道是 AppCrackr 手法過於粗暴引發爭議,核心功能被迫關閉了。幸好逆向工程社群裡還能找到 dumpdecrypted 這個方案,以下記錄整個砸殼過程。
下載位址:https://github.com/stefanesser/dumpdecrypted/archive/master.zip,在 Mac 中解壓。
此處環境為 iOS 7.1.x。
cd 到 dumpdecrypted 目錄,看看 Makefile 的內容:
CC_BIN=`xcrun --sdk iphoneos --find gcc` GCC_UNIVERSAL=$(GCC_BASE) -arch armv7 -arch armv7s -arch arm64 SDK=`xcrun --sdk iphoneos --show-sdk-path` CFLAGS = GCC_BASE = $(GCC_BIN) -Os $(CFLAGS) -Wimplicit -isysroot $(SDK) -F$(SDK)/System all: dumpdecrypted.dylib dumpdecrypted.dylib: dumpdecrypted.o $(GCC_UNIVERSAL) -dynamiclib -o $@ $^ %.o: %.c $(GCC_UNIVERSAL) -c -o $@ $< clean: rm -f *.o dumpdecrypted.dylib
關鍵在於確認 GCC_UNIVERSAL 與 SDK 這兩個變數的值與 iOS 裝置環境一致。
在 Mac 終端機執行以下指令查看 SDK 版本:
xcrun --sdk iphoneos --show-sdk-path /Applications/Xcode 5.1.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
SDK 是 7.1.x,與裝置一致,GCC_UNIVERSAL 這個變數可以略過。
確認設定一致後,在當前目錄執行 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 directory make: *** [dumpdecrypted.o] Error 127
原因是找不到 /Applications/Xcode 來執行其中的腳本。我的 Mac 上有三個 Xcode(5.0.2、5.1.1、6 Beta4),偏偏沒有 /Applications/Xcode。把 5.1.1 改名即可:
$ sudo mv Xcode\ 5.1.1.app/ Xcode.app/
再 make 還是同樣的錯誤。這類「找不到 Xcode」的問題通常該找 xcode-select 幫忙:
$ xcode-select -p /Applications/Xcode 5.1.1.app/Contents/Developer
原來 xcrun 查找工具時的路徑仍指向 Xcode 5.1.1,自然什麼都找不到。重置它即可(預設是 /Applications/Xcode.app/):
$ sudo xcode-select -r $ xcode-select -p /Applications/Xcode.app/Contents/Developer
再 make,這次通過了:
$ make `xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` ... -c -o dumpdecrypted.o dumpdecrypted.c `xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` ... -dynamiclib -o dumpdecrypted.dylib dumpdecrypted.o $ ls Makefile dumpdecrypted.c dumpdecrypted.o README dumpdecrypted.dylib
目錄中多了兩個檔案,其中 .dylib 就是我們要的動態庫,也就是砸殼用的「錘子」。
查看裝置 IP 後,用 scp 把 dylib 送過去:
$ scp dumpdecrypted.dylib root@192.168.xxx.xxx:/var/tmp root@192.168.xxx.xxx's password: dumpdecrypted.dylib 100% 81KB 81.0KB/s 00:00
選定一個用於教學分析的 App。在裝置上用 iFile 查到它的可執行檔路徑:
/var/mobile/Applications/EBBD26E9-DDBA-481E-9403-84D159436889/HBGC.app/HBGC
SSH 連到裝置,cd 到剛剛放動態庫的路徑:
$ ssh root@192.168.xxx.xxx root# cd /var/tmp/ root# ls dumpdecrypted.dylib* cydia.log launchd/ MediaCache/ ...
執行砸殼:
root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Applications/EBBD26E9-DDBA-481E-9403-84D159436889/HBGC.app/HBGC mach-o decryption dumper DISCLAIMER: 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/.../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# ls HBGC.decrypted cydia.log dumpdecrypted.dylib* launchd/ ...
其中的 HBGC.decrypted 就是目標產物,之後就能帶進 IDA 等工具做靜態分析了。
先看 xcrun 的說明:
$ xcrun -h Usage: xcrun [options] <tool name> ... arguments ... Find and execute the named command line tool from the active developer directory. ... --sdk <sdk name> find the tool for the given SDK name -f, --find only find and print the tool path -r, --run find and execute the tool (the default behavior) --show-sdk-path show selected SDK install path --show-sdk-version show selected SDK version
xcrun 的作用,是從「作用中的開發者目錄」裡查找並執行某個命令列工具。例如 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 的路徑,設為 cmd_tool_path。
(2)xcrun --sdk iphoneos cmd_tool_path —— 透過該路徑取得對應 iphoneos SDK 的具體工具並執行。
(3)GCC_BIN 就是這整個「查找並執行」過程的 shell 命令。
再看:xcrun --sdk iphoneos --show-sdk-path
$ 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
而 xcode-select 則負責列印或變更這個「作用中的開發者目錄」:
$ xcode-select -h Usage: xcode-select [options] -p, --print-path print the path of the active developer directory -s <path>, --switch <path> set the path for the active developer directory -r, --reset reset to the default command line tools path
它控制的是 Xcode 命令列工具(如 xcodebuild)與 BSD 開發指令(如 cc、make)實際採用哪一份工具鏈,而 xcrun 正是從這個目錄中查找工具。它的值通常是:
/Applications/Xcode 5.1.1.app/Contents/Developer
例如在 /Applications/Xcode 5.1.1.app/Contents/Developer/usr/bin 中,就能看到前面需要的 gcc 等工具(部分輸出):
$ ls BuildStrings gcc gcov-4.2 git ibtool ld xcrun xcodebuild ...
釐清這兩者的關係後,前面 make 失敗的原因就很清楚了:Xcode 被改名後,xcrun 查找工具的路徑仍指向舊位置,只要用 xcode-select -r 把開發者目錄重置回預設值,問題自然消失。
以上是我在自己裝置上的完整記錄,實際操作時請依自身環境調整。詳細內容可參考社群中的「用 dumpdecrypted 砸殼」相關討論。