Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import os
- import signal
- import sys
- from pandas.io import sql as psql
- from sqlalchemy import create_engine, text
- from utils.os_specific_functions import FOLDER_SEPARATOR
- 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']
- UPPER_CASE = [letter.upper() for letter in LOWER_CASE]
- NUMBERS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
- SPECIAL_CHARACTERS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '<', '>', '?', '/', '\\', '[', ']', '{', '}', '|', '~', '`', ';', ':']
- IGNORED_FILE = 'Instructions.md'
- SQLITE_DB_FILE = 'common_passwords.sqlite'
- base_directory = os.getcwd() + FOLDER_SEPARATOR + 'auth_tools'
- def create_sqlite_engine():
- db_uri = 'sqlite:///' + base_directory + FOLDER_SEPARATOR + SQLITE_DB_FILE
- # Configure pool_size, max_overflow, and pool_timeout to handle connection loads gracefully
- sqlite_engine = create_engine(
- url=db_uri,
- pool_size=20,
- max_overflow=0,
- pool_timeout=30,
- pool_recycle=1800
- )
- return sqlite_engine
- def query_db(query):
- response = psql.read_sql_query(query, con=sqlite_engine)
- return response
- def execute_db(query):
- with sqlite_engine.connect() as conn, conn.begin():
- conn.execute(text(query))
- def create_md5_hash(word):
- hashed = hashlib.md5(word.encode())
- hash_text = hashed.hexdigest()
- return hash_text
- def add_password_heuristics():
- common_passwords = []
- heuristics_folder = base_directory + FOLDER_SEPARATOR + 'add_password_heuristics'
- heuristic_files = os.listdir(heuristics_folder)
- if IGNORED_FILE in heuristic_files:
- heuristic_files.remove(IGNORED_FILE)
- if len(heuristic_files) == 0:
- return
- i = 0
- for heuristic_file in heuristic_files:
- if heuristic_file == IGNORED_FILE:
- continue
- print(f'Adding new definitions from {heuristic_file}')
- file_loc = heuristics_folder + FOLDER_SEPARATOR + heuristic_file
- with open(file_loc, 'r', encoding='utf-8') as file:
- for line in file:
- clean_line = line.strip()
- i += 1
- if i % 1000 == 0 and i != 1:
- print(f'{int(i / 1000)}k passwords collected')
- if clean_line not in common_passwords and simple_check(clean_line):
- common_passwords.append(clean_line)
- i = 0
- print(f'Adding {len(common_passwords)} Passwords to DB.')
- for password in common_passwords:
- hashed_pass = create_md5_hash(password)
- query = query_db(f"SELECT id FROM common_passwords WHERE password_hash = '{hashed_pass}'")
- if len(query) == 0:
- i += 1
- execute_db(f"INSERT INTO common_passwords(password_hash) VALUES('{hashed_pass}')")
- if i % 1000 == 0:
- print(f'{int(i / 1000)}k passwords added to db')
- for heuristic_file in heuristic_files:
- os.remove(heuristics_folder + FOLDER_SEPARATOR + heuristic_file)
- def simple_check(password):
- if len(password) < 8:
- return False
- contains_lower, contains_upper, contains_special = False, False, False
- for letter in password:
- if letter in LOWER_CASE:
- contains_lower = True
- elif letter in UPPER_CASE:
- contains_upper = True
- elif letter in SPECIAL_CHARACTERS:
- contains_special = True
- if contains_lower and contains_upper and contains_special:
- return True
- return False
- def confirm_strong_password(password):
- if not simple_check(password):
- return False
- hashed_pass = create_md5_hash(password)
- query = query_db(f"SELECT id FROM common_passwords WHERE password_hash = '{hashed_pass}'")
- if len(query) != 0:
- return False
- return True
- def graceful_exit(signum, frame):
- sqlite_engine.dispose()
- print(f'Disposing SQL Engine')
- sys.exit(0)
- signal.signal(signal.SIGINT, graceful_exit)
- signal.signal(signal.SIGTERM, graceful_exit)
- sqlite_engine = create_sqlite_engine()
- add_password_heuristics()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement