Advertisement
NevadeanCowboy

Untitled

Jun 16th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | Cybersecurity | 0 0
  1. # Import required libraries
  2.  
  3. import datetime # Displays date and time file data was produced.
  4. import hashlib # The only way to use hashing functions in Python is through this module
  5. import json # JSON data can only be opened with this module
  6. import os # This module is required for file paths
  7.  
  8. # Ask the user for login credentials from a JSON file to load the data
  9. # Print welcome message
  10.  
  11. print("Welcome to an automated script for password auditing.")
  12.  
  13. # Define function for loading a file's JSON data
  14.  
  15. def load_json(json_file_path):
  16. if not os.path.exists(json_file_path):
  17. print("Cannot open the file ", json_file_path)
  18. with open(json_file_path, 'rb') as file:
  19. data = json.load(file)
  20. return data
  21.  
  22. # Define function for loading a password list from a txt file
  23.  
  24. def load_common_passwords(passwords_file_path):
  25. if not os.path.exists(passwords_file_path):
  26. print("Error: File path doesn't exist.")
  27. return None
  28. with open(passwords_file_path, 'r', encoding='latin1') as file:
  29. common_passwords = [line.strip() for line in file]
  30. return common_passwords
  31.  
  32. # Function to hash passwords with SHA-256
  33.  
  34. def hash_password(password):
  35. return hashlib.sha256(password.encode()).hexdigest()
  36.  
  37.  
  38. hashed = hash_password(user_data, common_passwords)
  39. if hashed in common_passwords:
  40. matches.read(passwords_file_path)
  41. print(f"Password found:\nload_json: {line1.strip()}n\common_passwords: {line2.strip()}")
  42. else:
  43. print("Password not found.")
  44. # Function to produce hash as required
  45.  
  46. def produce_hash(string_to_hash):
  47. hashed_object = hashlib.sha256(string_to_hash.encode('latin-1'))
  48. return hashed_object.hexdigest()
  49. # Check if any password list entries match user passwords & output results
  50.  
  51. def check_password(user_data, common_passwords):
  52. results = []
  53. for user in user_data:
  54. username = user['user_name']
  55. hashed_password = user['user_password']
  56. plain_password = None
  57. for password in common_passwords:
  58. if hashed_password == hash_password(password):
  59. plain_password = password
  60. break
  61. if plain_password:
  62. results.append({'username': username, 'password': plain_password, 'password_found': True})
  63. print(f"{username}'s password found")
  64. else:
  65. results.append({'username': username, 'password': '', 'password_found': False})
  66. print(f"{username}'s password not found")
  67. return results
  68.  
  69. # Main function of checking for matches
  70.  
  71. # Begin main loop
  72. def main():
  73. user_wants_to_continue = True
  74. while user_wants_to_continue == True:
  75.  
  76. #Ask for file path
  77. json_file_path = input('Enter the full path of the file to exit (example: C:/test.txt) or [e] to exit: ')
  78. if json_file_path.lower() == "e":
  79. print("Goodbye!")
  80. user_wants_to_continue = False
  81.  
  82. # Ask the user for password list file
  83. passwords_file_path = input("Enter your password list file path: ")
  84.  
  85. # Load usernames and hashed passwords from JSON file
  86. user_data = load_json(json_file_path)
  87. if user_data is None:
  88. print("1")
  89. continue
  90.  
  91. # Load password list
  92. common_passwords = load_common_passwords(passwords_file_path)
  93. if common_passwords is None:
  94. print("2")
  95. break
  96. break
  97.  
  98. # Check if any passwords from password list match the user passwords
  99.  
  100. # Save data in JSON file
  101. script_dir = os.path.dirname(os.path.abspath(__file__))
  102. user_data = load_json(json_file_path)
  103. results = check_password(user_data, common_passwords)
  104. with open(json_file_path, 'r') as user_data, open(user_data, 'r') as common_passwords:
  105. for hashed_password in common_passwords:
  106. if common_Passwords is None:
  107. continue
  108. results_file_path = os.path.join(script_dir, 'password_check_results.json')
  109. with open(results_file_path, "w") as file:
  110. json.dump(results, file, indent=1)
  111. file.close
  112. fileToCheck = open(results_filepath,"rb")
  113.  
  114. # Calculate SHA256 hash value of the new file.
  115. sha256Hash = hashlib.sha256(results_filepath.read()).hexdigest()
  116.  
  117. # Get the time
  118. timeStamp = str(datetime.datetime.now())
  119.  
  120. # Update data
  121. updateVar = {'time_stamp':timeStamp,'file':filePath,'hash':sha256Hash}
  122. data.append(updateVar)
  123. print(data)
  124. print(produce_hash("Hash produced."))
  125. user_wants_to_continue = False
  126. # Main function execution
  127.  
  128. if __name__ == "__main__":
  129. main()
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement