Reworked vulnerability scan

This commit is contained in:
Luca Deri 2023-07-27 18:22:02 +02:00
parent 878bc21e9c
commit ac27756fe3
5 changed files with 51 additions and 29 deletions

View file

@ -0,0 +1,166 @@
--
-- (C) 2013-23 - ntop.org
--
--
-- This file implements some utility functions used by the REST API
-- in the vulnerability pages
--
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 vs_type_key = "ntopng.prefs.vs_type"
local json = require("dkjson")
local format_utils = require("format_utils")
local debug = false
--debug = true
local vs_utils = {}
-- **********************************************************
-- Function to save host before and after vulnerability scan
function vs_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 vs_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 vs_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 vs_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 vs_utils.retrieve_scan_types()
local scan_types = ntop.getCache(vs_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 vs_utils