-
Notifications
You must be signed in to change notification settings - Fork 2
/
Code.py
293 lines (236 loc) · 10.1 KB
/
Code.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
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
import glob
import hashlib
import os
import tkinter as tk
from tkinter.filedialog import askdirectory
import customtkinter as ctk
baselines = r"C:\Users\DELL\Desktop\Baselines"#Baseline.txt will be at this specified path
secure_path = ""
name_hash=""
baseline_path=""
files_changed = []
files_added = []
files_removed = []
files_all = []
spaces = " \n"
#Calculate hash from data in a file
def CalculateSha512Hash(file_name):
# BUF_SIZE is totally arbitrary, change as per your requirement
BUF_SIZE = 65536 # 65536 lets read stuff in 64kb chunks!
sha = hashlib.sha512()
with open(file_name,'rb') as file:
while True:
data = file.read(BUF_SIZE)
if not data:
break
sha.update(data)
# print("SHA: {0}".format(sha.hexdigest()))
return sha.hexdigest()
#Calculate hash from name of a file
def CalculateNameHash(filename):
md5 = hashlib.md5()
md5.update(filename.encode())
return md5.hexdigest()
#Updates baseline
def UpdateBaseline(dir,mode):
if dir=="":
label3.configure(text="Error : Folder not selected")
elif os.path.isdir(baselines)==False:
label3.configure(text="Message : Baselines Folder doesn't exists, so creating it")
os.makedirs(baselines)
label3.configure(text="Message : Updating Baseline...")
UpdateBaselineHelper(dir,mode)
label3.configure(text="Message : Updated Baseline Successfully")
else:
label3.configure(text="Message : Updating Baseline...")
UpdateBaselineHelper(dir,mode)
label3.configure(text="Message : Updated Baseline Successfully")
#Update Baseline Helper for [files in a folder] and [files in subfolders]
def UpdateBaselineHelper(dir,mode):
global name_hash,baseline_path
if(mode=='w'):
name_hash = CalculateNameHash(dir)
baseline_path = os.path.join(baselines,(name_hash+'.txt'))
files = [os.path.abspath(f) for f in glob.glob(os.path.join(dir,'*')) if os.path.isfile(f)]
with open(baseline_path,mode) as baseline:
for f in files:
hash = CalculateSha512Hash(os.path.join(dir,f))
baseline.write(f)
baseline.write("=")
baseline.write(str(hash))
baseline.write("\n")
directories = [d for d in glob.glob(os.path.join(dir,'*')) if os.path.isdir(d)]
for d in directories:
UpdateBaselineHelper(d,'a')
#Returns dictionary containing keys as file name and values as their hashes
def getKeyHashesFromBaseline():
global name_hash,baseline_path
dict = {}
with open(baseline_path,'r') as baseline:
for line in baseline:
key,value = line.split('=')
dict[key] = value[:-1]
return dict
#clears data in all 4 lists
def ClearData():
files_changed.clear()
files_added.clear()
files_removed.clear()
files_all.clear()
fc.configure(text="Files Changed :"+spaces)
fa.configure(text="Files Added :"+spaces)
fr.configure(text="Files Removed :"+spaces)
#Calculates hashes and Checks with the baseline
def CheckIntegrity(dir,number):
ClearData()#Clear data in all 4 lists
if dir=="":
label3.configure(text="Error : Folder not selected")
else:
CheckIntegrityHelper(dir,number)
# print("Files Changed:",files_changed)
# print("Files Added:",files_added)
# print("Files Removed:",files_removed)
fc.configure(text=fc.text+ '\n'.join(files_changed))
fa.configure(text=fa.text+ '\n'.join(files_added))
fr.configure(text=fr.text+ '\n'.join(files_removed))
label3.configure(text="Message : Integrity Checked Successfully")
#Helper () for Check Integrity
def CheckIntegrityHelper(dir,number):
global name_hash,baseline_path
if(number):
name_hash = CalculateNameHash(dir)
baseline_path = os.path.join(baselines,(name_hash+'.txt'))
try:
with open(baseline_path,'r') as baseline:
random=99
except IOError:
label3.configure(text='Error : Baseline file for specified folder not present')
return
files = [os.path.abspath(f) for f in glob.glob(os.path.join(dir,'*')) if os.path.isfile(f)]
for x in files:
files_all.append(x)
dict = getKeyHashesFromBaseline()
for f in files:
#Checking for changed files
temp_hash = CalculateSha512Hash(os.path.join(dir,f))
if str(os.path.join(dir,f)) in dict.keys() and temp_hash!=dict[f]:
files_changed.append(os.path.abspath(f).replace(os.path.abspath(folder),"."))
#Checking for added files
if str(os.path.join(dir,f)) not in dict.keys():
files_added.append(os.path.abspath(f).replace(os.path.abspath(folder),"."))
directories = [d for d in glob.glob(os.path.join(dir,'*')) if os.path.isdir(d)]
for d in directories:
CheckIntegrityHelper(d,0)
if number==1:
#checking for removed files
for x in list(dict.keys()):
if x not in files_all:
files_removed.append(os.path.abspath(x).replace(os.path.abspath(folder),"."))
################################# GUI #################################
ctk.set_appearance_mode("dark") # Modes: system (default), light, dark
ctk.set_default_color_theme("dark-blue") # Themes: blue (default), dark-blue, green
#Some Variables
font_data = ("Raleway",14)
label_text_clr = "#FFCF00"
btn_fg_clr = "#27ab55"
btn_text_clr = "#000000"
btn_hover_clr = "#148f3f"
error_label_clr = "#E94F37"
folder=""
#initialising root window
root = ctk.CTk()
root.title(" HashLine - A File Integrity Monitor")
root.geometry("650x700")#(x,y)
# root.geometry("")
#root.resizable(False,True)
root.wm_iconphoto(False,tk.PhotoImage(file="images/window_icon.png"))
#label1 : Monitor folder
label1 = ctk.CTkLabel(master=root,
text="Select a Folder",
text_font=font_data,
text_color=label_text_clr)
label1.place(relx=0.35, y=40,anchor=tk.CENTER)#absolute placing
#browse button
def open_file():
label3.configure(text="Message : ")
label2.configure(text="(Selected Folder path will appear here)")
global folder
folder = askdirectory(parent=root, title="Choose a folder")
if folder:
label3.configure(text="Message : Folder Selected Successfully")
label2.config(text=folder)
ClearData()
browse_btn = ctk.CTkButton( master=root,
text="Browse",
image=tk.PhotoImage(file="images/browse.png").subsample(10,10),
compound=ctk.RIGHT,
command=open_file,
fg_color=btn_fg_clr,
text_color=btn_text_clr,
text_font=font_data,
hover_color=btn_hover_clr,
height=40,
width=125 )
browse_btn.place(relx=0.7,y=40,anchor=tk.CENTER)
#Label2 : Selected Folder Path
label2 = ctk.CTkLabel(master=root,
text="(Selected Folder path will appear here)",
wraplength=500,
text_font=font_data,
text_color=label_text_clr)
label2.place(relx=0.5,y=110,anchor=tk.CENTER)
#Button : Update Baseline
update_baseline_btn = ctk.CTkButton( master=root,
text="Update Baseline ",
image=tk.PhotoImage(file="images/updatebaseline.png").subsample(18,18),
compound=ctk.RIGHT,
command=lambda:UpdateBaseline(folder,'w'),
fg_color=btn_fg_clr,
text_color=btn_text_clr,
text_font=font_data,
hover_color=btn_hover_clr,
height=40,
width=150 )
update_baseline_btn.place(relx=0.5, y=190, anchor=tk.CENTER)
#Button : Check Integrity
check_integrity_btn = ctk.CTkButton( master=root,
text="Check Integrity",
image=tk.PhotoImage(file="images/checkintegrity.png").subsample(9,9),
compound=ctk.RIGHT,
command=lambda:CheckIntegrity(folder,1),
fg_color=btn_fg_clr,
text_color=btn_text_clr,
text_font=font_data,
hover_color=btn_hover_clr,
height=40,
width=125 )
check_integrity_btn.place(relx=0.5, y=260, anchor=tk.CENTER)
#label3 : Message Label
label3 = ctk.CTkLabel( master=root,
text="Message : ",
text_font=font_data,
text_color=error_label_clr)
label3.pack(fill="both", expand=True)
label3.place(relx=0.5,y=310,anchor=tk.CENTER)
#label4 : Changed Files
fc = ctk.CTkLabel( master=root,
text="Files Changed :"+spaces,
text_font=font_data,
text_color=label_text_clr)
fc.pack(fill="both", expand=True)
fc.place(relx=0.5,y=370,anchor=tk.CENTER)
#label4 : Added Files
fa = ctk.CTkLabel( master=root,
text="Files Added : "+spaces,
text_font=font_data,
text_color=label_text_clr)
fa.pack(fill="both", expand=True)
fa.place(relx=0.5,y=470,anchor=tk.CENTER)
#label4 : Removed Files
fr = ctk.CTkLabel( master=root,
text="Files Removed : "+spaces,
text_font=font_data,
text_color=label_text_clr)
fr.pack(fill="both", expand=True)
fr.place(relx=0.5,y=570,anchor=tk.CENTER)
root.mainloop()