Code cleanup

This commit is contained in:
Luca Deri
2017-06-06 11:40:30 +02:00
parent 7bf508df0a
commit 984eedc5ea
+127 -111
View File
@@ -20,6 +20,7 @@
local ndpi_proto = Proto("ndpi", "nDPI", "nDPI Protocol Interpreter") local ndpi_proto = Proto("ndpi", "nDPI", "nDPI Protocol Interpreter")
ndpi_proto.fields = {} ndpi_proto.fields = {}
local ndpi_fds = 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.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.application_protocol = ProtoField.new("nDPI Application Protocol", "ndpi.protocol.application", ftypes.UINT8, nil, base.DEC)
@@ -33,6 +34,7 @@ ntop_fds.client_nw_rtt = ProtoField.new("TCP client network RTT (msec)", "nt
ntop_fds.server_nw_rtt = ProtoField.new("TCP server network RTT (msec)", "ntop.latency.server_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)
ntop_fds.appl_latency_rtt = ProtoField.new("Application Latency RTT (msec)", "ntop.latency.appl_rtt", ftypes.FLOAT, nil, base.NONE) ntop_fds.appl_latency_rtt = ProtoField.new("Application Latency RTT (msec)", "ntop.latency.appl_rtt", ftypes.FLOAT, nil, base.NONE)
local f_eth_trailer = Field.new("eth.trailer")
local f_vlan_id = Field.new("vlan.id") local f_vlan_id = Field.new("vlan.id")
local f_arp_opcode = Field.new("arp.opcode") local f_arp_opcode = Field.new("arp.opcode")
local f_arp_sender_mac = Field.new("arp.src.hw_mac") local f_arp_sender_mac = Field.new("arp.src.hw_mac")
@@ -45,6 +47,7 @@ local f_tcp_header_len = Field.new("tcp.hdr_len")
local f_ip_len = Field.new("ip.len") local f_ip_len = Field.new("ip.len")
local f_ip_hdr_len = Field.new("ip.hdr_len") local f_ip_hdr_len = Field.new("ip.hdr_len")
local f_ssl_server_name = Field.new("ssl.handshake.extensions_server_name") local f_ssl_server_name = Field.new("ssl.handshake.extensions_server_name")
local f_tcp_flags = Field.new('tcp.flags')
local ndpi_protos = {} local ndpi_protos = {}
local ndpi_flows = {} local ndpi_flows = {}
@@ -402,35 +405,7 @@ end
-- ############################################### -- ###############################################
local field_tcp_flags = Field.new('tcp.flags') function arp_dissector(tvb, pinfo, tree)
-- 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 == true) 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 vlan_id = f_vlan_id()
if(vlan_id ~= nil) then
vlan_id = tonumber(getval(vlan_id))
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
local arp_opcode = f_arp_opcode() local arp_opcode = f_arp_opcode()
if(arp_opcode ~= nil) then if(arp_opcode ~= nil) then
@@ -439,77 +414,34 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
local src_mac = getval(f_arp_sender_mac()) local src_mac = getval(f_arp_sender_mac())
local dst_mac = getval(f_arp_target_mac()) local dst_mac = getval(f_arp_target_mac())
dissectARP(isRequest, src_mac, dst_mac) dissectARP(isRequest, src_mac, dst_mac)
else end
-- ############# 2 nDPI Dissection #############
if(false) then
local srckey = tostring(pinfo.src)
local dstkey = tostring(pinfo.dst)
print("Processing packet "..pinfo.number .. "["..srckey.." / "..dstkey.."]")
end end
-- ###############################################
function vlan_dissector(tvb, pinfo, tree)
local vlan_id = f_vlan_id()
if(vlan_id ~= nil) then
vlan_id = tonumber(getval(vlan_id))
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
end
-- ###############################################
function mac_dissector(tvb, pinfo, tree)
local src_mac = tostring(pinfo.dl_src) local src_mac = tostring(pinfo.dl_src)
local src_ip = tostring(pinfo.src) local src_ip = tostring(pinfo.src)
if(mac_stats[src_mac] == nil) then mac_stats[src_mac] = {} end if(mac_stats[src_mac] == nil) then mac_stats[src_mac] = {} end
mac_stats[src_mac][src_ip] = 1 mac_stats[src_mac][src_ip] = 1
local pktlen = tvb:len()
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 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.."\t["..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)
num_ndpi_flows = num_ndpi_flows + 1
if(num_ndpi_flows == (2*max_num_entries)) then
break
end
end
end
end
end
ndpi_flows[flowkey] = ndpi_flows[flowkey] + pinfo.len
end
end -- nDPI
-- ###########################################
function ssl_dissector(tvb, pinfo, tree)
local ssl_server_name = f_ssl_server_name() local ssl_server_name = f_ssl_server_name()
if(ssl_server_name ~= nil) then if(ssl_server_name ~= nil) then
ssl_server_name = getval(ssl_server_name) ssl_server_name = getval(ssl_server_name)
@@ -521,9 +453,11 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
ssl_server_names[ssl_server_name] = ssl_server_names[ssl_server_name] + 1 ssl_server_names[ssl_server_name] = ssl_server_names[ssl_server_name] + 1
tot_ssl_flows = tot_ssl_flows + 1 tot_ssl_flows = tot_ssl_flows + 1
end end
end
-- ########################################### -- ###############################################
function dns_dissector(tvb, pinfo, tree)
local dns_response = f_dns_response() local dns_response = f_dns_response()
if(dns_response ~= nil) then if(dns_response ~= nil) then
local dns_ret_code = f_dns_ret_code() local dns_ret_code = f_dns_ret_code()
@@ -584,10 +518,12 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
end end
end end
end end
end
-- ########################################### -- ###############################################
local _tcp_flags = field_tcp_flags() function latency_dissector(tvb, pinfo, tree)
local _tcp_flags = f_tcp_flags()
local udp_len = f_udp_len() local udp_len = f_udp_len()
if((_tcp_flags ~= nil) or (udp_len ~= nil)) then if((_tcp_flags ~= nil) or (udp_len ~= nil)) then
@@ -599,7 +535,7 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
local ip_hdr_len local ip_hdr_len
if(udp_len == nil) then if(udp_len == nil) then
tcp_flags = field_tcp_flags().value tcp_flags = f_tcp_flags().value
tcp_header_len = f_tcp_header_len() tcp_header_len = f_tcp_header_len()
ip_len = f_ip_len() ip_len = f_ip_len()
ip_hdr_len = f_ip_hdr_len() ip_hdr_len = f_ip_hdr_len()
@@ -658,7 +594,6 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
end end
end end
tcp_flags = tonumber(tcp_flags) tcp_flags = tonumber(tcp_flags)
if(tcp_flags == 2) then if(tcp_flags == 2) then
@@ -728,27 +663,108 @@ function ndpi_proto.dissector(tvb, pinfo, tree)
end end
end 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 end
for k,v in pairs(fields) do -- the dissector function callback
print(k.." = "..v) 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 == true) then
local eth_trailer = f_eth_trailer()
if(eth_trailer ~= nil) then
local eth_trailer = getval(eth_trailer)
local magic = string.sub(eth_trailer, 1, 11)
if(magic == "19:68:09:24") then
local ndpikey, srckey, dstkey, flowkey
local elems = string.split(string.sub(eth_trailer, 12), ":")
local ndpi_subtree = tree:add(ndpi_proto, tvb(), "nDPI Protocol")
local network_protocol = tonumber(elems[2]..elems[3], 16) -- 16 = HEX
local application_protocol = tonumber(elems[4]..elems[5], 16) -- 16 = HEX
local name = ""
for i=6,21 do
name = name .. string.char(tonumber(elems[i], 16))
end
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)
if(application_protocol ~= 0) then
-- Set protocol name in the wireshark protocol column (if not Unknown)
--pinfo.cols.protocol = name
print(network_protocol .. "/" .. application_protocol .. "/".. name)
end
if(compute_flows_stats) then
ndpikey = tostring(slen(name))
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.."\t["..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)
num_ndpi_flows = num_ndpi_flows + 1
if(num_ndpi_flows == (2*max_num_entries)) then
break
end end
end end
end end
end end
end
ndpi_flows[flowkey] = ndpi_flows[flowkey] + pinfo.len
end
end
end -- nDPI
latency_dissector(tvb, pinfo, tree)
end
-- ###########################################
-- As we do not need to add fields to the dissection
-- there is no need to process the packet multiple times
if(pinfo.visited == true) 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)
if(false) then
local srckey = tostring(pinfo.src)
local dstkey = tostring(pinfo.dst)
print("Processing packet "..pinfo.number .. "["..srckey.." / "..dstkey.."]")
end
mac_dissector(tvb, pinfo, tree)
arp_dissector(tvb, pinfo, tree)
vlan_dissector(tvb, pinfo, tree)
ssl_dissector(tvb, pinfo, tree)
dns_dissector(tvb, pinfo, tree)
end
register_postdissector(ndpi_proto) register_postdissector(ndpi_proto)