Untitled
import os import hashlib import json # ================================ # CONFIGURATION: Update these values # ================================ DIRECTORIES = [ r"C:\Folder1", # Replace with your first directory path r"C:\Folder2" # Replace with your second directory path ] BASELINE_FILE = "baseline.json" # ================================ # FUNCTIONS # ================================ def calculate_md5(file_path): """Calculate and return the MD5 hash of a given file.""" hash_md5 = hashlib.md5() try: with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) except Exception as e: print(f"Error reading {file_path}: {e}") return None return hash_md5.hexdigest() def generate_baseline(directories, baseline_file): """Generate a baseline hash dictionary for all files in the specified directories.""" baseline = {} for directory in directories: print(f"Scanning directory: {directory}") for root, _, files in os.walk(directory): for file in files: full_path = os.path.join(root, file) file_hash = calculate_md5(full_path) if file_hash: baseline[full_path] = file_hash with open(baseline_file, "w") as bf: json.dump(baseline, bf, indent=4) print(f"Baseline generated and saved to {baseline_file}") def check_integrity(directories, baseline_file): """Compare current file hashes against the stored baseline and alert on any changes.""" try: with open(baseline_file, "r") as bf: baseline = json.load(bf) except FileNotFoundError: print("Baseline file not found. Generating a new baseline.") generate_baseline(directories, baseline_file) return current_state = {} changed_files = [] new_files = [] for directory in directories: print(f"Scanning directory: {directory}") for root, _, files in os.walk(directory): for file in files: full_path = os.path.join(root, file) file_hash = calculate_md5(full_path) if file_hash: current_state[full_path] = file_hash if full_path in baseline: if baseline[full_path] != file_hash: changed_files.append(full_path) else: new_files.append(full_path) removed_files = [file for file in baseline if file not in current_state] if changed_files or new_files or removed_files: print("Alert: File integrity changes detected!") if changed_files: print("\nChanged files:") for file in changed_files: print(f" - {file}") if new_files: print("\nNew files:") for file in new_files: print(f" - {file}") if removed_files: print("\nRemoved files:") for file in removed_files: print(f" - {file}") else: print("No changes detected. File integrity is intact.") # ================================ # MAIN EXECUTION # ================================ if __name__ == "__main__": # Automatically decide whether to generate a baseline or perform a check if not os.path.exists(BASELINE_FILE): print("Baseline file does not exist. Creating baseline.") generate_baseline(DIRECTORIES, BASELINE_FILE) else: print("Baseline file found. Checking file integrity.") check_integrity(DIRECTORIES, BASELINE_FILE)
Leave a Comment