#!/usr/bin/env python3 """Negative IDS counter drill: benign traffic does not match the IDS drop while a blocked src sits in IDS-BLOCK. Run on workstation: python3 tests/live_negative.py \ --output /path/to/evidence.json Flow: trigger a real block (sid 1000030 injector, spoofed src 203.0.113.12), then from a DIFFERENT host (second LXC) with a clean TEST-NET src (192.0.2.97) push benign TCP SYN flows (count below the 12-packet sid threshold) at the existing transit dst on a port the drill never touches. Proof while the block is active: - benign SYN packets are captured on the ingress mirror (does not establish destination receipt); - the IDS-BLOCK member for the blocked src is still present and verified; - rule-9 drop counter grows ONLY for the re-probed blocked src, not benign; - the benign source is absent from IDS-BLOCK and the state database. Then TTL expiry cleans up like live_e2e does. TTL=180 for the run; config restored on exit. Never adds listeners/services on the router; uses existing transit destinations. Health and capture metrics are included in the results. """ import argparse,json,subprocess,time,ipaddress from pathlib import Path BLOCKED='203.0.113.12' def main(): ap=argparse.ArgumentParser(description=__doc__) ap.add_argument('--sensor',default='yuru@192.168.100.84') ap.add_argument('--lxc',default='root@172.19.32.3') ap.add_argument('--benign-host',default='root@172.19.32.5') ap.add_argument('--benign-src',default='192.0.2.97') ap.add_argument('--ssh-key',default=str(Path.home()/'.ssh/id_ed25519_alice')) ap.add_argument('--output',type=Path,required=True) a=ap.parse_args(); root=Path(__file__).resolve().parents[1] try: addr=ipaddress.IPv4Address(a.benign_src) except ValueError: ap.error('--benign-src must be a TEST-NET IPv4 address') if not any(addr in ipaddress.ip_network(n) for n in ('192.0.2.0/24','198.51.100.0/24','203.0.113.0/24')) or a.benign_src==BLOCKED: ap.error('--benign-src must be a distinct TEST-NET address') ssh=['ssh','-F','/dev/null','-i',a.ssh_key,'-o','BatchMode=yes','-o','ConnectTimeout=8'] scp=['scp','-F','/dev/null','-i',a.ssh_key,'-o','BatchMode=yes'] def run(host,cmd): return subprocess.run(ssh+[host,cmd],capture_output=True,text=True,check=True,timeout=150).stdout def status(since=0): return json.loads(run(a.sensor,f'sudo -n python3 /tmp/leaf-live-helper.py status --since {since}')) def syn(host,src,dst,port,n): print(run(host,f'python3 /tmp/leaf-send-syn.py {src} {dst} {port} {n}').strip(),flush=True) def metrics_tail(): lines=run(a.sensor,'sudo -n journalctl -u leaf-controller --since "-4 min" --no-pager | grep "leaf.metrics" | tail -4') capture=run(a.sensor,'sudo -n tail -n 2 /var/log/leaf-controller/capture.log 2>/dev/null || true') return {'health':[l.split('leaf.metrics: ',1)[1] for l in lines.splitlines() if 'leaf.metrics'], 'capture_windows':[json.loads(l) for l in capture.splitlines() if l]} evidence={'started':time.time(),'passed':False} changed=False try: subprocess.run(scp+[str(root/'tests/live_helper.py'),a.sensor+':/tmp/leaf-live-helper.py'],check=True) subprocess.run(scp+[str(root/'tests/syn_packets.py'),a.lxc+':/tmp/leaf-send-syn.py'],check=True) subprocess.run(scp+[str(root/'tests/syn_packets.py'),a.benign_host+':/tmp/leaf-send-syn.py'],check=True) baseline=status(); evidence['baseline']=baseline if baseline['members'] or baseline['blocks']: raise RuntimeError('negative proof requires idle empty group/state for counter attribution') if BLOCKED in set(baseline['members']): raise RuntimeError('drill address already blocked; refusing to alter existing ownership') if a.benign_src in set(baseline['members']): raise RuntimeError('benign src already blocked; run clean of it first') # An empty baseline allows rule-9 changes to be attributed to the # test source. Ingress capture establishes observation, not delivery. clean_member_state = not baseline['members'] run(a.sensor,'sudo -n python3 /tmp/leaf-live-helper.py short-ttl') changed=True run(a.sensor,'sudo -n systemctl restart leaf-controller') t0=time.time(); evidence['traffic_started']=t0 capture=subprocess.Popen(ssh+[a.sensor,f"sudo -n timeout 15 tcpdump -ni eth1 -c 24 -w /tmp/leaf-neg-block-{int(t0)}.pcap 'tcp and src host 203.0.113.12 and dst port 22'"],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) time.sleep(1); syn(a.lxc,BLOCKED,'192.0.2.201',22,24) out,err=capture.communicate(timeout=20) evidence['block_trigger']={'exit':capture.returncode,'summary':err.strip()} if capture.returncode!=0 or '24 packets captured' not in err: raise RuntimeError('block-trigger capture did not prove 24 packets') deadline=time.time()+180 while time.time() benign flows observed on mirror -> ' 'rule-9 grows only for blocked src -> benign never entered IDS-BLOCK -> TTL cleanup',flush=True) finally: try: if changed: try: if not evidence['passed']: run(a.sensor,f'sudo -n python3 /tmp/leaf-live-helper.py cleanup --since {evidence.get("traffic_started",evidence["started"])}') finally: print(run(a.sensor,'sudo -n python3 /tmp/leaf-live-helper.py restore && sudo -n systemctl restart leaf-controller').strip(),flush=True) except Exception as exc: evidence['restoration_error']=str(exc) evidence['passed']=False raise finally: evidence['finished']=time.time() a.output.parent.mkdir(parents=True,exist_ok=True) a.output.write_text(json.dumps(evidence,indent=2)+'\n') if __name__=='__main__':main()