Helper scripts
This commit is contained in:
59
tests/syn_packets.py
Normal file
59
tests/syn_packets.py
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded TEST-NET SYN generator; --pcap writes a fixture without sending traffic."""
|
||||
import argparse
|
||||
import ipaddress
|
||||
import re
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
def checksum(data):
|
||||
if len(data)%2: data += b'\0'
|
||||
total=sum(struct.unpack('!%dH'%(len(data)//2),data))
|
||||
while total>>16: total=(total&0xffff)+(total>>16)
|
||||
return (~total)&0xffff
|
||||
|
||||
def packet(src,dst,dport,index,smac=b'\x02\0\0\0\0\1',dmac=b'\x02\0\0\0\0\2',flags=2):
|
||||
a,b=socket.inet_aton(src),socket.inet_aton(dst)
|
||||
tcp=struct.pack('!HHIIBBHHH',40000+index%20000,dport,7000+index,0,0x50,flags,1024,0,0)
|
||||
pseudo=a+b+struct.pack('!BBH',0,6,len(tcp))
|
||||
tcp=tcp[:16]+struct.pack('!H',checksum(pseudo+tcp))+tcp[18:]
|
||||
ip=struct.pack('!BBHHHBBH4s4s',0x45,0,40,index%65536,0,64,6,0,a,b)
|
||||
ip=ip[:10]+struct.pack('!H',checksum(ip))+ip[12:]
|
||||
return dmac+smac+b'\x08\0'+ip+tcp
|
||||
|
||||
def main():
|
||||
ap=argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument('src'); ap.add_argument('dst'); ap.add_argument('port',type=int); ap.add_argument('count',type=int)
|
||||
ap.add_argument('--pcap',type=Path); ap.add_argument('--interface',default='eth0'); ap.add_argument('--gateway',default='172.19.32.1')
|
||||
a=ap.parse_args()
|
||||
nets=[ipaddress.ip_network(n) for n in ('192.0.2.0/24','198.51.100.0/24','203.0.113.0/24')]
|
||||
if not all(any(ipaddress.ip_address(ip) in n for n in nets) for ip in (a.src,a.dst)):
|
||||
ap.error('source and destination must be TEST-NET addresses')
|
||||
if not 1<=a.count<=500 or not 1<=a.port<=65535: ap.error('count must be 1..500 and port 1..65535')
|
||||
if a.pcap:
|
||||
with a.pcap.open('wb') as f:
|
||||
f.write(struct.pack('<IHHIIII',0xa1b2c3d4,2,4,0,0,65535,1))
|
||||
t=time.time()
|
||||
for i in range(a.count):
|
||||
data=packet(a.src,a.dst,a.port,i)
|
||||
stamp=t+i*.001
|
||||
f.write(struct.pack('<IIII',int(stamp),int(stamp%1*1e6),len(data),len(data))+data)
|
||||
print('wrote',a.count,'packets to',a.pcap)
|
||||
return
|
||||
def mac(value): return bytes.fromhex(value.replace(':',''))
|
||||
smac=mac(Path('/sys/class/net',a.interface,'address').read_text().strip())
|
||||
subprocess.run(['ping','-c1','-W1',a.gateway],capture_output=True)
|
||||
text=subprocess.run(['ip','neigh','show',a.gateway,'dev',a.interface],capture_output=True,text=True,check=True).stdout
|
||||
match=re.search(r'lladdr ([0-9a-f:]{17})',text)
|
||||
if not match: ap.error('gateway MAC unresolved')
|
||||
with socket.socket(socket.AF_PACKET,socket.SOCK_RAW,socket.htons(0x0800)) as s:
|
||||
s.bind((a.interface,0))
|
||||
for i in range(a.count):
|
||||
s.send(packet(a.src,a.dst,a.port,i,smac,mac(match.group(1))))
|
||||
time.sleep(.01)
|
||||
print('sent',a.count,'SYNs',a.src,'->',a.dst+':'+str(a.port))
|
||||
|
||||
if __name__=='__main__': main()
|
||||
Reference in New Issue
Block a user