[VS] Refactor email format.

This commit is contained in:
Nicolo Maio 2023-11-27 17:54:14 +01:00
parent 4deb5af7d6
commit 8081492a90
4 changed files with 152 additions and 49 deletions

View file

@ -785,6 +785,11 @@ local function get_counter_periodic_all_scan_keys(exec_type)
end
end
-- **********************************************************
local function get_host_id(host_details)
local host_id = ternary(isEmptyString(host_details.host_name),host_details.host, string.format("%s (%s)",host_details.host_name,host_details.host))
return host_id
end
-- **********************************************************
-- Function to update counters of periodically scan or scan all
@ -803,8 +808,7 @@ local function update_scan_info_for_report(type_of_scan_execution, new_item, hos
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))
local host_id = get_host_id(new_item)
-- handle hosts down list for email
if (is_down) then
info_json.down_hosts = true
@ -1354,7 +1358,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_skipped_hosts"))
return(i18n("hosts_stats.page_scan_hosts.email.num_failed_scanned_hosts", {num = 0}))
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}))
@ -1467,9 +1471,63 @@ function vs_utils.generate_report(date)
local report_info = retrieve_report_info(date)
vs_db_utils.save_report_info(report_info)
return report_info
end
local function add_ports_open_for_email_report(l4_key_prefix, host_details)
if (tonumber(host_details[l4_key_prefix.."_ports"]) > 0) then
return (i18n("hosts_stats.page_scan_hosts.email.host_details_open_ports", {
ports_list = host_details[l4_key_prefix.."_ports_list"],
l4_proto = ternary(l4_key_prefix == "tcp", "TCP", "UDP")
}))
end
return nil
end
local function format_all_hosts_details_info_for_email(all_hosts_details)
local formatted_hosts_details_string = ""
for _,host_details in ipairs(all_hosts_details) do
-- by default the first element is the scan type always fullfil
local label_id_scan_type = string.format("hosts_stats.page_scan_hosts.scan_type_list.%s",host_details.scan_type)
local scan_type_label = i18n(label_id_scan_type)
local formatted_host_details_string = i18n("hosts_stats.page_scan_hosts.email.host_details_scan_type",{
scan_type = scan_type_label
})
local host_id = get_host_id(host_details)
local tcp_prefix = "tcp"
local udp_prefix = "udp"
local tcp_ports_details = add_ports_open_for_email_report(tcp_prefix, host_details)
if(tcp_ports_details) then
formatted_host_details_string = string.format("%s%s",formatted_host_details_string,tcp_ports_details)
end
local udp_ports_details = add_ports_open_for_email_report(udp_prefix, host_details)
if(udp_ports_details) then
formatted_host_details_string = string.format("%s%s",formatted_host_details_string,udp_ports_details)
end
if (host_details.num_vulnerabilities_found > 0) then
local cve_list_string = cve_utils.getFirst5(host_details.cve, host_details.scan_type, false)
formatted_host_details_string = string.format("%s%s",formatted_host_details_string,i18n("hosts_stats.page_scan_hosts.email.host_details_cves", {cves_num = host_details.num_vulnerabilities_found, cves_list = cve_list_string}))
end
local host_details_email_line = i18n("hosts_stats.page_scan_hosts.email.host_details", {
host_id = host_id,
details = formatted_host_details_string
})
formatted_hosts_details_string = string.format("%s%s",formatted_hosts_details_string,host_details_email_line)
end
return formatted_hosts_details_string
end
-- **********************************************************
-- Function to send notification after a periodic scan
@ -1509,10 +1567,13 @@ function vs_utils.notify_scan_results(exec_type, periodicity)
end
local skipped_hosts_list = ""
local no_down_hosts_br = ""
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
})
else
no_down_hosts_br = "</br><br>"
end
notification_message = i18n(email_body_i18n_key, {
@ -1521,27 +1582,40 @@ 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),
no_down_hosts_br = no_down_hosts_br,
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,
end_date = end_date_formatted,
})
local possible_discrepancies_info = ""
local add_br = ""
if (email_info.has_discrepancy) then
local discrepancies_info = i18n("hosts_stats.page_scan_hosts.email.discrepancy", {
possible_discrepancies_info = i18n("hosts_stats.page_scan_hosts.email.discrepancy", {
new_ports_open = ternary(email_info.new_open_ports ~= 0, format_high_num_value_for_tables({num = email_info.new_open_ports}, "num"),"0"),
cves_fixed = ternary(email_info.fixed_cves ~= 0, format_high_num_value_for_tables({num = email_info.fixed_cves }, "num"),"0"),
hosts_discrepancy_details = email_info.discrepancies_details
})
notification_message = string.format("%s%s",notification_message,discrepancies_info)
else
add_br = "<br>"
possible_discrepancies_info = i18n("hosts_stats.page_scan_hosts.email.no_discrepancy")
end
notification_message = string.format("%s%s",notification_message,possible_discrepancies_info)
local report_link_line = i18n("hosts_stats.page_scan_hosts.email.report_link_line",{url = string.format(getHttpHost() .. ntop.getHttpPrefix() .. "/lua/enterprise/vulnerability_scan_report.lua?epoch_end=%u&epoch_begin=%u",report_date,report_date), add_br = add_br})
notification_message = string.format("%s%s",notification_message, report_link_line)
vs_utils.generate_report(email_info.end_epoch_t)
--local hosts_details = format_all_hosts_details_info_for_email(report_info.all_data_details)
--notification_message = string.format("%s<br><br>%s",notification_message,hosts_details)
if verbose then
traceError(TRACE_NORMAL,TRACE_CONSOLE, "Vulnerability Scan completed. Sending " .. title .."\n")
end
vs_utils.generate_report(email_info.end_epoch_t)
recipients.sendMessageByNotificationType({periodicity = periodicity, success = true, message = notification_message, title = title}, "vulnerability_scans")
end