-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
102 lines (92 loc) · 2.76 KB
/
main.c
File metadata and controls
102 lines (92 loc) · 2.76 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
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
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
// Process ID for the child process
int syscall;
pid_t pid;
pid = fork();
// Setting the shared file
FILE* theFile;
theFile = fopen("data.txt","w");
if (theFile == NULL) {
printf("Error! Could not open file\n");
exit(-1);
}
fprintf(theFile, "%d", 0);
fclose(theFile);
// If there was a fork error, then exit
if (pid < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
} else if (pid == 0){
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
printf("The child process with PID: %d is created and is set to be traced \n", (int) getpid());
char *args[] = {argv[2], "C", "Programming", NULL};
execv(argv[1], args);
}
// Waiting for the child process to run
waitpid(pid, 0, 0);
ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_EXITKILL);
// The while loop runs until the child file is finished
while (1) {
theFile = fopen("data.txt","w");
// This loop is to extract the system calls and write them to
// the shared file
for(int i = 0; i < 5; i ++){
// a single step into the system call
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) == -1){}
if (waitpid(pid, 0, 0) == -1){}
syscall = ptrace(PTRACE_PEEKUSER, pid, sizeof(long)*ORIG_EAX);
if(syscall != -1) {
fprintf(theFile,"%d",syscall);
fprintf(theFile," ");
} else {
break;
}
// Exiting the child system call
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) == -1){}
if (waitpid(pid, 0, 0) == -1){}
}
fclose(theFile);
int checkNum = 2;
int checker = 0;
do{
theFile = fopen("data.txt","r");
if (theFile == NULL) {
printf("Error! Could not open file \n");
exit(-1);
}
fscanf(theFile, "%d", &checkNum);
fclose(theFile);
if(checkNum == 0 || checkNum == 1){
checker = 1;
}
} while(checker != 1);
// Prints the result of detection
if (checkNum == 1){
printf("Malware detected.\n");
kill(pid, SIGTERM);
break;
} else {
printf("Malware not detected.\n");
}
if(syscall == -1) {
exit(-1);
}
}
return 0;
}