-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.c
93 lines (76 loc) · 2.3 KB
/
sandbox.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <unistd.h>
void run_target(const char* programname) {
printf("Target started. Will run: %s\n", programname);
// Allow tracing of this process
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
perror("ptrace");
exit(1);
}
// Replace this process's image with the target program
execl(programname, programname, NULL);
}
void run_tracer(pid_t child_pid) {
int wait_status;
struct user_regs_struct regs;
// Wait for child to stop on its first instruction
waitpid(child_pid, &wait_status, 0);
while (1) {
// Wait for a system call entry or exit
if (ptrace(PTRACE_SYSCALL, child_pid, NULL, NULL) == -1) {
perror("ptrace");
exit(1);
}
// Wait for child to stop again
waitpid(child_pid, &wait_status, 0);
// Get the current register values
if (ptrace(PTRACE_GETREGS, child_pid, NULL, ®s) == -1) {
perror("ptrace");
exit(1);
}
// Check if this is a syscall entry
if (regs.orig_rax == -1) {
// This is not a syscall entry, continue
continue;
}
// Print the syscall number
printf("Syscall intercepted: %lld\n", regs.orig_rax);
// Continue to the next system call exit
if (ptrace(PTRACE_SYSCALL, child_pid, NULL, NULL) == -1) {
perror("ptrace");
exit(1);
}
// Wait for child to stop again
waitpid(child_pid, &wait_status, 0);
// Get the current register values
if (ptrace(PTRACE_GETREGS, child_pid, NULL, ®s) == -1) {
perror("ptrace");
exit(1);
}
}
}
int main(int argc, char* argv[]) {
pid_t child_pid;
if (argc < 2) {
fprintf(stderr, "Usage: %s <program to trace>\n", argv[0]);
return 1;
}
child_pid = fork();
if (child_pid == 0) {
// Child process: Run the target program
run_target(argv[1]);
} else if (child_pid > 0) {
// Parent process: Trace the child
run_tracer(child_pid);
} else {
perror("fork");
return 1;
}
return 0;
}