Files
LEAF/tests/live_negative.py

145 lines
9.7 KiB
Python
Raw Normal View History

2026-11-15 17:42:41 +01:00
#!/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()<deadline:
current=status(t0)
applied={row['ip'] for row in current['blocks'] if row['applied']}
verified={row['ip'] for row in current['audit'] if row.get('action')=='block' and row.get('verified')}
if BLOCKED in set(current['members']) and BLOCKED in applied and BLOCKED in verified: break
print('waiting for router commit',flush=True);time.sleep(3)
else: raise RuntimeError('block was not verified')
evidence['blocked']=current
print(f'block verified; benign flows from {a.benign_src} under active block',flush=True)
before_neg=current['drop_counters']
neg=subprocess.Popen(ssh+[a.sensor,f"sudo -n timeout 15 tcpdump -ni eth1 -c 12 -w /tmp/leaf-neg-benign-{int(t0)}.pcap 'tcp and src host {a.benign_src} and dst port 80'"],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
time.sleep(1); syn(a.benign_host,a.benign_src,'192.0.2.201',80,6)
time.sleep(2); syn(a.benign_host,a.benign_src,'192.0.2.201',80,6)
out,err=neg.communicate(timeout=20)
evidence['benign_capture']={'exit':neg.returncode,'summary':err.strip()}
if neg.returncode!=0 or '12 packets captured' not in err: raise RuntimeError('benign flow was not observed on the ingress mirror under active block')
current=status(t0); evidence['during_benign']=current
if BLOCKED not in set(current['members']): raise RuntimeError('blocked member vanished during benign window')
if a.benign_src in set(current['members']): raise RuntimeError('benign src entered IDS-BLOCK')
if a.benign_src in {row['ip'] for row in current['blocks']}: raise RuntimeError('benign src entered state db')
if clean_member_state and current['drop_counters'].get('9',0)!=before_neg.get('9',0):
raise RuntimeError('rule-9 drop counter grew for benign src')
print('benign flows observed without IDS drop-counter growth; re-probing blocked src',flush=True)
before=current['drop_counters']; syn(a.lxc,BLOCKED,'192.0.2.201',22,3)
current=status(t0); evidence['enforced']=current
if current['drop_counters'].get('9',0)<=before.get('9',0): raise RuntimeError('rule-9 drop counter did not grow for blocked src')
if clean_member_state:
again=status(t0); before2=again['drop_counters']; syn(a.benign_host,a.benign_src,'192.0.2.201',80,3)
current=status(t0); evidence['benign_reprobe']=current
if current['drop_counters'].get('9',0)!=before2.get('9',0): raise RuntimeError('rule-9 counter grew for benign re-probe')
# Socket mode has no file alerts. State/member absence above and counters
# are the evidence; ingress capture cannot prove destination delivery.
print('drop counter grew only for blocked src; waiting for TTL cleanup',flush=True)
deadline=time.time()+300
while time.time()<deadline:
current=status(t0)
tracked={row['ip'] for row in current['blocks']}
if BLOCKED not in set(current['members'])|tracked: break
time.sleep(5)
else: raise RuntimeError('TTL cleanup did not remove drill member')
evidence['expired']=current
counters=current['drop_counters']; syn(a.lxc,BLOCKED,'192.0.2.201',22,3)
current=status(t0); evidence['after_expiry_probes']=current
if current['drop_counters']!=counters: raise RuntimeError('drill drop counters still increment after expiry')
unblocked={row['ip'] for row in current['audit'] if row.get('action')=='unblock' and row.get('verified')}
if BLOCKED not in unblocked: raise RuntimeError('verified unblock audit missing')
final=status(); evidence['final']=final
if final['protected_hashes']!=baseline['protected_hashes']: raise RuntimeError('protected router configuration changed')
evidence['metrics']=metrics_tail()
evidence['passed']=True
print('NEGATIVE DRILL PASS: block verified -> 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()