-- -- (C) 2013-23 - ntop.org -- dirs = ntop.getDirs() package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path package.path = dirs.installdir .. "/scripts/lua/pro/modules/?.lua;" .. package.path local host_to_scan_key = "ntopng.prefs.host_to_scan" local vulnerability_scan_type_key = "ntopng.prefs.vulnerability_scan_type" local json = require("dkjson") local format_utils = require("format_utils") local debug = false --debug = true local vulnerability_scan_utils = {} -- ********************************************************** -- Function to save host before and after vulnerability scan function vulnerability_scan_utils.save_host_to_scan(scan_type, ip, scan_result, time, is_ok_last_scan) local saved_hosts_string = ntop.getCache(host_to_scan_key) local saved_hosts = {} if not isEmptyString(saved_hosts_string) then saved_hosts = json.decode(saved_hosts_string) local index_to_remove = 0 for index,value in ipairs(saved_hosts) do if value.host == ip and value.scan_type == scan_type then index_to_remove = index end end if index_to_remove ~= 0 then table.remove(saved_hosts, index_to_remove) end end local new_item = { host=ip, scan_type=scan_type, } if time then local time_formatted = format_utils.formatPastEpochShort(time) new_item.last_scan = { epoch = time, time = time_formatted } if is_ok_last_scan then new_item.is_ok_last_scan = is_ok_last_scan end end if not isEmptyString(scan_result) then if is_ok_last_scan then new_item.scan_result = scan_result end end saved_hosts[#saved_hosts+1] = new_item ntop.setCache(host_to_scan_key, json.encode(saved_hosts)) return 1 end -- ********************************************************** -- Function to retrieve hosts list to scan function vulnerability_scan_utils.retrieve_hosts_to_scan() local res_string = ntop.getCache(host_to_scan_key) if not isEmptyString(res_string) and res_string ~= "[[]]" and res_string ~= "[]" then if debug then tprint(res_string) tprint(json.decode(res_string)) end return json.decode(res_string) else return {} end end -- ********************************************************** -- Function to retrieve last host scan result function vulnerability_scan_utils.retrieve_hosts_scan_result(host, scan_type) local res_string = ntop.getCache(host_to_scan_key) if not isEmptyString(res_string) and res_string ~= "[[]]" and res_string ~= "[]" then if debug then tprint(json.decode(res_string)) end local scan_info = json.decode(res_string) for _, info in ipairs(scan_info) do if info.host == host and info.scan_type == scan_type then if not isEmptyString(info.scan_result) then return info.scan_result end end end end return "" end -- ********************************************************** -- Function to delete host to scan function vulnerability_scan_utils.delete_host_to_scan(ip, scan_type) local saved_hosts_string = ntop.getCache(host_to_scan_key) local saved_hosts = {} if not isEmptyString(saved_hosts_string) then saved_hosts = json.decode(saved_hosts_string) local index_to_remove = 0 for index,value in ipairs(saved_hosts) do if value.host == ip and value.scan_type == scan_type then index_to_remove = index end end if index_to_remove ~= 0 then table.remove(saved_hosts, index_to_remove) end end ntop.setCache(host_to_scan_key, json.encode(saved_hosts)) return 1 end -- ********************************************************** -- Function to retrieve scan types list function vulnerability_scan_utils.retrieve_scan_types() local scan_types = ntop.getCache(vulnerability_scan_type_key) if (not isEmptyString(scan_types)) then local result = json.decode(scan_types) or {} for _,scan_type in ipairs(result) do scan_type.label = i18n(scan_type.label_i18_n) end return result else return {} end end -- ********************************************************** return vulnerability_scan_utils