1 #! /usr/bin/env python3
2 # Used to inject various malformed packets
9 logging.getLogger("scapy").setLevel(logging.CRITICAL)
11 from scapy.all import IP, ICMP, IPOption
12 import scapy.layers.all
13 from scapy.layers.inet import ICMPEcho_am
14 from scapy.layers.tuntap import TunTapInterface
16 SRC_ADDR = "192.0.2.14"
17 DST_ADDR = "192.0.2.15"
22 # fill opts with nop (0x01)
28 # Create and configure a tun interface with an RFC5737 nonrouteable address
29 create_proc = subprocess.run(
30 args=["ifconfig", "tun", "create"],
34 iface = create_proc.stdout.strip()
35 tun = TunTapInterface(iface)
36 with open("tun.txt", "w") as f:
38 subprocess.run(["ifconfig", tun.iface, "up"])
39 subprocess.run(["ifconfig", tun.iface, SRC_ADDR, DST_ADDR])
41 ping = subprocess.Popen(
42 args=["/sbin/ping", "-v", "-c1", "-t1", DST_ADDR],
45 # Wait for /sbin/ping to ping us
48 # Construct the response packet
50 # Sending reply with IP options
54 options=IPOption(opts)
55 )/ICMP(type=0, code=0, id=echo_req.payload.id)/echo_req.payload.payload
57 # packet in packet (inner has options)
62 options=IPOption(opts)
63 )/ICMP(type=0, code=0, id=echo_req.payload.id)/echo_req.payload.payload
67 )/ICMP(type=3, code=1) # host unreach
69 echo_reply = outer/inner
71 # Sending normal echo reply
75 )/ICMP(type=0, code=0, id=echo_req.payload.id)/echo_req.payload.payload
77 print("unknown mode {}".format(mode))
81 outs, errs = ping.communicate()
83 sys.exit(ping.returncode)