-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.c
178 lines (150 loc) · 4.3 KB
/
extra.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <signal.h>
#define FAIL 127
unsigned int ptrace_setoptions = PTRACE_O_TRACEEXEC | PTRACE_O_EXITKILL | PTRACE_O_TRACEFORK | PTRACE_O_TRACECLONE;
bool v = false;
int propagate_signal(int status) {
int signal = 0;
if(WIFSTOPPED(status)) {
if(v) printf("Child was stopped.\nNumber of the signal received was %d.\n", WSTOPSIG(status));
}
if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTERM) {
if(v) printf("Child received SIGTERM.\n");
signal = SIGTERM;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGINT) {
if(v) printf("Child received SIGINT.\n");
signal = SIGINT;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGQUIT) {
if(v) printf("Child received SIGQUIT.\n");
signal = SIGQUIT;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGHUP) {
if(v) printf("Child received SIGHUP.\n");
signal = SIGHUP;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGPIPE) {
if(v) printf("Child received SIGPIPE.\n");
signal = SIGPIPE;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGCHLD) {
if(v) printf("Child received SIGCHLD.\n");
signal = SIGCONT;
}
else if(WIFSTOPPED(status) && WSTOPSIG(status) != SIGTRAP) {
signal = WSTOPSIG(status);
}
return signal;
}
int handle_exit(int status) {
if (WIFEXITED(status)) {
if(v) fprintf(stderr, "Exit status was %d.\n",
WEXITSTATUS(status));
if(v) fprintf(stderr, "The return code was %d.\n",
WEXITSTATUS(status));
return WEXITSTATUS(status);
}
if (WIFSIGNALED(status)) {
if(v) fprintf(stderr, "Child terminated by signal %d.\n",
WTERMSIG(status));
if(v) fprintf(stderr, "The return code was %d.\n",
128 + WTERMSIG(status));
return 128 + WTERMSIG(status);
}
return -1;
}
int handle_child(int argc, char **argv) {
ptrace(PTRACE_TRACEME, NULL, NULL, NULL);
raise(SIGSTOP);
if(v) return execvp(argv[2], argv + 2);
return execvp(argv[1], argv + 1);
}
void print_executable_path(pid_t pid) {
char *pathname = malloc(4096);
char buf[4096];
sprintf(pathname, "%s%d%s", "/proc/", pid, "/exe");
ssize_t len = readlink(pathname, buf, sizeof(buf) - 1);
if (len != -1) {
buf[len] = '\0';
} else {
perror("READLINK");
}
if(v) fprintf(stderr, "The executed program was : ");
fprintf(stderr, "%s\n", buf);
free(pathname);
}
int main(int argc, char **argv) {
if(argv[1] == NULL) {
fprintf(stderr, "Argument is missing.\n");
return FAIL;
}
if(!strcmp(argv[1], "-v")) {
if (argv[2] == NULL) {
fprintf(stderr, "Argument is missing.\n");
return FAIL;
}
v = true;
}
pid_t pid = fork();
if(pid == 1) {
perror("FORK");
return FAIL;
}
if(pid == 0) {
handle_child(argc, argv);
perror("EXECVP failed : ");
return FAIL;
}
if(v) printf("Fork() was successful.\n");
if(v) printf("Child process (tracee) was created.\n");
int status = 0;
int wait_check = 0;
int signal = 0;
pid_t new_pid = 0;
wait(&status);
ptrace(PTRACE_SETOPTIONS, pid, NULL, ptrace_setoptions);
ptrace(PTRACE_CONT, pid, NULL, NULL);
while((pid = wait(&status)) > 0) {
if(v) printf("\nStatus information provided "
"by call to wait() : %d.\n\n", status);
signal = propagate_signal(status);
if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
if(v) printf("Child received SIGTRAP.\n");
if(status>>8 == (SIGTRAP | (PTRACE_EVENT_EXEC<<8))) {
if(v) printf("===============\nEXECVE occurred\n===============\n");
print_executable_path(pid);
signal = 0;
}
else if(status>>8 == (SIGTRAP | (PTRACE_EVENT_FORK<<8)) ||
status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8))) {
if(v) printf("The calling process PID is %d.\n", getpid());
if (ptrace(PTRACE_GETEVENTMSG, wait_check, NULL, &new_pid) != -1) {
if(v) printf("==========================================="
"=====\nA new child process "
"with PID %d was created.\n===="
"============================================\n",
new_pid);
}
signal = 0;
}
else {
signal = SIGTRAP;
}
}
ptrace(PTRACE_CONT, pid, NULL, signal);
}
if (WIFEXITED(status) || WIFSIGNALED(status)) {
if(v) printf("Child exited.\n");
return handle_exit(status);
}
return EXIT_SUCCESS;
}