-
Notifications
You must be signed in to change notification settings - Fork 0
/
Traffic.py
75 lines (55 loc) · 1.95 KB
/
Traffic.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
70
71
72
73
74
75
#!/usr/bin/env python3
#Traffic flow
#!Author: Sacco Giuseppe
from scapy.all import *
from threading import Thread, Lock
import socket
import netifaces as ni
from Dispatcher import Dispatcher
from Visual import Visual
from Buffer import Buffer
from Pkt import PacketsStr
#packet handler
def traffic_handler(pkt):
global buff
with buff._mutex:
buff.add(pkt)
buff._mutex.notify()
#-------------------------------------------------------------------------------------------#
def main():
#interface on which capture packets
interface = input('interface: ')
if len(interface )== 0 :
print('Not valid interface.')
sys.exit()
#own ip
ipaddress = ni.ifaddresses(interface)[ni.AF_INET][0]['addr']
#-------------------------------------------------------------------------------------------#
#two data structure used to analyze packets and store the flows
global buff
buff = Buffer()
pkt_str = PacketsStr()
#threads used to analyze the flows and their content
dispatcher = Dispatcher(buff, pkt_str, ipaddress)
visual = Visual(pkt_str)
dispatcher.start()
visual.start()
#starting to sniff packets
capture = sniff(filter='ip', session=IPSession, prn=traffic_handler, count=0, iface=interface)
#at the and of the sniff, when Ctrl+C key is pressed, termination variable is set to notify threads
dispatcher.term.set()
visual.term.set()
#to wake the threads which are waiting for new packets
time.sleep(1)
with buff._mutex:
buff._mutex.notify()
with pkt_str._lockPkts:
pkt_str._lockPkts.notify()
dispatcher.join()
visual.join()
#print some information about the number of unknown and known packets
print("\nUnknown packets: %d" % len(pkt_str.unknown))
print("Packets of unknown process: %d" % len(pkt_str.unanalizedPkt))
print("Total packets: %d" % len(capture))
if __name__ == "__main__":
main()