-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic_mon.py
312 lines (268 loc) · 9.49 KB
/
traffic_mon.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from scapy.all import *
from scapy.contrib import gtp
from scapy.contrib import gtp_v2
from collections import Counter
from time import time
from influxdb import InfluxDBClient
brs11 = "" #brs11 link id
brs1u = "" #brs1u link id
brspgw = "" #brspgw link id
brsgi = "" #brsgi link id
packet_counts = Counter()
packet_bytes = {}
imsi_counts = Counter()
imsi_bytes = {}
teid_counts = Counter()
teid_bytes = {}
def parsing(pkt):
try:
if (pkt[0][1].proto == 17 and pkt[0][2].dport == 2123): #GTPv2 packets
parse_imsi(pkt,"GTPv2")
elif (pkt[0][1].proto == 1 and pkt[0][4].dport == 2123): #ICMP in s11 gtpv2 ternel
parse_imsi(pkt,"GTPv2_ICMP")
elif (pkt[0][1].proto == 17 and pkt[0][2].dport == 2152): #GTP packets
parse_teid(pkt,"GTP")
elif (pkt[0][1].proto == 1 and pkt[0][4].dport == 2152): #ICMP in s1u gtp ternel
parse_teid(pkt,"GTP_ICMP")
else:
parse_normal(pkt)
except AttributeError as error:
pkt[0].show()
return
def parse_imsi(pkt,proto):
try:#create session res
#get imsi information
imsi = pkt[0][4].IE_list[0].IMSI
#get 5-tuple information
src_ip = pkt[0][1].src
dst_ip = pkt[0][1].dst
protocol = proto
sport = pkt[0][2].sport
dport = pkt[0][2].dport
info = tuple([src_ip,dst_ip,protocol,sport,dport,imsi])
imsi_counts.update([info])
pkt_bytes = pkt[0][1].len
if info in imsi_bytes.keys():
pkt_bytes += imsi_bytes[info]
imsi_bytes.update({info : pkt_bytes})
else:
imsi_bytes.update({info : pkt_bytes})
except AttributeError as error: #create session res, modify bearer req/res
if (proto == "GTPv2_ICMP"):
#get 5-tuple information
src_ip = pkt[0][1].src
dst_ip = pkt[0][1].dst
protocol = proto
sport = pkt[0][2].sport
dport = pkt[0][2].dport
imsi = 0 #temp
info = tuple([src_ip,dst_ip,protocol,sport,dport,imsi])
imsi_counts.update([info])
pkt_bytes = pkt[0][1].len
if info in imsi_bytes.keys():
pkt_bytes += imsi_bytes[info]
imsi_bytes.update({info : pkt_bytes})
else:
imsi_bytes.update({info : pkt_bytes})
else:
#get 5-tuple information
src_ip = pkt[0][1].src
dst_ip = pkt[0][1].dst
protocol = proto
sport = pkt[0][2].sport
dport = pkt[0][2].dport
imsi = 0 #temp
info = tuple([src_ip,dst_ip,protocol,sport,dport,imsi])
imsi_counts.update([info])
pkt_bytes = pkt[0][1].len
if info in imsi_bytes.keys():
pkt_bytes += imsi_bytes[info]
imsi_bytes.update({info : pkt_bytes})
else:
imsi_bytes.update({info : pkt_bytes})
return
def parse_teid(pkt,proto):
try:
#get imsi information
teid = pkt[0][3].teid
#get GRE ternel information
t_src_ip = pkt[0][1].src
t_dst_ip = pkt[0][1].dst
t_protocol = proto
#get real packet information
src_ip = pkt[0][4].src
dst_ip = pkt[0][4].dst
protocol = pkt[0][4].proto
sport = pkt[0][5].sport
dport = pkt[0][5].dport
info = tuple([teid,t_src_ip,t_dst_ip,t_protocol,src_ip,dst_ip,protocol,sport,dport])
teid_counts.update([info])
pkt_bytes = pkt[0][1].len
if info in teid_bytes.keys():
pkt_bytes += teid_bytes[info]
teid_bytes.update({info : pkt_bytes})
else:
teid_bytes.update({info : pkt_bytes})
except AttributeError as error: #icmp
#get imsi information
teid = pkt[0][5].teid
#get GRE ternel information
t_src_ip = pkt[0][3].src
t_dst_ip = pkt[0][3].dst
t_protocol = proto
#get real packet information
src_ip = pkt[0][6].src
dst_ip = pkt[0][6].dst
protocol = pkt[0][6].proto
sport = pkt[0][7].sport
dport = pkt[0][7].dport
info = tuple([teid,t_src_ip,t_dst_ip,t_protocol,src_ip,dst_ip,protocol,sport,dport])
teid_counts.update([info])
pkt_bytes = pkt[0][1].len
if info in teid_bytes.keys():
pkt_bytes += teid_bytes[info]
teid_bytes.update({info : pkt_bytes})
else:
teid_bytes.update({info : pkt_bytes})
return
def parse_normal(pkt):
try:
#get 5 tuple information
src_ip = pkt[0][1].src
dst_ip = pkt[0][1].dst
protocol = pkt[0][1].proto
sport = pkt[0][2].sport
dport = pkt[0][2].dport
five_tuple = tuple([src_ip,dst_ip,protocol,sport,dport])
packet_counts.update([five_tuple])
pkt_bytes = pkt[0][1].len
if five_tuple in packet_bytes.keys():
pkt_bytes += packet_bytes[five_tuple]
packet_bytes.update({five_tuple : pkt_bytes})
else:
packet_bytes.update({five_tuple : pkt_bytes})
except AttributeError as error:
pkt[0].show()
return
def push_teid_data(teid_data, measurement):
for k,v in teid_data.items():
json_body = [
{
"measurement": measurement,
"tags": {"PM": "dpnm",
"interface": "br-s1u",
"teid": k[0],
"t_src_ip": k[1],
"t_dst_ip": k[2],
"t_protocol": k[3],
"src_ip": k[4],
"dst_ip": k[5],
"protocol": k[6],
"src_port": k[7],
"dst_port": k[8]
},
"time": (int(time())*1000000000),
"fields": {
measurement: v
}
}
]
client = InfluxDBClient('141.223.82.62', 8086, 'root', 'root', 'packets')
client.switch_database('packets')
client.write_points(json_body)
return
def push_imsi_data(imsi_data, measurement):
for k,v in imsi_data.items():
#get interface
if("192.168.103." in k[0] or "192.168.103." in k[1]):
iface = "br-s11"
elif("192.168.104." in k[0] or "192.168.104." in k[1]):
iface = "br-spgw"
elif("192.168.105." in k[0] or "192.168.105." in k[1]):
iface = "br-s1u"
elif("13.1.1." in k[0] or "16.255.255." in k[1] or "13.1.1." in k[1] or "16.255.255." in k[0]):
iface = "br-sgi"
else:
iface = "others"
json_body = [
{
"measurement": measurement,
"tags": {"PM": "dpnm",
"interface": iface,
"src_ip": k[0],
"dst_ip": k[1],
"protocol": k[2],
"src_port": k[3],
"dst_port": k[4],
"IMSI": k[5]
},
"time": (int(time())*1000000000),
"fields": {
measurement: v
}
}
]
client = InfluxDBClient('141.223.82.62', 8086, 'root', 'root', 'packets')
client.switch_database('packets')
client.write_points(json_body)
def push_data(pkt_data,measurement):
for k,v in pkt_data.items():
#get protocol type
if (k[3] == 2123 or k[4] == 2152):
protocol = "gtp"
else:
if (k[2] == 17):
protocol = "udp"
elif (k[2] == 1):
protocol = "icmp"
elif (k[2] == 6):
protocol = "tcp"
else:
protocol = "other"
#get interface
if("192.168.103." in k[0] or "192.168.103." in k[1]):
iface = "brs11"
elif("192.168.104." in k[0] or "192.168.104." in k[1]):
iface = "brspgw"
elif("192.168.105." in k[0] or "192.168.105." in k[1]):
iface = "brs1u"
elif("13.1.1." in k[0] or "16.255.255." in k[1] or "13.1.1." in k[1] or "16.255.255." in k[0]):
iface = "brsgi"
else:
iface = "other"
#create json body
json_body = [
{
"measurement": measurement,
"tags": {"PM": "dpnm",
"interface": iface,
"src_ip": k[0],
"dst_ip": k[1],
"protocol": protocol,
"src_port": k[3],
"dst_port": k[4]
},
"time": (int(time())*1000000000),
"fields": {
measurement: v
}
}
]
client = InfluxDBClient('141.223.82.62', 8086, 'root', 'root', 'packets')
client.switch_database('packets')
client.write_points(json_body)
return
while True:
#sniff packetsv
pkts=sniff(iface=[brs11,brs1u,brsgi,brspgw], filter="ip", prn=parsing, timeout=10)
#push_data to influx db
#normal packets
push_data(packet_counts,"packet_counts")
push_data(packet_bytes,"packet_bytes")
#aggregates traffic by each ue
push_imsi_data(imsi_counts,"imsi_counts")
push_imsi_data(imsi_bytes,"imsi_bytes")
#aggregates traffic by each teid
#needs to map imsi - teid
push_teid_data(teid_counts,"teid_counts")
push_teid_data(teid_bytes,"teid_bytes")