Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netpfil/common/sniffer.py
39534 views
1
#
2
# SPDX-License-Identifier: BSD-2-Clause
3
#
4
# Copyright (c) 2017 Kristof Provost <[email protected]>
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
8
# are met:
9
# 1. Redistributions of source code must retain the above copyright
10
# notice, this list of conditions and the following disclaimer.
11
# 2. Redistributions in binary form must reproduce the above copyright
12
# notice, this list of conditions and the following disclaimer in the
13
# documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
28
import threading
29
import scapy.all as sp
30
import sys
31
32
class Sniffer(threading.Thread):
33
def __init__(self, args, check_function, recvif, timeout=3, defrag=False):
34
threading.Thread.__init__(self)
35
36
self._sem = threading.Semaphore(0)
37
self._args = args
38
self._timeout = timeout
39
self._recvif = recvif
40
self._check_function = check_function
41
self._defrag = defrag
42
self.correctPackets = 0
43
44
self.start()
45
if not self._sem.acquire(timeout=30):
46
raise Exception("Failed to start sniffer")
47
48
def _checkPacket(self, packet):
49
ret = self._check_function(self._args, packet)
50
if ret:
51
self.correctPackets += 1
52
return ret
53
54
def _startedCb(self):
55
self._sem.release()
56
57
def run(self):
58
self.packets = []
59
# With fragment reassembly we can't stop the sniffer after catching
60
# the good packets, as those have not been reassembled. We must
61
# wait for sniffer to finish and check returned packets instead.
62
if self._defrag == 'IPv4':
63
self.packets = sp.sniff(session=sp.IPSession, iface=self._recvif,
64
timeout=self._timeout, started_callback=self._startedCb)
65
for p in self.packets:
66
self._checkPacket(p)
67
elif self._defrag == 'IPv6':
68
self.packets = sp.sniff(session=sp.DefaultSession, iface=self._recvif,
69
timeout=self._timeout, started_callback=self._startedCb)
70
for p in sp.defragment6(self.packets):
71
self._checkPacket(p)
72
else:
73
self.packets = sp.sniff(iface=self._recvif,
74
stop_filter=self._checkPacket, timeout=self._timeout,
75
started_callback=self._startedCb)
76
77