1*7dc08ffcSJunyu Lai% Regression tests for Scapy Answering Machines 2*7dc08ffcSJunyu Lai 3*7dc08ffcSJunyu Lai# More informations at http://www.secdev.org/projects/UTscapy/ 4*7dc08ffcSJunyu Lai 5*7dc08ffcSJunyu Lai 6*7dc08ffcSJunyu Lai############ 7*7dc08ffcSJunyu Lai############ 8*7dc08ffcSJunyu Lai+ Answering Machines 9*7dc08ffcSJunyu Lai 10*7dc08ffcSJunyu Lai= Generic answering machine mocker 11*7dc08ffcSJunyu Laiimport mock 12*7dc08ffcSJunyu Lai@mock.patch("scapy.ansmachine.sniff") 13*7dc08ffcSJunyu Laidef test_am(cls_name, packet_query, check_reply, mock_sniff, **kargs): 14*7dc08ffcSJunyu Lai def sniff(*args,**kargs): 15*7dc08ffcSJunyu Lai kargs["prn"](packet_query) 16*7dc08ffcSJunyu Lai mock_sniff.side_effect = sniff 17*7dc08ffcSJunyu Lai am = cls_name(**kargs) 18*7dc08ffcSJunyu Lai am.send_reply = check_reply 19*7dc08ffcSJunyu Lai am() 20*7dc08ffcSJunyu Lai 21*7dc08ffcSJunyu Lai 22*7dc08ffcSJunyu Lai= BOOT_am 23*7dc08ffcSJunyu Laidef check_BOOTP_am_reply(packet): 24*7dc08ffcSJunyu Lai assert(BOOTP in packet and packet[BOOTP].op == 2) 25*7dc08ffcSJunyu Lai assert(packet[BOOTP].yiaddr == "192.168.1.128" and packet[BOOTP].giaddr == "192.168.1.1") 26*7dc08ffcSJunyu Lai 27*7dc08ffcSJunyu Laitest_am(BOOTP_am, 28*7dc08ffcSJunyu Lai IP()/UDP()/BOOTP(op=1), 29*7dc08ffcSJunyu Lai check_BOOTP_am_reply) 30*7dc08ffcSJunyu Lai 31*7dc08ffcSJunyu Lai 32*7dc08ffcSJunyu Lai= DHCP_am 33*7dc08ffcSJunyu Laidef check_DHCP_am_reply(packet): 34*7dc08ffcSJunyu Lai assert(DHCP in packet and len(packet[DHCP].options)) 35*7dc08ffcSJunyu Lai assert(("domain", "localnet") in packet[DHCP].options) 36*7dc08ffcSJunyu Lai 37*7dc08ffcSJunyu Laitest_am(DHCP_am, 38*7dc08ffcSJunyu Lai IP()/UDP()/BOOTP(op=1)/DHCP(), 39*7dc08ffcSJunyu Lai check_DHCP_am_reply) 40*7dc08ffcSJunyu Lai 41*7dc08ffcSJunyu Lai 42*7dc08ffcSJunyu Lai= ARP_am 43*7dc08ffcSJunyu Laidef check_ARP_am_reply(packet): 44*7dc08ffcSJunyu Lai assert(ARP in packet and packet[ARP].psrc == "10.28.7.1") 45*7dc08ffcSJunyu Lai assert(packet[ARP].hwsrc == "00:01:02:03:04:05") 46*7dc08ffcSJunyu Lai 47*7dc08ffcSJunyu Laitest_am(ARP_am, 48*7dc08ffcSJunyu Lai Ether()/ARP(pdst="10.28.7.1"), 49*7dc08ffcSJunyu Lai check_ARP_am_reply, 50*7dc08ffcSJunyu Lai IP_addr="10.28.7.1", 51*7dc08ffcSJunyu Lai ARP_addr="00:01:02:03:04:05") 52*7dc08ffcSJunyu Lai 53*7dc08ffcSJunyu Lai 54*7dc08ffcSJunyu Lai= DNS_am 55*7dc08ffcSJunyu Laidef check_DNS_am_reply(packet): 56*7dc08ffcSJunyu Lai assert(DNS in packet and packet[DNS].ancount == 1) 57*7dc08ffcSJunyu Lai assert(packet[DNS].an.rdata == b"192.168.1.1") 58*7dc08ffcSJunyu Lai 59*7dc08ffcSJunyu Laitest_am(DNS_am, 60*7dc08ffcSJunyu Lai IP()/UDP()/DNS(qd=DNSQR(qname="www.secdev.org")), 61*7dc08ffcSJunyu Lai check_DNS_am_reply) 62*7dc08ffcSJunyu Lai 63*7dc08ffcSJunyu Lai= DHCPv6_am - Basic Instantiaion 64*7dc08ffcSJunyu Lai~ osx netaccess 65*7dc08ffcSJunyu Laia = DHCPv6_am() 66*7dc08ffcSJunyu Laia.usage() 67*7dc08ffcSJunyu Lai 68*7dc08ffcSJunyu Laia.parse_options(dns="2001:500::1035", domain="localdomain, local", duid=None, 69*7dc08ffcSJunyu Lai iface=conf.iface6, advpref=255, sntpservers=None, 70*7dc08ffcSJunyu Lai sipdomains=None, sipservers=None, 71*7dc08ffcSJunyu Lai nisdomain=None, nisservers=None, 72*7dc08ffcSJunyu Lai nispdomain=None, nispservers=None, 73*7dc08ffcSJunyu Lai bcmcsdomains=None, bcmcsservers=None, 74*7dc08ffcSJunyu Lai debug=1) 75*7dc08ffcSJunyu Lai 76*7dc08ffcSJunyu Lai= DHCPv6_am - SOLICIT 77*7dc08ffcSJunyu Lai~ osx netaccess 78*7dc08ffcSJunyu Laireq = IPv6(dst="::1")/UDP()/DHCP6(msgtype=1)/DHCP6OptClientId(duid=DUID_LLT()) 79*7dc08ffcSJunyu Laiassert a.is_request(req) 80*7dc08ffcSJunyu Laires = a.make_reply(req) 81*7dc08ffcSJunyu Laiassert not a.is_request(res) 82*7dc08ffcSJunyu Laiassert res[DHCP6_Advertise] 83*7dc08ffcSJunyu Laiassert res[DHCP6OptPref].prefval == 255 84*7dc08ffcSJunyu Laiassert res[DHCP6OptReconfAccept] 85*7dc08ffcSJunyu Laia.print_reply(req, res) 86*7dc08ffcSJunyu Lai 87*7dc08ffcSJunyu Lai= DHCPv6_am - INFO-REQUEST 88*7dc08ffcSJunyu Lai~ osx netaccess 89*7dc08ffcSJunyu Laireq = IPv6(dst="::1")/UDP()/DHCP6(msgtype=11)/DHCP6OptClientId(duid=DUID_LLT()) 90*7dc08ffcSJunyu Laiassert a.is_request(req) 91*7dc08ffcSJunyu Laires = a.make_reply(req) 92*7dc08ffcSJunyu Laiassert not a.is_request(res) 93*7dc08ffcSJunyu Laiassert res[DHCP6_Reply] 94*7dc08ffcSJunyu Laiassert "local" in res[DHCP6OptDNSDomains].dnsdomains 95*7dc08ffcSJunyu Laia.print_reply(req, res) 96*7dc08ffcSJunyu Lai 97*7dc08ffcSJunyu Lai= DHCPv6_am - REQUEST 98*7dc08ffcSJunyu Lai~ osx netaccess 99*7dc08ffcSJunyu Laireq = IPv6(dst="::1")/UDP()/DHCP6(msgtype=3)/DHCP6OptClientId(duid=DUID_LLT())/DHCP6OptServerId(duid=a.duid) 100*7dc08ffcSJunyu Laiassert a.is_request(req) 101*7dc08ffcSJunyu Laires = a.make_reply(req) 102*7dc08ffcSJunyu Laiassert not a.is_request(res) 103*7dc08ffcSJunyu Laiassert res[UDP].dport == 546 104*7dc08ffcSJunyu Laiassert res[DHCP6_Solicit] 105*7dc08ffcSJunyu Laia.print_reply(req, res) 106*7dc08ffcSJunyu Lai 107*7dc08ffcSJunyu Lai= WiFi_am 108*7dc08ffcSJunyu Laiimport mock 109*7dc08ffcSJunyu Lai@mock.patch("scapy.layers.dot11.sniff") 110*7dc08ffcSJunyu Laidef test_WiFi_am(packet_query, check_reply, mock_sniff, **kargs): 111*7dc08ffcSJunyu Lai def sniff(*args,**kargs): 112*7dc08ffcSJunyu Lai kargs["prn"](packet_query) 113*7dc08ffcSJunyu Lai mock_sniff.side_effect = sniff 114*7dc08ffcSJunyu Lai am = WiFi_am(**kargs) 115*7dc08ffcSJunyu Lai am.send_reply = check_reply 116*7dc08ffcSJunyu Lai am() 117*7dc08ffcSJunyu Lai 118*7dc08ffcSJunyu Laidef check_WiFi_am_reply(packet): 119*7dc08ffcSJunyu Lai assert(isinstance(packet, list) and len(packet) == 2) 120*7dc08ffcSJunyu Lai assert(TCP in packet[0] and Raw in packet[0] and raw(packet[0][Raw]) == b"5c4pY") 121*7dc08ffcSJunyu Lai 122*7dc08ffcSJunyu Laitest_WiFi_am(Dot11(FCfield="to-DS")/IP()/TCP()/"Scapy", 123*7dc08ffcSJunyu Lai check_WiFi_am_reply, 124*7dc08ffcSJunyu Lai iffrom="scapy0", ifto="scapy1", replace="5c4pY", pattern="Scapy") 125