美文网首页iOS 高级hookiOS OC
iOS 不常用技术解密之 hook

iOS 不常用技术解密之 hook

作者: wnido | 来源:发表于2017-08-12 10:08 被阅读177次

    在开发中,hook 技术虽然不常用,但在一些特定情况下有非常大的作用,另外了解下 hook 的原理,也可防止被 hook 改写,本篇就讲述下 hook 的基本用法。

    原理

    hook 其实就是处理消息的程序段,每当消息发出,在没有到达特定的程序或者窗口前,hook 程序会先将此消息截获,也就是先得到控制权,然后 hook 就可以对此段消息做出处理,查看或修改其内容,甚至更改其传递路线。

    常用 hook

    • fishhook
      fishhook 的原理是替换 mach-o 里的符号表,代码在执行到一个函数符号时,不知道这个符号对应的函数内存地址,需要函数表里查找这个地址。如果更改这个地址为另外一个函数的地址,那么就实现了传递路线的改变。
      fackbook 大牛实现,代码完全可以拜读,具体实现就不介绍了。开源地址:https://github.com/hewigovens/fishhook
    • method swizzle
      method swizzle 的原理主要是 Objetive-C 的 runtime 机制,可以在 ObjC 方法时动态采用 class_replaceMethod 等 runtime函数截获,变改变调用函数。因为是基于 runtime ,所以只能对 OC 进行 hook 。

    method swizzle

    正常开发中,使用的场景也不是很多,但在日志统计方面,可以避免使用继承的方式,使用少量代码实现

    下面举个例子

    对 viewController 页面行为进行统计

    核心代码

    void __gbh_tracer_swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector){
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
    

    hook viewDidAppear

    __gbh_tracer_swizzleMethod([self class], @selector(viewDidAppear:), @selector(__gbh_tracer_viewDidAppear:));
    
    - (void)__gbh_tracer_viewDidAppear:(BOOL)animated{
        [self __gbh_tracer_viewDidAppear:animated];  //由于方法已经被交换,这里调用的实际上是viewDidAppear:方法
        
        //设置不允许发送数据的Controller
        NSArray *filter = @[@"UINavigationController",@"UITabBarController"];
        NSString *className = NSStringFromClass(self.class);
        if ([filter containsObject:className]) return ; //如果该Controller在不允许发送log的列表里,则不能继续往下走
        
        if ([self.title isKindOfClass:[NSString class]] && self.title.length > 0){ //有标题的才符合我的要求
            // 这里发送log
        }
        NSLog(@"%@:the view has been fully transitioned onto the screen",[self class]);
    }
    

    同等方式可以对其他方法进行 hook ,这样就能通过注入的方式,实现页面的统计了

    详细代码:https://github.com/Supecomer/CIhookTest

    结束语

    正常开发中,如果项目架构完整,这样的方式是不需要的,相对来说,这样的方式达到的效果并不完美。技术没有好坏,只看怎么用;能力没有强弱,只看你熟不熟。

    相关文章

      网友评论

        本文标题:iOS 不常用技术解密之 hook

        本文链接:https://www.haomeiwen.com/subject/hcmdrxtx.html