Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (135 loc) · 5.22 KB
/
Copy pathmain.py
File metadata and controls
190 lines (135 loc) · 5.22 KB
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
import watchdog.observers
from watchdog.events import FileSystemEventHandler
import time
import hashlib
import os
created_count = 0
modified_count = 0
deleted_count = 0
file_hashes = {}
def write_log(message):
with open("logs.txt", "a") as file:
file.write(message + "\n")
def write_alert(message):
with open("alerts.txt", "a") as file:
file.write(message + "\n")
def save_report():
with open("report.txt", "a") as file:
file.write("\n===== SESSION REPORT =====\n")
file.write(f"Date: {time.ctime()}\n")
file.write(f"Files Created : {created_count}\n")
file.write(f"Files Modified: {modified_count}\n")
file.write(f"Files Deleted : {deleted_count}\n")
file.write("=========================\n")
sensitive_files = [
"passwords.txt",
"salary.xlsx",
"confidential.txt"
]
def calculate_hash(filepath):
with open(filepath, "rb") as file:
data = file.read()
return hashlib.sha256(data).hexdigest()
def load_existing_hashes(folder):
for root, dirs, files in os.walk(folder):
for file in files:
filepath = os.path.join(root, file)
try:
file_hashes[filepath] = calculate_hash(filepath)
except:
pass
class MonitorHandler(FileSystemEventHandler):
def on_created(self, event):
if event.src_path.endswith("desktop.ini"):
return
if not event.is_directory:
global created_count
created_count += 1
message = f"{time.ctime()} | File Created: {event.src_path}"
print(message)
write_log(message)
try:
file_hashes[event.src_path] = calculate_hash(event.src_path)
print("File hash stored")
except PermissionError:
print("File busy, hash skipped")
print(f"Total Files Created: {created_count}")
def on_modified(self, event):
if event.src_path.endswith("desktop.ini"):
return
if event.src_path.endswith("logs.txt"):
return
if event.src_path.endswith("alerts.txt"):
return
if event.src_path.endswith("report.txt"):
return
if not event.is_directory:
global modified_count
modified_count += 1
message = f"{time.ctime()} | File Modified: {event.src_path}"
filename = event.src_path.split("\\")[-1]
if event.src_path in file_hashes:
try:
old_hash = file_hashes[event.src_path]
new_hash = calculate_hash(event.src_path)
if old_hash != new_hash:
print(f"🚨 INTEGRITY ALERT: {filename}")
print(f"Old SHA256 : {old_hash}")
print(f"New SHA256 : {new_hash}")
write_log(f"Old SHA256 : {old_hash}")
write_log(f"New SHA256 : {new_hash}")
file_hashes[event.src_path] = new_hash
except PermissionError:
print("File busy, hash skipped")
else:
try:
file_hashes[event.src_path] = calculate_hash(event.src_path)
print("File hash stored")
except PermissionError:
print("File busy, hash skipped")
if filename in sensitive_files:
alert_message = (
f"{time.ctime()} | "
f"Sensitive File Modified - {filename}"
)
print(f"🚨 {alert_message}")
write_alert(alert_message)
print(message)
write_log(message)
print(f"Total Files Modified: {modified_count}")
def on_deleted(self, event):
if event.src_path.endswith("desktop.ini"):
return
if not event.is_directory:
global deleted_count
deleted_count += 1
message = f"{time.ctime()} | File Deleted: {event.src_path}"
filename = event.src_path.split("\\")[-1]
if filename in sensitive_files:
alert_message = (
f"{time.ctime()} | "
f"Sensitive File Deleted - {filename}"
)
print(f"🚨 {alert_message}")
write_alert(alert_message)
print(message)
write_log(message)
print(f"Total Files Deleted: {deleted_count}")
event_handler = MonitorHandler()
observer = watchdog.observers.Observer()
path = "test_files"
load_existing_hashes(path)
observer.schedule(event_handler, path, recursive=True)
observer.start()
print("Monitoring Started...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n===== SESSION REPORT =====")
print(f"Files Created : {created_count}")
print(f"Files Modified: {modified_count}")
print(f"Files Deleted : {deleted_count}")
save_report()
observer.stop()
observer.join()