IOS security defense (24): Protection against sensitive logic (1)
Objective-C code is easy to hook and exposed information is too naked. For security purposes, use C to write it!
Of course, not all code must be written in C. I mean sensitive business logic code.
This article introduces a simple method to rewrite Objective-C logic code to C code at a low learning cost.
Maybe, there is a class like this in the program:
@interface XXUtil : NSObject+ (BOOL)isVerified;+ (BOOL)isNeedSomething;+ (void)resetPassword:(NSString *)password;@end
After being dumped by class-dump, cylinder is easy to implement attacks and hook, posing a great security risk.
I want to change it, but I don't want to change the program structure. Is it swollen?
Hide the function name in the struct and store it as a function pointer member.
The advantage of doing so is that after compilation, only the address is left, the name and parameter table are removed, and the reverse cost and attack threshold are increased.
The rewritten program is as follows:
//XXUtil.h#import <Foundation/Foundation.h>typedef struct _util { BOOL (*isVerified)(void); BOOL (*isNeedSomething)(void); void (*resetPassword)(NSString *password);}XXUtil_t ;#define XXUtil ([_XXUtil sharedUtil])@interface _XXUtil : NSObject+ (XXUtil_t *)sharedUtil;@end
//XXUtil.m#import "XXUtil.h"static BOOL _isVerified(void){ //bala bala ... return YES;}static BOOL _isNeedSomething(void){ //bala bala ... return YES;}static void _resetPassword(NSString *password){ //bala bala ...}static XXUtil_t * util = NULL;@implementation _XXUtil+(XXUtil_t *)sharedUtil{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ util = malloc(sizeof(XXUtil_t)); util->isVerified = _isVerified; util->isNeedSomething = _isNeedSomething; util->resetPassword = _resetPassword; }); return util;}+ (void)destroy{ util ? free(util): 0; util = NULL;}@end
Finally, according to Xcode's Error Reporting Guidelines
[XXUtil isVerified];
Changed:
XXUtil->isVerified();
You can.
Yes, no worries.