[VS] Fix email format. (#8048)

This commit is contained in:
Nicolo Maio 2023-11-24 18:21:23 +01:00
parent ff30882d6f
commit 8894d6f272
2 changed files with 50 additions and 20 deletions

View file

@ -789,7 +789,7 @@ end
-- Function to update counters of periodically scan or scan all
-- @param is_periodic (true -> is a periodic scan, false -> is a scan all)
local function update_scan_info_for_report(type_of_scan_execution, new_item, host_hash_key, discrepancies)
local function update_scan_info_for_report(type_of_scan_execution, new_item, host_hash_key, discrepancies, is_down)
-- select correctly redis keys
local redis_info_key = get_counter_periodic_all_scan_keys(type_of_scan_execution)
@ -799,11 +799,34 @@ local function update_scan_info_for_report(type_of_scan_execution, new_item, hos
info_json = json.decode(info_string)
end
if (info_json == nil) then
info_json = {}
end
local host_id = ternary(isEmptyString(new_item.host_name),new_item.host, string.format("%s (%s)",new_item.host_name,new_item.host))
-- handle hosts down list for email
if (is_down) then
info_json.down_hosts = true
local host_down = i18n("hosts_stats.page_scan_hosts.email.host_down_item", {
host_id = host_id
})
if (info_json and isEmptyString(info_json.down_hosts_string_list)) then
info_json.down_hosts_string_list = host_down
else
info_json.down_hosts_string_list = string.format("%s%s",info_json.down_hosts_string_list,host_down)
end
if (info_json and info_json.not_scanned_hosts ~= nil) then
-- count just in success case
info_json.not_scanned_hosts = tonumber(info_json.not_scanned_hosts) + 1
else
info_json.not_scanned_hosts = 1
end
goto continue
end
-- **********************************************************
if (new_item.num_vulnerabilities_found ~= nil) then
if (info_json ~= {} and info_json.cves ~= nil) then
@ -837,15 +860,7 @@ local function update_scan_info_for_report(type_of_scan_execution, new_item, hos
info_json.scanned_hosts = tonumber(info_json.scanned_hosts) + 1
else
info_json.scanned_hosts = 1
end
elseif (new_item.is_ok_last_scan == vs_utils.scan_status.failed) then
if (info_json ~= {} and info_json.not_scanned_hosts ~= nil) then
-- count just in success case
info_json.not_scanned_hosts = tonumber(info_json.not_scanned_hosts) + 1
else
info_json.not_scanned_hosts = 1
end
end
end
@ -869,7 +884,6 @@ local function update_scan_info_for_report(type_of_scan_execution, new_item, hos
prefix_key = "udp_"
end
local host_id = ternary(isEmptyString(new_item.host_name),new_item.host, string.format("%s (%s)",new_item.host_name,new_item.host))
if (not cve_case and info_json ~= {}) then
-- DISCREPANCY PORTS CASES
@ -917,6 +931,7 @@ local function update_scan_info_for_report(type_of_scan_execution, new_item, hos
end
::continue::
ntop.setCache(redis_info_key, json.encode(info_json))
end
@ -1078,6 +1093,7 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
-- case host is not up and running, possible just in TCP/UDP portscan
trigger_alert_host_down(host,host_name,last_scan_time)
is_down = true
end
@ -1246,11 +1262,11 @@ function vs_utils.save_host_to_scan(scan_type, host, scan_result, last_scan_time
end
if (new_item.is_periodicity) then
update_scan_info_for_report(vs_utils.scan_in_exec_type.periodic_scan, new_item, host_hash_key, host_info_differences)
update_scan_info_for_report(vs_utils.scan_in_exec_type.periodic_scan, new_item, host_hash_key, host_info_differences, is_down)
end
if (new_item.is_all) then
update_scan_info_for_report(vs_utils.scan_in_exec_type.scan_all, new_item, host_hash_key, host_info_differences)
update_scan_info_for_report(vs_utils.scan_in_exec_type.scan_all, new_item, host_hash_key, host_info_differences, is_down)
end
remove_scanning_host({host=host, scan_type=scan_type, ports=ports})
@ -1338,7 +1354,7 @@ local function format_num_for_email(num, case)
-- not scanned_hosts --> hosts unreachable
if (num == 0) then
return(i18n("hosts_stats.page_scan_hosts.email.no_scanned_hosts"))
return(i18n("hosts_stats.page_scan_hosts.email.no_skipped_hosts"))
else
local formatted_num = format_high_num_value_for_tables({num = num}, "num")
return(i18n("hosts_stats.page_scan_hosts.email.num_failed_scanned_hosts", {num = formatted_num}))
@ -1379,7 +1395,8 @@ local function retrieve_email_info(exec_type)
end_epoch_t = os.time(),
report_type = exec_type,
-- has_dicrepancy must be true only if there are new open ports or cves fixed
has_discrepancy = ((info_json.new_open_ports or 0) > 0) or ((info_json.num_cve_solved or 0) > 0)
has_discrepancy = ((info_json.new_open_ports or 0) > 0) or ((info_json.num_cve_solved or 0) > 0),
down_hosts = info_json.down_hosts
}
if (email_info.has_discrepancy) then
@ -1387,6 +1404,9 @@ local function retrieve_email_info(exec_type)
email_info.fixed_cves = info_json.num_cve_solved or 0
email_info.discrepancies_details = tostring(info_json.hosts_discrepancies_details)
end
if (email_info.down_hosts) then
email_info.down_hosts_string_list = tostring(info_json.down_hosts_string_list)
end
email_info.duration = email_info.end_epoch_t - email_info.begin_epoch_t
@ -1488,6 +1508,12 @@ function vs_utils.notify_scan_results(exec_type, periodicity)
email_body_i18n_key = "hosts_stats.page_scan_hosts.email.scan_all_ended"
end
local skipped_hosts_list = ""
if (email_info.down_hosts) then
skipped_hosts_list = i18n("hosts_stats.page_scan_hosts.email.host_down_list", {
host_down_items = email_info.down_hosts_string_list
})
end
notification_message = i18n(email_body_i18n_key, {
cves = format_num_for_email(email_info.cve_num,0),
@ -1495,6 +1521,7 @@ function vs_utils.notify_scan_results(exec_type, periodicity)
tcp_ports = format_num_for_email(email_info.tcp_ports,2),
scanned_hosts = format_num_for_email(email_info.scanned_hosts, 3),
not_scanned_hosts = format_num_for_email(email_info.not_scanned_hosts, 4),
skipped_hosts_list = skipped_hosts_list,
url = string.format(getHttpHost() .. ntop.getHttpPrefix() .. "/lua/enterprise/vulnerability_scan_report.lua?epoch_end=%u&epoch_begin=%u",report_date,report_date),
duration = duration_label,
start_date = start_date_formatted,