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

      

       

  • 相关阅读:
    Winform学习笔记
    ASP.NET后台注册javascript脚本方法
    使用MultipleActiveResultSets复用Sql Server 2008数据库连接
    angular 2 新建组件命令
    一个关于日志操作方法
    vs2017 打开包管理器 (程序包管理控制台)
    Asp.Net Core Identity 怎么获取当前登录的用户信息?
    abp 实现所有审计的接口
    IIS8.5 布署 Asp.Net Core 老是报500的错误怎么办?
    .NET Core 1.1布署后出现“HTTP Error 502.5 Process Failure”的解决办法
  • 原文地址:https://www.cnblogs.com/ZachRobin/p/6898452.html
Copyright © 2011-2022 走看看