From 54baad9ca4169f44d39d03cf84fdc3b0a4810f4d Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 29 Nov 2024 14:34:33 -0300 Subject: [PATCH] Add: script run duration stats per host For testing, run the following script. Stats are collected only if the log_whole_attack scanner preference is set. Later, the script must run to generate the result. ``` if(description) { script_oid("1.3.6.1.4.1.25623.1.200.1"); script_version("2024-11-29T13:05:23+0000"); script_tag(name:"last_modification", value:"2024-11-29 13:05:23 +0000 (Fri, 29 Nov 2024)"); script_tag(name:"creation_date", value:"2024-11-29 13:05:23 +0100 (Fri, 29 Nov 2024)"); script_tag(name:"cvss_base_vector", value:"AV:N/AC:L/Au:N/C:N/I:N/A:N"); script_tag(name:"cvss_base", value:"0.0"); script_name("Script Stats"); script_copyright("Copyright (C) 2024 Greenbone AG"); script_family("Service detection"); # nb: Needs to run at the end of the scan because of the required info only available in this phase... script_category(ACT_END); script_tag(name:"summary", value:"This scripts logs the run duration of the scripts."); script_timeout(3600); exit(0); } log_message( data: collect_host_stats() ); exit( 0 ); ``` --- nasl/nasl_init.c | 1 + nasl/nasl_scanner_glue.c | 45 ++++++++++++++++++++++++++++++++++++++++ nasl/nasl_scanner_glue.h | 3 +++ src/pluginlaunch.c | 9 ++++++++ 4 files changed, 58 insertions(+) diff --git a/nasl/nasl_init.c b/nasl/nasl_init.c index 0c8dab42d..67a65b115 100644 --- a/nasl/nasl_init.c +++ b/nasl/nasl_init.c @@ -78,6 +78,7 @@ static init_func libfuncs[] = { {"script_xref", script_xref}, {"script_tag", script_tag}, {"vendor_version", nasl_vendor_version}, + {"collect_host_stats", nasl_collect_host_stats}, {"update_table_driven_lsc_data", nasl_update_table_driven_lsc_data}, {"get_preference", nasl_get_preference}, {"safe_checks", safe_checks}, diff --git a/nasl/nasl_scanner_glue.c b/nasl/nasl_scanner_glue.c index 0338c7420..b8fb7b566 100644 --- a/nasl/nasl_scanner_glue.c +++ b/nasl/nasl_scanner_glue.c @@ -1053,6 +1053,51 @@ nasl_get_preference (lex_ctxt *lexic) return retc; } +tree_cell * +nasl_collect_host_stats (lex_ctxt *lexic) +{ + tree_cell *retc; + struct script_infos *script_infos = lexic->script_infos; + kb_t kb = script_infos->key; + GString *data = g_string_new (""); + struct kb_item *stats = NULL, *stats_tmp = NULL; + int first = 1; + + stats = kb_item_get_pattern (kb, "general/script_stats*"); + stats_tmp = stats; + + g_string_append_c (data, '['); + while (stats_tmp) + { + char **spl = g_strsplit (stats_tmp->v_str, "/", 0); + char *buf = NULL; + + if (!first) + g_string_append_c (data, ','); + + buf = g_strdup_printf ("{\"%s\": {\"start\": %s, \"stop\": %s}}", spl[0], + spl[1], spl[2]); + + g_string_append (data, buf); + g_strfreev (spl); + g_free (buf); + + stats_tmp = stats_tmp->next; + if (first) + first = 0; + } + g_string_append_c (data, ']'); + + kb_item_free (stats); + + retc = alloc_typed_cell (CONST_STR); + retc->x.str_val = strdup (data->str); + retc->size = data->len; + g_string_free (data, TRUE); + + return retc; +} + tree_cell * nasl_vendor_version (lex_ctxt *lexic) { diff --git a/nasl/nasl_scanner_glue.h b/nasl/nasl_scanner_glue.h index 5ea732f19..6c2308293 100644 --- a/nasl/nasl_scanner_glue.h +++ b/nasl/nasl_scanner_glue.h @@ -118,4 +118,7 @@ nasl_vendor_version (lex_ctxt *); tree_cell * nasl_update_table_driven_lsc_data (lex_ctxt *); +tree_cell * +nasl_collect_host_stats (lex_ctxt *); + #endif diff --git a/src/pluginlaunch.c b/src/pluginlaunch.c index 25880f99d..b054d7c72 100644 --- a/src/pluginlaunch.c +++ b/src/pluginlaunch.c @@ -183,6 +183,7 @@ update_running_processes (kb_t main_kb, kb_t kb) processes[i].start.tv_sec++; now.tv_usec += 1000000; } + if (log_whole) { char *name = nvticache_get_filename (oid); @@ -193,6 +194,14 @@ update_running_processes (kb_t main_kb, kb_t kb) (long) ((now.tv_usec - processes[i].start.tv_usec) / 1000)); g_free (name); + + char msg[2048]; + g_snprintf (msg, sizeof (msg), "%s/%ld/%ld", oid, + (long) ((now.tv_sec * 1000) + + (long) (now.tv_usec / 1000)), + (long) (processes[i].start.tv_sec * 1000 + + processes[i].start.tv_usec / 1000)); + kb_item_push_str (kb, "general/script_stats", msg); } now = old_now; do