1*7dc08ffcSJunyu Lai% send, sniff, sr* tests for Scapy 2*7dc08ffcSJunyu Lai 3*7dc08ffcSJunyu Lai~ netaccess 4*7dc08ffcSJunyu Lai 5*7dc08ffcSJunyu Lai############ 6*7dc08ffcSJunyu Lai############ 7*7dc08ffcSJunyu Lai+ Test bridge_and_sniff() using tap sockets 8*7dc08ffcSJunyu Lai 9*7dc08ffcSJunyu Lai~ tap linux 10*7dc08ffcSJunyu Lai 11*7dc08ffcSJunyu Lai= Create two tap interfaces 12*7dc08ffcSJunyu Lai 13*7dc08ffcSJunyu Laiimport subprocess 14*7dc08ffcSJunyu Laifrom threading import Thread 15*7dc08ffcSJunyu Lai 16*7dc08ffcSJunyu Laitap0, tap1 = [TunTapInterface("tap%d" % i) for i in range(2)] 17*7dc08ffcSJunyu Lai 18*7dc08ffcSJunyu Laiif LINUX: 19*7dc08ffcSJunyu Lai for i in range(2): 20*7dc08ffcSJunyu Lai assert subprocess.check_call(["ip", "link", "set", "tap%d" % i, "up"]) == 0 21*7dc08ffcSJunyu Laielse: 22*7dc08ffcSJunyu Lai for i in range(2): 23*7dc08ffcSJunyu Lai assert subprocess.check_call(["ifconfig", "tap%d" % i, "up"]) == 0 24*7dc08ffcSJunyu Lai 25*7dc08ffcSJunyu Lai= Run a sniff thread on the tap1 **interface** 26*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 1.2.3.4 have been sniffed 27*7dc08ffcSJunyu Lait_sniff = Thread( 28*7dc08ffcSJunyu Lai target=sniff, 29*7dc08ffcSJunyu Lai kwargs={"iface": "tap1", "count": 5, "prn": Packet.summary, 30*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"} 31*7dc08ffcSJunyu Lai) 32*7dc08ffcSJunyu Lait_sniff.start() 33*7dc08ffcSJunyu Lai 34*7dc08ffcSJunyu Lai= Run a bridge_and_sniff thread between the taps **sockets** 35*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 1.2.3.4 have been forwarded 36*7dc08ffcSJunyu Lait_bridge = Thread(target=bridge_and_sniff, args=(tap0, tap1), 37*7dc08ffcSJunyu Lai kwargs={"store": False, "count": 5, 'prn': Packet.summary, 38*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"}) 39*7dc08ffcSJunyu Lait_bridge.start() 40*7dc08ffcSJunyu Lai 41*7dc08ffcSJunyu Lai= Send five IP packets from 1.2.3.4 to the tap0 **interface** 42*7dc08ffcSJunyu Laitime.sleep(1) 43*7dc08ffcSJunyu Laisendp([Ether(dst=ETHER_BROADCAST) / IP(src="1.2.3.4") / ICMP()], iface="tap0", 44*7dc08ffcSJunyu Lai count=5) 45*7dc08ffcSJunyu Lai 46*7dc08ffcSJunyu Lai= Wait for the threads 47*7dc08ffcSJunyu Lait_bridge.join() 48*7dc08ffcSJunyu Lait_sniff.join() 49*7dc08ffcSJunyu Lai 50*7dc08ffcSJunyu Lai= Run a sniff thread on the tap1 **interface** 51*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 2.3.4.5 have been sniffed 52*7dc08ffcSJunyu Lait_sniff = Thread( 53*7dc08ffcSJunyu Lai target=sniff, 54*7dc08ffcSJunyu Lai kwargs={"iface": "tap1", "count": 5, "prn": Packet.summary, 55*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "2.3.4.5"} 56*7dc08ffcSJunyu Lai) 57*7dc08ffcSJunyu Lait_sniff.start() 58*7dc08ffcSJunyu Lai 59*7dc08ffcSJunyu Lai= Run a bridge_and_sniff thread between the taps **sockets** 60*7dc08ffcSJunyu Lai* It will "NAT" packets from 1.2.3.4 to 2.3.4.5 and will terminate when 5 IP packets have been forwarded 61*7dc08ffcSJunyu Laidef nat_1_2(pkt): 62*7dc08ffcSJunyu Lai if IP in pkt and pkt[IP].src == "1.2.3.4": 63*7dc08ffcSJunyu Lai pkt[IP].src = "2.3.4.5" 64*7dc08ffcSJunyu Lai del pkt[IP].chksum 65*7dc08ffcSJunyu Lai return pkt 66*7dc08ffcSJunyu Lai return False 67*7dc08ffcSJunyu Lai 68*7dc08ffcSJunyu Lait_bridge = Thread(target=bridge_and_sniff, args=(tap0, tap1), 69*7dc08ffcSJunyu Lai kwargs={"store": False, "count": 5, 'prn': Packet.summary, 70*7dc08ffcSJunyu Lai "xfrm12": nat_1_2, 71*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"}) 72*7dc08ffcSJunyu Lait_bridge.start() 73*7dc08ffcSJunyu Lai 74*7dc08ffcSJunyu Lai= Send five IP packets from 1.2.3.4 to the tap0 **interface** 75*7dc08ffcSJunyu Laitime.sleep(1) 76*7dc08ffcSJunyu Laisendp([Ether(dst=ETHER_BROADCAST) / IP(src="1.2.3.4") / ICMP()], iface="tap0", 77*7dc08ffcSJunyu Lai count=5) 78*7dc08ffcSJunyu Lai 79*7dc08ffcSJunyu Lai= Wait for the threads 80*7dc08ffcSJunyu Lait_bridge.join() 81*7dc08ffcSJunyu Lait_sniff.join() 82*7dc08ffcSJunyu Lai 83*7dc08ffcSJunyu Lai= Delete the tap interfaces 84*7dc08ffcSJunyu Laidel tap0, tap1 85*7dc08ffcSJunyu Lai 86*7dc08ffcSJunyu Lai 87*7dc08ffcSJunyu Lai############ 88*7dc08ffcSJunyu Lai############ 89*7dc08ffcSJunyu Lai+ Test bridge_and_sniff() using tun sockets 90*7dc08ffcSJunyu Lai 91*7dc08ffcSJunyu Lai~ tun linux not_pcapdnet 92*7dc08ffcSJunyu Lai 93*7dc08ffcSJunyu Lai= Create two tun interfaces 94*7dc08ffcSJunyu Lai 95*7dc08ffcSJunyu Laiimport subprocess 96*7dc08ffcSJunyu Laifrom threading import Thread 97*7dc08ffcSJunyu Lai 98*7dc08ffcSJunyu Laitun0, tun1 = [TunTapInterface("tun%d" % i) for i in range(2)] 99*7dc08ffcSJunyu Lai 100*7dc08ffcSJunyu Laiif LINUX: 101*7dc08ffcSJunyu Lai for i in range(2): 102*7dc08ffcSJunyu Lai assert subprocess.check_call(["ip", "link", "set", "tun%d" % i, "up"]) == 0 103*7dc08ffcSJunyu Laielse: 104*7dc08ffcSJunyu Lai for i in range(2): 105*7dc08ffcSJunyu Lai assert subprocess.check_call(["ifconfig", "tun%d" % i, "up"]) == 0 106*7dc08ffcSJunyu Lai 107*7dc08ffcSJunyu Lai= Run a sniff thread on the tun1 **interface** 108*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 1.2.3.4 have been sniffed 109*7dc08ffcSJunyu Lait_sniff = Thread( 110*7dc08ffcSJunyu Lai target=sniff, 111*7dc08ffcSJunyu Lai kwargs={"iface": "tun1", "count": 5, "prn": Packet.summary, 112*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"} 113*7dc08ffcSJunyu Lai) 114*7dc08ffcSJunyu Lait_sniff.start() 115*7dc08ffcSJunyu Lai 116*7dc08ffcSJunyu Lai= Run a bridge_and_sniff thread between the tuns **sockets** 117*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 1.2.3.4 have been forwarded. 118*7dc08ffcSJunyu Lait_bridge = Thread(target=bridge_and_sniff, args=(tun0, tun1), 119*7dc08ffcSJunyu Lai kwargs={"store": False, "count": 5, 'prn': Packet.summary, 120*7dc08ffcSJunyu Lai "xfrm12": lambda pkt: pkt, 121*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"}) 122*7dc08ffcSJunyu Lait_bridge.start() 123*7dc08ffcSJunyu Lai 124*7dc08ffcSJunyu Lai= Send five IP packets from 1.2.3.4 to the tun0 **interface** 125*7dc08ffcSJunyu Laitime.sleep(1) 126*7dc08ffcSJunyu Laiconf.route.add(net="1.2.3.4/32", dev="tun0") 127*7dc08ffcSJunyu Laisend(IP(src="1.2.3.4", dst="1.2.3.4") / ICMP(), count=5) 128*7dc08ffcSJunyu Laiconf.route.delt(net="1.2.3.4/32", dev="tun0") 129*7dc08ffcSJunyu Lai 130*7dc08ffcSJunyu Lai= Wait for the threads 131*7dc08ffcSJunyu Lait_bridge.join() 132*7dc08ffcSJunyu Lait_sniff.join() 133*7dc08ffcSJunyu Lai 134*7dc08ffcSJunyu Lai= Run a sniff thread on the tun1 **interface** 135*7dc08ffcSJunyu Lai* It will terminate when 5 IP packets from 2.3.4.5 have been sniffed 136*7dc08ffcSJunyu Lait_sniff = Thread( 137*7dc08ffcSJunyu Lai target=sniff, 138*7dc08ffcSJunyu Lai kwargs={"iface": "tun1", "count": 5, "prn": Packet.summary, 139*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "2.3.4.5"} 140*7dc08ffcSJunyu Lai) 141*7dc08ffcSJunyu Lait_sniff.start() 142*7dc08ffcSJunyu Lai 143*7dc08ffcSJunyu Lai= Run a bridge_and_sniff thread between the tuns **sockets** 144*7dc08ffcSJunyu Lai* It will "NAT" packets from 1.2.3.4 to 2.3.4.5 and will terminate when 5 IP packets have been forwarded 145*7dc08ffcSJunyu Laidef nat_1_2(pkt): 146*7dc08ffcSJunyu Lai if IP in pkt and pkt[IP].src == "1.2.3.4": 147*7dc08ffcSJunyu Lai pkt[IP].src = "2.3.4.5" 148*7dc08ffcSJunyu Lai del pkt[IP].chksum 149*7dc08ffcSJunyu Lai return pkt 150*7dc08ffcSJunyu Lai return False 151*7dc08ffcSJunyu Lai 152*7dc08ffcSJunyu Lait_bridge = Thread(target=bridge_and_sniff, args=(tun0, tun1), 153*7dc08ffcSJunyu Lai kwargs={"store": False, "count": 5, 'prn': Packet.summary, 154*7dc08ffcSJunyu Lai "xfrm12": nat_1_2, 155*7dc08ffcSJunyu Lai "lfilter": lambda p: IP in p and p[IP].src == "1.2.3.4"}) 156*7dc08ffcSJunyu Lait_bridge.start() 157*7dc08ffcSJunyu Lai 158*7dc08ffcSJunyu Lai= Send five IP packets from 1.2.3.4 to the tun0 **interface** 159*7dc08ffcSJunyu Laitime.sleep(1) 160*7dc08ffcSJunyu Laiconf.route.add(net="1.2.3.4/32", dev="tun0") 161*7dc08ffcSJunyu Laisend(IP(src="1.2.3.4", dst="1.2.3.4") / ICMP(), count=5) 162*7dc08ffcSJunyu Laiconf.route.delt(net="1.2.3.4/32", dev="tun0") 163*7dc08ffcSJunyu Lai 164*7dc08ffcSJunyu Lai= Wait for the threads 165*7dc08ffcSJunyu Lait_bridge.join() 166*7dc08ffcSJunyu Lait_sniff.join() 167*7dc08ffcSJunyu Lai 168*7dc08ffcSJunyu Lai= Delete the tun interfaces 169*7dc08ffcSJunyu Laidel tun0, tun1 170