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

      

       

  • 相关阅读:
    入门菜鸟
    FZU 1202
    XMU 1246
    Codeforces 294E Shaass the Great 树形dp
    Codeforces 773D Perishable Roads 最短路 (看题解)
    Codeforces 814E An unavoidable detour for home dp
    Codeforces 567E President and Roads 最短路 + tarjan求桥
    Codeforces 567F Mausoleum dp
    Codeforces 908G New Year and Original Order 数位dp
    Codeforces 813D Two Melodies dp
  • 原文地址:https://www.cnblogs.com/ZachRobin/p/6898452.html
Copyright © 2011-2022 走看看