forked from jmpews/HookZzModules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntiDebug.m
182 lines (153 loc) · 4.52 KB
/
AntiDebug.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "AntiDebug.h"
#import <UIKit/UIKit.h>
#import <dlfcn.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <unistd.h>
#if !defined(PT_DENY_ATTACH)
#define PT_DENY_ATTACH 31
#endif
#if !defined(SYS_ptrace)
#define SYS_ptrace 26
#endif
#if !defined(SYS_syscall)
#define SYS_syscall 0
#endif
static __attribute__((always_inline)) void asm_exit() {
#ifdef __arm64__
__asm__("mov X0, #0\n"
"mov w16, #1\n"
"svc #0x80\n"
"mov x1, #0\n"
"mov sp, x1\n"
"mov x29, x1\n"
"mov x30, x1\n"
"ret");
#endif
}
static __attribute__((always_inline)) void check_svc_integrity() {
int pid;
static jmp_buf protectionJMP;
#ifdef __arm64__
__asm__("mov x0, #0\n"
"mov w16, #20\n"
"svc #0x80\n"
"cmp x0, #0\n"
"b.ne #24\n"
"mov x1, #0\n"
"mov sp, x1\n"
"mov x29, x1\n"
"mov x30, x1\n"
"ret\n"
"mov %[result], x0\n"
: [result] "=r" (pid)
:
:
);
if(pid == 0) {
longjmp(protectionJMP, 1);
}
#endif
}
// ------------------------------------------------------------------
typedef int (*PTRACE_T)(int request, pid_t pid, caddr_t addr, int data);
static void AntiDebug_001() {
void *handle = dlopen(NULL, RTLD_GLOBAL | RTLD_NOW);
PTRACE_T ptrace_ptr = dlsym(handle, "ptrace");
ptrace_ptr(PT_DENY_ATTACH, 0, 0, 0);
}
// ------------------------------------------------------------------
// runtime to get symbol address, but must link with `
// -Wl,-undefined,dynamic_lookup` or you can use `dlopen` and `dlsym`
extern int ptrace(int request, pid_t pid, caddr_t addr, int data);
static void AntiDebug_002() { ptrace(PT_DENY_ATTACH, 0, 0, 0); }
// ------------------------------------------------------------------
static __attribute__((always_inline)) void AntiDebug_003() {
#ifdef __arm64__
__asm__("mov X0, #31\n"
"mov X1, #0\n"
"mov X2, #0\n"
"mov X3, #0\n"
"mov w16, #26\n"
"svc #0x80");
#endif
}
// ------------------------------------------------------------------
static __attribute__((always_inline)) void AntiDebug_004() {
#ifdef __arm64__
__asm__("mov X0, #26\n"
"mov X1, #31\n"
"mov X2, #0\n"
"mov X3, #0\n"
"mov X4, #0\n"
"mov w16, #0\n"
"svc #0x80");
#endif
}
// ------------------------------------------------------------------
void AntiDebug_005() { syscall(SYS_ptrace, PT_DENY_ATTACH, 0, 0, 0); }
// ------------------------------------------------------------------
static int DetectDebug_sysctl() __attribute__((always_inline));
int DetectDebug_sysctl() {
size_t size = sizeof(struct kinfo_proc);
struct kinfo_proc info;
int ret, name[4];
memset(&info, 0, sizeof(struct kinfo_proc));
name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PID;
name[3] = getpid();
#if 0
if ((ret = (sysctl(name, 4, &info, &size, NULL, 0)))) {
return ret; // sysctl() failed for some reason
}
#else
// also can change as `AntiDebug_003` and `AntiDebug_004`
// https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
__asm__ volatile("mov x0, %[name_ptr]\n"
"mov x1, #4\n"
"mov x2, %[info_ptr]\n"
"mov x3, %[size_ptr]\n"
"mov x4, #0\n"
"mov x5, #0\n"
"mov w16, #202\n"
"svc #0x80"
:
: [name_ptr] "r"(name), [info_ptr] "r"(&info),
[size_ptr] "r"(&size));
#endif
return (info.kp_proc.p_flag & P_TRACED) ? 1 : 0;
}
void AntiDebug_006() {
if (DetectDebug_sysctl()) {
asm_exit();
}
}
// ------------------------------------------------------------------
#include <unistd.h>
void AntiDebug_007() {
if (isatty(1)) {
asm_exit();
} else {
}
}
// ------------------------------------------------------------------
#include <sys/ioctl.h>
void AntiDebug_008() {
if (!ioctl(1, TIOCGWINSZ)) {
asm_exit();
} else {
}
}
// ------------------------------------------------------------------
void AntiCracker() {
check_svc_integrity();
AntiDebug_001();
AntiDebug_002();
AntiDebug_003();
AntiDebug_003();
AntiDebug_005();
AntiDebug_006();
AntiDebug_007();
AntiDebug_008();
}