-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJC-AntiPtrace-v1-arm64.c
314 lines (300 loc) · 9.4 KB
/
JC-AntiPtrace-v1-arm64.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/ptrace.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
// Author: JC0o0l,Jerrybird
// WeChat: JC_SecNotes
// version: 1.0
//
#if defined(__aarch64__)
#define pt_regs user_pt_regs
#define uregs regs
#define ARM_r0 regs[0]
#define ARM_r7 regs[7]
#define ARM_1r regs[30]
#define ARM_sp sp
#define ARM_pc pc
#define ARM_cpsr pstate
#define NT_PRSTATUS 1
#define NT_foo 1
#endif
void enterSysCall(pid_t pid);
int leaveSysCall(pid_t pid);
long getSysCallNo(int pid,struct pt_regs* regs);
int getCmdline(pid_t pid,char* des);
extern int errno;
int flag = 0;
void show_helper(){
printf(
"\n\nJC-AntiPtrace-v1.o [-v] -p <zygote_pid> -t <appname> [-n <syscallNo>] [-r<returnValue> [-e]]\n"
"options:\n"
"\t-v : verbose\n"
"\t-p <zygote_pid> : pid of zygote or zygote64\n"
"\t-t <appname> : application name of to hook\n"
"\t-n <syscallno> : syscalll number to hook(十进制)\n"
"\t\t 117:ptrace\n"
"\t\t 220:clone\n"
"\t\t 260:wait\n"
"\t-r<returnValue> : update return value of the syscallno\n"
"\t-h : show helper\n"
"\t-e : detach when updated return value\n"
);
}
void show_banner(){
printf(
"888888888888 88 ad88888ba \n"
" ,88 ,d88 d8\" \"8b \n"
" ,88\" 888888 Y8, \n"
" ,88\" 88 `Y8aaaaa, ,adPPYba, ,adPPYba, \n"
" ,88\" 88 aaaaaaaa `\"\"\"\"\"8b, a8P_____88 a8\" \"\" \n"
" ,88\" 88 \"\"\"\"\"\"\"\" `8b 8PP\"\"\"\"\"\"\" 8b \n"
"88\" 88 Y8a a8P \"8b, ,aa \"8a, ,aa \n"
"888888888888 88 \"Y88888P\" `\"Ybbd8\"' `\"Ybbd8\"' \n"
"\n\nTool: JC-AntiPtrace-v1.0\nAuthor: JC0o0l\nVersion: 1.0\nWeChat: JC_SecNotes\n\n"
);
}
int syscall_tohook = 117;
int return_tohook = -1;
int debug = -1;
int pid = -1;
char* appname;
int exit_tohook = -1;
int main(int argc,char* argv[]){
// 命令行解析函数
//
// -d -p <pid> -t <appname>
int opt;
char* optString = "vp:t:hn:r::e";
if (argc < 3){
show_banner();
show_helper();
return 0;
}
show_banner();
while((opt = getopt(argc,argv,optString))!= -1){
if(opt == 'v'){
debug = 11;
}else if(opt == 'p'){
pid = atoi(optarg);
}else if(opt == 't'){
appname = optarg;
}else if(opt == 'h'){
show_helper();
return 0;
}else if(opt == 'n'){
syscall_tohook = atoi(optarg);
}else if(opt == 'r'){
return_tohook = atoi(optarg);
}else if(opt == 'e'){
exit_tohook = 1;
}
}
if(pid == -1){
show_helper();
return 0;
}
//int debug = atoi(argv[1]);
//int pid = atoi(argv[2]);
//char* appname = argv[3];
printf("pid: %d\n",pid);
printf("appname: %s\n",appname);
// 附加到zygote进程
int res = ptrace(PTRACE_ATTACH,pid,0,0);
if(res == -1){
printf("res: %d\n",res);
printf("hook zygote error\n");
show_helper();
return -1;
}
// 等待附加完成
waitpid(pid, NULL, 0);
// 拦截 zygote 进程的 fork
res = ptrace(PTRACE_SETOPTIONS, pid, (void *)0, (void *)(PTRACE_O_TRACEFORK));
printf("res: %d\n",res);
if (res == -1) {
printf("FATAL ERROR: ptrace(PTRACE_SETOPTIONS, ...)\n");
printf("errno: %d\n",errno);
return -1;
}
// 让zygote恢复运行
ptrace(PTRACE_CONT, pid, (void *)1, 0);
int wait_pid;
int stat;
int zygote = 0;
for (;;) {
// fork后子进程的pid
wait_pid = waitpid(-1, &stat, __WALL | WUNTRACED);
// 判断fork后的程序是不是我们指定的应用
if (wait_pid != 0){
if (debug > 1)
printf(".");
char fname[256];
sprintf(fname, "/proc/%d/cmdline", wait_pid);
int fp = open(fname, O_RDONLY);
if (fp < 0) {
printf("fp < 0\n");
ptrace(PTRACE_SYSCALL, wait_pid, 0, 0);
//ptrace(PTRACE_SETOPTIONS, wait_pid, (void *)0, (void *)(PTRACE_O_TRACEFORK));
//ptrace(PTRACE_CONT, wait_pid, (void *)1, 0);
continue;
}
read(fp, fname, sizeof(fname));
close(fp);
// -s 传进来的参数
if (strcmp(fname, appname) == 0) {
printf("匹配到appname: %s\n",appname);
if (debug > -1)
printf("zygote -> %s\n", fname);
// detach from zygote
ptrace(PTRACE_DETACH, pid, 0, (void *)SIGCONT);
printf("Detach from zygote\n");
// now perform on new process
pid = wait_pid;
printf("appname: %s pid: %d\n",appname,pid);
zygote = 1;
break;
} else {
if(debug > -1){
printf("Next fork,current app: %s\n",fname);
}
ptrace(PTRACE_SYSCALL, wait_pid, 0, 0);
// ptrace(PTRACE_SETOPTIONS, wait_pid, (void *)0, (void *)(PTRACE_O_TRACEFORK));
// ptrace(PTRACE_CONT, wait_pid, (void *)1, 0);
continue;
}
}
}
sleep(5);
// 获取到子进程pid
if (zygote) {
// 获取到指定进程pid后,拦截它的system_call
ptrace(PTRACE_SYSCALL, pid, 0, 0);
//ptrace(PTRACE_CONT, pid, (void *)1, 0);
pid_t new_pid;
while (1) {
flag = 0;
wait_pid = waitpid(pid, &stat, __WALL | WUNTRACED);
if(WIFEXITED(stat)){
printf("pid: %d,exited\n",pid);
return 0;
}
if(debug > -1){
printf("appname: %s,pid: %d,wait_pid: %d\n",appname,pid,wait_pid);
}
if(ptrace(PTRACE_GETEVENTMSG,wait_pid,0,&new_pid) == -1){
printf("wait_pid: %d\n",wait_pid);
printf("new_pid: %d\n",new_pid);
printf("errno: %d\n",errno);
printf("ptrace geteventmsg error\n");
continue;
}
//printf("系统调用前修改调用参数\n");
enterSysCall(pid);
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, NULL, 0);
// 修改系统调用结果
//printf("系统调用后修改调用结果\n");
pid_t tmp = leaveSysCall(pid);
if(flag == 0)
ptrace(PTRACE_SYSCALL, pid, 0, 0);
if(exit_tohook == 1 && flag == 1)
{
ptrace(PTRACE_DETACH, pid, 0, (void *)SIGCONT);
//ptrace(PTRACE_SYSCALL, tmp, 0, 0);
printf("detach from %s\n",appname);
return 0;
}
ptrace(PTRACE_SYSCALL, pid, 0, 0);
}
}
return 0;
}
int getCmdline(pid_t pid,char* fname){
sprintf(fname, "/proc/%d/cmdline", pid);
int fp = open(fname, O_RDONLY);
if (fp < 0) {
printf("get cmdline:pid: %d, errorno: %d\n",pid,errno);
return -1;
}
read(fp, fname, sizeof(fname));
close(fp);
//printf("%s\n",fname);
return 0;
}
void enterSysCall(pid_t pid) {
struct pt_regs regs;
int sysCallNo = 0;
struct {
void* ufb;
size_t len;
} regsvec = {®s,sizeof(struct pt_regs) };
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, ®svec);
sysCallNo = regs.regs[8];
// clone: 220
// wait4: 260
// ptrace: 117
if(debug > -1){
printf("\n\n===Enter Syscall===\npid: %d call syscallNo: %d \n",pid,sysCallNo);
printf("Before: argv[0]: %d\n",regs.regs[0]);
printf("Before: argv[1]: 0x%x\n",regs.regs[1]);
printf("Before: argv[2]: 0x%x\n",regs.regs[2]);
printf("Before: argv[3]: 0x%x\n",regs.regs[3]);
}else if(sysCallNo == syscall_tohook){
printf("\n\n===Enter Syscall===\npid: %d call syscallNo: %d \n",pid,sysCallNo);
printf("Before: argv[0]: %d\n",regs.regs[0]);
printf("Before: argv[1]: 0x%x\n",regs.regs[1]);
printf("Before: argv[2]: 0x%x\n",regs.regs[2]);
printf("Before: argv[3]: 0x%x\n",regs.regs[3]);
}
}
int leaveSysCall(pid_t pid) {
struct pt_regs regs;
int sysCallNo = 0;
struct {
void* ufb;
size_t len;
} regsvec = {®s,sizeof(struct pt_regs) };
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, ®svec);
sysCallNo = regs.regs[8];
if(debug > -1){
printf("===Leave Syscall===\npid: %d call syscallNo: %d \n",pid,sysCallNo);
printf("Leave: return Value: %d\n\n",regs.regs[0]);
printf("Leave: argv[1]: 0x%x\n",regs.regs[1]);
printf("Leave: argv[2]: 0x%x\n",regs.regs[2]);
printf("Leave: argv[3]: 0x%x\n",regs.regs[3]);
if(sysCallNo == syscall_tohook && return_tohook != -1){
// 修改返回值
regs.regs[0] = return_tohook;
ptrace(PTRACE_SETREGSET,pid,NT_PRSTATUS,®svec);
printf("update: return Value: %d\n\n",regs.regs[0]);
flag = 1;
}
}else if(sysCallNo == syscall_tohook){
printf("===Leave Syscall===\npid: %d call syscallNo: %d \n",pid,sysCallNo);
printf("Leave: return Value: %d\n\n",regs.regs[0]);
if(return_tohook != -1){
// 修改返回值
regs.regs[0] = return_tohook;
ptrace(PTRACE_SETREGSET,pid,NT_PRSTATUS,®svec);
printf("update: return Value: %d\n\n",regs.regs[0]);
flag = 1;
}
}
return regs.regs[0];
}
long getSysCallNo(int pid,struct pt_regs* regs){
//printf("获取syscallNo\n");
long scno = 0;
// edited by JC0o0l
scno = regs->regs[8];
printf("pid: %d call syscallNo: %d \n",pid,scno);
return scno;
}