其他分享
首页 > 其他分享> > iOS逆向之代码注入!(下)

iOS逆向之代码注入!(下)

作者:互联网

本文主要是以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注入的方式

这里破坏的微信的注册,主要是通过runtime方法进行注册方法的hook

1、获取注册的相关信息

2、简单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获取密码

此时问题来了,如果我们想通过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{

}@end

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

- (void)my_onNext{    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];    NSLog(@"密码是:%@", pwd.text);    //调回原来的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
    [self my_onNext];
}

解决崩溃的方案:添加一个method

@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);
}

更好的方式

+ (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,不管你是小白还是大牛都欢迎入驻 ,让我们一起进步,共同发展!(群内会免费提供一些群主收藏的免费学习书籍资料以及整理好的几百道面试题和答案文档!)

总结


标签:逆向,WCAccountMainLoginViewController,self,iOS,class,method,onNext,方法,代码
来源: https://blog.51cto.com/u_15146321/2800953