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