forked from hmgle/graftcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
384 lines (361 loc) · 8.01 KB
/
util.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* graftcp
* Copyright (C) 2018, 2021 Hmgle <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "graftcp.h"
struct socket_info *SOCKET_INFO_TAB = NULL;
void add_socket_info(struct socket_info *s)
{
HASH_ADD_INT(SOCKET_INFO_TAB, magic_fd, s);
}
void del_socket_info(struct socket_info *s)
{
HASH_DEL(SOCKET_INFO_TAB, s);
}
struct socket_info *find_socket_info(uint64_t magic_fd)
{
struct socket_info *s;
HASH_FIND_INT(SOCKET_INFO_TAB, &magic_fd, s);
return s;
}
struct proc_info *PROC_INFO_TAB = NULL;
void add_proc_info(struct proc_info *p)
{
HASH_ADD_INT(PROC_INFO_TAB, pid, p);
}
void del_proc_info(struct proc_info *p)
{
HASH_DEL(PROC_INFO_TAB, p);
}
struct proc_info *find_proc_info(pid_t pid)
{
struct proc_info *p;
HASH_FIND_INT(PROC_INFO_TAB, &pid, p);
return p;
}
struct proc_info *alloc_proc_info(pid_t pid)
{
struct proc_info *newp = calloc(1, sizeof(*newp));
newp->pid = pid;
add_proc_info(newp);
return newp;
}
#ifndef offsetof
#define offsetof(a, b) __builtin_offsetof(a,b)
#endif
#if defined(__arm64__) || defined(__aarch64__)
struct arm_pt_regs {
uint32_t uregs[18];
};
#define ARM_cpsr uregs[16]
#define ARM_pc uregs[15]
#define ARM_lr uregs[14]
#define ARM_sp uregs[13]
#define ARM_ip uregs[12]
#define ARM_fp uregs[11]
#define ARM_r10 uregs[10]
#define ARM_r9 uregs[9]
#define ARM_r8 uregs[8]
#define ARM_r7 uregs[7]
#define ARM_r6 uregs[6]
#define ARM_r5 uregs[5]
#define ARM_r4 uregs[4]
#define ARM_r3 uregs[3]
#define ARM_r2 uregs[2]
#define ARM_r1 uregs[1]
#define ARM_r0 uregs[0]
#define ARM_ORIG_r0 uregs[17]
static union {
struct user_pt_regs aarch64_r;
struct arm_pt_regs arm_r;
} arm_regs_union;
#endif
int get_syscall_number(pid_t pid)
{
#if defined(__x86_64__)
#if 1
errno = 0;
int offset = offsetof(struct user, regs.orig_rax);
long val = ptrace(PTRACE_PEEKUSER, pid, offset);
assert(errno == 0);
return (int)val;
#else /* another way */
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
return regs.orig_rax;
#endif
#elif defined(__i386__)
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
return regs.orig_eax;
#elif defined(__arm__)
errno = 0;
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
return regs.ARM_r7;
#elif defined(__arm64__) || defined(__aarch64__)
struct iovec iov = {
.iov_base = &arm_regs_union,
.iov_len = sizeof(struct user_pt_regs)
};
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
assert(errno == 0);
switch (iov.iov_len) {
case sizeof(struct user_pt_regs):
return arm_regs_union.aarch64_r.regs[8];
case sizeof(struct arm_pt_regs):
return arm_regs_union.arm_r.ARM_r7;
}
return -1;
#endif
}
int get_retval(pid_t pid)
{
#if defined(__x86_64__)
#if 1
errno = 0;
int offset = offsetof(struct user, regs.rax);
long val = ptrace(PTRACE_PEEKUSER, pid, offset);
return (int)val;
#else /* another way */
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
return regs.rax;
#endif
#elif defined(__i386__)
errno = 0;
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
return regs.eax;
#elif defined(__arm__)
errno = 0;
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
return regs.ARM_r0;
#elif defined(__arm64__) || defined(__aarch64__)
struct iovec iov = {
.iov_base = &arm_regs_union,
.iov_len = sizeof(struct user_pt_regs)
};
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
assert(errno == 0);
switch (iov.iov_len) {
case sizeof(struct user_pt_regs):
return arm_regs_union.aarch64_r.regs[0];
case sizeof(struct arm_pt_regs):
return arm_regs_union.arm_r.ARM_r0;
}
return -1;
#endif
}
void set_retval(pid_t pid, long new_val)
{
#if defined(__x86_64__)
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
if ((long)regs.rax == new_val)
return;
regs.rax = new_val;
ptrace(PTRACE_SETREGS, pid, 0, ®s);
assert(errno == 0);
#elif defined(__i386__)
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
if ((long)regs.eax == new_val)
return;
regs.eax = new_val;
ptrace(PTRACE_SETREGS, pid, 0, ®s);
assert(errno == 0);
#elif defined(__arm__)
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
assert(errno == 0);
if ((long)regs.ARM_r0 == new_val)
return;
regs.ARM_r0 = new_val;
ptrace(PTRACE_SETREGS, pid, 0, ®s);
assert(errno == 0);
#elif defined(__arm64__) || defined(__aarch64__)
struct iovec iov = {
.iov_base = &arm_regs_union,
.iov_len = sizeof(struct user_pt_regs)
};
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
assert(errno == 0);
switch (iov.iov_len) {
case sizeof(struct user_pt_regs):
arm_regs_union.aarch64_r.regs[0] = new_val;
case sizeof(struct arm_pt_regs):
arm_regs_union.arm_r.ARM_r0 = new_val;
}
ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov);
assert(errno == 0);
#endif
}
long get_syscall_arg(pid_t pid, int order)
{
long val;
#if defined(__x86_64__)
int offset;
switch (order) {
case 0:
offset = offsetof(struct user, regs.rdi);
break;
case 1:
offset = offsetof(struct user, regs.rsi);
break;
case 2:
offset = offsetof(struct user, regs.rdx);
break;
case 3:
offset = offsetof(struct user, regs.r10);
break;
case 4:
offset = offsetof(struct user, regs.r8);
break;
case 5:
offset = offsetof(struct user, regs.r9);
break;
default:
return -1;
}
errno = 0;
val = ptrace(PTRACE_PEEKUSER, pid, offset);
assert(errno == 0);
#elif defined(__i386__)
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
switch (order) {
case 0:
val = regs.ebx;
break;
case 1:
val = regs.ecx;
break;
case 2:
val = regs.edx;
break;
case 3:
val = regs.esi;
break;
case 4:
val = regs.edi;
break;
case 5:
val = regs.ebp;
break;
default:
return -1;
}
#elif defined(__arm__)
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
switch (order) {
case 0:
val = regs.ARM_ORIG_r0;
break;
case 1:
val = regs.ARM_r1;
break;
case 2:
val = regs.ARM_r2;
break;
case 3:
val = regs.ARM_r3;
break;
case 4:
val = regs.ARM_r4;
break;
case 5:
val = regs.ARM_r5;
break;
default:
return -1;
}
#elif defined(__arm64__) || defined(__aarch64__)
switch (order) {
case 0:
val = arm_regs_union.aarch64_r.regs[0];
break;
case 1:
val = arm_regs_union.aarch64_r.regs[1];
break;
case 2:
val = arm_regs_union.aarch64_r.regs[2];
break;
case 3:
val = arm_regs_union.aarch64_r.regs[3];
break;
case 4:
val = arm_regs_union.aarch64_r.regs[4];
break;
case 5:
val = arm_regs_union.aarch64_r.regs[5];
break;
default:
return -1;
}
#endif
return val;
}
void getdata(pid_t child, long addr, char *dst, int len)
{
char *laddr;
int i, j;
union u {
long val;
char chars[sizeof(long)];
} data;
i = 0;
j = len / sizeof(long);
laddr = dst;
while (i < j) {
data.val = ptrace(PTRACE_PEEKDATA, child, addr i * 8, NULL);
memcpy(laddr, data.chars, sizeof(long));
i;
laddr = sizeof(long);
}
j = len % sizeof(long);
if (j != 0) {
data.val = ptrace(PTRACE_PEEKDATA, child, addr i * 8, NULL);
memcpy(laddr, data.chars, j);
}
}
void putdata(pid_t child, long addr, char *src, int len)
{
char *laddr;
int i, j;
union u {
long val;
char chars[sizeof(long)];
} data;
i = 0;
j = len / sizeof(long);
laddr = src;
while (i < j) {
memcpy(data.chars, laddr, sizeof(long));
ptrace(PTRACE_POKEDATA, child, addr i * 8, data.val);
i;
laddr = sizeof(long);
}
j = len % sizeof(long);
if (j != 0) {
memcpy(data.chars, laddr, j);
ptrace(PTRACE_POKEDATA, child, addr i * 8, data.val);
}
}