iOS 系統(tǒng)不斷升級,結構不斷調整,所以我們可以利用的動態(tài)庫注入方法也根據(jù)系統(tǒng)版本的不同而不同。
在此之前,我們可以利用環(huán)境變量 DYLD_INSERT_LIBRARY 來添加動態(tài)庫,iOS7 被成功越獄后,我們需要自己去探索實踐 iOS7 動態(tài)庫注入的方式。
本文將在 iOS7.0.4 環(huán)境下,以 hook 支付寶 app 程序中 ALPLauncherController 的視圖加載方法為例,介紹在 iOS7 下,如何實現(xiàn)動態(tài)庫注入攻擊。
先總結羅列一下相關編譯、鏈接工具的位置路徑信息,在各位自行下載的 iOS SDK中
clang : /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
gcc : /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2
ld : /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ld
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
sdk : /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/
我們編寫一個 hook 支付寶 app 程序中 ALPLauncherController 的 viewDidLoad 方法,具體方法是利用 Method Swizzling 。
不熟悉 Method Swizzling 的話,可以參看我之前的這篇文章:Objective-C的hook方案(一): Method Swizzling
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation UIViewController (HookPortal)
-(void)myViewDidLoad
{
NSLog(@"----------------------- myViewDidLoad ----------------------");
}
@end
static void __attribute__((constructor)) initialize(void)
{
NSLog(@"======================= initialize ========================");
Class class = objc_getClass("ALPLauncherController");
Method ori_Method = class_getInstanceMethod(class, @selector(viewDidLoad));
Method my_Method = class_getInstanceMethod(class, @selector(myViewDidLoad));
method_exchangeImplementations(ori_Method, my_Method);
}
我們可以利用 xcode 直接幫忙編譯 .o,或者自己手動使用 clang 編譯,然后手動 ld :
ld -dylib -lsystem -lobjc -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/ -o libwq.dylib xxx.o
將編譯好的 libwq.dylib 拷貝到 iPhone 文件系統(tǒng)中 /Library/MobileSubstrate/DynamicLibraries/ 下
如果不放心庫是否能正常工作,可以加一步驗證操作,寫一個 demo 嘗試打開自己的庫:
voidvoid *handle = (void*)dlopen("/Library/MobileSubstrate/DynamicLibraries/libwq.dylib", 0x2);
handle = dlsym(handle, "myViewDidLoad");
if (handle) {
NSLog(@"++++");
}else{
NSLog(@"----");
}
到了驗證效果的時候,重啟設備后者執(zhí)行:
killall SpringBoard
啟動支付寶 app,然后觀察 log 信息:
Portal[3631] <Notice>: MS:Notice: Injecting: com.alipay.iphoneclient `[Portal] (847.21)`
Portal[3631] <Notice>: MS:Notice: Loading: /Library/MobileSubstrate/DynamicLibraries/libwq.dylib
Portal[3631] <Warning>: ======================= initialize ========================
Portal[3631] <Warning>: ----------------------- myViewDidLoad ----------------------
證明我們的動態(tài)庫已經(jīng)被加載, 我們的 Hook 也成功了。 剩下的就要自己去思考了,除了加句無聊的 Log,我們還可以做點什么呢?