Download mutation_watcher.py from TTOPM/belel-protocol: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/TTOPM/belel-protocol/resolve/main/mutation_watcher.py
- Command line
-
hf download hf://TTOPM/belel-protocol/mutation_watcher.py
-
curl -L -o mutation_watcher.py https://huggingface.co/TTOPM/belel-protocol/resolve/main/mutation_watcher.py
1.73 kB
| # mutation_watcher.py | |
| import json | |
| import hashlib | |
| import os | |
| from datetime import datetime | |
| from webhook_alert import send_alert | |
| # File to watch for mutations | |
| CANONICAL_FILE = "canonical_config.json" | |
| BASELINE_HASH_FILE = "hash_baseline.json" | |
| LOG_FILE = "mutation_watch.log" | |
| def get_file_hash(path): | |
| try: | |
| with open(path, "rb") as f: | |
| content = f.read() | |
| return hashlib.sha256(content).hexdigest() | |
| except FileNotFoundError: | |
| return None | |
| def log_mutation(details): | |
| timestamp = datetime.utcnow().isoformat() | |
| with open(LOG_FILE, "a") as f: | |
| f.write(f"[⛔ MUTATION] {timestamp} - {details}\n") | |
| def load_baseline(): | |
| if not os.path.exists(BASELINE_HASH_FILE): | |
| return {} | |
| with open(BASELINE_HASH_FILE, "r") as f: | |
| return json.load(f) | |
| def save_baseline(hash_value): | |
| with open(BASELINE_HASH_FILE, "w") as f: | |
| json.dump({"canonical_config": hash_value}, f, indent=4) | |
| def validate_integrity(): | |
| current_hash = get_file_hash(CANONICAL_FILE) | |
| baseline = load_baseline() | |
| if not current_hash: | |
| log_mutation("canonical_config.json not found.") | |
| send_alert("[❌] Mutation Watcher: canonical_config.json missing!") | |
| return | |
| expected_hash = baseline.get("canonical_config") | |
| if not expected_hash: | |
| save_baseline(current_hash) | |
| print("[🟢] Baseline hash saved.") | |
| return | |
| if current_hash != expected_hash: | |
| log_mutation("canonical_config.json has changed!") | |
| send_alert("[🚨] ALERT: canonical_config.json mutated! Possible tampering or unauthorized edit.") | |
| else: | |
| print("[✅] No mutation detected.") | |
| if __name__ == "__main__": | |
| validate_integrity() | |