Fix edit host. (#7755)

This commit is contained in:
Nicolo Maio 2023-08-11 20:15:18 +02:00
parent 6790142885
commit 14c22ef251
8 changed files with 104 additions and 25 deletions

View file

@ -226,13 +226,27 @@ end
-- **********************************************************
-- Function to save host configuration
local function isAlreadyPresent(item)
local hosts_details = vs_utils.retrieve_hosts_to_scan()
for _,value in ipairs(hosts_details) do
if (item.host == value.host ) then
return true
end
end
return false
end
-- **********************************************************
-- Function to save host configuration
function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time, last_duration,
is_ok_last_scan, ports, scan_frequency, num_open_ports,
num_vulnerabilities_found, cve)
num_vulnerabilities_found, cve, id)
--local saved_hosts_string = ntop.getCache(host_to_scan_key)
local saved_hosts = {}
local host_hash_key = vs_utils.get_host_hash_key(host, scan_type)
--local saved_hosts = {}
--local host_hash_key = vs_utils.get_host_hash_key( id)
--if not isEmptyString(saved_hosts_string) then
local checks = require "checks"
@ -272,6 +286,14 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
end
end
local epoch_id = ""
if isEmptyString(id) then
local key = "ntopng.prefs.last_host_id"
local res = ntop.incrCache(key)
epoch_id = res
else
epoch_id = id
end
local new_item = {
host = host,
scan_type = scan_type,
@ -279,6 +301,7 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
num_open_ports = num_open_ports,
num_vulnerabilities_found = num_vulnerabilities_found,
cve = cve,
id = epoch_id
}
if last_scan_time or last_duration then
@ -311,9 +334,19 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
local result = handle:write(scan_result)
handle:close()
end
if not isEmptyString(id) then
vs_utils.delete_host_to_scan_by_id(id)
end
if(not isAlreadyPresent(new_item)) then
--saved_hosts[#saved_hosts+1] = new_item
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(new_item))
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(new_item))
elseif not isEmptyString(id) then
-- edit case
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(new_item))
end
--ntop.setCache(host_to_scan_key, json.encode(saved_hosts))
return 1
@ -400,6 +433,30 @@ function vs_utils.delete_host_to_scan(host, scan_type, all)
return true
end
-- Function to delete host to scan by id
function vs_utils.delete_host_to_scan_by_id(id)
local hosts_details = vs_utils.retrieve_hosts_to_scan()
local host_to_delete = {}
local id_number = tonumber(id)
for _,value in ipairs(hosts_details) do
if(tonumber(value.id) == id_number ) then
host_to_delete.host = value.host
host_to_delete.scan_type = value.scan_type
break
end
end
local host_hash_key = vs_utils.get_host_hash_key(host_to_delete.host, host_to_delete.scan_type)
local path_to_s_result = get_report_path(host_to_delete.scan_type, host_to_delete.host, false)
os.remove(path_to_s_result)
ntop.delHashCache(host_to_scan_key, host_hash_key)
return true
end
-- **********************************************************
-- Function to retrieve scan types list
@ -445,12 +502,12 @@ end
-- **********************************************************
-- Function to exec single host scan
function vs_utils.scan_host(scan_type, host, ports)
function vs_utils.scan_host(scan_type, host, ports, scan_id)
local scan_module = vs_utils.load_module(scan_type)
local result,duration,scan_result,num_open_ports,num_vulnerabilities_found, cve = scan_module:scan_host(host, ports)
vs_utils.save_host_to_scan(scan_type, host, result, now, duration, scan_result,
ports, nil, num_open_ports, num_vulnerabilities_found, cve)
ports, nil, num_open_ports, num_vulnerabilities_found, cve, scan_id)
return true
end
@ -458,7 +515,7 @@ end
-- **********************************************************
-- Function to update single host status
function vs_utils.set_status_scan(scan_type, host, ports)
function vs_utils.set_status_scan(scan_type, host, ports, id)
local host_hash_key = vs_utils.get_host_hash_key(host, scan_type)
local host_hash_value_string = ntop.getHashCache(host_to_scan_key, host_hash_key)
@ -476,9 +533,9 @@ end
-- **********************************************************
function vs_utils.schedule_host_scan(scan_type, host, ports)
function vs_utils.schedule_host_scan(scan_type, host, ports, scan_id)
local scan = { scan_type = scan_type, host = host, ports = ports }
vs_utils.set_status_scan(scan_type, host, ports)
vs_utils.set_status_scan(scan_type, host, ports, scan_id)
ntop.rpushCache(host_scan_queue_key, json.encode(scan))
@ -492,7 +549,7 @@ function vs_utils.schedule_all_hosts_scan(scan_type, host, ports)
if #host_to_scan_list > 0 then
for _,scan_info in ipairs(host_to_scan_list) do
vs_utils.schedule_host_scan(scan_info.scan_type, scan_info.host, scan_info.ports)
vs_utils.schedule_host_scan(scan_info.scan_type, scan_info.host, scan_info.ports, scan_info.id)
end
end
@ -510,7 +567,7 @@ function vs_utils.schedule_periodic_scan(periodicity)
local frequency = scan_info.scan_frequency
if(frequency == periodicity) then
vs_utils.schedule_host_scan(scan_info.scan_type, scan_info.host, scan_info.ports)
vs_utils.schedule_host_scan(scan_info.scan_type, scan_info.host, scan_info.ports, scan_info.id)
end
end
end
@ -528,7 +585,7 @@ function vs_utils.process_oldest_scheduled_scan()
if((elem ~= nil) and (elem ~= "")) then
local elem = json.decode(elem)
vs_utils.scan_host(elem.scan_type, elem.host, elem.ports)
vs_utils.scan_host(elem.scan_type, elem.host, elem.ports, elem.id)
return true
else