初探防护-代码实例

反HOOK基本防护

  • 利用fishhook修改MethodSwizzle相关的函数
  • 防护代码要最先被加载,否则HOOK修改完毕了,防护无效
  • 原始工程所编写的Framwork库会优先于注入库加载。所以适合写防护代码。
#import "ViewController.h"
#import 
#import "fishhook.h"


@interface ViewController ()

@end

@implementation ViewController


+(void)load {
    //防护代码
    struct rebinding bd;
    bd.name = "method_exchangeImplementations";
    bd.replacement = myExchange;
    bd.replaced = (void *)&exchangeP;
    struct rebinding rebs[1] = {bd};
    rebind_symbols(rebs, 1);
    

    //进攻的代码!
    // getIMP  setIMP
    Method old = class_getInstanceMethod(self, @selector(btnClick1:));
    Method newMethod = class_getInstanceMethod(self, @selector(click1Hook:));
    method_exchangeImplementations(old, newMethod);
}

//函数指针变量
void (*exchangeP)(Method _Nonnull m1, Method _Nonnull m2);
void myExchange(Method _Nonnull m1, Method _Nonnull m2) {
    NSLog(@"检测到HOOK!!!");
}


- (void)click1Hook:(id)sender{
    NSLog(@"HOOK成功!!");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}



- (IBAction)btnClick1:(id)sender {
    NSLog(@"按钮1调用了!");
}

- (IBAction)btnClick2:(id)sender {
    NSLog(@"按钮2调用了!");
}


@end
  • 如果自己也想使用 method_exchangeImplementations方法 ,那就把method_exchangeImplementations定义为宏

你可能感兴趣的:(初探防护-代码实例)