码迷,mamicode.com
首页 > 移动开发 > 详细

iOS安全攻防之结构体保护使用

时间:2017-05-24 14:47:04      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:duti   mount   static   参数表   malloc   destory   iss   ***   tor   

  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 后看到的。

  技术分享

   

iOS安全攻防之结构体保护使用

标签:duti   mount   static   参数表   malloc   destory   iss   ***   tor   

原文地址:http://www.cnblogs.com/ZachRobin/p/6898452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!