"""Blast-radius model: starting from one phished workstation, which hosts can be reached over SMB/RDP?
Network reachability only. It ignores credentials, patches and everything else."""
import json
from collections import deque
m = json.load(open("data/network_model.json"))

def reachable(design):
    flows = m["designs"][design]["flows"]
    hosts = m["hosts"]
    if flows == "all":
        edges = {(a, b) for a in hosts for b in hosts if a != b}
    else:
        edges = {(a, b) for a, b, p in flows if p in m["lateral_ports"]}
    seen, q = {m["start"]}, deque([m["start"]])
    while q:
        h = q.popleft()
        for a, b in edges:
            if a == h and b not in seen: seen.add(b); q.append(b)
    return sorted(seen - {m["start"]})

def scenario(name, design, backup_mode):
    r = reachable(design)
    others = len(m["hosts"]) - 1
    backups_hit = "backup01" in r and backup_mode == "online share"
    print(f"  {name:<34} reached {len(r)}/{others}: {', '.join(r) or '-':<42} backups at risk: {'YES' if backups_hit else 'no'}")

if __name__ == "__main__":
    print(f"== start: {m['start']} (phished).  Backup copy = 'online share' or 'offline/immutable'")
    scenario("flat + online-share backups", "flat", "online share")
    scenario("segmented + online-share backups", "segmented", "online share")
    scenario("flat + offline/immutable backups", "flat", "offline/immutable")
    scenario("segmented + offline/immutable", "segmented", "offline/immutable")
