Spaces:
Running
Running
Download server/faults/firewall.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/firewall.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/faults/firewall.py
-
curl -L -o firewall.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/firewall.py
3.91 kB
| """Firewall/ACL faults (F26-F28): selective drop, network partition, control-plane isolation.""" | |
| from __future__ import annotations | |
| from server.constants import GroundTruth | |
| def inject_selective_drop(injector, router: str, | |
| protocol: str | None = None, | |
| dport: int | None = None, | |
| src_subnet: str | None = None) -> GroundTruth: | |
| """F26 — Block specific traffic via iptables (protocol, port, or subnet).""" | |
| node = injector._nm.get_router(router) | |
| rule_parts = ["iptables", "-A", "FORWARD"] | |
| undo_parts = ["iptables", "-D", "FORWARD"] | |
| if protocol: | |
| rule_parts.extend(["-p", protocol]) | |
| undo_parts.extend(["-p", protocol]) | |
| if dport: | |
| rule_parts.extend(["--dport", str(dport)]) | |
| undo_parts.extend(["--dport", str(dport)]) | |
| if src_subnet: | |
| rule_parts.extend(["-s", src_subnet]) | |
| undo_parts.extend(["-s", src_subnet]) | |
| rule_parts.extend(["-j", "DROP"]) | |
| undo_parts.extend(["-j", "DROP"]) | |
| node.cmd(" ".join(rule_parts)) | |
| rule_str = " ".join(rule_parts) | |
| undo_str = " ".join(undo_parts) | |
| target = f"proto={protocol}" if protocol else f"src={src_subnet}" | |
| if dport: | |
| target += f":port={dport}" | |
| def undo(): | |
| node.cmd(undo_str) | |
| return injector._register(GroundTruth( | |
| fault_type="selective_drop_bgp", component=router, | |
| details={"router": router, "rule": rule_str, "target": target, | |
| "protocol": protocol, "dport": dport, "src_subnet": src_subnet}, | |
| ), undo) | |
| def inject_network_partition(injector, router_a: str, router_b: str, | |
| subnet_a: str, subnet_b: str) -> GroundTruth: | |
| """F27 — Block data-plane traffic between two subnets on both border routers.""" | |
| node_a = injector._nm.get_router(router_a) | |
| node_b = injector._nm.get_router(router_b) | |
| node_a.cmd(f"iptables -A FORWARD -d {subnet_b} -j DROP") | |
| node_a.cmd(f"iptables -A FORWARD -s {subnet_b} -j DROP") | |
| node_b.cmd(f"iptables -A FORWARD -d {subnet_a} -j DROP") | |
| node_b.cmd(f"iptables -A FORWARD -s {subnet_a} -j DROP") | |
| def undo(): | |
| node_a.cmd(f"iptables -D FORWARD -d {subnet_b} -j DROP") | |
| node_a.cmd(f"iptables -D FORWARD -s {subnet_b} -j DROP") | |
| node_b.cmd(f"iptables -D FORWARD -d {subnet_a} -j DROP") | |
| node_b.cmd(f"iptables -D FORWARD -s {subnet_a} -j DROP") | |
| return injector._register(GroundTruth( | |
| fault_type="network_partition", | |
| component=f"{router_a}-{router_b}", | |
| details={"router_a": router_a, "router_b": router_b, | |
| "subnet_a": subnet_a, "subnet_b": subnet_b}, | |
| ), undo) | |
| def inject_control_plane_isolation(injector, router: str) -> GroundTruth: | |
| """F28 — Block BGP (tcp/179) and OSPF (protocol 89) on a router.""" | |
| node = injector._nm.get_router(router) | |
| node.cmd("iptables -A INPUT -p tcp --dport 179 -j DROP") | |
| node.cmd("iptables -A OUTPUT -p tcp --dport 179 -j DROP") | |
| node.cmd("iptables -A INPUT -p 89 -j DROP") | |
| node.cmd("iptables -A OUTPUT -p 89 -j DROP") | |
| def undo(): | |
| node.cmd("iptables -D INPUT -p tcp --dport 179 -j DROP") | |
| node.cmd("iptables -D OUTPUT -p tcp --dport 179 -j DROP") | |
| node.cmd("iptables -D INPUT -p 89 -j DROP") | |
| node.cmd("iptables -D OUTPUT -p 89 -j DROP") | |
| return injector._register(GroundTruth( | |
| fault_type="control_plane_isolation", component=router, | |
| details={"router": router, "blocked": ["tcp/179", "ospf/89"]}, | |
| ), undo) | |
| FAULTS = { | |
| "selective_drop_bgp": {"fn": inject_selective_drop, "difficulty": "medium", "needs": "router"}, | |
| "network_partition": {"fn": inject_network_partition, "difficulty": "hard", "needs": "link"}, | |
| "control_plane_isolation": {"fn": inject_control_plane_isolation, "difficulty": "hard", "needs": "router"}, | |
| } | |