zoukankan      html  css  js  c++  java
  • iOS安全攻防之结构体保护使用

      Objective-C 代码很容易被 hook,因此需要对一些重要的业务逻辑进行保护,可以改用结构体的形式,把函数名隐藏在结构体里,以函数指针成员的形式存储。这样编译后只留了下地址,去掉了名字和参数表,提高了逆向成本和攻击门槛。

      例如,把以下代码进行保护:

    + (BOOL)isPermission:(int)level;
    + (CGFloat)totalAmont;
    + (void)somePraviteMethod:(NSString *)paraStr1 numberValue:(double)numberValue;

      改为.h:

      

    #import <Foundation/Foundation.h>
    
    #import <UIKit/UIKit.h>
    
    typedef struct protectUtil {
        BOOL (*isPermission)(int level);
        CGFloat (*totalAmont)(void);
        void (*somePraviteMethod)(NSString *paraStr1, double numberValue);
    }StructProtectUtil_t;
    
    @interface StructProtectUtil : NSObject
    
    + (StructProtectUtil_t *)sharedUtil;
    
    @end

      .m 文件:

      

    #import "StructProtectUtil.h"
    
    static BOOL _isPermission (int level) {
        NSLog(@"****** level = %d", level);
        if (level > 12) {
            return YES;
        }
        return NO;
    }
    
    static CGFloat _totalAmont() {
        NSLog(@"==== totalAmount");
        return 1900;
    }
    
    static void _somePraviteMethod (NSString *paraStr1, double numberValue) {
        NSLog(@"paraStr1 = %@, numberValue = %f", paraStr1, numberValue);
    }
    
    static StructProtectUtil_t *protectUtil = NULL;
    
    @implementation StructProtectUtil
    
    + (StructProtectUtil_t *)sharedUtil
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            protectUtil = malloc(sizeof(StructProtectUtil_t));
            protectUtil->isPermission = _isPermission;
            protectUtil->totalAmont = _totalAmont;
            protectUtil->somePraviteMethod = _somePraviteMethod;
        });
        return protectUtil;
    }
    
    + (void)destory
    {
        protectUtil ? free(protectUtil) : 0;
        protectUtil = NULL;
    }
    
    @end

      调用时:  

    [StructProtectUtil sharedUtil] -> isPermission(1000);
    [StructProtectUtil sharedUtil] -> totalAmont();
    [StructProtectUtil sharedUtil] -> somePraviteMethod(@"ParaStr", 3820);

      

      然后对工程进行 class-dump:

      class-dump -H /Users/zhangtibin/Library/Developer/Xcode/DerivedData/TestSecurityAdvance-gflhcslxswowdrfflsfchjmlzfdt/Build/Products/Debug-iphoneos/TestSecurityAdvance.app/TestSecurityAdvance -o /Users/zhangtibin/class-dump/Struct

      查看反编译后的文件,结果如下:

      

      这样就实现了敏感逻辑的保护。

      以下对没有保护的文件进行 Class-dump 后看到的。

      

       

  • 相关阅读:
    [C++] 用Xcode来写C++程序[5] 函数的重载与模板
    【转】字符编码详解——彻底理解掌握编码知识,“乱码”不复存在
    【转】无法将notepad++添加到打开方式列表中的解决办法
    【转】关于启用 HTTPS 的一些经验分享
    【转】GPU 与CPU的作用协调,工作流程、GPU整合到CPU得好处
    【转】excel 末尾是0 恢复数据方法
    【转】怎么让VS2015编写的程序在XP中顺利运行
    【转】深入 Docker:容器和镜像
    【转】SSL/TLS协议运行机制的概述
    【转】C++怎么读写windows剪贴板的内容?比如说自动把一个字符串复制.
  • 原文地址:https://www.cnblogs.com/ZachRobin/p/6898452.html
Copyright © 2011-2022 走看看