-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
581 lines (468 loc) · 15.8 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// GRIGORI DMITRII
// 09-05-24
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#define MAX_CHR_PATH 256
// -v option value
int verbose_option = 0;
// -s option value
char safe_directory_option[MAX_CHR_PATH];
struct file
{
char filepath[MAX_CHR_PATH];
struct stat st_stat;
};
char *separator()
{
#ifdef _WIN32
return "\\";
#else
return "/";
#endif
}
/* Creates a new snapshot binary file with [n] amount of [struct file] elements*/
void writeToFileBinary(char* filename, struct file **st_file_current, int n)
{
// open file in write-only, create if doesnt exist, truncate
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1)
{
perror("open error");
exit(1);
}
ssize_t bytes_written = write(fd, *st_file_current, n * sizeof(struct file));
if (bytes_written == -1)
{
perror("write error");
close(fd);
exit(1);
}
if (close(fd) == -1)
{
perror("close error");
exit(1);
}
}
/* Reads from snapshot binary file*/
struct file* readFromFileBinary(int *st_file_src_count, char* filename)
{
// open with read only
int fd = open(filename, O_RDONLY);
// return NULL if file doesn't exist
if (fd == -1)
{
return NULL;
}
// get file size
off_t file_size = lseek(fd, 0, SEEK_END);
if (file_size == -1)
{
perror("lseek error");
close(fd);
exit(1);
}
// return to start
off_t offset = lseek(fd, 0, SEEK_SET);
if (offset == -1)
{
perror("lseek set error");
close(fd);
exit(1);
}
// number of elements of directory
*st_file_src_count = file_size / sizeof(struct file);
struct file* st_file = (struct file*) malloc(*st_file_src_count * sizeof(struct file));
if (st_file == NULL)
{
perror("malloc error");
close(fd);
exit(1);
}
ssize_t bytes_read = read(fd, st_file, *st_file_src_count * sizeof(struct file));
if (bytes_read == -1)
{
perror("read error");
free(st_file);
close(fd);
exit(1);
}
// close fd
if (close(fd) == -1)
{
perror("close fd error");
free(st_file);
exit(1);
}
return st_file;
}
/* Recursive function to read all files from a directory and pass each file
* information to [st_file_current] and verify through a bash script for
* malicious file that will be moved to a safe directory*/
void rec_readdir(int *st_file_current_count, struct file **st_file_current, DIR *dir, char *filepath, int *malicious)
{
struct dirent *st_dirent = readdir(dir);
// read each file of the directory in st_dirent
while (st_dirent)
{
struct stat st_stat;
char filename[MAX_CHR_PATH];
// get current file path -> filename
sprintf(filename, "%s%s%s", filepath, separator(), st_dirent->d_name);
// copy global safe directory path to local variable
char safe_directory[MAX_CHR_PATH];
strcpy(safe_directory, safe_directory_option);
if (lstat(filename, &st_stat) != 0)
{
perror(NULL);
closedir(dir);
exit(1);
}
// check file for null permissions amd execute script to check for malware
if ((st_stat.st_mode & S_IRWXU) == 0 &&
(st_stat.st_mode & S_IRWXG) == 0 &&
(st_stat.st_mode & S_IRWXO) == 0)
{
if (verbose_option)
{
printf("The file %s has no permissions.\n", filename);
}
pid_t pid;
char exec_output_string[256];
int pfd[2];
if (pipe(pfd) < 0)
{
perror("pipe error\n");
exit(1);
}
if ((pid = fork()) < 0)
{
perror("fork error\n");
exit(1);
}
if (pid == 0)
{
// slave code
close(pfd[0]); // close read pipe -> writes in pipe
if (dup2(pfd[1], STDOUT_FILENO) == -1)
{
perror("dup2");
exit(1);
}
char *args[] = {"./verify_malicious.sh", filename, NULL};
execvp(args[0], args);
// if reached exec has failed
printf("Exec Failed.\n");
exit(1);
}
// master code
close(pfd[1]); // close write pipe -> reads from pipe
FILE *stream = fdopen(pfd[0], "r");
// get char* from FILE* -> exec_output_string
fscanf(stream, "%s", exec_output_string);
close(pfd[0]); /* la sfarsit inchide si capatul utilizat */
int status;
waitpid(pid, &status, WUNTRACED);
// check exec results -> "SAFE" - do nothing, [FILENAME] - move to safe directory
if (strcmp(exec_output_string, "SAFE") != 0)
{
// amount of malicious files in a directory
(*malicious)++;
// check if safe directory ends in '/', if not -> add one
if (safe_directory[strlen(safe_directory) - 1] != separator()[0])
{
strcat(safe_directory, separator());
}
strcat(safe_directory, exec_output_string);
// move file to safe directory
if (rename(filename, safe_directory) < 0)
{
perror("rename error\n");
exit(1);
}
}
if (verbose_option)
{
printf("PID %d terminated with exit code %d\n", pid, status);
}
}
else
{
// save file in st_file
if (!(*st_file_current = realloc(*st_file_current, (*st_file_current_count + 1) * sizeof(struct file))))
{
perror("rec_readdir");
closedir(dir);
free(*st_file_current);
exit(1);
}
// save file information to st_file_current
strcpy((*st_file_current)[*st_file_current_count].filepath, filename);
(*st_file_current)[*st_file_current_count].st_stat = st_stat;
(*st_file_current_count)++;
}
// check if file is directory
if (S_ISDIR(st_stat.st_mode))
{
// every directory in linux has 2 files [.] and [..] which we skip if found
// because on every file modification inside a directory affect the size of
// those files and affect the snapshot behaviour
if (strcmp(st_dirent->d_name, ".") != 0 && strcmp(st_dirent->d_name, "..") != 0)
{
DIR *inputdir = opendir(filename);
rec_readdir(st_file_current_count, st_file_current, inputdir, filename, malicious);
}
}
st_dirent = readdir(dir);
}
if (closedir(dir) == -1)
{
perror("closedir");
free(st_file_current);
exit(1);
}
}
/* Compare saved snapshot stuct in system and current read and print file differences*/
void compare_snapshots(struct file *st_file_current, struct file *st_file_src, int st_file_current_count, int st_file_src_count)
{
struct stat st_stat_current;
int file_found = 0;
//compare st_file_src to st_file_current
//show modified files
//show files which were not found -> deleted.
for (int i = 0; i < st_file_src_count; i++)
{
char *filepath = st_file_src[i].filepath;
for (int j = 0; j < st_file_current_count; j++)
{
//find the exact file in both st
if (strcmp(filepath, st_file_current[j].filepath) == 0)
{
// filter out aux directories because it modifies on every file add/delete and floods output
if (filepath[strlen(filepath) - 1 ] != '.')
{
//read lstat of file st_file_current
if (lstat(st_file_current[j].filepath, &st_stat_current) != 0)
{
perror(NULL);
exit(1);
}
//st_size differs -> file was modified
if (st_file_src[i].st_stat.st_size != st_stat_current.st_size)
{
printf("[*]\t%s - was modified\n", filepath);
}
//st_mode differs -> file permissions were modified
if (st_file_src[i].st_stat.st_mode != st_stat_current.st_mode)
{
printf("[*]\t%s - permissions were modified\n", filepath);
}
//st_nlink differs -> hard link count modified
if (st_file_src[i].st_stat.st_nlink != st_stat_current.st_nlink)
{
printf("[*]\t%s - hard link count was modified\n", filepath);
}
}
file_found = 1;
break;
}
}
//file was not found in st_file_current -> it was deleted
if (file_found == 0 && filepath[strlen(filepath) - 1 ] != '.')
{
printf("[-]\t%s - was deleted\n", filepath);
}
//reset flag
file_found = 0;
}
//compare st_file_current to st_file_src
//show files which were not found -> added.
for (int i = 0; i < st_file_current_count; i++)
{
char *filepath = st_file_current[i].filepath;
for (int j = 0; j < st_file_src_count; j++)
{
//find the exact file in both st
if (strcmp(filepath, st_file_src[j].filepath) == 0)
{
file_found = 1;
break;
}
}
//file was not found in st_file_current -> it was added
if (file_found == 0 && filepath[strlen(filepath) - 1 ] != '.')
{
printf("[+]\t%s - was added\n", filepath);
}
//reset flag
file_found = 0;
}
}
int main(int argc, char* argv[])
{
char array_of_directories[10][MAX_CHR_PATH];
int count_of_directories = 0;
char snapshot_path[MAX_CHR_PATH];
int snapshot_path_flag = 0;
int opt;
// get program arguments
while((opt = getopt(argc, argv, "vo:d:s:")) != -1)
{
switch(opt)
{
case 'v':
//verbose mode
verbose_option = 1;
break;
case 's':
//safe directory for malicious files
strcpy(safe_directory_option, optarg);
break;
case 'o':
// get snapshot location
snapshot_path_flag = 1;
strcpy(snapshot_path, optarg);
break;
case 'd':
// get directory names
optind--;
for( ;optind < argc && *argv[optind] != '-'; optind++)
{
// exit if count of dir > 10
if (count_of_directories == 10)
{
printf("too many arguments for -d: max 10\n");
exit(1);
}
// directory can be passed as [dir] or [dir/] and its easier to
// delete the separator before processing if found than modify
// current logic
if (argv[optind][strlen(argv[optind]) - 1] == separator()[0])
{
argv[optind][strlen(argv[optind]) - 1] = '\0';
}
// insert in array
strcpy(array_of_directories[count_of_directories], argv[optind]);
count_of_directories++;
}
break;
}
}
// -s option can't be null
if (strcmp(safe_directory_option, "") == 0)
{
printf("-s option is not set\n");
exit(1);
}
struct pids
{
pid_t pid;
int malicious_files;
} pids[10];
int pids_count = 0;
pid_t pid;
for (int i = 0; i < count_of_directories; i++)
{
if ((pid = fork()) < 0)
{
perror("fork error");
exit(1);
}
if (pid != 0)
{
//master code
//save pid of slave, continue to next dir
pids[pids_count].pid = pid;
pids_count++;
continue;
}
//slave code
//execute and quit if slave
// get directory name
char *inputdirstring = array_of_directories[i];
// get snapshot file name
// starts with a . , (optional: folowing snapshot location), following directory name -> .[dirname] or [dirlocation]/.[dirname]
char snapshot_filename[MAX_CHR_PATH] = ".";
// check if flag is 1 -> -o option is specified
// [dirlocation]/.[dirname]
if (snapshot_path_flag == 1)
{
char string[MAX_CHR_PATH] = "";
strcat(string, snapshot_path);
strcat(string, separator());
strcat(string, ".");
strcat(string, inputdirstring);
strcpy(snapshot_filename, string);
}
// .[dirname]
else
{
strcat(snapshot_filename, inputdirstring);
}
// open directory
DIR *inputdir = opendir(inputdirstring);
// skip if not directory
if (!inputdir)
{
printf("%s - is not a directory\n", inputdirstring);
continue;
}
//st_file of current directory state
struct file *st_file_current = NULL;
//st_file of snapshot
struct file *st_file_src = NULL;
//elements count
int st_file_current_count = 0;
int st_file_src_count = 0;
// read current directory structure
rec_readdir(&st_file_current_count, &st_file_current, inputdir, inputdirstring, &pids[pids_count].malicious_files);
// read snapshot file if exists
st_file_src = readFromFileBinary(&st_file_src_count, snapshot_filename);
// no snapshot file -> nothing to compare, create new snapshot
if (st_file_src == NULL)
{
writeToFileBinary(snapshot_filename, &st_file_current, st_file_current_count);
}
// compare snapshots
else
{
compare_snapshots(st_file_current, st_file_src, st_file_current_count, st_file_src_count);
//write new version
writeToFileBinary(snapshot_filename, &st_file_current, st_file_current_count);
}
printf("Snapshot for directory %s created succesfully.\n", inputdirstring);
//free memory
free(st_file_current);
free(st_file_src);
st_file_current = NULL;
st_file_src = NULL;
st_file_current_count = 0;
st_file_src_count = 0;
// exit with number of malicious files found
exit(pids[pids_count].malicious_files);
}
//master -> wait for slaves
if (pid != 0)
{
for (int i = 0; i < pids_count; i++)
{
int status;
waitpid(pids[i].pid, &status, WUNTRACED);
int es = 0;
if (WIFEXITED(status))
{
es = WEXITSTATUS(status);
}
printf("Child process %d terminated with PID %d. Found %d malicious files\n", i, pids[i].pid, es);
}
}
return 0;
}