[VS] Fix backup configs (#7961)

This commit is contained in:
Nicolo Maio 2023-10-30 18:23:58 +01:00
parent 1efe883551
commit f25c177b5e
5 changed files with 203 additions and 52 deletions

View file

@ -39,22 +39,24 @@ package.path = dirs.installdir .. "/scripts/lua/modules/recipients/?.lua;" .. pa
require "lua_utils" -- used by tprint (debug)
local host_to_scan_key = "ntopng.prefs.host_to_scan"
local host_to_scan_periodicity_key = "ntopng.prefs.host_to_scan.periodicity_scan"
local host_to_scan_all_key = "ntopng.prefs.host_to_scan.scan_all"
local host_to_scan_key = "ntopng.vs.hosts.scanned_values"
local prefs_host_values_key = "ntopng.prefs.vs.hosts_conf"
local host_to_scan_periodicity_key = "ntopng.vs.periodic_scan"
local host_to_scan_all_key = "ntopng.vs.scan_all"
local host_scannned_count_key = "ntopng.prefs.host_to_scan.count_scanned"
local host_scan_queue_key = "ntopng.vs_scan_queue"
local host_scan_queue_key = "ntopng.vs.scan_queue"
local scanned_hosts_changes_key = "ntopng.alerts.scanned_hosts_changes"
local host_in_scanning_hash_key = "ntopng.prefs.host_to_scan.in_scanning"
local host_in_scanning_hash_key = "ntopng.vs.hosts.in_scanning"
-- redis key for last scan report dates
local hosts_scan_last_report_dates = "ntopng.prefs.host_to_scan.report_dates"
local hosts_scan_last_report_dates = "ntopng.vs.report_dates"
-- redis keys for periodic scan info
local host_periodic_scan_info = "ntopng.prefs.host_to_scan.periodicity_scan.info"
local host_periodic_scan_info = "ntopng.vs.periodic_scan.info"
-- redis keys for scan all info
local host_scan_all_info = "ntopng.prefs.host_to_scan.scan_all.info"
local host_scan_all_info = "ntopng.vs.scan_all.info"
local json = require("dkjson")
local format_utils = require("format_utils")
@ -736,7 +738,87 @@ end
-- **********************************************************
-- Function to save host configuration
-- Function to restore backup config
function vs_utils.restore_config_backup(vs_backup)
-- remove old hash entries
ntop.delCache(host_to_scan_key)
ntop.delCache(prefs_host_values_key)
for _,item in ipairs(vs_backup) do
-- restoring hash entries with status not scanned
local host_hash_key = vs_utils.get_host_hash_key(item.host, item.scan_type)
local item_to_restore = item
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(item_to_restore))
ntop.setHashCache(prefs_host_values_key, host_hash_key, json.encode(item_to_restore))
end
end
-- *********************************************************
-- Function to retrieve hosts keys for backups
function vs_utils.retrieve_hosts_backup()
local hash_keys = ntop.getHashKeysCache(prefs_host_values_key)
local rsp = {}
if hash_keys then
for k in pairs(hash_keys) do
local hash_value_string = ntop.getHashCache(prefs_host_values_key, k)
if (not isEmptyString(hash_value_string)) then
local hash_value = json.decode(hash_value_string)
rsp[#rsp+1] = hash_value
end
end
end
return rsp
end
-- **********************************************************
--Function to save
function vs_utils.add_host_pref(scan_type, host, ports, scan_frequency)
local host_hash_key = vs_utils.get_host_hash_key(host, scan_type)
local new_item = {
host = host,
host_name = host_name,
scan_type = scan_type,
ports = ports,
}
if not isEmptyString(scan_frequency) then
new_item.scan_frequency = scan_frequency
end
local result = 1 -- success
if (debug_me) then
tprint("SAVING HOST: "..new_item.host)
end
--saved_hosts[#saved_hosts+1] = new_item
ntop.setHashCache(prefs_host_values_key, host_hash_key, json.encode(new_item))
return result
end
function vs_utils.edit_host_pref(scan_type, host, ports, scan_frequency)
local host_hash_key = vs_utils.get_host_hash_key(host, scan_type)
local old_item_string = ntop.getHashCache(prefs_host_values_key,host_hash_key)
if (not isEmptyString(old_item_string)) then
local old_item = json.decode(old_item_string)
old_item.ports = ports
old_item.scan_frequency = scan_frequency
ntop.setHashCache(prefs_host_values_key, host_hash_key, json.encode(old_item))
return 1 --ok
end
return 2 -- not found
end
-- **********************************************************
-- Function to update host scan values
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, id, is_edit, udp_ports, tcp_ports)
@ -806,15 +888,11 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
local cve_formatted, max_score_cve = get_cve_with_score(cve)
local new_item = {
host = host,
host_name = host_name,
scan_type = scan_type,
ports = ports,
num_open_ports = num_open_ports,
num_vulnerabilities_found = num_vulnerabilities_found,
cve = cve_formatted,
max_score_cve = max_score_cve,
id = epoch_id,
is_ok_last_scan = is_ok_last_scan
}
@ -872,26 +950,16 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
handle:close()
end
if not isEmptyString(id) and is_edit then
vs_utils.delete_host_to_scan_by_id(id)
end
local result = 1 -- success
if(not isAlreadyPresent(new_item)) 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_me) then
tprint("UPDATING HOST: "..new_item.host)
tprint("UPDATING HOST: "..host)
end
-- edit case
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(new_item))
else
result = 2 -- already_present
end
local counts = vs_utils.update_ts_counters()
@ -1197,15 +1265,36 @@ end
-- Function to retrieve hosts list to scan
function vs_utils.retrieve_hosts_to_scan()
local hash_keys = ntop.getHashKeysCache(host_to_scan_key)
local hash_keys = ntop.getHashKeysCache(prefs_host_values_key)
local rsp = {}
if hash_keys then
for k in pairs(hash_keys) do
local hash_value_string = ntop.getHashCache(host_to_scan_key, k)
local hash_prefs_string = ntop.getHashCache(prefs_host_values_key,k)
local hash_value = json.decode(hash_prefs_string)
if (not isEmptyString(hash_value_string)) then
local hash_value = json.decode(hash_value_string)
rsp[#rsp+1] = hash_value
-- hash value found
hash_value = json.decode(hash_value_string)
local hash_pref_value = json.decode(hash_prefs_string) or {}
for key,value in pairs(hash_pref_value) do
if (key ~= 'is_ok_last_scan') then
hash_value[key] = value
end
end
else
-- hash value not found
ntop.setHashCache(host_to_scan_key, k, hash_prefs_string)
end
rsp[#rsp+1] = hash_value
end
end
@ -1260,6 +1349,7 @@ end
-- Function to delete host to scan
function vs_utils.delete_host_to_scan(host, scan_type, all)
if all then
ntop.delCache(prefs_host_values_key)
ntop.delCache(host_to_scan_key)
ntop.delCache(host_scan_queue_key)
ntop.delCache(host_in_scanning_hash_key)
@ -1273,6 +1363,7 @@ function vs_utils.delete_host_to_scan(host, scan_type, all)
local path_to_s_result = get_report_path(scan_type, host, false)
os.remove(path_to_s_result)
ntop.delHashCache(host_to_scan_key, host_hash_key)
ntop.delHashCache(prefs_host_values_key, host_hash_key)
-- Remove this host from active schedules
local elems = {}
@ -1318,6 +1409,7 @@ function vs_utils.delete_host_to_scan_by_id(id)
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)
ntop.delHashCache(prefs_host_values_key, host_hash_key)
return true
@ -1455,22 +1547,27 @@ function vs_utils.set_status_scan(scan_type, host, ports, id, is_periodicity, is
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)
local host_hash_value
if(not isEmptyString(host_hash_value_string)) then
local host_hash_value = json.decode(host_hash_value_string)
host_hash_value.is_ok_last_scan = status
if (is_periodicity ~= nil) then
host_hash_value.is_periodicity = is_periodicity
end
if (is_all ~= nil) then
host_hash_value.is_all = is_all
end
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(host_hash_value))
host_hash_value = json.decode(host_hash_value_string) or {}
else
host_hash_value = {}
end
host_hash_value.is_ok_last_scan = status
if (is_periodicity ~= nil) then
host_hash_value.is_periodicity = is_periodicity
end
if (is_all ~= nil) then
host_hash_value.is_all = is_all
end
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(host_hash_value))
return true
end
@ -1702,6 +1799,16 @@ function vs_utils.update_all_periodicity(scan_frequency)
ntop.setHashCache(host_to_scan_key, host_hash_key, json.encode(host_hash_value))
end
local prefs_host_value_string = ntop.getHashCache(prefs_host_values_key, host_hash_key)
if (not isEmptyString(prefs_host_value_string)) then
local host_hash_value = json.decode(prefs_host_value_string)
host_hash_value.scan_frequency = scan_frequency
ntop.setHashCache(prefs_host_values_key, host_hash_key, json.encode(host_hash_value))
end
end
return true
@ -1885,6 +1992,27 @@ end
-- Migrate old configurations
function vs_utils.migrate_keys()
local old_hash_key = "ntopng.prefs.hosts_to_scan"
local old_hosts = ntop.getHashKeysCache(old_hash_key) or {}
for key,_ in pairs(old_hosts) do
local hash_value_string = ntop.getHashCache(old_hash_key, key)
local old_hash_value = json.decode(hash_value_string)
if (old_hash_value) then
local new_hash_value = {
host = old_hash_value.host,
host_name = old_hash_value.host_name,
scan_type = old_hash_value.scan_type,
ports = old_hash_value.ports,
}
ntop.setHashCache(prefs_host_values_key, key,json.encode(new_hash_value))
end
end
ntop.delCache(old_hash_key)
local hosts = ntop.getHashKeysCache(host_to_scan_key) or {}
local from_key = "tcp_openports"
local to_key = "tcp_portscan"
@ -1900,6 +2028,7 @@ function vs_utils.migrate_keys()
ntop.delHashCache(host_to_scan_key, key)
end
end
end
-- **********************************************************