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
|
--
-- (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 fds = ndpi_proto.fields
fds.network_protocol = ProtoField.new("nDPI Network Protocol", "ndpi.protocol.network", ftypes.UINT8, nil, base.DEC)
fds.application_protocol = ProtoField.new("nDPI Application Protocol", "ndpi.protocol.application", ftypes.UINT8, nil, base.DEC)
fds.name = ProtoField.new("nDPI Protocol Name", "ndpi.protocol.name", ftypes.STRING)
local f_eth_trailer = Field.new("eth.trailer")
local ndpi_protos = {}
local ndpi_flows = {}
local compute_flows_stats = true
-- ###############################################
function ndpi_proto.init()
ndpi_protos = {}
ndpi_flows = {}
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
-- the dissector function callback
function ndpi_proto.dissector(tvb, pinfo, tree)
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(fds.network_protocol, network_protocol)
ndpi_subtree:add(fds.application_protocol, application_protocol)
ndpi_subtree:add(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
end
ndpi_flows[flowkey] = ndpi_flows[flowkey] + pinfo.len
end
end
end
register_postdissector(ndpi_proto)
-- ###############################################
function round(num, idp) return tonumber(string.format("%." .. (idp or 0) .. "f", num)) 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 ndpi_dialog_menu()
local win = TextWindow.new("nDPI Protocol Statistics");
local label = ""
local i
local max_i = 10
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_i) 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_i) 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
|