1% Regression tests for Scapy BPF mode
2
3# More informations at http://www.secdev.org/projects/UTscapy/
4
5
6############
7############
8+ Addresses manipulation functions
9
10= Get the packet IPv4 address configured on conf.iface
11
12get_if_raw_addr(conf.iface)
13
14
15= Get the packed MAC address of conf.iface
16
17get_if_raw_hwaddr(conf.iface)
18
19= Get the packed MAC address of LOOPBACK_NAME
20
21get_if_raw_hwaddr(LOOPBACK_NAME) == (ARPHDR_LOOPBACK, b'\x00'*6)
22
23
24############
25############
26+ BPF related functions
27
28= Get a BPF handler
29~ needs_root
30
31from scapy.arch.bpf.supersocket import get_dev_bpf
32fd, _ = get_dev_bpf()
33
34= Attach a BPF filter
35~ needs_root
36
37from scapy.arch.bpf.supersocket import attach_filter
38attach_filter(fd, conf.iface, "arp or icmp")
39
40
41= Get network interfaces list
42
43iflist = get_if_list()
44len(iflist) > 0
45
46
47= Get working network interfaces
48~ needs_root
49
50from scapy.arch.bpf.core import get_working_ifaces
51ifworking = get_working_ifaces()
52len(ifworking)
53
54from scapy.arch.bpf.core import get_working_if
55len(ifworking) and get_working_if() == ifworking[0][0]
56
57
58= Misc functions
59~ needs_root
60
61from scapy.arch.bpf.supersocket import isBPFSocket, bpf_select
62isBPFSocket(L2bpfListenSocket()) and isBPFSocket(L2bpfSocket()) and isBPFSocket(L3bpfSocket())
63
64l = bpf_select([L2bpfSocket()])
65l = bpf_select([L2bpfSocket(), sys.stdin.fileno()])
66
67
68############
69############
70+ BPF sockets
71
72= L2bpfListenSocket - initialization variants
73~ needs_root
74
75L2bpfListenSocket()
76L2bpfListenSocket(iface=conf.iface)
77L2bpfListenSocket(promisc=True)
78L2bpfListenSocket(filter="icmp")
79L2bpfListenSocket(iface=conf.iface, promisc=True, filter="icmp")
80
81
82= L2bpfListenSocket - set_*()
83~ needs_root
84
85s = L2bpfListenSocket()
86s.set_promisc(0)
87s.set_nonblock(1)
88s.set_promisc(0)
89s.close()
90
91s = L2bpfListenSocket()
92s.set_nonblock(set_flag=False)
93s.set_nonblock(set_flag=True)
94s.set_nonblock(set_flag=False)
95s.close()
96
97= L2bpfListenSocket - recv as nonblocking
98~ needs_root
99
100s = L2bpfListenSocket()
101s.set_nonblock(set_flag=True)
102
103def test_nonblock_recv(s):
104    for i in range(1, 100):
105        a = s.recv()
106        if not a:
107            return True
108    return False
109
110assert test_nonblock_recv(s)
111
112= L2bpfListenSocket - get_*()
113~ needs_root
114
115s = L2bpfListenSocket()
116blen = s.get_blen()
117blen > 0 and type(blen) == int
118s.close()
119
120s = L2bpfListenSocket()
121stats = s.get_stats()
122len(stats) == 2 and type(stats) == tuple
123s.close()
124
125
126= L2bpfListenSocket - other methods
127~ needs_root
128
129s = L2bpfListenSocket()
130type(s.fileno()) == int
131s.close()
132
133s = L2bpfListenSocket()
134guessed = s.guess_cls()
135issubclass(guessed, Packet)
136s.close()
137
138
139= L2bpfSocket - nonblock_recv()
140~ needs_root
141
142s = L2bpfSocket()
143s.nonblock_recv()
144s.close()
145
146
147= L*bpfSocket - send()
148~ needs_root
149
150s = L2bpfSocket()
151s.send(Ether()/IP(dst="8.8.8.8")/ICMP())
152
153s = L3bpfSocket()
154s.send(IP(dst="8.8.8.8")/ICMP())
155
156s = L3bpfSocket()
157s.assigned_interface = LOOPBACK_NAME
158s.send(IP(dst="8.8.8.8")/ICMP())
159