|
1 | | -# Local_file_monitor |
| 1 | +# file monitor |
2 | 2 |
|
3 | | -It monitors local files |
| 3 | +It monitors files |
4 | 4 |
|
| 5 | +## Dir structure |
| 6 | + |
| 7 | +``` |
| 8 | +. |
| 9 | +├── CHANGELOG.json |
| 10 | +├── CMakeLists.txt |
| 11 | +├── common.h |
| 12 | +├── data_types.h |
| 13 | +├── headers.h |
| 14 | +├── helpers |
| 15 | +│ ├── delete.h |
| 16 | +│ ├── read_write.h |
| 17 | +│ └── rename.h |
| 18 | +├── monitor.c |
| 19 | +├── README.md |
| 20 | +├── VERSION.txt |
| 21 | +└── vmlinux.h |
| 22 | +``` |
| 23 | + |
| 24 | +## BPF traces |
| 25 | + |
| 26 | +### VFS Read |
| 27 | + |
| 28 | +```sh |
| 29 | +sudo bpftrace -e 'kprobe:vfs_read { |
| 30 | + $file = (struct file *)arg0; |
| 31 | + $dentry = $file->f_path.dentry; |
| 32 | + |
| 33 | + if ($dentry != 0) { |
| 34 | + printf("READ - File: %s, Size: %d bytes, PID: %d, Comm: %s\n", |
| 35 | + str($dentry->d_name.name), |
| 36 | + arg2, // count/size parameter |
| 37 | + pid, |
| 38 | + comm); |
| 39 | + |
| 40 | + $parent = $dentry->d_parent; |
| 41 | + if ($parent != 0) { |
| 42 | + printf(" Parent dir: %s\n", str($parent->d_name.name)); |
| 43 | + } |
| 44 | + } |
| 45 | +}' |
| 46 | +``` |
| 47 | + |
| 48 | +### VFS Write |
| 49 | + |
| 50 | +```sh |
| 51 | +sudo bpftrace -e 'kprobe:vfs_write { |
| 52 | + $file = (struct file *)arg0; |
| 53 | + $dentry = $file->f_path.dentry; |
| 54 | + |
| 55 | + if ($dentry != 0) { |
| 56 | + printf("WRITE - File: %s, Size: %d bytes, PID: %d, Comm: %s\n", |
| 57 | + str($dentry->d_name.name), |
| 58 | + arg2, // count/size parameter |
| 59 | + pid, |
| 60 | + comm); |
| 61 | + |
| 62 | + $parent = $dentry->d_parent; |
| 63 | + if ($parent != 0) { |
| 64 | + printf(" Parent dir: %s\n", str($parent->d_name.name)); |
| 65 | + } |
| 66 | + } |
| 67 | +}' |
| 68 | +``` |
| 69 | + |
| 70 | +### VFS Rename |
| 71 | + |
| 72 | +```sh |
| 73 | +sudo bpftrace -e 'kprobe:vfs_rename { |
| 74 | + $old_dentry = (struct dentry *)arg1; |
| 75 | + $new_dentry = (struct dentry *)arg3; |
| 76 | + |
| 77 | + if ($old_dentry != 0 && $new_dentry != 0) { |
| 78 | + printf("RENAME - From: %s, To: %s, PID: %d, Comm: %s\n", |
| 79 | + str($old_dentry->d_name.name), |
| 80 | + str($new_dentry->d_name.name), |
| 81 | + pid, |
| 82 | + comm); |
| 83 | + |
| 84 | + $old_parent = $old_dentry->d_parent; |
| 85 | + $new_parent = $new_dentry->d_parent; |
| 86 | + |
| 87 | + if ($old_parent != 0) { |
| 88 | + printf(" Source dir: %s\n", str($old_parent->d_name.name)); |
| 89 | + } |
| 90 | + |
| 91 | + if ($new_parent != 0) { |
| 92 | + printf(" Target dir: %s\n", str($new_parent->d_name.name)); |
| 93 | + } |
| 94 | + } |
| 95 | +}' |
| 96 | +``` |
| 97 | + |
| 98 | +### VFS Unlink |
| 99 | + |
| 100 | +```sh |
| 101 | +sudo bpftrace -e 'kprobe:vfs_unlink { |
| 102 | + $dentry = (struct dentry *)arg2; |
| 103 | + printf("dentry ptr: %p, d_name.len: %d, d_name.name_ptr: %s\n", |
| 104 | + $dentry, $dentry->d_name.len, str($dentry->d_name.name)); |
| 105 | + |
| 106 | + $parent = $dentry->d_parent; |
| 107 | + if ($parent != 0) { |
| 108 | + printf("Parent name: %s\n", str($parent->d_name.name)); |
| 109 | + } else { |
| 110 | + printf("Parent: NULL\n"); |
| 111 | + } |
| 112 | +}' |
| 113 | +``` |
0 commit comments