forked from 0x413x4/watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py
executable file
·155 lines (131 loc) · 4.41 KB
/
watcher.py
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
#!/usr/bin/env python3
# @0x413x4 - 2019
""" watcher.py
Small program to monitor linux filesystem events. It is based on the inotify
linux kernel API, and can be used to for various purposes such as reverse
engineering and penetration testing.
Usage:
watcher.py [options] <directory>
"""
__author__ = "alexa"
__version__ = "1.0"
__status__ = "dev"
__licence__ = "MIT"
import argparse
import colorama
import inotify.adapters
import os
# Define colours for events
colorama.init(autoreset=True)
_EVENTS = {
'IN_ACCESS': colorama.Fore.WHITE + '[!] {} accessed: {}',
'IN_ATTRIB': colorama.Fore.CYAN + '[!] {} metadata has changed: {}',
'IN_CLOSE_NOWRITE': colorama.Fore.WHITE + '[!] {} closed {}',
'IN_CLOSE_WRITE': colorama.Fore.WHITE + '[!] {} closed {}',
'IN_CREATE': colorama.Fore.GREEN + '[+] {} created: {}',
'IN_DELETE': colorama.Fore.RED + '[-] {} deleted: {}',
'IN_DELETE_SELF': colorama.Fore.RED + '[-] File monitored deleted',
'IN_MODIFY': colorama.Fore.YELLOW + '[!] {} modified: {}',
'IN_MOVED_FROM': colorama.Fore.YELLOW + '[+] {} moved from: {}',
'IN_MOVED_TO': colorama.Fore.YELLOW + '[+] {} moved to: {}',
'IN_MOVE_SELF': colorama.Fore.YELLOW + '[+] {} monitored moved: {}',
'IN_OPEN': colorama.Fore.WHITE + '[!] {} opened: {}'
}
_DEFAULT_EVENTS = [
'IN_CREATE',
'IN_DELETE',
'IN_DELETE_SELF',
'IN_MOVED_FROM',
'IN_MOVED_TO',
'IN_MOVE_SELF'
]
_VERBOSE_EVENTS = [
'IN_CREATE',
'IN_DELETE',
'IN_DELETE_SELF',
'IN_MOVED_FROM',
'IN_MOVED_TO',
'IN_MOVE_SELF',
'IN_MODIFY',
'IN_ATTRIB'
]
_ALL_EVENTS = [
'IN_ACCESS',
'IN_ATTRIB',
'IN_CLOSE_NOWRITE',
'IN_CLOSE_WRITE',
'IN_CREATE',
'IN_DELETE',
'IN_DELETE_SELF',
'IN_MODIFY',
'IN_MOVED_FROM',
'IN_MOVED_TO',
'IN_MOVE_SELF',
'IN_OPEN'
]
def monitor(directory, recursive=False, events=_DEFAULT_EVENTS):
# Check directory exist and is readable
if not os.path.exists(directory):
print('[!] ERROR: {} does not exist'.format(directory))
exit(1)
# Event subscription
if recursive:
watcher = inotify.adapters.InotifyTree(directory)
else:
watcher = inotify.adapters.Inotify()
watcher.add_watch(directory)
# Print events as they come
for e in watcher.event_gen(yield_nones=False):
(_, tn, path, filename) = e
if tn[0] in events and 'IN_EXCL_UNLINK' not in tn:
f = os.path.join(path, filename)
if 'IN_ISDIR' in tn:
print(_EVENTS[tn[0]].format('directory', f))
else:
print(_EVENTS[tn[0]].format('file', f))
if __name__ == '__main__':
_description = 'Monitor filesystem changes on Linux'
parser = argparse.ArgumentParser(description=_description)
# Directory
parser.add_argument('directory',
action='store',
help='directory to monitor')
# Recursive watching
parser.add_argument('-r', '--recursive',
action='store_true',
help='enable recursive watching')
# Events
group = parser.add_mutually_exclusive_group()
group.add_argument('-v', '--verbose', action='store_true')
group.add_argument('-a', '--all', action='store_true')
group.add_argument('--events', nargs='*',
action='store', choices=_ALL_EVENTS,
help='individual events to monitor')
# Prepare options
args = parser.parse_args()
if args.events is not None:
events = args.events
elif args.verbose:
events = _VERBOSE_EVENTS
elif args.all:
events = _ALL_EVENTS
else:
events = _DEFAULT_EVENTS
cm = colorama.Fore.MAGENTA
ct = colorama.Fore.RED
print(ct + '=============================================================')
print(ct + ' WATCHER.PY')
print(ct + ' @0x413x4 - 2019')
print(ct + '=============================================================')
print
print(cm + '\nConfiguration:')
print(cm + "|--> Directory watched : {}".format(args.directory))
print(cm + "|--> Recursive watching: {}".format(args.recursive))
print(cm + "|--> Events monitored: ")
for e in events:
print(cm + " * {}".format(e))
print('')
try:
monitor(args.directory, args.recursive, events)
except KeyboardInterrupt:
print('Stopped monitoring')