Reworked vulnerability scan

Added trace
This commit is contained in:
Luca Deri 2023-10-23 21:16:51 +02:00
parent 60750831cc
commit 74963a5ba0
5 changed files with 18 additions and 19 deletions

View file

@ -63,6 +63,8 @@ local recipients = require("recipients")
local cve_utils = require("cve_utils")
local debug_print = false
local debug_me = true
local vs_utils = {}
-- **********************************************************
@ -1343,11 +1345,13 @@ function vs_utils.scan_host(scan_type, host, ports, scan_id, use_coroutines)
if(tcp_ports ~= nil) then
tcp_ports = {ports = format_port_list_to_string(tcp_ports), num_ports = #tcp_ports}
end
if scan_result then
scan_result = vs_utils.scan_status.ok
ntop.incrCache(host_scannned_count_key)
end
if debug_print then
traceError(TRACE_NORMAL,TRACE_CONSOLE,"End scan Host ".. host .. ", result: " .. result .. "\n")
end
@ -1356,6 +1360,7 @@ function vs_utils.scan_host(scan_type, host, ports, scan_id, use_coroutines)
vs_utils.save_host_to_scan(scan_type, host, result, now, duration, scan_result,
ports_scan_param, nil, num_open_ports, num_vulnerabilities_found, cve, scan_id, false, udp_ports, tcp_ports)
end
return true
end
@ -1463,7 +1468,6 @@ end
-- Process a single host scan request that has been queued
function vs_utils.process_oldest_scheduled_scan(use_coroutines)
local debug_me = false
local elem = ntop.lpopCache(host_scan_queue_key)
if((elem ~= nil) and (elem ~= "")) then
@ -1474,7 +1478,7 @@ function vs_utils.process_oldest_scheduled_scan(use_coroutines)
local elem = json.decode(elem)
if(use_coroutines) then
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "Starting scan on host "..elem.host.."["..elem.scan_type .."]") end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Starting scan on host "..elem.host.."["..elem.scan_type .."]") end
return(coroutine.create(function () vs_utils.scan_host(elem.scan_type, elem.host, elem.ports, elem.id, use_coroutines) end))
else
vs_utils.scan_host(elem.scan_type, elem.host, elem.ports, elem.id, use_coroutines)
@ -1483,7 +1487,7 @@ function vs_utils.process_oldest_scheduled_scan(use_coroutines)
end
else
if(use_coroutines) then
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "No host to scan") end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "No host to scan") end
return nil
else
return false
@ -1497,11 +1501,10 @@ end
function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
local num = 0
local co = {}
local debug_me = false
if(max_num_scans == nil) then max_num_scans = 9999 end
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "Starting up to "..max_num_scans.." scans...") end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Starting up to "..max_num_scans.." scans...") end
while(max_num_scans > 0) do
local res = vs_utils.process_oldest_scheduled_scan(use_coroutines)
@ -1526,7 +1529,7 @@ function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
end
end
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "Started "..num.." scans") end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Started "..num.." scans") end
if(use_coroutines and (num > 0)) then
-- See snmp_poll.lua
@ -1543,7 +1546,7 @@ function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
-- Therefore, if there is any error inside a coroutine, Lua will not show the error message,
-- but instead will return it to the resume call.
if not rc then
traceError(TRACE_ERROR, TRACE_CONSOLE, msg or "Unknown error occurred")
traceError(TRACE_NORMAL, TRACE_CONSOLE, msg or "Unknown error occurred")
end
keep_on = rc or keep_on
@ -1556,7 +1559,7 @@ function vs_utils.process_all_scheduled_scans(max_num_scans, use_coroutines)
end -- while
end
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "All "..num.." scans are completed") end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "All "..num.." scans are completed") end
return num
end
@ -1723,7 +1726,7 @@ function vs_utils.runCommand(scan_command, use_coroutines)
result = handle:read("*a")
handle:close()
else
if(debug_me) then traceError(TRACE_ERROR, TRACE_CONSOLE, "Started " .. scan_command) end
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Started " .. scan_command) end
if(use_coroutines) then
if(true) then
@ -1753,7 +1756,6 @@ end
function vs_utils.nmap_scan_host(command, host_ip, ports, use_coroutines, module_name)
local scan_command
local debug_me = false
-- IPv6 check
if(string.contains(host_ip, ':')) then command = command .. " -6 " end
@ -1762,7 +1764,7 @@ function vs_utils.nmap_scan_host(command, host_ip, ports, use_coroutines, module
scan_command = string.format("%s %s", command, host_ip)
-- io.write("[UDP] "..scan_command.."\n")
if(debug_me) then traceError(TRACE_NORMAL, TRACE_CONSOLE, "Executing: "..scan_command.."\n") end
local begin_epoch = os.time()
local result = vs_utils.runCommand(scan_command, use_coroutines)