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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
--
-- (C) 2017 - ntop.org
--
-- This plugin is part of nDPI (https://github.com/ntop/nDPI)
--
--
local ndpi_proto = Proto("ndpi", "nDPI", "nDPI Protocol Interpreter")
ndpi_proto.fields = {}
local ndpi_fds = ndpi_proto.fields
ndpi_fds.network_protocol = ProtoField.new("nDPI Network Protocol", "ndpi.protocol.network", ftypes.UINT8, nil, base.DEC)
ndpi_fds.application_protocol = ProtoField.new("nDPI Application Protocol", "ndpi.protocol.application", ftypes.UINT8, nil, base.DEC)
ndpi_fds.name = ProtoField.new("nDPI Protocol Name", "ndpi.protocol.name", ftypes.STRING)
local ntop_proto = Proto("ntop", "ntop", "ntop Extensions")
ntop_proto.fields = {}
local ntop_fds = ntop_proto.fields
ntop_fds.client_nw_rtt = ProtoField.new("TCP client network RTT (msec)", "ntop.latency.client_rtt", ftypes.FLOAT, nil, base.NONE)
ntop_fds.server_nw_rtt = ProtoField.new("TCP server network RTT (msec)", "ntop.latency.server_rtt", ftypes.FLOAT, nil, base.NONE)
local f_eth_trailer = Field.new("eth.trailer")
local ndpi_protos = {}
local ndpi_flows = {}
local num_ndpi_flows = 0
local arp_stats = {}
local mac_stats = {}
local vlan_stats = {}
local vlan_found = false
local syn = {}
local synack = {}
local lower_ndpi_flow_id = 0
local lower_ndpi_flow_volume = 0
local compute_flows_stats = true
local max_num_entries = 10
local max_num_flows = 50
local num_pkts = 0
local last_processed_packet_number = 0
local debug = false
-- ##############################################
function string.contains(String,Start)
if type(String) ~= 'string' or type(Start) ~= 'string' then
return false
end
return(string.find(String,Start,1) ~= nil)
end
-- ##############################################
function string.starts(String,Start)
if type(String) ~= 'string' or type(Start) ~= 'string' then
return false
end
return string.sub(String,1,string.len(Start))==Start
end
-- ##############################################
function string.ends(String,End)
if type(String) ~= 'string' or type(End) ~= 'string' then
return false
end
return End=='' or string.sub(String,-string.len(End))==End
end
-- ###############################################
function round(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
function formatPctg(p)
local p = round(p, 1)
if(p < 1) then return("< 1 %") end
return p.." %"
end
-- ###############################################
string.split = function(s, p)
local temp = {}
local index = 0
local last_index = string.len(s)
while true do
local i, e = string.find(s, p, index)
if i and e then
local next_index = e + 1
local word_bound = i - 1
table.insert(temp, string.sub(s, index, word_bound))
index = next_index
else
if index > 0 and index <= last_index then
table.insert(temp, string.sub(s, index, last_index))
elseif index == 0 then
temp = nil
end
break
end
end
return temp
end
-- ###############################################
-- Convert bytes to human readable format
function bytesToSize(bytes)
if(bytes == nil) then
return("0")
else
precision = 2
kilobyte = 1024;
megabyte = kilobyte * 1024;
gigabyte = megabyte * 1024;
terabyte = gigabyte * 1024;
bytes = tonumber(bytes)
if((bytes >= 0) and (bytes < kilobyte)) then
return round(bytes, precision) .. " Bytes";
elseif((bytes >= kilobyte) and (bytes < megabyte)) then
return round(bytes / kilobyte, precision) .. ' KB';
elseif((bytes >= megabyte) and (bytes < gigabyte)) then
return round(bytes / megabyte, precision) .. ' MB';
elseif((bytes >= gigabyte) and (bytes < terabyte)) then
return round(bytes / gigabyte, precision) .. ' GB';
elseif(bytes >= terabyte) then
return round(bytes / terabyte, precision) .. ' TB';
else
return round(bytes, precision) .. ' Bytes';
end
end
end
-- ###############################################
function pairsByValues(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, function(x, y) return f(t[x], t[y]) end)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
-- ###############################################
function asc(a,b) return (a < b) end
function rev(a,b) return (a > b) end
-- ###############################################
local function BitOR(a,b)--Bitwise or
local p,c=1,0
while a+b>0 do
local ra,rb=a%2,b%2
if ra+rb>0 then c=c+p end
a,b,p=(a-ra)/2,(b-rb)/2,p*2
end
return c
end
local function BitNOT(n)
local p,c=1,0
while n>0 do
local r=n%2
if r<1 then c=c+p end
n,p=(n-r)/2,p*2
end
return c
end
local function BitAND(a,b)--Bitwise and (portable edition)
local p,c=1,0
while a>0 and b>0 do
local ra,rb=a%2,b%2
if ra+rb>1 then c=c+p end
a,b,p=(a-ra)/2,(b-rb)/2,p*2
end
return c
end
-- ###############################################
function ndpi_proto.init()
ndpi_protos = { }
ndpi_flows = { }
num_ndpi_flows = 0
lower_ndpi_flow_id = 0
lower_ndpi_flow_volume = 0
num_pkts = 0
last_processed_packet_number = 0
-- ARP
arp_stats = { }
-- MAC
mac_stats = { }
-- VLAN
vlan_stats = { }
vlan_found = false
-- TCP
syn = {}
synack = {}
end
function slen(str)
local i = 1
local len = 0
local zero = string.char(0)
for i = 1, 16 do
local c = str:sub(i,i)
if(c ~= zero) then
len = len + 1
else
break
end
end
return(str:sub(1, len))
end
-- Print contents of `tbl`, with indentation.
-- You can call it as tprint(mytable)
-- The other two parameters should not be set
function tprint(s, l, i)
l = (l) or 1000; i = i or "";-- default item limit, indent string
if (l<1) then io.write("ERROR: Item limit reached.\n"); return l-1 end;
local ts = type(s);
if (ts ~= "table") then io.write(i..' '..ts..' '..tostring(s)..'\n'); return l-1 end
io.write(i..' '..ts..'\n');
for k,v in pairs(s) do
local indent = ""
if(i ~= "") then
indent = i .. "."
end
indent = indent .. tostring(k)
l = tprint(v, l, indent);
if (l < 0) then break end
end
return l
end
-- ###############################################
local function getstring(finfo)
local ok, val = pcall(tostring, finfo)
if not ok then val = "(unknown)" end
return val
end
local function getval(finfo)
local ok, val = pcall(tostring, finfo)
if not ok then val = nil end
return val
end
function dump_pinfo(pinfo)
local fields = { all_field_infos() }
for ix, finfo in ipairs(fields) do
-- output = output .. "\t[" .. ix .. "] " .. finfo.name .. " = " .. getstring(finfo) .. "\n"
--print(finfo.name .. "\n")
print("\t[" .. ix .. "] " .. finfo.name .. " = " .. getstring(finfo) .. "\n")
end
end
-- ###############################################
function initARPEntry(mac)
if(arp_stats[mac] == nil) then
arp_stats[mac] = { request_sent=0, request_rcvd=0, response_sent=0, response_rcvd=0 }
end
end
function dissectARP(isRequest, src_mac, dst_mac)
local mac
-- print(num_pkts)
if(isRequest == 1) then
-- ARP Request
initARPEntry(src_mac)
arp_stats[src_mac].request_sent = arp_stats[src_mac].request_sent + 1
initARPEntry(dst_mac)
arp_stats[dst_mac].request_rcvd = arp_stats[dst_mac].request_rcvd + 1
else
-- ARP Response
initARPEntry(src_mac)
arp_stats[src_mac].response_sent = arp_stats[src_mac].response_sent + 1
initARPEntry(dst_mac)
arp_stats[dst_mac].response_rcvd = arp_stats[dst_mac].response_rcvd + 1
end
end
-- ###############################################
function abstime_diff(a, b)
local secs1, frac1 = math.modf(a)
local secs2, frac2 = math.modf(b)
local diff
local diff_sec = secs1 - secs2
local diff_res = frac1 - frac2
if(diff_res < 0) then diff_sec = diff_sec + 1 end
return(diff_sec + diff_res)
end
-- ###############################################
local field_tcp_flags = Field.new('tcp.flags')
-- the dissector function callback
function ndpi_proto.dissector(tvb, pinfo, tree)
-- Wireshark dissects the packet twice. We ignore the first
-- run as on that step the packet is still undecoded
-- The trick below avoids to process the packet twice
if(pinfo.visited == false) then return end
num_pkts = num_pkts + 1
if((num_pkts > 1) and (pinfo.number == 1)) then return end
if(last_processed_packet_number < pinfo.number) then
last_processed_packet_number = pinfo.number
end
-- print(num_pkts .. " / " .. pinfo.number .. " / " .. last_processed_packet_number)
-- ############# ARP / VLAN #############
local offset = 12
local eth_proto = tostring(tvb(offset,2))
if(eth_proto == "8100") then
local vlan_id = BitAND(tonumber(tostring(tvb(offset+2,2))), 0xFFF)
if(vlan_stats[vlan_id] == nil) then vlan_stats[vlan_id] = 0 end
vlan_stats[vlan_id] = vlan_stats[vlan_id] + 1
vlan_found = true
end
while(eth_proto == "8100") do
offset = offset + 4
eth_proto = tostring(tvb(offset,2))
end
if(eth_proto == "0806") then
-- ARP
local isRequest = tonumber(tvb(21,1))
--print(eth_proto.." ["..tostring(pinfo.dl_src).." / ".. tostring(pinfo.dl_dst) .."] [" .. tostring(pinfo.src).." -> "..tostring(pinfo.dst).."]")
dissectARP(isRequest, tostring(pinfo.dl_src), tostring(pinfo.dl_dst))
else
-- ############# 2 nDPI Dissection #############
if(false) then
local srckey = tostring(pinfo.src)
local dstkey = tostring(pinfo.dst)
print("Processing packet "..pinfo.number .. "["..srckey.." / "..dstkey.."]")
end
local src_mac = tostring(pinfo.dl_src)
local src_ip = tostring(pinfo.src)
if(mac_stats[src_mac] == nil) then mac_stats[src_mac] = {} end
mac_stats[src_mac][src_ip] = 1
local pktlen = tvb:len()
local eth_trailer = f_eth_trailer()
local magic = tostring(tvb(pktlen-28,4))
if(magic == "19680924") then
local ndpi_subtree = tree:add(ndpi_proto, tvb(), "nDPI Protocol")
local network_protocol = tvb(pktlen-24,2)
local application_protocol = tvb(pktlen-22,2)
local name = tvb(pktlen-20,16)
local name_str = name:string(ENC_ASCII)
local ndpikey, srckey, dstkey, flowkey
ndpi_subtree:add(ndpi_fds.network_protocol, network_protocol)
ndpi_subtree:add(ndpi_fds.application_protocol, application_protocol)
ndpi_subtree:add(ndpi_fds.name, name)
local pname = ""..application_protocol
if(pname ~= "0000") then
-- Set protocol name in the wireshark protocol column (if not Unknown)
pinfo.cols.protocol = name_str
end
if(compute_flows_stats) then
ndpikey = tostring(slen(name_str))
if(ndpi_protos[ndpikey] == nil) then ndpi_protos[ndpikey] = 0 end
ndpi_protos[ndpikey] = ndpi_protos[ndpikey] + pinfo.len
srckey = tostring(pinfo.src)
dstkey = tostring(pinfo.dst)
flowkey = srckey.." / "..dstkey.." ["..ndpikey.."]"
if(ndpi_flows[flowkey] == nil) then
ndpi_flows[flowkey] = 0
num_ndpi_flows = num_ndpi_flows + 1
if(num_ndpi_flows > max_num_flows) then
-- We need to harvest the flow with least packets beside this new one
local tot_removed = 0
for k,v in pairsByValues(ndpi_flows, asc) do
if(k ~= flowkey) then
table.remove(ndpi_flows, k)
tot_removed = tot_removed + 1
if(tot_removed == max_num_entries) then
break
end
end
end
end
end
ndpi_flows[flowkey] = ndpi_flows[flowkey] + pinfo.len
end
end -- nDPI
local _tcp_flags = field_tcp_flags()
if(_tcp_flags ~= nil) then
local key
local tcp_flags = field_tcp_flags().value
tcp_flags = tonumber(tcp_flags)
if(tcp_flags == 2) then
-- SYN
if(debug) then print("SYN @ ".. pinfo.abs_ts.." "..key) end
key = getstring(pinfo.src).."_"..getstring(pinfo.src_port).."_"..getstring(pinfo.dst).."_"..getstring(pinfo.dst_port)
syn[key] = pinfo.abs_ts
elseif(tcp_flags == 18) then
-- SYN|ACK
if(debug) then print("SYN|ACK @ ".. pinfo.abs_ts.." "..key) end
key = getstring(pinfo.dst).."_"..getstring(pinfo.dst_port).."_"..getstring(pinfo.src).."_"..getstring(pinfo.src_port)
synack[key] = pinfo.abs_ts
if(syn[key] ~= nil) then
local diff = abstime_diff(synack[key], syn[key]) * 1000 -- msec
if(debug) then print("Client RTT --> ".. diff .. " sec") end
local ntop_subtree = tree:add(ntop_proto, tvb(), "ntop")
ntop_subtree:add(ntop_fds.client_nw_rtt, diff)
-- syn[key] = nil
end
elseif(tcp_flags == 16) then
-- ACK
if(debug) then print("ACK @ ".. pinfo.abs_ts.." "..key) end
key = getstring(pinfo.src).."_"..getstring(pinfo.src_port).."_"..getstring(pinfo.dst).."_"..getstring(pinfo.dst_port)
if(synack[key] ~= nil) then
local diff = abstime_diff(pinfo.abs_ts, synack[key]) * 1000 -- msec
if(debug) then print("Server RTT --> ".. diff .. " sec") end
local ntop_subtree = tree:add(ntop_proto, tvb(), "ntop")
ntop_subtree:add(ntop_fds.server_nw_rtt, diff)
-- synack[key] = nil
end
end
end
if(debug) then
local fields = { }
local _fields = { all_field_infos() }
-- fields['pinfo.number'] = pinfo.number
for k,v in pairs(_fields) do
local value = getstring(v)
if(value ~= nil) then
fields[v.name] = value
end
end
for k,v in pairs(fields) do
print(k.." = "..v)
end
end
end
end
register_postdissector(ndpi_proto)
-- ###############################################
local function ndpi_dialog_menu()
local win = TextWindow.new("nDPI Protocol Statistics");
local label = ""
local i
if(ndpi_protos ~= {}) then
label = "nDPI Protocol Breakdown\n"
label = label .. "-----------------------\n"
i = 0
for k,v in pairsByValues(ndpi_protos, rev) do
-- label = label .. k .. "\t".. bytesToSize(v) .. "\n"
label = label .. string.format("%-32s\t%s\n", k, bytesToSize(v))
if(i == max_num_entries) then break else i = i + 1 end
end
-- #######
label = label .. "\nTop nDPI Flows\n"
label = label .. "-----------\n"
i = 0
for k,v in pairsByValues(ndpi_flows, rev) do
label = label .. string.format("%-32s\t%s\n", k, bytesToSize(v))
if(i == max_num_entries) then break else i = i + 1 end
end
win:set(label)
end
end
-- ###############################################
if(compute_flows_stats) then
register_menu("nDPI", ndpi_dialog_menu, MENU_STAT_UNSORTED)
end
-- ###############################################
local function arp_dialog_menu()
local win = TextWindow.new("ARP Statistics");
local label = ""
local _stats
local found = false
_stats = {}
for k,v in pairs(arp_stats) do
if(k ~= "Broadcast") then
_stats[k] = v.request_sent + v.request_rcvd + v.response_sent + v.response_rcvd
found = true
end
end
if(not found) then
label = "No ARP Traffic detected"
else
label = "Top ARP Senders/Receivers\n\nMAC Address\tTot Pkts\tPctg\tARP Breakdown\n"
i = 0
for k,v in pairsByValues(_stats, rev) do
local s = arp_stats[k]
local pctg = formatPctg((v * 100) / last_processed_packet_number)
local str = k .. "\t" .. v .. "\t" .. pctg .. "\t" .. "[sent: ".. (s.request_sent + s.response_sent) .. "][rcvd: ".. (s.request_rcvd + s.response_rcvd) .. "]\n"
label = label .. str
if(i == max_num_entries) then break else i = i + 1 end
end
end
win:set(label)
end
-- ###############################################
local function vlan_dialog_menu()
local win = TextWindow.new("VLAN Statistics");
local label = ""
local _macs
local num_hosts = 0
if(vlan_found) then
i = 0
label = "VLAN\tPackets\n"
for k,v in pairsByValues(vlan_stats, rev) do
local pctg = formatPctg((v * 100) / last_processed_packet_number)
label = label .. k .. "\t" .. v .. " pkts [".. pctg .."]\n"
if(i == max_num_entries) then break else i = i + 1 end
end
else
label = "No VLAN traffic found"
end
win:set(label)
end
-- ###############################################
local function ip_mac_dialog_menu()
local win = TextWindow.new("IP-MAC Statistics");
local label = ""
local _macs, _manufacturers
local num_hosts = 0
_macs = {}
_manufacturers = {}
for mac,v in pairs(mac_stats) do
local num = 0
local m = string.split(mac, "_")
local manuf
if(m == nil) then
m = string.split(mac, ":")
manuf = m[1]..":"..m[2]..":"..m[3]
else
manuf = m[1]
end
for a,b in pairs(v) do
num = num +1
end
_macs[mac] = num
if(_manufacturers[manuf] == nil) then _manufacturers[manuf] = 0 end
_manufacturers[manuf] = _manufacturers[manuf] + 1
num_hosts = num_hosts + num
end
if(num_hosts > 0) then
i = 0
label = label .. "MAC\t\t# Hosts\tPercentage\n"
for k,v in pairsByValues(_macs, rev) do
local pctg = formatPctg((v * 100) / num_hosts)
label = label .. k .. "\t" .. v .. "\t".. pctg .."\n"
if(i == max_num_entries) then break else i = i + 1 end
end
i = 0
label = label .. "\n\nManufacturer\t# Hosts\tPercentage\n"
for k,v in pairsByValues(_manufacturers, rev) do
local pctg = formatPctg((v * 100) / num_hosts)
label = label .. k .. "\t\t" .. v .. "\t".. pctg .."\n"
if(i == max_num_entries) then break else i = i + 1 end
end
else
label = label .. "\nIP-MAC traffic found"
end
win:set(label)
end
-- ###############################################
register_menu("ARP", arp_dialog_menu, MENU_STAT_UNSORTED)
register_menu("VLAN", vlan_dialog_menu, MENU_STAT_UNSORTED)
register_menu("IP-MAC", ip_mac_dialog_menu, MENU_STAT_UNSORTED)
|