1、創(chuàng)建一個(gè)動(dòng)態(tài)庫Inject,引入fishhook及sysctl、MyPtraceHeader頭文件
2、創(chuàng)建一個(gè)繼承自NSObject的類 如 XCInject
3、編碼
#import "XCAntDebug.h"
#import "XCPtrance.h"
#import <sys/sysctl.h>
@implementation XCAntDebug
static dispatch_source_t timer;
//檢測是否被調(diào)試
BOOLisDebugger() {
? ? //控制碼
? ? int name[4];//里面放字節(jié)碼,查詢信息
? ? name[0] =CTL_KERN;//內(nèi)核查看
? ? name[1] =KERN_PROC;//查詢進(jìn)程
? ? name[2] = KERN_PROC_PID;//傳遞的參數(shù)是進(jìn)程的ID(PID)
? ? name[3] =getpid();//PID的值
? ? struct kinfo_proc info; //接受進(jìn)程查詢結(jié)果的結(jié)構(gòu)體
? ? size_tinfo_size =sizeof(info);//結(jié)構(gòu)體的大小
? ? interror =sysctl(name,sizeof(name)/sizeof(*name), &info, &info_size,0,0);
? ? assert(error == 0);//0就是沒有錯(cuò)誤,其他就是錯(cuò)誤碼
? ? return((info.kp_proc.p_flag&P_TRACED) !=0);
}
voiddebugCheck(){
? ? timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
? ? dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
? ? dispatch_source_set_event_handler(timer, ^{
? ? ? ? if(isDebugger()) {
? ? ? ? ? ? //? ? ? ? exit(0);
? ? ? ? ? ? NSLog(@"有debugServer");
? ? ? ? }else{
? ? ? ? ? ? NSLog(@"無debugServer");
? ? ? ? }
? ? });
? ? dispatch_resume(timer);
}
+ (void)load {
? ? NSLog(@"XCAntDebug *************");
? ? debugCheck();
? ? ptrace(PT_DENY_ATTACH, 0, 0, 0);
}
@end