如何在我的代码中实现反调试?

Car*_*ard 1 xcode objective-c ios

我试图了解如何通过从最简单的方法 PT_DENY_ATTACH 开始来实现反调试,并尝试使用 lldb 对其进行调试。但我不知道我需要在 Objective-C 的哪一部分中实现它。

我为登录页面编写了一个简单的 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
Run Code Online (Sandbox Code Playgroud)

如何实现反调试?

Ild*_*r.Z 6

首先,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]));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以将目标构建配置更改为发布并检查 Xcode 是否断开连接。希望有帮助!