-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzeCode.py
More file actions
500 lines (400 loc) · 24.4 KB
/
analyzeCode.py
File metadata and controls
500 lines (400 loc) · 24.4 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import string
import tkinter as tk
import os
import mmap
import pickle
from servClass import services
from tkinter import filedialog
from tkinter import messagebox
import sys
# function to print output and call functions used for analyzing * DEBUGGED WITH AI *
def analyze(password, output_widget):
clear_output(output_widget) # Clear previous output in the widget
# password = finalGUIcode.input_box.get() # store user input
if len(password) == 0: # check if something was actually input
output_widget.insert("end", f"Nothing entered try again\n") # tell user to re-enter if length equals 0
return
password = password.replace(' ', '_') # replace all spaces with underscores
# Define a tag for bold text
output_widget.tag_configure("bold", font=("Helvetica", 10, "bold"))
output_widget.insert("end", f"The password entered: {password}\n\n", "bold")
# Analyze length
lenStrength = int(check_length(password, output_widget))
output_widget.insert("end", f"Password length score: {lenStrength}\n\n", "bold")
# Analyze variation
varStrength = check_variation(password, output_widget)
output_widget.insert("end", f"Password variation score: {varStrength}\n\n", "bold")
# Analyze substitutions
mPassword = make_subs(password, output_widget)
# Analyze dictionary
subStength, wordStrength = check_dictionary(password, mPassword, output_widget)
# scores for 3 and 4 are printed in the function
# Analyze names
nameStrength = int(check_names(password, mPassword, output_widget))
output_widget.insert("end", f"Names used score: {nameStrength}\n\n", "bold")
# Analyze places
placeStrength = int(check_places(password, mPassword, output_widget))
output_widget.insert("end", f"Places used score: {placeStrength}\n\n", "bold")
# Analyze dates
dateStrength = int(check_dates(password, mPassword, output_widget))
output_widget.insert("end", f"Dates used score: {dateStrength}\n\n", "bold")
# Analyze numerical/alphabetical strings
stringStrength = int(check_strings(password, mPassword, output_widget))
output_widget.insert("end", f"Numerical/alphabetical strings used score: {stringStrength}\n\n", "bold")
# Compare with rockyou.txt file
rockyouStrength = int(check_rockyou(password, mPassword, output_widget))
output_widget.insert("end", f"Rockyou comparison score: {rockyouStrength}\n\n", "bold")
# Compare with previous input passwords
prevpassStrength = int(check_prev(password, mPassword, output_widget))
output_widget.insert("end", f"Previous password comparison score: {prevpassStrength}\n\n", "bold")
# print out total score
output_widget.insert("end", "Your overall score is calculated as the sum of the above criteria scores: the higher the score, the stronger your password.\n")
overallScore = (lenStrength + varStrength + subStength + wordStrength + nameStrength +
placeStrength + dateStrength + stringStrength + rockyouStrength + prevpassStrength)
output_widget.insert("end", f"The overall score of your password: {overallScore}\n\n", "bold")
# function called to clear the text box used for output
def clear_output(output_widget):
output_widget.delete("1.0", "end")
# 1. The length of the password is a minimum of twelve characters
def check_length(password, output_widget):
pwd_len = len(password) # store password length
lenStrength = 0 # set strength score of the length to 0
# check the length, print to the user how it stands, and update the strength score
if pwd_len >= 12:
output_widget.insert("end", "The length of your password is equal to or above the recommended length of 12\n")
lenStrength = 10
elif pwd_len >= 8:
output_widget.insert("end", "The length of your password is below the recommended length of 12 BUT it is equal to or above what most websites and services require\n")
lenStrength = 5
else:
output_widget.insert("end", "The length of your password doesn't meet the recommended length of 12 OR the minimum requirement of most websites\n")
lenStrength = 0
return(lenStrength) # return the strength score
# 2. The variation in lowercase letters, uppercase letters, special characters, and numbers
def check_variation(password, output_widget):
length = len(password)
varStrength = 10
checkL = 2 # set the check number
lowercase = 0 # set the lowercase counter to 0
uppercase = 0 # set the uppercase counter to 0
digit = 0 # set the number counter to 0
specialchar = 0 # set the special character counter to 0
for char in password: # loop through characters in the password
if char.islower(): # check if lowercase, uppercase, etc.
lowercase += 1 # increase counter
elif char.isupper():
uppercase += 1
elif char.isdigit():
digit += 1
elif char in string.punctuation:
specialchar += 1
# initialize an empty list to store password tips
tips = []
# check and append necessary tips
if lowercase < checkL: # check counter is lower than the predetermined check number
tips.append("Adding more lowercase letters") # append tip if below the predetermined check number
varStrength -= 2.5 # subtract from variation strength score
if uppercase < checkL:
tips.append("Adding more uppercase letters")
varStrength -= 2.5
if digit < checkL:
tips.append("Adding more digits")
varStrength -= 2.5
if specialchar < checkL:
tips.append("Adding more special characters")
varStrength -= 2.5
# decide what to print based on strength score
if varStrength < 10:
output_widget.insert("end", "You should consider:\n") # print tips on what to improve
for tip in tips:
output_widget.insert("end", f"{tip}\n")
else:
varStrength = int(varStrength)
output_widget.insert("end", "Password includes a strong amount of variation\n")
return(varStrength) # return the strength score
# make the subs that will be checked in the next step (ex. 3 instead of e)
def make_subs(password, output_widget):
mPassword = [] # create array for modified password
for char in password: # check each char for commonly used subsitutions
if char == "@":
mPassword.append("a") # append mPasswod with updated sub and loop through the rest
elif char == "0":
mPassword.append("o")
elif char == "3":
mPassword.append("e")
elif char == "!":
mPassword.append("i")
elif char == "z":
mPassword.append("s")
else:
mPassword.append(char) # even if not a sub, still append mPassword
mPassword = ''.join(mPassword) # join array into a string
print(f"The password after replacing common substitutions: {mPassword}\n")
return(mPassword) # return mPassword
# class for reading and comparing with the files needed to check for some of the criteria
class FileComparing:
def __init__(self, file_path, password, mPassword):
self.file_path = file_path
self.password = password
self.mPassword = mPassword
def file_check(self): # check if the file exists
if os.path.exists(self.file_path): # mainly used for debugging
print(f"{self.file_path} exists")
else:
print("File does not exist.")
return False
def compare(self):
file = self.file_path
matching_words = []
matching_words_modified = []
total_words = sum(1 for _ in open(file, encoding="utf-8", errors="ignore")) # estimate progress for debugging
processed = 0 # counter to track words that have been checked
wordStrength = 10
wordStrengthM = 10
# print(f"Modified password for dictionary check: {mPassword.lower()}") # print mPassword for debugging
with open(file, 'r', encoding="utf-8", errors="ignore") as file: # open the file storing dictionary words
dictionary_words = {word.strip().lower() for word in file.readlines()} # store words in a set for faster lookups
for word in dictionary_words:
word = word.strip().lower() # remove any whitespace or newline characters
if "dates" not in self.file_path and (len(word) > len(self.password) or len(word) < 4): # check the length before checking if it's in the password
continue
elif "places" in self.file_path and len(word) < 5:
continue
elif "lang" in self.file_path and len(word) <= len(self.password):
if word in self.password.lower(): # check if the word is in the password
matching_words.append(word) # store the matching word
wordStrength -= 1 # remove a point from the strength score
if word in self.mPassword.lower(): # check if the word is in the password
matching_words_modified.append(word) # store the matching word
wordStrengthM -= 1 # remove a point from the strength score
else:
if word in self.password.lower(): # check if the word is in the password
matching_words.append(word) # store the matching word
wordStrength -= 0.5 # remove a point from the strength score
if word in self.mPassword.lower(): # check if the word is in the password
matching_words_modified.append(word) # store the matching word
wordStrengthM -= 0.5 # remove a point from the strength score
# DEBUGGING
#if processed >= 345000: # if processing halts at 345,000, stop there for debugging
# print(f"Stopping at {processed} for debugging")
# break # temporary break for debugging
#processed += 1 # progress indicator for debugging
#if processed % 2500 == 0: # print progress every 2500 words processed
# print(f"Processed {processed}/{total_words} words...")
# time.sleep(0.01)
# print(f"Found matching dictionary words: ") # begin printing found words for debugging
# for word in matching_words: # loop through words
# match_percentage = int((len(word) / len(self.password)) * 100) # check matching percentage
# print(f"{word}: {match_percentage}")
# print(f"\nFound matching dictionary words for the modified password: ") # begin printing found words for debugging
# for word in matching_words_modified: # loop through words
# match_percentage = int((len(word) / len(self.mPassword)) * 100) # check matching percentage
# print(f"{word}: {match_percentage}")
# DEBUGGING
if self.password == self.mPassword and "lang-english.txt" in self.file_path:
wordStrengthM = 10
return(matching_words, matching_words_modified, wordStrength, wordStrengthM)
# 3. The use of common substitutions of letters (ex. 3 instead of e)
# 4. The use of dictionary words * DEBUGGED WITH AI *
def check_dictionary(password, mPassword, output_widget):
file_path = "lang-english.txt"
matching_words, matching_words_modified, wordStrength, subStrength = FileComparing(file_path, password, mPassword).compare()
common_words = list(set(matching_words) | set(matching_words_modified))
if subStrength == 10 and password != mPassword: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no dictionary words found\n")
elif subStrength == 10 and password == mPassword:
output_widget.insert("end", "No common substitutions were used\n")
else: # if it wasn't a perfect score, print the score and password advice
output_widget.insert("end", "There were dictionary words found after replacing common substitutions\n")
output_widget.insert("end", "With your passwords try to avoid the following: 3 = e/E, 0 = o, @ = a, and ! = i\n")
output_widget.insert("end", f"Common substitution score: {subStrength}\n\n", "bold")
# check the use of dictionary words
if wordStrength == 10: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no dictionary words found in your password\n")
elif wordStrength > 6:
output_widget.insert("end", "There were a couple of dictionary words found in your password\n")
elif wordStrength > 3:
output_widget.insert("end", "There were a handful of dictionary words found in your password\n")
else:
output_widget.insert("end", "There were a lot dictionary words found in your password\n")
output_widget.insert("end", f"Words used score: {wordStrength}\n\n", "bold")
# password advice no matter the score
# print("Keep in mind, there might not be any dictionary words in the original but when modified with common substitutions, there might be.")
return(subStrength, wordStrength)
# 5. The use of names
def check_names(password, mPassword, output_widget):
file_path = "names.txt"
matching_words, matching_words_modified, nameStrength, nameStrengthM = FileComparing(file_path, password, mPassword).compare()
common_names = list(set(matching_words) & set(matching_words_modified))
if nameStrength == 10 and nameStrengthM == 10: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no names found in your password\n")
elif nameStrengthM < 10 and nameStrength == 10:
output_widget.insert("end", f"After replacing common substitutions, the listed names were found: {matching_words_modified}\n")
elif nameStrength < 10 and nameStrengthM == 10:
nameStrength = 0
output_widget.insert("end", f"The listed names were found in your password: {matching_words}\n")
else:
nameStrength = 0
output_widget.insert("end", f"The listed names were found in the original and modified password: {common_names}\n")
overNameStrength = int((nameStrength + nameStrengthM) / 2)
# password advice no matter the score
# print("Keep in mind, there might not be any names in the original but when modified with common substitutions, there might be. And you shouldn't be putting any names in your password.")
return(overNameStrength)
# 6. The use of places
def check_places(password, mPassword, output_widget):
file_path = "places_sanitized.txt"
matching_words, matching_words_modified, placeStrength, placeStrengthM = FileComparing(file_path, password, mPassword).compare()
common_places = list(set(matching_words) & set(matching_words_modified))
if placeStrength == 10 and placeStrengthM == 10: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no places found in your password\n")
elif placeStrengthM < 10 and placeStrength == 10:
output_widget.insert("end", f"The listed places were found in the modified password: {matching_words_modified}\n")
elif placeStrength < 10 and placeStrengthM == 10:
placeStrength = 0
output_widget.insert("end", f"The listed places were found in your password: {matching_words}\n")
else:
placeStrength = 0
output_widget.insert("end", f"The listed places were found in the original and modified password: {common_places}\n")
overPlaceStrength = (placeStrength + placeStrengthM) / 2
# password advice no matter the score
# print("Keep in mind, there might not be any names of places in the original but when modified with common substitutions, there might be. And you shouldn't be putting any names in your password.")
return(overPlaceStrength)
# 7. The use of dates
def check_dates(password, mPassword, output_widget):
file_path = "dates.txt"
matching_dates, matching_dates_modified, dateStrength, dateStrengthM = FileComparing(file_path, password, mPassword).compare()
common_dates = list(set(matching_dates) & set(matching_dates_modified))
# USED FOR DEBUGGING
# print(dateStrength)
# print(dateStrengthM)
if dateStrength == 10 and dateStrengthM == 10: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no dates found in your password\n")
elif dateStrengthM < 0 and dateStrength == 10:
output_widget.insert("end", f"The listed dates were found in the modified password: {matching_dates_modified}\n")
elif dateStrength < 0 and dateStrengthM == 10:
dateStrength = 0
output_widget.insert("end", f"The listed dates were found in your password: {matching_dates}\n")
else:
dateStrength = 0
output_widget.insert("end", f"the listed dates were found in the original and modified password: {common_dates}\n")
# calculate average
overDateStrength = int((dateStrength + dateStrengthM) / 2)
# return calculated score
return(overDateStrength)
# 8. The use of numerical or alphabetical strings (ex. 12345, abcde, etc.)
def check_strings(password, mPassword, output_widget):
file_path = "strings.txt"
matching_words, matching_words_modified, stringStrength, stringStrengthM = FileComparing(file_path, password, mPassword).compare()
common_strings = list(set(matching_words) & set(matching_words_modified))
if stringStrength == 10 and stringStrengthM == 10: # check the strength score and print a message related to the score
output_widget.insert("end", "There were no numerical/alphabetical strings found in your password\n")
elif stringStrengthM < 10 and stringStrength == 10:
output_widget.insert("end", f"The listed numerical/alphabetical strings were found in the modified password: {matching_words_modified}\n")
elif stringStrength < 10 and stringStrengthM == 10:
stringStrength = 0
output_widget.insert("end", f"The listed numerical/alphabetical strings were found in your password: {matching_words}\n")
else:
stringStrength = 0
output_widget.insert("end", f"The listed numerical/alphabetical strings were found in the original and modified password: {common_strings}\n")
# calculate average
overStringStrength = (stringStrength + stringStrengthM) / 2
# return calculated score
return(overStringStrength)
# 9. Compare with rockyou.txt * DEBUGGED WITH AI *
def check_rockyou(password, mPassword, output_widget):
word_strengths = [10, 10] # [Original password strength, Modified password strength]
matching_words = []
matching_words_modified = []
fileFound = False
try:
with open("rockyou.txt", "r", encoding="utf-8", errors="ignore") as file:
# Memory-map the file for efficient reading
fileFound = True
with mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as mmapped_file:
# begin comparing and adjusting score
for line in iter(mmapped_file.readline, b""):
word = line.decode("utf-8", errors="ignore").strip().lower()
if not word or len(word) < 4:
continue
if word in password.lower() and word not in matching_words:
matching_words.append(word)
word_strengths[0] = max(0, word_strengths[0] - 1)
if word in mPassword.lower() and word not in matching_words_modified:
matching_words_modified.append(word)
word_strengths[1] = max(0, word_strengths[1] - 1)
except FileNotFoundError:
word_strengths[0] = 0
word_strengths[1] = 0
# Find common words in both lists
common_words = list(set(matching_words) & set(matching_words_modified))
# Output results
if word_strengths[0] == 10 and word_strengths[1] == 10:
output_widget.insert("end", "There were no matching passwords in rockyou.txt found in your password.\n")
elif word_strengths[1] < 10 and word_strengths[0] == 10:
output_widget.insert("end", "There were matching passwords in rockyou.txt found in your modified password.\n")
elif word_strengths[0] < 10 and word_strengths[1] == 10:
output_widget.insert("end", "There were matching passwords in rockyou.txt found in your password.\n")
elif fileFound == False:
output_widget.insert("end", "rockyou.txt wasn't found, wasn't able to compare the input, automatically setting the score to zero.\n")
else:
output_widget.insert("end", f"There were matching passwords in rockyou.txt found in both original and modified passwords.\n")
# Calculate average strength
over_word_strength = sum(word_strengths) / 2
return over_word_strength
# 10. Compare with previous inputs
def check_prev(password, mPassword, output_widget):
word_strengths = [10, 10] # [Original password strength, Modified password strength]
matching_words = []
matching_words_modified = []
try:
# open passwords file
with open("passwords.pickle", "rb") as file:
entries = pickle.load(file)
# begin comparing strings
for entry in entries:
if entry.password == password:
# matching_words.append(word)
word_strengths[0] = max(0, word_strengths[0] - 1)
if entry.password == mPassword:
# matching_words_modified.append(word)
word_strengths[1] = max(0, word_strengths[1] - 1)
except FileNotFoundError:
output_widget.insert("end", "No previous passwords saved.\n")
return 10
# Output results
if word_strengths[0] == 10 and word_strengths[1] == 10:
output_widget.insert("end", "There were no matching passwords entered previously.\n")
elif word_strengths[1] < 10 and word_strengths[0] == 10:
output_widget.insert("end", "There were matching passwords entered previously found in your modified password.\n")
elif word_strengths[0] < 10 and word_strengths[1] == 10:
output_widget.insert("end", "There were matching passwords entered previously found in your password.\n")
else:
output_widget.insert("end", f"There were matching passwords entered previously found in both original and modified passwords.\n")
# Calculate average strength
over_word_strength = sum(word_strengths) / 2
return over_word_strength
def export_password(output_widget):
# open dialog for saving file
file_path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All files", "*.*")]
)
try:
# save text from file
if file_path:
# Retrieve all text from the output widget
output_text = output_widget.get("1.0", "end-1c") # Get all text, excluding the trailing newline
with open(file_path, 'w') as file:
file.write(output_text) # Write text to the selected file
print(f"Output successfully saved to {file_path}") # Optional: for debugging purposes
except FileNotFoundError as e:
messagebox.showerror("Error", f"File not found: {e}")
except IOError as e:
messagebox.showerror("Error", f"An I/O error occurred: {e}")
except Exception as e:
# Log or display the error message for unexpected exceptions
error_message = f"An unexpected error occurred: {e}"
print(error_message) # Log error to console (optional)
messagebox.showerror("Error", error_message)
# Optionally, use sys to print more details about the exception
sys.print_exception(e)