-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·146 lines (127 loc) · 4.06 KB
/
app.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
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
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from time import sleep
import pyshark
import json
from threading import Lock
DEBUG = False
NAMESPACE = '/sniffer'
app = Flask(__name__)
socketio = SocketIO(app, engineio_logger=DEBUG, async_mode="threading")
# Initial capture - all packets
cap = [
pyshark.LiveCapture(interface='wlp2s0', only_summaries=1),
None
# pyshark.LiveCapture(interface='wlp2s0')
]
# Set up a worker and a worker threa
worker = None
worker_thread = None
class Worker(object):
def __init__(self, socketio, cap):
self.socketio = socketio
self.cap = cap;
self.active = False;
def run(self):
self.active = True
# Single sniffer (no port data)
for packet in self.cap[0].sniff_continuously():
if self.active is True:
self.socketio.emit('packet', {'pkt': self.format_packet(packet, None)}, namespace=NAMESPACE)
sleep(0.1)
else:
return
# Dobule sniffer (extra data)
# for summary, full in zip(self.cap[0], self.cap[1]):
# if self.active is True:
# self.socketio.emit('packet', {'pkt': self.format_packet(summary, full)}, namespace=NAMESPACE)
# else:
# return
def start(self):
self.active = True
def stop(self):
self.active = False
def set_capture(self, cap):
self.cap = cap
def format_packet(self, pkt, pkt2):
packet = {
# "delta": pkt.delta,
"no": pkt.no,
"source": pkt.source,
"destination": pkt.destination,
"info": pkt.info,
"length": pkt.length,
"protocol": pkt.protocol,
"summary_line": pkt.summary_line,
"time": pkt.time
}
# Check for transport layer ports
if hasattr(pkt2, 'udp'):
packet["src_port"] = pkt2.udp.srcport
packet["dst_port"] = pkt2.udp.dstport
elif hasattr(pkt2, 'tcp'):
packet["src_port"] = pkt2.tcp.srcport
packet["dst_port"] = pkt2.tcp.dstport
return packet
# Routes and socket events
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect', namespace=NAMESPACE)
def sniff():
print('Sniffer connected.')
global worker
global worker_thread
if worker is not None:
worker.stop()
if worker_thread is not None:
worker_thread.join(5)
worker = Worker(socketio, cap)
# worker_thread = socketio.start_background_task(target=worker.run)
socketio.emit('successful connection', namespace=NAMESPACE)
@socketio.on('stop', namespace=NAMESPACE)
def stop():
print('Sniffer stopped.')
global worker
worker.stop()
@socketio.on('start', namespace=NAMESPACE)
def stop():
print('Sniffer started.')
global worker
global worker_thread
if worker_thread is not None:
worker_thread.join(5)
worker.start()
worker_thread = socketio.start_background_task(target=worker.run)
@socketio.on('filter', namespace=NAMESPACE)
def filter(df):
print('Filter received: ' + df)
# Edit 'all' filter to be '' (all packets)
if df == 'all':
df = ''
# Stop the existing worker thread
global worker
global worker_thread
worker.stop()
print('Worker stopped.')
cap = [
pyshark.LiveCapture(interface='wlp2s0', only_summaries=1, display_filter=df),
None
# pyshark.LiveCapture(interface='wlp2s0', display_filter=df)
]
# Restart the worker thread with new data
if worker_thread is not None:
worker_thread.join(5)
print('Thread joined.')
worker.cap = cap;
worker_thread = socketio.start_background_task(target=worker.run)
print('New thread started.')
# Notify filter change
socketio.emit('filter changed', namespace=NAMESPACE);
@socketio.on('disconnect', namespace=NAMESPACE)
def disconnect():
print('Sniffer disconnected.')
global worker
worker.stop()
if __name__ == '__main__':
socketio.run(app)