Method Swizzling 原理
在Objective-C中调用一个方法,其实是向一个对象发送消息,即查找消息的唯一依据是selector的名字。每一个SEL都对应着其方法实现真实的运行地址(IMP)。 如下图:
Method Swizzling 可以使得两个SEL所对应的IMP相互交换达到HOOK的目的如下图
接下来,我们通过一个小小的案例,从逆向的角度来了解HOOK的强大
首先我们看看微信的主页
案例的目标:hook登录点击事件,并弹出alert
第一步【移植】
把 中dylib的代码直接拿过来,在上面进行开发
第二步【分析】
利用Debug View 从界面上先分析登录界面代码
从图可以直接看到登录Button的Target与Action,接下来通过LLDB分析查看Target/Action里面到底是啥子东西
(lldb) po 0x1c3a7f800{ className = WCAccountLoginControlLogic memoryAddress = 0x1c0322300;}(lldb) po 0x1c3a7fa80{ className = "__NSCFString"; memoryAddress = 0x1c0e289c0;}(lldb) po 0x1c0e289c0onFirstViewLogin复制代码
我们以前是不是通过以下方法,为Button添加点击事件的? - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
有了这些我们就能很肯定,当点击登录按钮的时候触发的是这个WCAccountLoginControlLogic控制器 下的 onFirstViewLogin方法
第三方【写代码】
直接在我们的dylib中进行开发,我就直接上代码了
#import "PFLibrary.h"#include#import @implementation PFLibrary- (void) loginBtnPressed { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"你想登录??" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]]; [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:alertVC animated:true completion:nil];}+ (void)load { NSLog(@"Dylib注入成功..."); // 原微信的点击触发的方法 Method old = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), @selector(onFirstViewLogin)); // 本地自己的方法 Method new = class_getInstanceMethod(self, @selector(loginBtnPressed)); //交换两个方法的IMP method_exchangeImplementations(old, new); NSLog(@"????????Hook成功????????");}@end复制代码
这段代码就是利用Method Swizzling进行了HOOK。由于比较简单,我就不不细致分析了,我相信大伙通过我的注释都看得懂。
来~看看结果!