简体   繁体   中英

How do I implement an anti-debugging into my code?

I'm trying to understand how to implement an anti-debugging by starting with the simplest method, PT_DENY_ATTACH, and try to debug it with lldb. But I have no idea in which part of my objective-c that I need to implement it in.

I wrote a simple objective-c code for a login page.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *password;

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UILabel *notificationLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.username = @"Sally";
    self.password = @"password123";

    self.passwordTextField.secureTextEntry = YES;

}
- (IBAction)loginWasPressed:(id)sender {

    BOOL isUsersEqual = [self.username isEqualToString:[self.usernameTextField text]];
    BOOL isPasswordEqual = [self.password isEqualToString:[self.passwordTextField text]];

    if (isUsersEqual && isPasswordEqual) {

        NSLog(@"SUCCESS!");
        [self.notificationLabel setText:@"Logged In!"];

    }
    else {

        NSLog(@"FAILURE!");
        [self.notificationLabel setText:@"Incorrect!"];

    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}

@end

How do I implement the anti-debugging?

First of all ptrace() is not part of public API on iOS. As per the AppStore publishing policy, use of non-public API is prohibited and use of them may lead to rejection of the app from the AppStore, so we need call it via function pointer using dlsym.

Full Code:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <dlfcn.h>
#import <sys/types.h>

typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
void anti_debug() {
    ptrace_ptr_t ptrace_ptr = (ptrace_ptr_t)dlsym(RTLD_SELF, "ptrace");
    ptrace_ptr(31, 0, 0, 0); // PTRACE_DENY_ATTACH = 31
}

int main(int argc, char * argv[]) {

    #ifndef DEBUG
    anti_debug();
    #endif
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Then you can change your target Build configuration to Release and check if is Xcode disconnect. Hope it's help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM