初探防护-代码实例

反HOOK基本防护

  • 利用fishhook修改MethodSwizzle相关的函数
  • 防护代码要最先被加载,否则HOOK修改完毕了,防护无效
  • 原始工程所编写的Framwork库会优先于注入库加载。所以适合写防护代码。
#import "ViewController.h"
#import <objc/runtime.h>
#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定义为宏
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容