-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_monitor1.py
80 lines (69 loc) · 2.62 KB
/
file_monitor1.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
# Modified example, original given here:
# http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
import os
import tempfile
import threading
import win32con
import win32file
FILE_CREATED = 1
FILE_DELETED = 2
FILE_MODIFIED = 3
FILE_RENAMED_FROM = 4
FILE_RENAMED_TO = 5
FILE_LIST_DIRECTORY = 0x0001
PATHS = ['c:\\Windows\\Temp', tempfile.gettempdir()]
def monitor(path_to_watch):
h_directory = win32file.CreateFile(
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
while True:
try:
results = win32file.ReadDirectoryChangesW(
h_directory,
1024,
True,
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY |
win32con.FILE_NOTIFY_CHANGE_SIZE,
None,
None
)
for action, file_name in results:
full_filename = os.path.join(path_to_watch, file_name)
if action == FILE_CREATED:
print(f'[+] Created {full_filename}')
elif action == FILE_DELETED:
print(f'[-] Deleted {full_filename}')
elif action == FILE_MODIFIED:
print(f'[*] Modified {full_filename}')
try:
with open(full_filename) as f:
contents = f.read()
print('[vvv] Dumping contents ... ')
print(contents)
print('[^^^] Dump complete.')
except Exception as e:
print(f'[!!!] Dump failed. {e}')
elif action == FILE_RENAMED_FROM:
print(f'[>] Renamed from {full_filename}')
elif action == FILE_RENAMED_TO:
print(f'[<] Renamed to {full_filename}')
else:
print(f'[?] Unknown action on {full_filename}')
except KeyboardInterrupt:
break
except Exception:
pass
if __name__ == '__main__':
for path in PATHS:
monitor_thread = threading.Thread(target=monitor, args=(path,))
monitor_thread.start()