Files
LEAF/tests/replay_fixture.py
2026-11-15 17:42:55 +01:00

32 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Run on the sensor as root: offline packet/rule proof with isolated outputs."""
import json,subprocess,tempfile
from pathlib import Path
import yaml
from syn_packets import packet
import struct,time
def main():
root=Path(tempfile.mkdtemp(prefix='leaf-replay-'))
pcap=root/'fixture.pcap'
with pcap.open('wb') as out:
out.write(struct.pack('<IHHIIII',0xa1b2c3d4,2,4,0,0,65535,1))
for i in range(24):
frame=packet('203.0.113.12','192.0.2.201',22,i)
out.write(struct.pack('<IIII',int(time.time()),i*1000,len(frame),len(frame))+frame)
cfg=yaml.safe_load(Path('/etc/suricata-leaf/suricata-leaf.yaml').read_text())
cfg['outputs']=[{'eve-log': {'enabled': True, 'filetype': 'regular',
'filename': str(root/'eve.json'), 'types': [{'alert': {'metadata': True}}, 'stats']}}]
conf=root/'config.yaml';conf.write_text('%YAML 1.1\n---\n'+yaml.safe_dump(cfg))
subprocess.run(['suricata','-r',str(pcap),'-c',str(conf),'-l',str(root),'--runmode','single'],check=True)
events=[json.loads(line) for line in (root/'eve.json').read_text().splitlines()]
alerts=[e for e in events if e.get('event_type')=='alert']
stats=[e['stats'] for e in events if e.get('event_type')=='stats'][-1]
assert [e['alert']['signature_id'] for e in alerts]==[1000030,1000030],alerts
assert stats['decoder']['pkts']==24 and stats['decoder']['invalid']==0
assert stats['tcp']['syn']==24 and stats['tcp']['invalid_checksum']==0
assert stats['defrag']['ipv4']['fragments']==0
print('OFFLINE REPLAY PASS: 24 valid SYNs, zero fragments/checksum errors, two sid 1000030 alerts')
print('Artifacts:',root)
if __name__=='__main__':main()