Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import required libraries
- import datetime # Displays date and time file data was produced.
- import hashlib # The only way to use hashing functions in Python is through this module
- import json # JSON data can only be opened with this module
- import os # This module is required for file paths
- # Ask the user for login credentials from a JSON file to load the data
- # Print welcome message
- print("Welcome to an automated script for password auditing.")
- # Define function for loading a file's JSON data
- def load_json(json_file_path):
- if not os.path.exists(json_file_path):
- print("Cannot open the file ", json_file_path)
- with open(json_file_path, 'rb') as file:
- data = json.load(file)
- return data
- # Define function for loading a password list from a txt file
- def load_common_passwords(passwords_file_path):
- if not os.path.exists(passwords_file_path):
- print("Error: File path doesn't exist.")
- return None
- with open(passwords_file_path, 'r', encoding='latin1') as file:
- common_passwords = [line.strip() for line in file]
- return common_passwords
- # Function to hash passwords with SHA-256
- def hash_password(password):
- return hashlib.sha256(password.encode()).hexdigest()
- hashed = hash_password(user_data, common_passwords)
- if hashed in common_passwords:
- matches.read(passwords_file_path)
- print(f"Password found:\nload_json: {line1.strip()}n\common_passwords: {line2.strip()}")
- else:
- print("Password not found.")
- # Function to produce hash as required
- def produce_hash(string_to_hash):
- hashed_object = hashlib.sha256(string_to_hash.encode('latin-1'))
- return hashed_object.hexdigest()
- # Check if any password list entries match user passwords & output results
- def check_password(user_data, common_passwords):
- results = []
- for user in user_data:
- username = user['user_name']
- hashed_password = user['user_password']
- plain_password = None
- for password in common_passwords:
- if hashed_password == hash_password(password):
- plain_password = password
- break
- if plain_password:
- results.append({'username': username, 'password': plain_password, 'password_found': True})
- print(f"{username}'s password found")
- else:
- results.append({'username': username, 'password': '', 'password_found': False})
- print(f"{username}'s password not found")
- return results
- # Main function of checking for matches
- # Begin main loop
- def main():
- user_wants_to_continue = True
- while user_wants_to_continue == True:
- #Ask for file path
- json_file_path = input('Enter the full path of the file to exit (example: C:/test.txt) or [e] to exit: ')
- if json_file_path.lower() == "e":
- print("Goodbye!")
- user_wants_to_continue = False
- # Ask the user for password list file
- passwords_file_path = input("Enter your password list file path: ")
- # Load usernames and hashed passwords from JSON file
- user_data = load_json(json_file_path)
- if user_data is None:
- print("1")
- continue
- # Load password list
- common_passwords = load_common_passwords(passwords_file_path)
- if common_passwords is None:
- print("2")
- break
- break
- # Check if any passwords from password list match the user passwords
- # Save data in JSON file
- script_dir = os.path.dirname(os.path.abspath(__file__))
- user_data = load_json(json_file_path)
- results = check_password(user_data, common_passwords)
- with open(json_file_path, 'r') as user_data, open(user_data, 'r') as common_passwords:
- for hashed_password in common_passwords:
- if common_Passwords is None:
- continue
- results_file_path = os.path.join(script_dir, 'password_check_results.json')
- with open(results_file_path, "w") as file:
- json.dump(results, file, indent=1)
- file.close
- fileToCheck = open(results_filepath,"rb")
- # Calculate SHA256 hash value of the new file.
- sha256Hash = hashlib.sha256(results_filepath.read()).hexdigest()
- # Get the time
- timeStamp = str(datetime.datetime.now())
- # Update data
- updateVar = {'time_stamp':timeStamp,'file':filePath,'hash':sha256Hash}
- data.append(updateVar)
- print(data)
- print(produce_hash("Hash produced."))
- user_wants_to_continue = False
- # Main function execution
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement