-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathptrace.c
280 lines (263 loc) · 6.92 KB
/
ptrace.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
//=====================================================//
// Copyright (c) 2015, Dan Staples (https://disman.tl) //
//=====================================================//
#include "ptrace.h"
#include "debug.h"
#include "registers.h"
#include <sys/user.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int
ptrace_attach(int pid)
{
CHECK(ptrace(PTRACE_ATTACH, (pid_t)pid, NULL, NULL) == 0,
"Failed to attach to target process %d", pid);
return 1;
error:
return 0;
}
int
ptrace_detach(int pid)
{
CHECK(ptrace(PTRACE_DETACH, (pid_t)pid, NULL, NULL) == 0,
"Failed to detach to target process %d", pid);
return 1;
error:
return 0;
}
int
ptrace_getregs(int pid, struct user_regs_struct *regs)
{
CHECK(ptrace(PTRACE_GETREGS, (pid_t)pid, NULL, regs) == 0,
"Failed to get registers of target process %d", pid);
return 1;
error:
return 0;
}
int
ptrace_setregs(int pid, struct user_regs_struct *regs)
{
CHECK(ptrace(PTRACE_SETREGS, (pid_t)pid, NULL, regs) == 0,
"Failed to set registers of target process %d", pid);
return 1;
error:
return 0;
}
static int
_askstep(void)
{
char ans[10] = {0};
printf("Single step or wait until target address?[Y/n/w]: ");
fgets(ans, 9, stdin);
if (ans[0] == 'n' || ans[0] == 'N') {
dprintf("Continuing...");
return 0;
} else if (ans[0] == 'w' || ans[0] == 'W') {
dprintf("Waiting until we reach stop address...");
return 2;
} else {
dprintf("Stepping...");
return 1;
}
}
int
wait_stopped(int pid)
{
int status = 0;
while(1) {
CHECK(waitpid(pid, &status, 0) != -1,
"waitpid error");
if (WIFSTOPPED(status)) {
dprintf("Process stopped with signal %d", WSTOPSIG(status));
}
if (WIFEXITED(status)) {
dprintf("Process exited with signal %d", WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
dprintf("Process terminated with signal %d", WTERMSIG(status));
if (WCOREDUMP(status))
dprintf("Process core dumped");
}
if (WIFCONTINUED(status)) {
dprintf("Process was resumed by delivery of SIGCONT");
}
CHECK(!WIFEXITED(status), "Target process has exited");
if (WIFSTOPPED(status))
break;
}
return 1;
error:
return 0;
}
int
ptrace_next_syscall(int pid)
{
struct user_regs_struct regs = {0};
long eax;
do {
CHECK(ptrace(PTRACE_SYSCALL, pid, NULL, NULL) == 0,
"Failed to continue execution until next syscall enter/exit");
CHECK(wait_stopped(pid), "Failed to wait until target process in stopped state");
errno = 0;
eax = ptrace(PTRACE_PEEKUSER, pid, USER_EAX, NULL);
CHECK(errno == 0, "Failed to read EAX of target process");
dprintf("EAX after syscall: %ld", eax);
} while (eax == -ENOSYS); // RAX/EAX == -ENOSYS means just entered a syscall, != -ENOSYS means just exited syscall (offsetof(struct user, regs.orig_eax) can be used to see syscall number if you want)
return 1;
error:
return 0;
}
int
ptrace_continue(int pid, void *stop_addr)
{
#ifndef DEBUG
dprintf("Continuing execution of target process %d", pid);
CHECK(ptrace(PTRACE_CONT, (pid_t)pid, NULL, NULL) == 0,
"Failed to continue execution of target process %d", pid);
#else
dprintf("Stepping execution of target process %d", pid);
if (stop_addr)
dprintf("Stop address: %p", stop_addr);
int choice = _askstep();
if (choice == 0) {
dprintf("Continuing execution of target process %d", pid);
CHECK(ptrace(PTRACE_CONT, (pid_t)pid, NULL, NULL) == 0,
"Failed to continue execution of target process %d", pid);
return 1;
}
while(1) {
struct user_regs_struct regs = {0};
CHECK(ptrace_getregs(pid, ®s), "ptrace_getregs() error");
#ifdef __i386__
unsigned char eip[8] = {0};
CHECK(ptrace_readmem(pid, (void*)regs.eip, (void*)eip, sizeof(eip)),
"Failed to read 8 bytes at EIP 0x%lx", regs.eip);
if (choice != 2 || (void*)regs.eip == stop_addr) {
printf("eax 0x%lx\n" \
"ebx 0x%lx\n" \
"ecx 0x%lx\n" \
"edx 0x%lx\n" \
"esi 0x%lx\n" \
"edi 0x%lx\n" \
"ebp 0x%lx\n" \
"eip 0x%lx\n" \
"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
regs.eax,
regs.ebx,
regs.ecx,
regs.edx,
regs.esi,
regs.edi,
regs.ebp,
regs.eip,
eip[0],
eip[1],
eip[2],
eip[3],
eip[4],
eip[5],
eip[6],
eip[7]);
choice = _askstep();
}
#elif defined(__x86_64__)
unsigned char rip[16] = {0};
CHECK(ptrace_readmem(pid, (void*)regs.rip, (void*)rip, sizeof(rip)),
"Failed to read 16 bytes at RIP 0x%llx", regs.rip);
if (choice != 2 || (void*)regs.rip == stop_addr) {
printf("rax 0x%llx\n" \
"rdi 0x%llx\n" \
"rsi 0x%llx\n" \
"rdx 0x%llx\n" \
"r10 0x%llx\n" \
"r8 0x%llx\n" \
"r9 0x%llx\n" \
"rbx 0x%llx\n" \
"rip 0x%llx\n" \
"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n" \
"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
regs.rax,
regs.rdi,
regs.rsi,
regs.rdx,
regs.r10,
regs.r8,
regs.r9,
regs.rbx,
regs.rip,
rip[0],
rip[1],
rip[2],
rip[3],
rip[4],
rip[5],
rip[6],
rip[7],
rip[8],
rip[9],
rip[10],
rip[11],
rip[12],
rip[13],
rip[14],
rip[15]);
choice = _askstep();
}
#endif
CHECK(ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) == 0,
"Failed to step execution");
int status = 0;
CHECK(waitpid(pid, &status, 0) != -1,
"waitpid error");
if (choice == 0) {
dprintf("Continuing execution of target process %d", pid);
CHECK(ptrace(PTRACE_CONT, (pid_t)pid, NULL, NULL) == 0,
"Failed to continue execution of target process %d", pid);
return 1;
}
}
#endif
return 1;
error:
return 1;
}
int
ptrace_readmem(int pid, void *addr, unsigned char *buf, size_t len)
{
CHECK(len % sizeof(void*) == 0, "Length of memory to read must be word-aligned");
size_t wordlen = len / sizeof(void*);
void **wordbuf = (void**)buf;
errno = 0;
for (size_t i = 0; i < wordlen; i++) {
wordbuf[i] = (void*)ptrace(PTRACE_PEEKDATA, (pid_t)pid, addr + (i * sizeof(void*)), NULL);
CHECK(errno == 0,
"Failed to read memory of target process %d at location %p",
pid,
addr + (i * sizeof(void*)));
}
return 1;
error:
return 0;
}
int
ptrace_writemem(int pid, void *addr, unsigned char *buf, size_t len)
{
CHECK(len % sizeof(void*) == 0, "Length of memory to read must be word-aligned");
size_t wordlen = len / sizeof(void*);
void **wordbuf = (void**)buf;
for (size_t i = 0; i < wordlen; i++) {
long result = ptrace(PTRACE_POKEDATA, (pid_t)pid, addr + (i * sizeof(void*)), wordbuf[i]);
CHECK(result == 0,
"Failed to write memory to target process %d at location %p",
pid,
addr + (i * sizeof(void*)));
}
return 1;
error:
return 0;
}