Last active
August 29, 2015 14:11
-
-
Save jcayzac/70d6e59dcabd39d9c4e2 to your computer and use it in GitHub Desktop.
Detect if iOS app is being debugged/instrumented
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// attempt 1 | |
static BOOL isBeingDebugged() | |
{ | |
static BOOL cachedValue = NO; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
char buffer[2*MAXCOMLEN + 1]; | |
int len = proc_name(getppid(), buffer, 2*MAXCOMLEN); | |
buffer[len] = 0; | |
cachedValue = !strncmp(buffer, "debugserver", len); | |
}); | |
return cachedValue; | |
} | |
// attempt 2 | |
#import <unistd.h> | |
#include <sys/sysctl.h> | |
extern int proc_name(int pid, void * buffer, uint32_t buffersize); | |
static BOOL isBeingDebugged() | |
{ | |
static BOOL cachedValue = NO; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
struct kinfo_proc info = { | |
.kp_proc.p_flag = 0 | |
}; | |
size_t size = sizeof(info); | |
if (!sysctl((int[]){CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}, 4, &info, &size, 0, 0)) | |
{ | |
cachedValue = !!(info.kp_proc.p_flag & P_TRACED); | |
} | |
}); | |
return cachedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment