美文网首页
iOS工程加固

iOS工程加固

作者: iOS鑫 | 来源:发表于2021-03-05 15:36 被阅读0次

    前言

    企业级App在交付给客户时(尤其是国企)通常会进行一项步骤:[等保测评]。
    那么除了一些第三方的付费加固方案,我们开发者自己还能做哪些操作呢 ?
    接下来,我将摘取我们iOS应用安全风险评估报告中的几个高风险进行操作。

    程序被恶意调试风险

    风险描述

    攻击者可以利用GDB、IDA、Ptrace等调试器跟踪运行的目标程序,查看、修改内存中的代码和数据,甚至分析/篡改程序的业务逻辑,对客户关键数据或者服务器进行恶意攻击,例如修改客户端业务操作的数据,比如转账账号、金额等,导致用户的损失。

    修复建议

    【开发者修复】集成Native层的反调试保护功能,避免应用被Xcode、IDA等工具调试,进而保护业务安全。

    修复操作

    [反调试]

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import <dlfcn.h>
    #import <sys/types.h>
    
    typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr ,int _data);
    #if !defined(PT_DENT_ATTACH)
    #define PT_DENT_ATTACH 31
    #endif
    
    void disable_gdb() {
        void * handle = dlopen(0, RTLD_GLOBAL|RTLD_NOW);
        ptrace_ptr_t ptrace_ptr = dlsym(handle, "ptrace");
        ptrace_ptr(PT_DENT_ATTACH, 0, 0, 0);
        dlclose(handle);
    }
    int main(int argc, char * argv[]) {
        disable_gdb();
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }
    
    

    程序代码被恶意注入风险

    风险描述

    应用程序运行时会在内存中产生一些敏感数据,比如密钥Key、本地解密数据、通信解密数据,攻击者可以利用frida等工具,对程序关键函数注入代码,通过破坏业务逻辑可以获取明文数据或直接对服务器发起攻击。

    修复建议

    【开发者修复】集成防注入/防Hook保护功能,避免应用被注入/HOOK。

    修复操作

    [fishhook]

    #import "AppDelegate.h"
    #import "fishhook.h"
    #import <objc/runtime.h>
    
    @implementation AppDelegate
    #pragma mark ---- 防护代码------
    //函数指针变量
    void(*exchangeP)(Method _Nonnull m1, Method _Nonnull m2);
    
    //static NSMutableArray *methods;
    void myExchange(Method _Nonnull m1, Method _Nonnull m2)
    {
    //    if (!methods) {
    //        methods = [NSMutableArray array];
    //    }
    
    //    SEL oriMethodName = method_getName(m1);
        SEL oriMethodName2 = method_getName(m2);
    //    IMP myMethodImp = method_getImplementation(m1);
    //    IMP myMethodImp2 =method_getImplementation(m2);
    
    // 先从项目中找到目前全部的   method_exchangeImplementations 方法 
    // 项目中没有的就是不安全的,直接exit。
    
        NSString *newMethod = NSStringFromSelector(oriMethodName2);
    //    [methods addObject:newMethod];
    //    HTLog(@"%@",methods);
    
        NSArray *wzArr = @[
            @"af_resume", @"af_suspend",@"sd_setText:",@"sd_layoutSubviews",
            @"sd_button_layoutSubviews",@"sd_reloadData",@"sd_reloadRowsAtIndexPaths:withRowAnimation:",
            @"sd_deleteRowsAtIndexPaths:withRowAnimation:",
                               @"mj_reloadData",
                               @"mj_reloadData",
                               @"fd_reloadData",
                               @"fd_insertSections:withRowAnimation:",
                               @"fd_deleteSections:withRowAnimation:",
                               @"fd_reloadSections:withRowAnimation:",
                               @"fd_moveSection:toSection:",
                               @"fd_insertRowsAtIndexPaths:withRowAnimation:",
                               @"fd_deleteRowsAtIndexPaths:withRowAnimation:",
                               @"fd_reloadRowsAtIndexPaths:withRowAnimation:",
                               @"fd_moveRowAtIndexPath:toIndexPath:"];
    
        if (![wzArr containsObject:newMethod]) {
            HTLog(@"恶意代码HOOK:%@",newMethod);
            exit(0);
        }
    }
    
    +(void)load{
        /**
         防护代码:
         这里使用fishHOOK 对method_exchangeImplementations进行HOOK替换即可
         */
        struct rebinding bd;
        bd.name = "method_exchangeImplementations";
        bd.replacement = myExchange;
        bd.replaced = (void *)&exchangeP;
        struct rebinding rebs[1] = {bd};
        rebind_symbols(rebs, 1);
    }
    
    

    这里有一个iOS开发交流群:130595548!如果你也是一个有梦想的iOS开发者,欢迎你的加入!

    相关文章

      网友评论

          本文标题:iOS工程加固

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