簡體   English   中英

如何在我的代碼中實現反調試?

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

我試圖通過從最簡單的方法PT_DENY_ATTACH開始來了解如何實現反調試,並嘗試使用lldb進行調試。 但是我不知道我需要在哪個部分實現它。

我為登錄頁面編寫了一個簡單的objective-c代碼。

#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

我該如何實現反調試?

首先,ptrace()不是iOS上的公共API的一部分。 根據AppStore發布策略,禁止使用非公共API,使用它們可能會導致AppStore拒絕應用程序,因此我們需要使用dlsym通過函數指針調用它。

完整代碼:

#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]));
    }
}

然后,您可以將目標Build配置更改為Release並檢查Xcode是否斷開連接。 希望它有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM