-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniff.py
executable file
·69 lines (57 loc) · 1.92 KB
/
sniff.py
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
#!/usr/bin/env python3
from filters.filter_from_str import filter_from_str
from packets.visitors.first_verbosity_visitor import FirstVerbosityVisitor
from packets.visitors.second_verbosity_visitor import SecondVerbosityVisitor
from packets.visitors.zero_verbosity_visitor import ZeroVerbosityVisitor
from parsers.main_parser import get_parser
from dump_writers.empty_writer import EmptyWriter
from dump_writers.pcap_writer import PcapWriter
from output_writer.formatted_writer import FormattedWriter
from receivers.socket_receiver import SocketReceiver
from sniffer.sinffer import Sniffer
def main():
parser = get_parser()
args = parser.parse_args()
args.verbosity = min(args.verbosity, 2)
args.ifaces = list() if args.ifaces is None else args.ifaces
path = args.dump_file
verbosity_to_visitor = [
ZeroVerbosityVisitor(),
FirstVerbosityVisitor(),
SecondVerbosityVisitor()
]
try:
filter_func = filter_from_str(args.filter)
except ValueError as e:
print(e)
return
try:
if path is not None:
dump_writer = PcapWriter(path, args.max_size)
else:
dump_writer = EmptyWriter()
out_writer = FormattedWriter(
verbosity_to_visitor[args.verbosity],
args.time
)
try:
receiver = SocketReceiver(*args.ifaces)
except OSError as e:
print(f'Error: {e}')
return 0
except FileNotFoundError:
print(f'File {args.dump_file} is not found or could not be created')
return
except PermissionError:
print(
f'Permission error. '
f'You need to use root privileges to run the program'
)
return
except ValueError as e:
print(e)
return
sniffer = Sniffer(dump_writer, out_writer, receiver, filter_func)
sniffer.start_sniffing()
if __name__ == '__main__':
main()