Fixes endless shutdown due to pending vs scan in execution

This commit is contained in:
Luca Deri 2023-10-25 17:27:21 +02:00
parent ab59ed592a
commit d9714dc3c6
7 changed files with 116 additions and 53 deletions

View file

@ -58,8 +58,7 @@ local format_utils = require("format_utils")
local recipients = require("recipients")
local cve_utils = require("cve_utils")
local debug_print = false
local debug_me = false
local debug_me = false
local vs_utils = {}
@ -253,10 +252,9 @@ end
-- ##############################################
local function analyze_ports_diff(ports_difference)
local rsp = {}
if (ports_difference.trigger) then
if (debug_print) then
if (debug_me) then
tprint("found ports differences")
tprint(ports_difference)
end
@ -270,10 +268,10 @@ local function analyze_ports_diff(ports_difference)
}
rsp["ports_case"] = ports_difference.case
if (debug_print) then
if (debug_me) then
tprint(ports_difference.case)
end
elseif (debug_print) then
elseif (debug_me) then
tprint("IS IT TRIGGERED: ")
tprint(ports_difference.trigger)
end
@ -346,7 +344,7 @@ local function check_differences(host, host_name, scan_type, old_data, new_data)
if (scan_type == "tcp_portscan" or scan_type == "tcp_openports") then
tcp_old_ports = split_port_list(old_data, true)
tcp_new_ports = split_port_list(new_data, true)
if (debug_print) then
if (debug_me) then
tprint("TCP OLD PORTS: ")
tprint(tcp_old_ports)
tprint("TCP NEW PORTS: ")
@ -369,7 +367,7 @@ local function check_differences(host, host_name, scan_type, old_data, new_data)
udp_old_ports = split_port_list(old_data, false)
udp_new_ports = split_port_list(new_data, false)
if (debug_print) then
if (debug_me) then
tprint("UDP OLD PORTS: ")
tprint(udp_old_ports)
tprint("UDP NEW PORTS")
@ -748,7 +746,7 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
if already_scanned then
if(debug_print) then
if(debug_me) then
tprint("ALREADY PRESENT-> CHECKING DIFF")
end
@ -870,13 +868,13 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
local result = 1 -- success
if(not isAlreadyPresent(new_item)) then
if (debug_print) then
if (debug_me) then
tprint("SAVING HOST: "..new_item.host)
end
--saved_hosts[#saved_hosts+1] = new_item
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(new_item))
elseif not isEmptyString(id) then
if (debug_print) then
if (debug_me) then
tprint("UPDATING HOST: "..new_item.host)
end
-- edit case
@ -1049,7 +1047,6 @@ end
-- Function to verify if periodic scan is ended
function vs_utils.is_periodic_scan_over()
local periodicity_scan_in_progress = ntop.getCache(host_to_scan_periodicity_key) == "1"
if (periodicity_scan_in_progress) then
@ -1339,9 +1336,11 @@ end
-- Function to exec single host scan
function vs_utils.scan_host(scan_type, host, ports, scan_id, use_coroutines)
if(ntop.isShuttingDown()) then return(false) end
if(use_coroutines == nil) then use_coroutines = false end
if debug_print then
if debug_me then
if (ports ~= nil) then
traceError(TRACE_NORMAL,TRACE_CONSOLE, "Scanning Host ".. host .. " on Ports: " .. ports .. "\n")
else
@ -1360,6 +1359,8 @@ function vs_utils.scan_host(scan_type, host, ports, scan_id, use_coroutines)
end
end
if(ntop.isShuttingDown()) then return(false) end
vs_utils.set_status_scan(scan_type, host, ports_scan_param, id, nil,nil, vs_utils.scan_status.scanning)
-- Save on redis the scanning host to avoid inconsistent state on ntopng restarts
@ -1384,7 +1385,7 @@ function vs_utils.scan_host(scan_type, host, ports, scan_id, use_coroutines)
ntop.incrCache(host_scannned_count_key)
end
if debug_print then
if debug_me then
traceError(TRACE_NORMAL,TRACE_CONSOLE,"End scan Host ".. host .. ", result: " .. result .. "\n")
end
@ -1506,10 +1507,12 @@ end
-- Process a single host scan request that has been queued
function vs_utils.process_oldest_scheduled_scan(use_coroutines)
if(ntop.isShuttingDown()) then return(false) end
local elem = ntop.lpopCache(host_scan_queue_key)
if((elem ~= nil) and (elem ~= "")) then
if debug_print then
if debug_me then
traceError(TRACE_NORMAL,TRACE_CONSOLE, "Found vulnerability scan: ".. elem .. "\n")
end
@ -1544,7 +1547,7 @@ function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Starting up to "..max_num_scans.." scans...") end
while(max_num_scans > 0) do
while((max_num_scans > 0) and not(ntop.isShuttingDown())) do
local res = vs_utils.process_oldest_scheduled_scan(use_coroutines)
local do_inc = true
@ -1572,7 +1575,7 @@ function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
if(use_coroutines and (num > 0)) then
-- See snmp_poll.lua
while(true) do
while(not(ntop.isShuttingDown())) do
local tot = #co
local keep_on = false
@ -1772,7 +1775,7 @@ function vs_utils.runCommand(scan_command, use_coroutines)
result = nil
while(result == nil) do
while((result == nil) and not(ntop.isShuttingDown())) do
coroutine.yield()
result = ntop.readResultCmdAsync(job_id)
ntop.msleep(100)
@ -1794,6 +1797,10 @@ end
function vs_utils.nmap_scan_host(command, host_ip, ports, use_coroutines, module_name)
local scan_command
if(ntop.isShuttingDown()) then
return nil
end
-- IPv6 check
if(string.contains(host_ip, ':')) then command = command .. " -6 " end