在Objective-C中调用一个方法,实际上是向一个对象发送消息,即查找消息的惟一依据是selector的名字。每个SEL都对应着其方法实现真实的运行地址(IMP)。 以下图:bash
Method Swizzling 可使得两个SEL所对应的IMP相互交换达到HOOK的目的以下图微信
首先咱们看看微信的主页spa
案例的目标:hook登陆点击事件,并弹出alert ![]()
把 iOS逆向之旅(进阶篇) — 代码注入 中dylib的代码直接拿过来,在上面进行开发3d
利用Debug View 从界面上先分析登陆界面代码code
从图能够直接看到登陆Button的Target与Action,接下来经过LLDB分析查看Target/Action里面究竟是啥子东西cdn
(lldb) po 0x1c3a7f800
{
className = WCAccountLoginControlLogic
memoryAddress = 0x1c0322300;
}
(lldb) po 0x1c3a7fa80
{
className = "__NSCFString";
memoryAddress = 0x1c0e289c0;
}
(lldb) po 0x1c0e289c0
onFirstViewLogin
复制代码
咱们之前是否是经过如下方法,为Button添加点击事件的? - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
有了这些咱们就能很确定,当点击登陆按钮的时候触发的是这个WCAccountLoginControlLogic控制器 下的 onFirstViewLogin方法对象
直接在咱们的dylib中进行开发,我就直接上代码了blog
#import "PFLibrary.h"
#include <objc/runtime.h>
#import <UIKit/UIKit.h>
@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。因为比较简单,我就不不细致分析了,我相信大伙经过个人注释都看得懂。事件
来~看看结果! ip
![]()