-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptrace_p1.c
More file actions
47 lines (46 loc) · 1.63 KB
/
ptrace_p1.c
File metadata and controls
47 lines (46 loc) · 1.63 KB
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
/*
Notes:
- by using PTRACE_PEEKUSER as the first argument, we can examine the contents of the USER area where register contents and other
information is stored
*/
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/reg.h>
#include <stdio.h>/* For constants
ORIG_EAX etc */
int main(){
pid_t child;
long orig_rax;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("/bin/ls", "ls", NULL);
}
else {
else {
while(1) {
wait(&status);
if(WIFEXITED(status))
break;
orig_rax = ptrace(PTRACE_PEEKUSER, child, sizeof(long) * ORIG_RAX, NULL);
if(orig_rax == SYS_write) {
if(insyscall == 0) {
/* Syscall entry */
insyscall = 1;
params[0] = ptrace(PTRACE_PEEKUSER, child, sizeof(long) * RDI, NULL);
params[1] = ptrace(PTRACE_PEEKUSER, child, sizeof(long) * RSI, NULL);
params[2] = ptrace(PTRACE_PEEKUSER, child, sizeof(long) * RDX, NULL);
printf("Write called with " "%ld, %ld, %ld\n", params[0], params[1], params[2]);
}
else { /* Syscall exit */
rax = ptrace(PTRACE_PEEKUSER, child, sizeof(long) * RAX, NULL);
printf("Write returned with %ld\n", rax);
insyscall = 0;
}
}
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
}
return 0;
}