Upload violation_scanner.py
Browse files
src/violation_scanner/violation_scanner.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/protocol/security/violation_scanner.py 🔍🧠
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import hashlib
|
| 5 |
+
import json
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from src.protocol.permanent_memory import PermanentMemory
|
| 8 |
+
from src.protocol.decentralized_comm.ipfs_client import IPFSClient
|
| 9 |
+
|
| 10 |
+
class ViolationScanner:
|
| 11 |
+
"""
|
| 12 |
+
Scans for and logs violations of file integrity, particularly related to Symbiont operations.
|
| 13 |
+
"""
|
| 14 |
+
def __init__(self, monitored_files, memory_path="./memory_store.json", baseline_path="./hash_baseline.json"):
|
| 15 |
+
self.monitored_files = monitored_files
|
| 16 |
+
self.memory = PermanentMemory(memory_path)
|
| 17 |
+
self.baseline_path = baseline_path
|
| 18 |
+
self.ipfs = IPFSClient()
|
| 19 |
+
self.baseline = self._load_baseline()
|
| 20 |
+
|
| 21 |
+
def _load_baseline(self):
|
| 22 |
+
if os.path.exists(self.baseline_path):
|
| 23 |
+
with open(self.baseline_path, "r") as f:
|
| 24 |
+
return json.load(f)
|
| 25 |
+
else:
|
| 26 |
+
return self._generate_baseline()
|
| 27 |
+
|
| 28 |
+
def _generate_baseline(self):
|
| 29 |
+
baseline = {}
|
| 30 |
+
for file_path in self.monitored_files:
|
| 31 |
+
if os.path.exists(file_path):
|
| 32 |
+
with open(file_path, "rb") as f:
|
| 33 |
+
file_hash = hashlib.sha256(f.read()).hexdigest()
|
| 34 |
+
baseline[file_path] = file_hash
|
| 35 |
+
with open(self.baseline_path, "w") as f:
|
| 36 |
+
json.dump(baseline, f, indent=4)
|
| 37 |
+
return baseline
|
| 38 |
+
|
| 39 |
+
def scan_for_violations(self, agent_id="Unknown"):
|
| 40 |
+
for file_path in self.monitored_files:
|
| 41 |
+
if not os.path.exists(file_path):
|
| 42 |
+
self._log_violation(file_path, "FILE_MISSING", agent_id)
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
with open(file_path, "rb") as f:
|
| 46 |
+
current_hash = hashlib.sha256(f.read()).hexdigest()
|
| 47 |
+
|
| 48 |
+
baseline_hash = self.baseline.get(file_path)
|
| 49 |
+
if not baseline_hash:
|
| 50 |
+
self._log_violation(file_path, "NO_BASELINE_FOUND", agent_id)
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
if current_hash != baseline_hash:
|
| 54 |
+
self._log_violation(file_path, "HASH_MISMATCH", agent_id)
|
| 55 |
+
self.memory.log_symbiont_breach(
|
| 56 |
+
file_path=file_path,
|
| 57 |
+
breach_type="HASH_MISMATCH",
|
| 58 |
+
agent_id=agent_id
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def _log_violation(self, file_path, breach_type, agent_id="Unknown"):
|
| 62 |
+
event = {
|
| 63 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 64 |
+
"type": breach_type,
|
| 65 |
+
"file": file_path,
|
| 66 |
+
"agent": agent_id,
|
| 67 |
+
"symbiont_event": True,
|
| 68 |
+
"source_script": "violation_scanner.py"
|
| 69 |
+
}
|
| 70 |
+
self.memory.write("violation", event)
|
| 71 |
+
|
| 72 |
+
# Store hash in IPFS for tamper-proof history
|
| 73 |
+
if os.path.exists(file_path):
|
| 74 |
+
with open(file_path, "rb") as f:
|
| 75 |
+
ipfs_hash = self.ipfs.store(f.read())
|
| 76 |
+
event["ipfs_cid"] = ipfs_hash
|
| 77 |
+
self.memory.write("violation_ipfs", event)
|
| 78 |
+
|
| 79 |
+
print(f"[Symbiont Alert] {breach_type} in {file_path} by agent {agent_id}")
|