Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import hashlib
  2. import os
  3. import signal
  4. import sys
  5.  
  6. from pandas.io import sql as psql
  7. from sqlalchemy import create_engine, text
  8. from utils.os_specific_functions import FOLDER_SEPARATOR
  9.  
  10. LOWER_CASE = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  11. UPPER_CASE = [letter.upper() for letter in LOWER_CASE]
  12. NUMBERS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  13. SPECIAL_CHARACTERS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '<', '>', '?', '/', '\\', '[', ']', '{', '}', '|', '~', '`', ';', ':']
  14.  
  15. IGNORED_FILE = 'Instructions.md'
  16. SQLITE_DB_FILE = 'common_passwords.sqlite'
  17.  
  18. base_directory = os.getcwd() + FOLDER_SEPARATOR + 'auth_tools'
  19.  
  20. def create_sqlite_engine():
  21. db_uri = 'sqlite:///' + base_directory + FOLDER_SEPARATOR + SQLITE_DB_FILE
  22. # Configure pool_size, max_overflow, and pool_timeout to handle connection loads gracefully
  23. sqlite_engine = create_engine(
  24. url=db_uri,
  25. pool_size=20,
  26. max_overflow=0,
  27. pool_timeout=30,
  28. pool_recycle=1800
  29. )
  30. return sqlite_engine
  31.  
  32. def query_db(query):
  33. response = psql.read_sql_query(query, con=sqlite_engine)
  34. return response
  35.  
  36.  
  37. def execute_db(query):
  38. with sqlite_engine.connect() as conn, conn.begin():
  39. conn.execute(text(query))
  40.  
  41. def create_md5_hash(word):
  42. hashed = hashlib.md5(word.encode())
  43. hash_text = hashed.hexdigest()
  44. return hash_text
  45.  
  46. def add_password_heuristics():
  47. common_passwords = []
  48. heuristics_folder = base_directory + FOLDER_SEPARATOR + 'add_password_heuristics'
  49. heuristic_files = os.listdir(heuristics_folder)
  50. if IGNORED_FILE in heuristic_files:
  51. heuristic_files.remove(IGNORED_FILE)
  52. if len(heuristic_files) == 0:
  53. return
  54. i = 0
  55. for heuristic_file in heuristic_files:
  56. if heuristic_file == IGNORED_FILE:
  57. continue
  58. print(f'Adding new definitions from {heuristic_file}')
  59. file_loc = heuristics_folder + FOLDER_SEPARATOR + heuristic_file
  60. with open(file_loc, 'r', encoding='utf-8') as file:
  61. for line in file:
  62. clean_line = line.strip()
  63. i += 1
  64. if i % 1000 == 0 and i != 1:
  65. print(f'{int(i / 1000)}k passwords collected')
  66. if clean_line not in common_passwords and simple_check(clean_line):
  67. common_passwords.append(clean_line)
  68. i = 0
  69. print(f'Adding {len(common_passwords)} Passwords to DB.')
  70. for password in common_passwords:
  71. hashed_pass = create_md5_hash(password)
  72. query = query_db(f"SELECT id FROM common_passwords WHERE password_hash = '{hashed_pass}'")
  73. if len(query) == 0:
  74. i += 1
  75. execute_db(f"INSERT INTO common_passwords(password_hash) VALUES('{hashed_pass}')")
  76. if i % 1000 == 0:
  77. print(f'{int(i / 1000)}k passwords added to db')
  78. for heuristic_file in heuristic_files:
  79. os.remove(heuristics_folder + FOLDER_SEPARATOR + heuristic_file)
  80.  
  81. def simple_check(password):
  82. if len(password) < 8:
  83. return False
  84. contains_lower, contains_upper, contains_special = False, False, False
  85. for letter in password:
  86. if letter in LOWER_CASE:
  87. contains_lower = True
  88. elif letter in UPPER_CASE:
  89. contains_upper = True
  90. elif letter in SPECIAL_CHARACTERS:
  91. contains_special = True
  92. if contains_lower and contains_upper and contains_special:
  93. return True
  94. return False
  95.  
  96.  
  97.  
  98. def confirm_strong_password(password):
  99. if not simple_check(password):
  100. return False
  101. hashed_pass = create_md5_hash(password)
  102. query = query_db(f"SELECT id FROM common_passwords WHERE password_hash = '{hashed_pass}'")
  103. if len(query) != 0:
  104. return False
  105. return True
  106.  
  107. def graceful_exit(signum, frame):
  108. sqlite_engine.dispose()
  109. print(f'Disposing SQL Engine')
  110. sys.exit(0)
  111.  
  112. signal.signal(signal.SIGINT, graceful_exit)
  113. signal.signal(signal.SIGTERM, graceful_exit)
  114.  
  115. sqlite_engine = create_sqlite_engine()
  116.  
  117. add_password_heuristics()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement