This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsnoop.c
531 lines (477 loc) · 14.3 KB
/
fsnoop.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
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fanotify.h>
#include <sys/stat.h>
#include <unistd.h>
#include "fsnoop.h"
#include "resolve.h"
#include "cache.h"
#include "stubs.h"
// =============================================================================
// Options
int optFlagDontFollow = 0;
int optFlagOnlydir = 0;
int optFlagMount = 0;
int optFlagFilesystem = 0;
int optMaskAccess = 0;
int optMaskModify = 0;
int optMaskClose = 0;
int optMaskCloseWrite = 0;
int optMaskCloseNowrite = 0;
int optMaskOpen = 0;
int optMaskOpenExec = 0;
int optMaskAttrib = 0;
int optMaskCreate = 0;
int optMaskDelete = 0;
int optMaskDeleteSelf = 0;
int optMaskMove = 0;
int optMaskMovedFrom = 0;
int optMaskMovedTo = 0;
int optMaskQOverflow = 0;
int optMaskOpenPerm = 0;
int optMaskOpenExecPerm = 0;
int optMaskAccessPerm = 0;
int optMaskOndir = 0;
int optMaskEventOnChild = 0;
int optVerbose = LOG_INFO;
int optCache = 1;
char **optPaths = NULL;
// =============================================================================
int lprintf (const int level, const char *format, ...) {
int printed = 0;
va_list arguments;
va_start (arguments, format);
if (level <= optVerbose) {
switch (level) {
case LOG_CRITICAL:
printf ("Critical: ");
break;
case LOG_ERROR:
printf ("Error: ");
break;
case LOG_WARNING:
printf ("Warning: ");
break;
case LOG_NOTICE:
printf ("Notice: ");
break;
case LOG_INFO:
printf ("Info: ");
break;
case LOG_VERBOSE:
printf ("Verbose: ");
break;
case LOG_DEBUG1:
printf ("Debug1: ");
break;
case LOG_DEBUG2:
printf ("Debug2: ");
break;
case LOG_DEBUG3:
printf ("Debug3: ");
break;
default:
printf ("Unknown: ");
break;
}
printed += 11;
printed += vprintf (format, arguments);
printed += printf ("\n");
}
va_end (arguments);
return (printed);
}
// =============================================================================
unsigned int faBuildMarkFlags () {
unsigned int faMarkFlags = 0;
if (optFlagDontFollow == 1) {
if (FAN_MARK_DONT_FOLLOW == 0) {
lprintf (LOG_WARNING, "FAN_MARK_DONT_FOLLOW is not available");
}
faMarkFlags |= FAN_MARK_DONT_FOLLOW;
}
if (optFlagOnlydir == 1) {
if (FAN_MARK_ONLYDIR == 0) {
lprintf (LOG_WARNING, "FAN_MARK_ONLYDIR is not available");
}
faMarkFlags |= FAN_MARK_ONLYDIR;
}
if (optFlagMount == 1) {
if (FAN_MARK_MOUNT == 0) {
lprintf (LOG_WARNING, "FAN_MARK_MOUNT is not available");
}
faMarkFlags |= FAN_MARK_MOUNT;
}
if (optFlagFilesystem == 1) {
if (FAN_MARK_FILESYSTEM == 0) {
lprintf (LOG_WARNING, "FAN_MARK_FILESYSTEM is not available");
}
faMarkFlags |= FAN_MARK_FILESYSTEM;
}
return (faMarkFlags);
}
unsigned int faBuildMarkMask () {
unsigned int faMarkMask = 0;
if (optMaskAccess == 1) {
if (FAN_ACCESS == 0) {
lprintf (LOG_WARNING, "FAN_ACCESS is not available");
}
faMarkMask |= FAN_ACCESS;
}
if (optMaskModify == 1) {
if (FAN_MODIFY == 0) {
lprintf (LOG_WARNING, "FAN_MODIFY is not available");
}
faMarkMask |= FAN_MODIFY;
}
if (optMaskClose == 1) {
if (FAN_CLOSE == 0) {
lprintf (LOG_WARNING, "FAN_CLOSE is not available");
}
faMarkMask |= FAN_CLOSE;
}
if (optMaskCloseWrite == 1) {
if (FAN_CLOSE_WRITE == 0) {
lprintf (LOG_WARNING, "FAN_CLOSE_WRITE is not available");
}
faMarkMask |= FAN_CLOSE_WRITE;
}
if (optMaskCloseNowrite == 1) {
if (FAN_CLOSE_NOWRITE == 0) {
lprintf (LOG_WARNING, "FAN_CLOSE_NOWRITE is not available");
}
faMarkMask |= FAN_CLOSE_NOWRITE;
}
if (optMaskOpen == 1) {
if (FAN_OPEN == 0) {
lprintf (LOG_WARNING, "FAN_OPEN is not available");
}
faMarkMask |= FAN_OPEN;
}
if (optMaskOpenExec == 1) {
if (FAN_OPEN_EXEC == 0) {
lprintf (LOG_WARNING, "FAN_OPEN_EXEC is not available");
}
faMarkMask |= FAN_OPEN_EXEC;
}
if (optMaskAttrib == 1) {
if (FAN_ATTRIB == 0) {
lprintf (LOG_WARNING, "FAN_ATTRIB is not available");
}
faMarkMask |= FAN_ATTRIB;
}
if (optMaskCreate == 1) {
if (FAN_CREATE == 0) {
lprintf (LOG_WARNING, "FAN_CREATE is not available");
}
faMarkMask |= FAN_CREATE;
}
if (optMaskDelete == 1) {
if (FAN_DELETE == 0) {
lprintf (LOG_WARNING, "FAN_DELETE is not available");
}
faMarkMask |= FAN_DELETE;
}
if (optMaskDeleteSelf == 1) {
if (FAN_DELETE_SELF == 0) {
lprintf (LOG_WARNING, "FAN_DELETE_SELF is not available");
}
faMarkMask |= FAN_DELETE_SELF;
}
if (optMaskMove == 1) {
if (FAN_MOVE == 0) {
lprintf (LOG_WARNING, "FAN_MOVE is not available");
}
faMarkMask |= FAN_MOVE;
}
if (optMaskMovedFrom == 1) {
if (FAN_MOVED_FROM == 0) {
lprintf (LOG_WARNING, "FAN_MOVED_FROM is not available");
}
faMarkMask |= FAN_MOVED_FROM;
}
if (optMaskMovedTo == 1) {
if (FAN_MOVED_TO == 0) {
lprintf (LOG_WARNING, "FAN_MOVED_TO is not available");
}
faMarkMask |= FAN_MOVED_TO;
}
if (optMaskQOverflow == 1) {
if (FAN_Q_OVERFLOW == 0) {
lprintf (LOG_WARNING, "FAN_Q_OVERFLOW is not available");
}
faMarkMask |= FAN_Q_OVERFLOW;
}
if (optMaskOpenPerm == 1) {
if (FAN_OPEN_PERM == 0) {
lprintf (LOG_WARNING, "FAN_OPEN_PERM is not available");
}
faMarkMask |= FAN_OPEN_PERM;
}
if (optMaskOpenExecPerm == 1) {
if (FAN_OPEN_EXEC_PERM == 0) {
lprintf (LOG_WARNING, "FAN_OPEN_EXEC_PERM is not available");
}
faMarkMask |= FAN_OPEN_EXEC_PERM;
}
if (optMaskAccessPerm == 1) {
if (FAN_ACCESS_PERM == 0) {
lprintf (LOG_WARNING, "FAN_ACCESS_PERM is not available");
}
faMarkMask |= FAN_ACCESS_PERM;
}
if (optMaskOndir == 1) {
if (FAN_ONDIR == 0) {
lprintf (LOG_WARNING, "FAN_ONDIR is not available");
}
faMarkMask |= FAN_ONDIR;
}
if (optMaskEventOnChild == 1) {
if (FAN_EVENT_ON_CHILD == 0) {
lprintf (LOG_WARNING, "FAN_EVENT_ON_CHILD is not available");
}
faMarkMask |= FAN_EVENT_ON_CHILD;
}
return (faMarkMask);
}
int faInit () {
int fanotify, pathi;
// Init fanotify structure
fanotify = fanotify_init (FAN_CLASS_NOTIF, O_RDONLY);
if (fanotify == -1) {
return (fanotify);
}
for (pathi = 0; optPaths [pathi] != NULL; pathi++) {
if (fanotify_mark (fanotify, FAN_MARK_ADD | faBuildMarkFlags (), faBuildMarkMask (), AT_FDCWD, optPaths [pathi]) == -1) {
return (-1);
}
}
return (fanotify);
}
void faLoop (const int fanotify) {
int buffer_len;
char buffer [4096], mask [256], path [PATH_MAX + 1], exe [PATH_MAX + 1], cmdline [8192];
struct fanotify_event_metadata* metadata;
// Loop for(ever)
for (;;) {
// Read from the fanotify fd
buffer_len = read (fanotify, buffer, sizeof (buffer));
// Extract the metadata from what we just read
metadata = (struct fanotify_event_metadata*) &buffer;
// Process fanotify events
while (FAN_EVENT_OK (metadata, buffer_len)) {
// Overflow?
if (metadata->mask & FAN_Q_OVERFLOW) {
// Shit. Print a warning and move on
lprintf (LOG_WARNING, "Queue overflow");
continue;
}
// Get the name of the file by reading the link of the passed FD
fileByFD (metadata->fd, path, sizeof (path));
// Get the name of the process by reading the exe link for the passed PID
exeByPID (metadata->pid, exe, sizeof (exe));
// Get the command line arguments for the PID
if (cmdlineByPID (metadata->pid, cmdline, sizeof (cmdline)) == -1) {
lprintf (LOG_WARNING, "Could not read commandline for %d: %s", metadata->pid, strerror (errno));
}
// Build a textual version of the operations mask
sprintmask (metadata->mask, mask);
// Print out a line
//printf ("%5d %s (%s = %016llX): %s\n", metadata->pid, exe, mask, metadata->mask, path);
printf (
"pid:%d exe:%s %s%s%sevents:[%s]: %s\n",
metadata->pid,
exe,
(strlen (cmdline) > 0 ? "args:[" : ""),
cmdline,
(strlen (cmdline) > 0 ? "] " : ""),
mask,
path
);
// Close the FD
close (metadata->fd);
// Move on to the next event
metadata = FAN_EVENT_NEXT (metadata, buffer_len);
}
}
}
// =============================================================================
void usage (int exit_status) {
printf ("Usage: %s [OPTIONS] <PATH> [<PATH>...]\n", "fsnoop");
printf ("\n");
printf ("Options:\n");
printf (" --help -h This help\n");
printf (" --verbose -v Increased output verbosity\n");
printf ("\n");
printf (" --cache -C Enable caching of PID:Path information so that closes\n");
printf (" have path information (default)\n");
printf (" --no-cache -c Disable caching of PID:Path information\n");
printf ("\n");
printf ("Path flags:\n");
printf (" --dont-follow Do not follow symlinks specified as paths\n");
printf (" --only-dir Throw an error if a specified path is not a directory\n");
printf (" --mount If specified paths are not mount points, change them\n");
printf (" to the mount points containing the specified path\n");
printf (" --filesystem Add the filesystems containing the specified paths,\n");
printf (" including all mount points for the filesystems\n");
printf ("\n");
printf ("Events:\n");
printf (" --create File or directory created\n");
printf (" --open File or directory opened\n");
printf (" --open-exec File opened with intent to execute\n");
printf (" --access File or directory accessed (read)\n");
printf (" --modify File modified\n");
printf (" --close File or directory closed\n");
printf (" --close-write Writable file closed\n");
printf (" --close-no-write Read-only file or directory closed\n");
printf (" --attrib File or directory metadata modified\n");
printf (" --delete File or directory deleted\n");
printf (" --delete-self File or directory specified as path delete\n");
printf (" --move File or directory moved\n");
printf (" --moved-from File or directory moved from a specified path\n");
printf (" --moved-to File or directory moved into a specified path\n");
printf (" --ondir Apply events to directories as well as files\n");
//printf ("\n");
//printf (" --open-perm File or directory asking to be opened\n");
//printf (" --open-exec_perm File asking to be opened with intent to execute\n");
//printf (" --access-perm File or directory asking to be accessed (read)\n");
printf ("\n");
printf (" --all -a All supported events\n");
printf ("\n");
exit (exit_status);
}
int parseArgs (const int argc, const char **argv) {
struct option long_opts [] = {
// General options
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"all", no_argument, 0, 'a'},
// Caching control
{"cache", no_argument, &optCache, 1},
{"no-cache", no_argument, &optCache, 0},
// fanotify flags
{"dont-follow", no_argument, &optFlagDontFollow, 1},
{"only-dir", no_argument, &optFlagOnlydir, 1},
{"mount", no_argument, &optFlagMount, 1},
{"filesystem", no_argument, &optFlagFilesystem, 1},
// fanotify masks
{"access", no_argument, &optMaskAccess, 1},
{"modify", no_argument, &optMaskModify, 1},
{"close", no_argument, &optMaskClose, 1},
{"close-write", no_argument, &optMaskCloseWrite, 1},
{"close-no-write", no_argument, &optMaskCloseNowrite, 1},
{"open", no_argument, &optMaskOpen, 1},
{"open-exec", no_argument, &optMaskOpenExec, 1},
{"attrib", no_argument, &optMaskAttrib, 1},
{"create", no_argument, &optMaskCreate, 1},
{"delete", no_argument, &optMaskDelete, 1},
{"delete-self", no_argument, &optMaskDeleteSelf, 1},
{"move", no_argument, &optMaskMove, 1},
{"moved-from", no_argument, &optMaskMovedFrom, 1},
{"moved-to", no_argument, &optMaskMovedTo, 1},
{"q-overflow", no_argument, &optMaskQOverflow, 1},
{"open-perm", no_argument, &optMaskOpenPerm, 1},
{"open-exec_perm", no_argument, &optMaskOpenExecPerm, 1},
{"access-perm", no_argument, &optMaskAccessPerm, 1},
{"ondir", no_argument, &optMaskOndir, 1},
{"event-on-child", no_argument, &optMaskEventOnChild, 1},
//{"delete", required_argument, 0, 'd'},
{0, 0, 0, 0}
};
int opt, opt_index, path_index;
while (1) {
opt_index = 0;
opt = getopt_long (argc, (char * const *) argv, "hvcCa", long_opts, &opt_index);
if (opt == -1) {
break;
}
switch (opt) {
case 0:
/* If this option set a flag, do nothing else now. */
if (long_opts [opt_index].flag != 0) {
break;
}
//long_opts [opt_index].name
//optarg
break;
case 'h':
usage (0);
break;
case 'v':
optVerbose++;
break;
case 'a':
optMaskAccess = 1;
optMaskModify = 1;
optMaskClose = 1;
optMaskCloseWrite = 1;
optMaskCloseNowrite = 1;
optMaskOpen = 1;
optMaskOpenExec = 1;
optMaskAttrib = 1;
optMaskCreate = 1;
optMaskDelete = 1;
optMaskDeleteSelf = 1;
optMaskMove = 1;
optMaskMovedFrom = 1;
optMaskMovedTo = 1;
optMaskOndir = 1;
break;
case 'c':
optCache = 0;
break;
case 'C':
optCache = 1;
break;
//case 'c':
// printf ("option -c with value `%s'\n", optarg);
// break;
default:
usage (1);
break;
}
}
if (optind < argc) {
optPaths = calloc (argc - optind + 1, sizeof (*optPaths));
if (optPaths == NULL) {
return (-1);
}
memset (optPaths, 0, (argc - optind + 1) * sizeof (*optPaths));
for (path_index = 0; optind < argc; optind++, path_index++) {
optPaths [path_index] = malloc (strlen (argv [optind]));
if (optPaths [path_index] == NULL) {
return (-1);
}
memset (optPaths [path_index], 0, strlen (argv [optind]) + 1);
strcpy (optPaths [path_index], argv [optind]);
}
} else
usage(1);
return (0);
}
// =============================================================================
int main (const int argc, const char** argv) {
int fanotify;
if (parseArgs (argc, argv) == -1) {
printf ("Error parsing arguments: %s\n", strerror (errno));
return (1);
}
if (optCache) {
if (pidCacheInit () == -1) {
printf ("Error initializing cache: %s\n", strerror (errno));
return (1);
}
}
if ((fanotify = faInit ()) == -1) {
printf ("Error configuring fanotify: %s\n", strerror (errno));
return (1);
}
faLoop (fanotify);
return (1);
}