File: bpf.uts

package info (click to toggle)
scapy 2.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,956 kB
  • sloc: python: 163,618; sh: 90; makefile: 11
file content (166 lines) | stat: -rw-r--r-- 3,359 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
% Regression tests for Scapy BPF mode

# More information at http://www.secdev.org/projects/UTscapy/


############
############
+ Addresses manipulation functions

= Get the packet IPv4 address configured on conf.iface

get_if_raw_addr(conf.iface)


= Get the MAC address of conf.iface

get_if_hwaddr(conf.iface)

= Get the MAC address of conf.loopback_name

get_if_hwaddr(conf.loopback_name) == "00:00:00:00:00:00"


############
############
+ BPF related functions

= Imports

from scapy.arch.bpf.supersocket import L3bpfSocket, L2bpfListenSocket, L2bpfSocket

= Get a BPF handler
~ needs_root

from scapy.arch.bpf.supersocket import get_dev_bpf
fd, _ = get_dev_bpf()

= Attach a BPF filter
~ needs_root libpcap

from scapy.arch.bpf.supersocket import attach_filter
attach_filter(fd, "arp or icmp", conf.iface)


= Get network interfaces list

iflist = get_if_list()
len(iflist) > 0

= Misc functions
~ needs_root

from scapy.arch.bpf.supersocket import bpf_select

l = bpf_select([L2bpfSocket()])
l = bpf_select([L2bpfSocket(), sys.stdin.fileno()])


############
############
+ BPF sockets

= L2bpfListenSocket - initialization variants
~ needs_root

L2bpfListenSocket()
L2bpfListenSocket(iface=conf.iface)
L2bpfListenSocket(promisc=True)
L2bpfListenSocket(filter="icmp")
L2bpfListenSocket(iface=conf.iface, promisc=True, filter="icmp")


= L2bpfListenSocket - set_*()
~ needs_root

s = L2bpfListenSocket()
s.set_promisc(0)
s.set_nonblock(1)
s.set_promisc(0)
s.close()

s = L2bpfListenSocket()
s.set_nonblock(set_flag=False)
s.set_nonblock(set_flag=True)
s.set_nonblock(set_flag=False)
s.close()

= L2bpfListenSocket - get_*()
~ needs_root

s = L2bpfListenSocket()
blen = s.get_blen()
blen > 0 and type(blen) == int
s.close()

s = L2bpfListenSocket()
stats = s.get_stats()
len(stats) == 2 and type(stats) == tuple
s.close()


= L2bpfListenSocket - other methods
~ needs_root

s = L2bpfListenSocket()
type(s.fileno()) == int
s.close()

s = L2bpfListenSocket()
guessed = s.guess_cls()
issubclass(guessed, Packet)
s.close()

= L2bpfListenSocket - read failure
~ needs_root

from unittest import mock

@mock.patch("scapy.arch.bpf.supersocket.os.read")
def _test_osread(osread):
    osread.side_effect = OSError()
    s = L2bpfListenSocket()
    assert s.recv_raw() == (None, None, None)

_test_osread()

= L2bpfSocket - nonblock_recv()
~ needs_root

s = L2bpfSocket()
s.nonblock_recv()
s.close()


= L*bpfSocket - send()        
~ needs_root
                              
s = L2bpfSocket()             
s.send(Ether()/IP(dst="8.8.8.8")/ICMP())
                              
s = L3bpfSocket()             
s.send(IP(dst="8.8.8.8")/ICMP())
                              
s = L3bpfSocket()             
s.assigned_interface = conf.loopback_name
s.send(IP(dst="8.8.8.8")/ICMP())

= L3bpfSocket - send and sniff on loopback
~ needs_root

localhost_ip = conf.ifaces[conf.loopback_name].ips[4][0]

def cb():
    # Send a ping to the loopback IP.
    s = L3bpfSocket(iface=conf.loopback_name)
    s.send(IP(dst=localhost_ip)/ICMP(seq=1001))

t = AsyncSniffer(iface=conf.loopback_name, started_callback=cb)
t.start()
time.sleep(1)
t.stop()
t.join(timeout=1)

# We expect to see our packet and kernel's response.
len(t.results.filter(lambda p: (
    IP in p and ICMP in p and (p[IP].src == localhost_ip or p[IP].dst == localhost_ip) and p[ICMP].seq == 1001))) == 2