为了账号安全,请及时绑定邮箱和手机立即绑定

iOS 代码混淆的新进展

标签:
iOS

最近研究了基于LLVM的混淆工具 Hikari 中文文档 ,从编译器层面完成了代码的安全加固,可以说是非常牛了。但作者并没有实现Objective-C的方法名/类名混淆,于是想到了老办法。这个办法最大的难点在于如何提取混淆的关键字,之前都是尝试用脚本去提取关键字,方法名加前缀等等,github有不少工具、Demo,大多都不太好使。后来经大神提点,可以用Objective-C强大的运行时来处理关键字的提取。

周末抽空写了个小工具 XDSecurityDefense,实现了

  1. 过滤掉苹果SDK的类名、方法名

  2. 过滤掉setter、getter方法

  3. 过滤掉协议方法

  4. 过滤掉继承自父类的方法

/**
 获取所有开发者创建的类的名称

 @return 类的名称集合
 */- (NSSet *)customClassNames {    NSMutableSet *customClassName = [NSMutableSet set];    unsigned int classNamesCount = 0;    // 用 executablePath 获取当前 app image
    NSString *appImage = [NSBundle mainBundle].executablePath;    // objc_copyClassNamesForImage 获取到的是 image 下的类,直接排除了系统的类
    const char **classNames = objc_copyClassNamesForImage([appImage UTF8String], &classNamesCount);    if (classNames) {        for (unsigned int i = 0; i < classNamesCount; i++) {            const char *className = classNames[i];            NSString *classNameString = [NSString stringWithUTF8String:className];
            [customClassName addObject:classNameString];
        }
        free(classNames);
    }    return customClassName;
}
/**
 检查方法是否继承自父类

 @param class 类
 @param sel 方法名
 @return 是否继承自父类
 */- (BOOL)superClass:(Class)class respondsToSelector:(SEL)sel
{
    Class supClass = class_getSuperclass(class);    BOOL bTespondsToSelector= NO;    while (supClass != nil) {        if (class_respondsToSelector(supClass,sel)) {
            bTespondsToSelector = YES;
            supClass = nil;
        } else {
            supClass = class_getSuperclass(supClass);
        }
    }    return bTespondsToSelector;
}
/**
 获取类遵循所有协议的协议方法名

 @param classNameString 类的集合
 @return 方法名集合
 */- (NSSet *)protocalMethodListWithClass:(NSString *)classNameString {    NSMutableSet *protocalMethodList = [NSMutableSet set];
    Class className = NSClassFromString(classNameString);    unsigned int methodCount = 0;
    __unsafe_unretained Protocol **protocolList = class_copyProtocolList(className, &methodCount);    for (int i = 0; i < methodCount; i++) {
        Protocol *protocal = protocolList[i];//        const char *pName = protocol_getName(protocal);//        NSLog(@"protocol[%d] ---- %@", i, [NSString stringWithUTF8String:pName]);
        
        unsigned int protocolMethodCount = 0;        struct objc_method_description * methodList = protocol_copyMethodDescriptionList(protocal, NO, YES, &protocolMethodCount);        for (int i = 0; i < protocolMethodCount; i++) {            struct objc_method_description method = methodList[i];            NSString *protocolMethodName = NSStringFromSelector(method.name);
            [protocalMethodList addObject:protocolMethodName];
        }
        free(methodList);
        methodList = protocol_copyMethodDescriptionList(protocal, YES, YES, &protocolMethodCount);        for (int i = 0; i < protocolMethodCount; i++) {            struct objc_method_description method = methodList[i];            NSString *protocolMethodName = NSStringFromSelector(method.name);
            [protocalMethodList addObject:protocolMethodName];
        }
    }
    free(protocolList);    return protocalMethodList;
}

使用方法 不再赘述

踩的一些坑

1.如果有用到 NSClassFromString NSSelectorFromString等方法要检查是否会出错
2.有使用NSCodingarchivedDataWithRootObject等归档操作也要注意运行时是否出错



作者:施孝达
链接:https://www.jianshu.com/p/c0a81c904b4b

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消