本文主要是以WeChat为例,讲解如何破坏WeChat注册、以及如何获取登录密码
![5da782f86491113476f404a9fb01b027.png]()
引子
在进行WeChat实践操作时,首先需要了解一个概念:Method Swizzing(即方法交换)
Method Swizzing(即方法交换)是利用OC的Runtime特性,动态改变SEL(方法编号)和IMP(方法实现)的对应关系,达到OC方法调用流程改变的目的,主要用于OC方法。
在OC中,SEL和IMP之间的关系,类似与一本书的目录,是一一对应的
同时,Runtime中也提供了用于交换两个SEL和IMP的方法,method_exchangeIMP,我们可以通过这个函数交换两个SEL和IMP的对应关系
破坏微信注册
准备工作:需要新建一个工程,并重签名,且采用Framework注入的方式
- 重签名参考文章:iOS逆向之应用重签名(下)
- Framework注入方式参考文章:iOS逆向之代码注入(上)
这里破坏的微信的注册,主要是通过runtime方法进行注册方法的hook
1、获取注册的相关信息
1、通过lldb调试获取WeChat的注册
![d060b30c5b49a2a122481fa8376da156.webp]()
2、获取注册的target、action
![1f529c597d6c3f5094a316d541ec79f1.webp]()
2、简单hook演示
- 1、通过
class_getInstanceMethod +method_exchangeImplementations方法,hook注册的点击事件
@implementation inject+ (void)load{// 改变微信的注册
Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), @selector(onFirstViewRegister));
Method newMethod = class_getInstanceMethod(self, @selector(my_method));
method_exchangeImplementations(oldMethod, newMethod);
}
- (void)my_method{ NSLog(@"CJLHook --- 注册不了了!");
}@end
3、点击登录时,获取用户的密码
准备工作
方式1:通过lldb获取密码
llfb获取密码的调试如下
![bef4400cbc05de35bf684369d9db10ed.gif]()
此时问题来了,如果我们想通过hook登录方法,在点击登录按钮时,如何获取密码呢?有以下几种方式
hook登录按钮 - 动态调试获取
- 1、- 在CJLHook的inject类中hook登录按钮的方法
@implementation inject+ (void)load{// 改变微信的注册
Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
Method newMethod = class_getInstanceMethod(self, @selector(my_onNext));
method_exchangeImplementations(oldMethod, newMethod);
}
- (void)my_onNext{
}@end
2、通过class_dump工具会dump出OC中类列表(包括成员变量)、方法列表,具体操作如下:
3、通过sublime Text打开headers文件夹,在其中查找WCAccountMainLoginViewController类,找到密码的成员变量_textFieldUserPwdItem
![349130ab7e5fd7ea243d2f9d125aad2a.webp]()
4、通过获取的成员变量,利用lldb动态调试获取密码
![483f0c5ea7b4bc78b6b1b16a0152a1a9.webp]()
po [(WCAccountMainLoginViewController *)0x10a84e800 valueForKey:@"_textFieldUserPwdItem"]
po [(WCAccountTextFieldItem *)0x281768360 valueForKey:@"m_textField"]
po ((WCUITextField *)0x10a1566e0).text
hook登录按钮 - hook代码注入方式获取
@implementation inject+ (void)load{// 改变微信的注册
Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
Method newMethod = class_getInstanceMethod(self, @selector(my_onNext));
method_exchangeImplementations(oldMethod, newMethod);
}
- (void)my_onNext{ UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"]; NSLog(@"密码是:%@", pwd.text);
}@end
运行结果如下所示
![020c4e94610c6e463a318edb930f4fbe.webp]()
- 2、然后此时需要在my_onNext中调用原来的方法,走回原来的登录流程,此时的
my_onNext方法修改如下
- (void)my_onNext{ UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"]; NSLog(@"密码是:%@", pwd.text); //调回原来的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
[self my_onNext];
}
解决崩溃的方案:添加一个method
- 修改inject中的代码,此时是通过
class_addMethod在WCAccountMainLoginViewController中新增一个方法,然后在和原来的onNext交换新增后的方法
@implementation inject+ (void)load{ //原始的method
Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext)); //给WCAccountMainLoginViewController添加新方法
class_addMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(new_onNext), new_onNext, "v@:"); //获取添加后的方法
Method newMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(new_onNext)); //交换
method_exchangeImplementations(oldMethod, newMethod);
}//新的IMPvoid new_onNext(id self, SEL _cmd){ UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"]; NSLog(@"密码是:%@", pwd.text); //调回原来的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
[self performSelector:@selector(new_onNext)];
}@end
重新运行后查看可以走到原来的登录流程,且同时可以获取用户密码
代码注入优化:通过替换的方式
但是上面的代码看上不并不简洁,所以我们来对其一些优化,采用class_replaceMethod函数进行替换原来的onNext方法,修改后的代码如下
+ (void)load{ //原始的method
Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext)); //替换
old_onNext = class_replaceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext), new_onNext, "v@:");
}//原来的IMPIMP (*old_onNext)(id self, SEL _cmd);//新的IMPvoid new_onNext(id self, SEL _cmd){ UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"]; NSLog(@"密码是:%@", pwd.text); //调回原来的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
old_onNext(self, _cmd);
}
更好的方式
- 通过
method_getImplementation、method_setImplementation方法进行覆盖原来onNext方法的IMP
+ (void)load{ //原始的method
old_onNext = method_getImplementation(class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext))); //通过set覆盖原始的IMP
method_setImplementation(class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext)), new_onNext);
}//原来的IMPIMP (*old_onNext)(id self, SEL _cmd);//新的IMPvoid new_onNext(id self, SEL _cmd){ UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"]; NSLog(@"密码是:%@", pwd.text); //调回原来的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
old_onNext(self, _cmd);
}
作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS开发交流群:130 595 548,不管你是小白还是大牛都欢迎入驻 ,让我们一起进步,共同发展!(群内会免费提供一些群主收藏的免费学习书籍资料以及整理好的几百道面试题和答案文档!)
总结