From 516253750028bd71e6b17ae7f09a06fcef19a9b4 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:42:39 -0300 Subject: [PATCH 01/20] move store_file() from src/ to misc/ --- misc/scanneraux.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ misc/scanneraux.h | 4 ++ src/utils.c | 95 ----------------------------------------------- src/utils.h | 4 -- 4 files changed, 97 insertions(+), 99 deletions(-) diff --git a/misc/scanneraux.c b/misc/scanneraux.c index 33a351b987..abb86a7c84 100644 --- a/misc/scanneraux.c +++ b/misc/scanneraux.c @@ -27,3 +27,96 @@ destroy_scan_globals (struct scan_globals *globals) g_free (globals); globals = NULL; } + +/** + * @brief Adds a 'translation' entry for a file sent by the client. + * + * Files sent by the client are stored in memory on the server side. + * In order to access these files, their original name ('local' to the client) + * can be 'translated' into the file contents of the in-memory copy of the + * file on the server side. + * + * @param globals Global struct. + * @param file_hash hash to reference the file. + * @param contents Contents of the file. + */ +static void +files_add_translation (struct scan_globals *globals, const char *file_hash, + char *contents) +{ + GHashTable *trans = globals->files_translation; + // Register the mapping table if none there yet + if (trans == NULL) + { + trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + globals->files_translation = trans; + } + + g_hash_table_insert (trans, g_strdup (file_hash), contents); +} + +/** + * @brief Adds a 'content size' entry for a file sent by the client. + * + * Files sent by the client are stored in memory on the server side. + * Because they may be binary we need to store the size of the uploaded file as + * well. This function sets up a mapping from the original name sent by the + * client to the file size. + * + * @param globals Global struct. + * @param file_hash hash to reference the file. + * @param filesize Size of the file in bytes. + */ +static void +files_add_size_translation (struct scan_globals *globals, const char *file_hash, + const long filesize) +{ + GHashTable *trans = globals->files_size_translation; + gchar *filesize_str = g_strdup_printf ("%ld", filesize); + + // Register the mapping table if none there yet + if (trans == NULL) + { + trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + globals->files_size_translation = trans; + } + + g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str)); +} + +/** + * @brief Stores a file type preference in a hash table. + * + * @param globals Global struct. + * @param file File content. + * @param file_hash hash to reference the file. + * + * @return 0 if successful, -1 in case of errors. + */ +int +store_file (struct scan_globals *globals, const char *file, + const char *file_hash) +{ + char *origname; + gchar *contents = NULL; + + size_t bytes = 0; + if (!file_hash || *file_hash == '\0') + return -1; + + origname = g_strdup (file_hash); + + contents = (gchar *) g_base64_decode (file, &bytes); + if (contents == NULL) + { + g_warning ("store_file: Failed to allocate memory for uploaded file."); + g_free (origname); + return -1; + } + + files_add_translation (globals, origname, contents); + files_add_size_translation (globals, origname, bytes); + + g_free (origname); + return 0; +} diff --git a/misc/scanneraux.h b/misc/scanneraux.h index 6407212178..e70578aa03 100644 --- a/misc/scanneraux.h +++ b/misc/scanneraux.h @@ -44,4 +44,8 @@ struct script_infos void destroy_scan_globals (struct scan_globals *); +int +store_file (struct scan_globals *globals, const char *file, + const char *file_hash); + #endif /* not MISC_SCANNERAUX_H */ diff --git a/src/utils.c b/src/utils.c index 861ff93328..61c8b45366 100644 --- a/src/utils.c +++ b/src/utils.c @@ -35,101 +35,6 @@ extern int global_max_checks; */ #define G_LOG_DOMAIN "sd main" -/** - * @brief Adds a 'translation' entry for a file sent by the client. - * - * Files sent by the client are stored in memory on the server side. - * In order to access these files, their original name ('local' to the client) - * can be 'translated' into the file contents of the in-memory copy of the - * file on the server side. - * - * @param globals Global struct. - * @param file_hash hash to reference the file. - * @param contents Contents of the file. - */ -static void -files_add_translation (struct scan_globals *globals, const char *file_hash, - char *contents) -{ - GHashTable *trans = globals->files_translation; - // Register the mapping table if none there yet - if (trans == NULL) - { - trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - globals->files_translation = trans; - } - - g_hash_table_insert (trans, g_strdup (file_hash), contents); -} - -/** - * @brief Adds a 'content size' entry for a file sent by the client. - * - * Files sent by the client are stored in memory on the server side. - * Because they may be binary we need to store the size of the uploaded file as - * well. This function sets up a mapping from the original name sent by the - * client to the file size. - * - * @param globals Global struct. - * @param file_hash hash to reference the file. - * @param filesize Size of the file in bytes. - */ -static void -files_add_size_translation (struct scan_globals *globals, const char *file_hash, - const long filesize) -{ - GHashTable *trans = globals->files_size_translation; - gchar *filesize_str = g_strdup_printf ("%ld", filesize); - - // Register the mapping table if none there yet - if (trans == NULL) - { - trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - globals->files_size_translation = trans; - } - - g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str)); -} - -/** - * @brief Stores a file type preference in a hash table. - * - * @param globals Global struct. - * @param file File content. - * @param file_hash hash to reference the file. - * - * @return 0 if successful, -1 in case of errors. - */ -int -store_file (struct scan_globals *globals, const char *file, - const char *file_hash) -{ - char *origname; - gchar *contents = NULL; - - size_t bytes = 0; - - if (!file_hash || *file_hash == '\0') - return -1; - - origname = g_strdup (file_hash); - - contents = (gchar *) g_base64_decode (file, &bytes); - - if (contents == NULL) - { - g_debug ("store_file: Failed to allocate memory for uploaded file."); - g_free (origname); - return -1; - } - - files_add_translation (globals, origname, contents); - files_add_size_translation (globals, origname, bytes); - - g_free (origname); - return 0; -} - /** * Get the max number of hosts to test at the same time. */ diff --git a/src/utils.h b/src/utils.h index c47ecdbcb1..f11ff9622c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -34,10 +34,6 @@ wait_for_children1 (void); int is_scanner_only_pref (const char *); -int -store_file (struct scan_globals *globals, const char *file, - const char *file_hash); - int check_host_still_alive (kb_t, const char *); From a4e9d1e636051a228bde96e09afd5791c536bf41 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:43:10 -0300 Subject: [PATCH 02/20] minor fix --- src/attack.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/attack.c b/src/attack.c index b16dc74ceb..977fc6ddb1 100644 --- a/src/attack.c +++ b/src/attack.c @@ -12,6 +12,7 @@ #include "attack.h" +#include "../nasl/nasl_credentials.h" #include "../misc/ipc_openvas.h" #include "../misc/kb_cache.h" #include "../misc/network.h" /* for auth_printf */ @@ -488,7 +489,7 @@ launch_plugin (struct scan_globals *globals, struct scheduler_plugin *plugin, plugin->running_state = PLUGIN_STATUS_DONE; goto finish_launch_plugin; } - + /* Do not launch NVT if mandatory key is missing (e.g. an important tool * was not found). */ if (!mandatory_requirements_met (args->host_kb, nvti)) @@ -1080,7 +1081,7 @@ set_alive_detection_tid (pthread_t tid) alive_detection_tid = tid; } static pthread_t -get_alive_detection_tid () +get_alive_detection_tid (void) { return alive_detection_tid; } @@ -1112,7 +1113,7 @@ handle_scan_stop_signal () } static void -scan_stop_cleanup () +scan_stop_cleanup (void) { kb_t main_kb = NULL; char *pid; From 9f92b82434c442f97ac024d86b94192aaf85ba84 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:44:14 -0300 Subject: [PATCH 03/20] Add: functions for handling credential override --- misc/credentials.c | 849 +++++++++++++++++++++++++++++++++++++++++++++ misc/credentials.h | 136 ++++++++ 2 files changed, 985 insertions(+) create mode 100644 misc/credentials.c create mode 100644 misc/credentials.h diff --git a/misc/credentials.c b/misc/credentials.c new file mode 100644 index 0000000000..feafd7729b --- /dev/null +++ b/misc/credentials.c @@ -0,0 +1,849 @@ +/* SPDX-FileCopyrightText: 2023 Greenbone AG + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * @file credentials.c + * @brief Functions to set and get the credentials. + */ + +#include "credentials.h" + +#include "base/hosts.h" +#include "scanneraux.h" + +#include +#include +#include +#include /* for prefs_get */ +#include +#include /* gvm_uuid_make */ +#include + +#undef G_LOG_DOMAIN +/** + * @brief GLib logging domain. + */ +#define G_LOG_DOMAIN "lib misc" + +#define OID_SMB_AUTH_USER "1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:" +#define OID_SMB_AUTH_PASS "1.3.6.1.4.1.25623.1.0.90023:2:password:SMB password:" +#define OID_SMB_AUTH_DOMAIN \ + "1.3.6.1.4.1.25623.1.0.90023:3:entry:SMB domain (optional):" + +#define OID_SSH_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):" +#define OID_SSH_AUTH_USER "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:" +#define OID_SSH_AUTH_PRIV_USER \ + "1.3.6.1.4.1.25623.1.0.103591:7:entry:SSH privilege login name:" +#define OID_SSH_AUTH_PRIV_PASS \ + "1.3.6.1.4.1.25623.1.0.103591:8:password:SSH privilege password:" +#define OID_SSH_AUTH_PRIV_KEY \ + "1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:" +#define OID_SSH_AUTH_PASSPHRASE \ + "1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:" + +#define OID_KRB5_AUTH_USER "1.3.6.1.4.1.25623.1.0.102114:1:entry:KRB5 login:" +#define OID_KRB5_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.102114:2:password:KRB5 password:" +#define OID_KRB5_AUTH_REALM "1.3.6.1.4.1.25623.1.0.102114:3:entry:KRB5 realm:" +#define OID_KRB5_AUTH_KDC "1.3.6.1.4.1.25623.1.0.102114:4:entry:KRB5 kdc:" + +#define OID_ESXI_AUTH_USER \ + "1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:" +#define OID_ESXI_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:" + +#define OID_SNMP_AUTH_USER \ + "1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:" +#define OID_SNMP_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:" +#define OID_SNMP_AUTH_COMMUNITY \ + "1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:" +#define OID_SNMP_AUTH_PRIV_PASS \ + "1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:" +#define OID_SNMP_AUTH_AUTH_ALGO \ + "1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:" +#define OID_SNMP_AUTH_PRIV_ALGO \ + "1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:" + + +int ssh_credential_count = 0; +int smb_credential_count = 0; +int snmp_credential_count = 0; +int krb5_credential_count = 0; +int esxi_credential_count = 0; + +struct ssh_credential_type +{ + int port; + char *username; + char *password; + char *private_key; + char *private_key_uuid; + char *privilege_username; + char *privilege_password; + gvm_hosts_t *host_list; +}; + +struct smb_credential_type +{ + char *username; + char *password; + gvm_hosts_t *host_list; + +}; + +struct krb5_credential_type +{ + char *username; + char *password; + char *realm; + char *kdc; + gvm_hosts_t *host_list; + }; + +struct snmp_credential_type +{ + char *username; + char *password; + char *community; + char *privacy_password; + char *auth_proto; // snmp authorization protocol. 0 for md5, 1 for sha1. + char *privacy_proto; // snmp private protocol. 0 for des, 1 for aes + gvm_hosts_t *host_list; +}; + +struct esxi_credential_type +{ + char *username; + char *password; + gvm_hosts_t *host_list; +}; + +static int +credential_ssh_new (cJSON *service, credential_t **credential) +{ + ssh_credential_t *cred = g_malloc0 (sizeof (ssh_credential_t)); + cJSON *item; + + cred->port = gvm_json_obj_int (service, "port"); + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->privilege_username = + g_strdup (gvm_json_obj_str (item, "privilege_username")); + cred->privilege_password = + g_strdup (gvm_json_obj_str (item, "privilege_password")); + } + else + { + item = cJSON_GetObjectItem (service, "usk"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->private_key = g_strdup (gvm_json_obj_str (item, "private")); + cred->privilege_username = + g_strdup (gvm_json_obj_str (item, "privilege_username")); + cred->privilege_password = + g_strdup (gvm_json_obj_str (item, "privilege_password")); + } + } + if (item == NULL) + { + g_free (cred); + return -1; + } + (*credential)->type = SSH; + (*credential)->ssh_credential = cred; + ssh_credential_count++; + + return 0; +} + +static int +credential_smb_new (cJSON *service, credential_t **credential) +{ + smb_credential_t *cred = g_malloc0 (sizeof (smb_credential_t)); + cJSON *item; + + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + } + if (item == NULL) + { + g_free (cred); + return -1; + }; + + (*credential)->type = SMB; + (*credential)->smb_credential = cred; + smb_credential_count++; + + return 0; +} + +static int +credential_esxi_new (cJSON *service, credential_t **credential) +{ + esxi_credential_t *cred = g_malloc0 (sizeof (esxi_credential_t)); + cJSON *item; + + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + } + if (item == NULL) + { + g_free (cred); + return -1; + } + + (*credential)->type = ESXi; + (*credential)->esxi_credential = cred; + esxi_credential_count++; + + return 0; +} + +static int +credential_snmp_new (cJSON *service, credential_t **credential) +{ + snmp_credential_t *cred = g_malloc0 (sizeof (snmp_credential_t)); + cJSON *item; + + item = cJSON_GetObjectItem (service, "snmp"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->community = g_strdup (gvm_json_obj_str (item, "community")); + cred->privacy_password = + g_strdup (gvm_json_obj_str (item, "privacy_password")); + cred->auth_proto = g_strdup (gvm_json_obj_str (item, "auth_algorithm")); + cred->privacy_proto = + g_strdup (gvm_json_obj_str (item, "privacy_algorithm")); + } + if (item == NULL) + { + g_free (cred); + return -1; + } + + (*credential)->type = SNMP; + (*credential)->snmp_credential = cred; + snmp_credential_count++; + return 0; +} + +static int +credential_krb5_new (cJSON *service, credential_t **credential) +{ + krb5_credential_t *cred = g_malloc0 (sizeof (krb5_credential_t)); + cJSON *item; + + item = cJSON_GetObjectItem (service, "krb5"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->realm = g_strdup (gvm_json_obj_str (item, "realm")); + cred->kdc = g_strdup (gvm_json_obj_str (item, "kdc")); + } + if (item == NULL) + { + g_free (cred); + return -1; + } + (*credential)->type = KRB5; + (*credential)->krb5_credential = cred; + krb5_credential_count++; + + return 0; +} + +static void +reset_credential_counters (void) +{ + ssh_credential_count = 0; + smb_credential_count = 0; + snmp_credential_count = 0; + krb5_credential_count = 0; + esxi_credential_count = 0; +} + +int +get_ssh_credential_count (void) +{ + return ssh_credential_count; +} + +int +get_smb_credential_count (void) +{ + return smb_credential_count; +} + +int +get_snmp_credential_count (void) +{ + return snmp_credential_count; +} + +int +get_krb5_credential_count (void) +{ + return krb5_credential_count; +} + +int +get_esxi_credential_count (void) +{ + return esxi_credential_count; +} + +int +process_credentials_json (const char *json_credentials, + struct scan_globals **globals, char **err) +{ + cJSON *parser; + cJSON *service_obj = NULL; + int ret = 0; + + reset_credential_counters (); + + parser = cJSON_Parse (json_credentials); + if (parser == NULL || !cJSON_IsArray (parser)) + { + *err = g_strdup ("Unable to parse credentials data"); + ret = -1; + goto res_cleanup; + } + + cJSON_ArrayForEach (service_obj, parser) + { + if (!cJSON_IsObject (service_obj)) + { + *err = g_strdup ("Unable to parse credential data object"); + ret = -1; + goto res_cleanup; + } + + if (service_obj != NULL) + { + char *service = gvm_json_obj_str (service_obj, "service"); + credential_t *credential = g_malloc0 (sizeof (credential_t)); + ret = 0; + if (!g_strcmp0 (service, "ssh")) + { + ret = credential_ssh_new (service_obj, &credential); + if (ret == 0 && credential->ssh_credential->private_key != NULL + && credential->ssh_credential->private_key[0] != '\0') + { + char *file_uuid = gvm_uuid_make (); + if (store_file (*globals, + credential->ssh_credential->private_key, + file_uuid)) + { + g_free (file_uuid); + g_debug ("%s: Failed to parse file tipe private key.", + + __func__); + } + else + { + credential->ssh_credential->private_key_uuid = file_uuid; + } + } + } + else if (!g_strcmp0 (service, "smb")) + { + ret = credential_smb_new (service_obj, &credential); + } + else if (!g_strcmp0 (service, "esxi")) + { + ret = credential_esxi_new (service_obj, &credential); + } + else if (!g_strcmp0 (service, "snmp")) + { + ret = credential_snmp_new (service_obj, &credential); + } + else if (!g_strcmp0 (service, "krb5")) + { + ret = credential_krb5_new (service_obj, &credential); + } + else + { + g_free (credential); + g_warning ("%s: Unknown credential service type %s", __func__, + service); + continue; + } + if (ret == 0) + (*globals)->credentials = + g_slist_append ((*globals)->credentials, credential); + else + { + g_free (credential); + g_warning ( + "%s: not possible to parse a credential. Missing information", + __func__); + } + } + } + +res_cleanup: + cJSON_Delete (parser); + + return ret; +} + +static void +free_credential (gpointer data) +{ + credential_t *credential = (credential_t *) data; + if (credential->type == SSH) + { + g_free (credential->ssh_credential->username); + g_free (credential->ssh_credential->password); + g_free (credential->ssh_credential->private_key); + g_free (credential->ssh_credential->private_key_uuid); + g_free (credential->ssh_credential->privilege_username); + g_free (credential->ssh_credential->privilege_password); + g_free (credential->ssh_credential); + } + else if (credential->type == SMB) + { + g_free (credential->smb_credential->username); + g_free (credential->smb_credential->password); + g_free (credential->smb_credential); + } + else if (credential->type == ESXi) + { + g_free (credential->esxi_credential->username); + g_free (credential->esxi_credential->password); + g_free (credential->esxi_credential); + } + else if (credential->type == KRB5) + { + g_free (credential->krb5_credential->username); + g_free (credential->krb5_credential->password); + g_free (credential->krb5_credential->realm); + g_free (credential->krb5_credential->kdc); + g_free (credential->krb5_credential); + } + else if (credential->type == SNMP) + { + g_free (credential->snmp_credential->username); + g_free (credential->snmp_credential->password); + g_free (credential->snmp_credential->community); + g_free (credential->snmp_credential->privacy_password); + g_free (credential->snmp_credential); + } +} + +void +destroy_credentials (GSList **credentials) +{ + g_slist_free_full (*credentials, (GDestroyNotify) free_credential); +} + +credential_type_t +get_credential_type (credential_t *credential) +{ + return credential->type; +} + +int +get_ssh_credential_port (credential_t *credential) +{ + return credential->ssh_credential->port; +} + +char * +get_ssh_credential_username (credential_t *credential) +{ + return credential->ssh_credential->username; +} + +char * +get_ssh_credential_password (credential_t *credential) +{ + return credential->ssh_credential->password; +} + +char * +get_ssh_credential_private_key (credential_t *credential) +{ + return credential->ssh_credential->private_key; +} + +char * +get_ssh_credential_private_key_uuid (credential_t *credential) +{ + return credential->ssh_credential->private_key_uuid; +} + +char * +get_ssh_credential_privilege_username (credential_t *credential) +{ + return credential->ssh_credential->privilege_username; +} + +char * +get_ssh_credential_privilege_password (credential_t *credential) +{ + return credential->ssh_credential->privilege_password; +} + +char * +get_smb_credential_username (credential_t *credential) +{ + return credential->smb_credential->username; +} + +char * +get_smb_credential_password (credential_t *credential) +{ + return credential->smb_credential->password; +} + +char * +get_krb5_credential_username (credential_t *credential) +{ + return credential->krb5_credential->username; +} +char * +get_krb5_credential_password (credential_t *credential) +{ + return credential->krb5_credential->password; +} + +char * +get_krb5_credential_realm (credential_t *credential) +{ + return credential->krb5_credential->realm; +} + +char * +get_krb5_credential_kdc (credential_t *credential) +{ + return credential->krb5_credential->kdc; +} + +char * +get_snmp_credential_username (credential_t *credential) +{ + return credential->snmp_credential->username; +} + +char * +get_snmp_credential_password (credential_t *credential) +{ + return credential->snmp_credential->password; +} + +char * +get_snmp_credential_community (credential_t *credential) +{ + return credential->snmp_credential->community; +} +char * +get_snmp_credential_privacy_password (credential_t *credential) +{ + return credential->snmp_credential->privacy_password; +} + +char * +get_snmp_credential_auth_proto (credential_t *credential) +{ + return credential->snmp_credential->auth_proto; +} + +char * +get_snmp_credential_privacy_proto (credential_t *credential) +{ + return credential->snmp_credential->privacy_proto; +} + +char * +get_esxi_credential_username (credential_t *credential) +{ + return credential->esxi_credential->username; +} + +char * +get_esxi_credential_password (credential_t *credential) +{ + return credential->esxi_credential->password; +} + + + +static int +is_match_host (const char *host, gvm_hosts_t *host_list) +{ + (void) host; + (void) host_list; + return 0; +} + +static void +store_smb_credential (credential_t *credential) +{ + // prefs set will replace the old values if any. + prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential)); + prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential)); + // SMB credentials carry no domain. Should I drop any value left by a previous one? + //prefs_set (OID_SMB_AUTH_DOMAIN, ""); +} + +static int +match_smb_host_credential (credential_t *credential, + const char *host) +{ + const char *username = get_smb_credential_username (credential); + const char *password = get_smb_credential_password (credential); + + if (!username || !*username || !password) + return 0; + + if (!is_match_host (host, credential->smb_credential->host_list)) + return 0; + + store_smb_credential (credential); + return 1; +} + +static void +store_krb5_credential (credential_t *credential) +{ + // prefs set will replace the old values if any. + prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential)); + prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential)); + prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential)); + prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential)); +} + +static int +match_krb5_host_credential (credential_t *cred, + const char *host) +{ + if (!is_match_host (host, cred->krb5_credential->host_list)) + return 0; + + store_krb5_credential (cred); + return 1; +} + +static void +store_esxi_credential (credential_t *cred) +{ + // prefs set will replace the old values if any. + prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred)); + prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred)); +} + +struct data +{ + char *memory; + size_t size; +}; + + +static int +match_esxi_host_credential (credential_t *cred, + const char *host) +{ + if (!is_match_host (host, cred->esxi_credential->host_list)) + return 0; + + store_esxi_credential (cred); + return 1; +} + +static void +store_snmp_credential (credential_t *credential) +{ + prefs_set (OID_SNMP_AUTH_COMMUNITY, + get_snmp_credential_community (credential)); + prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential)); + prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential)); + prefs_set (OID_SNMP_AUTH_AUTH_ALGO, + get_snmp_credential_auth_proto (credential)); + prefs_set (OID_SNMP_AUTH_PRIV_PASS, + get_snmp_credential_privacy_password (credential)); + prefs_set (OID_SNMP_AUTH_PRIV_ALGO, + get_snmp_credential_privacy_proto (credential)); +} + +static int +match_snmp_host_credential (credential_t *credential, + const char *host_target) +{ + if (!is_match_host (host_target, credential->snmp_credential->host_list)) + return 0; + + store_snmp_credential (credential); + return 1; +} + +static void +store_ssh_credential (credential_t *credential) +{ + const char *private_key = get_ssh_credential_private_key (credential); + const char *password = get_ssh_credential_password (credential); + gboolean key_auth = private_key != NULL && private_key[0] != '\0'; + char port_string[6]; + int port = get_ssh_credential_port (credential); + + if (port) + { + g_snprintf (port_string, sizeof (port_string), "%d", port); + prefs_set ("auth_port_ssh", port_string); + } + + prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential)); + prefs_set (OID_SSH_AUTH_PASS, key_auth ? "" : (password ? password : "")); + prefs_set (OID_SSH_AUTH_PASSPHRASE, key_auth && password ? password : ""); + prefs_set (OID_SSH_AUTH_PRIV_USER, + get_ssh_credential_privilege_username (credential) + ? get_ssh_credential_privilege_username (credential) + : ""); + prefs_set (OID_SSH_AUTH_PRIV_PASS, + get_ssh_credential_privilege_password (credential) + ? get_ssh_credential_privilege_password (credential) + : ""); + prefs_set (OID_SSH_AUTH_PRIV_KEY, + key_auth && get_ssh_credential_private_key_uuid (credential) + ? get_ssh_credential_private_key_uuid (credential) + : ""); +} + + +static int +match_ssh_host_credential (credential_t *credential, + const char *host) +{ + if (!is_match_host (host, credential->ssh_credential->host_list)) + return 0; + + store_ssh_credential (credential); + return 1; +} + +struct credential_check_data +{ + const char *host; + credential_type_t type; + gboolean ssh_selected; + gboolean smb_selected; + gboolean snmp_selected; + gboolean krb5_selected; + gboolean esxi_selected; +}; + +static void +set_host_credential (gpointer credential, gpointer user_data) +{ + struct credential_check_data *data = user_data; + credential_t *cred = credential; + + if (cred->type != data->type) + return; + + if (cred->type == SSH && data->ssh_selected != TRUE) + { + data->ssh_selected = match_ssh_host_credential (cred, data->host); + } + else if (cred->type == SNMP && data->snmp_selected != TRUE) + { + data->snmp_selected = match_snmp_host_credential (cred, data->host); + } + else if (cred->type == SMB && data->smb_selected != TRUE) + { + data->smb_selected = match_smb_host_credential (cred, data->host); + } + else if (cred->type == KRB5 && data->krb5_selected != TRUE) + { + data->krb5_selected = match_krb5_host_credential (cred, data->host); + } + else if (cred->type == ESXi && data->esxi_selected != TRUE) + { + data->esxi_selected = match_esxi_host_credential (cred, data->host); + } +} + +static void +set_host_credentials (GSList *credentials, + const char *host, credential_type_t type) +{ + if (host == NULL) + return; + + struct credential_check_data *user_data = + g_malloc0 (sizeof (struct credential_check_data)); + user_data->host = host; + user_data->type = type; + + g_slist_foreach (credentials, (GFunc) set_host_credential, + (gpointer) user_data); + g_free (user_data); +} + +void +select_host_ssh_credential (struct scan_globals *globals, const char *host) +{ + if (globals == NULL || host == NULL + || get_ssh_credential_count () <= 1) + return; + + set_host_credentials (globals->credentials, host, SSH); +} + +void +select_host_smb_credential (struct scan_globals *globals, const char *host) +{ + if (globals == NULL || host == NULL + || get_smb_credential_count () <= 1) + return; + + set_host_credentials ( globals->credentials, host, SMB); +} + +void +select_host_snmp_credential (struct scan_globals *globals, const char *host) +{ + if (globals == NULL || host == NULL + || get_snmp_credential_count () <= 1) + return; + set_host_credentials ( globals->credentials, host, SNMP); +} + +void +select_host_krb5_credential (struct scan_globals *globals, const char *host) +{ + if (globals == NULL || host == NULL + || get_krb5_credential_count () <= 1) + return; + set_host_credentials (globals->credentials, host, KRB5); +} + +void +select_host_esxi_credential (struct scan_globals *globals, const char *host) +{ + if (globals == NULL || host == NULL + || get_esxi_credential_count () <= 1) + return; + + set_host_credentials (globals->credentials, host, ESXi); +} diff --git a/misc/credentials.h b/misc/credentials.h new file mode 100644 index 0000000000..85aa62dd43 --- /dev/null +++ b/misc/credentials.h @@ -0,0 +1,136 @@ +/* SPDX-FileCopyrightText: 2023 Greenbone AG + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * @file credentials.h + * @brief Module for handling credentials. + */ + +#ifndef MISC_CREDENTIALS_H +#define MISC_CREDENTIALS_H + +#include "scanneraux.h" + +#include +#include +#include /* for prefs_get */ + +typedef struct ssh_credential_type ssh_credential_t; +typedef struct smb_credential_type smb_credential_t; +typedef struct krb5_credential_type krb5_credential_t; +typedef struct snmp_credential_type snmp_credential_t; +typedef struct esxi_credential_type esxi_credential_t; + +enum credential_type +{ + SSH, + SMB, + KRB5, + SNMP, + ESXi, +}; + +typedef enum credential_type credential_type_t; + +struct credential +{ + credential_type_t type; + union + { + ssh_credential_t *ssh_credential; + smb_credential_t *smb_credential; + esxi_credential_t *esxi_credential; + snmp_credential_t *snmp_credential; + krb5_credential_t *krb5_credential; + }; +}; +typedef struct credential credential_t; + +int +process_credentials_json (const char *, struct scan_globals **, char **); + +void +destroy_credentials (GSList **); + +credential_type_t +get_credential_type (credential_t *); +int +get_ssh_credential_port (credential_t *); +char * +get_ssh_credential_username (credential_t *); +char * +get_ssh_credential_password (credential_t *); +char * +get_ssh_credential_private_key (credential_t *); +char * +get_ssh_credential_private_key_uuid (credential_t *); +char * +get_ssh_credential_privilege_username (credential_t *); +char * +get_ssh_credential_privilege_password (credential_t *); +int +get_ssh_credential_count (void); + +char * +get_smb_credential_username (credential_t *); +char * +get_smb_credential_password (credential_t *); +int +get_smb_credential_count (void); + +char * +get_krb5_credential_username (credential_t *); +char * +get_krb5_credential_password (credential_t *); +char * +get_krb5_credential_realm (credential_t *); +char * +get_krb5_credential_kdc (credential_t *); +int +get_krb5_credential_count (void); + +char * +get_snmp_credential_username (credential_t *); +char * +get_snmp_credential_password (credential_t *); +char * +get_snmp_credential_community (credential_t *); +char * +get_snmp_credential_privacy_password (credential_t *); +char * +get_snmp_credential_auth_proto (credential_t *); +char * +get_snmp_credential_privacy_proto (credential_t *); +int +get_snmp_credential_count (void); + +char * +get_esxi_credential_username (credential_t *); +char * +get_esxi_credential_password (credential_t *); +int +get_esxi_credential_count (void); + +struct scan_globals; + +/* Select and publish one type credential in the per-host scanner process per + * service. */ +void +select_host_ssh_credential (struct scan_globals *, const char *); + +void +select_host_smb_credential (struct scan_globals *, const char *); + +void +select_host_snmp_credential (struct scan_globals *, const char *); + +void +select_host_krb5_credential (struct scan_globals *, const char *); + +void +select_host_esxi_credential (struct scan_globals *, const char *); + + +#endif // MISC_CREDENTIALS_H From 9b2cb65d9ec909b4213d61e96a53987b39db3f44 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:45:56 -0300 Subject: [PATCH 04/20] Adjust dependencies for building --- .github/install-openvas-dependencies.sh | 1 + misc/CMakeLists.txt | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/install-openvas-dependencies.sh b/.github/install-openvas-dependencies.sh index b5e14d72de..d021d9eb8c 100755 --- a/.github/install-openvas-dependencies.sh +++ b/.github/install-openvas-dependencies.sh @@ -37,6 +37,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \ libpopt0 \ libcurl4 \ libcurl4-gnutls-dev \ + libcjson-dev \ libhiredis-dev \ libmagic-dev diff --git a/misc/CMakeLists.txt b/misc/CMakeLists.txt index 6cf54b468d..b6464ae210 100644 --- a/misc/CMakeLists.txt +++ b/misc/CMakeLists.txt @@ -15,6 +15,7 @@ pkg_check_modules (GLIB REQUIRED glib-2.0>=2.42) pkg_check_modules (GLIB_JSON REQUIRED json-glib-1.0>=1.4.4) pkg_check_modules (GNUTLS REQUIRED gnutls>=3.6.4) pkg_check_modules (CURL REQUIRED libcurl>=7.74.0) +pkg_check_modules(CJSON REQUIRED libcjson>=1.7.14) pkg_check_modules (KRB5 REQUIRED mit-krb5) pkg_check_modules (KRB5_GSSAPI REQUIRED mit-krb5-gssapi) @@ -87,7 +88,7 @@ include_directories (${GLIB_INCLUDE_DIRS} ${GLIB_JSON_INCLUDE_DIRS} set (FILES bpf_share.c ftp_funcs.c vendorversion.c network.c plugutils.c pcap.c scan_id.c strutils.c table_driven_lsc.c ipc.c ipc_openvas.c ipc_pipe.c - user_agent.c scanneraux.c kb_cache.c heartbeat.c openvas-krb5.c) + user_agent.c scanneraux.c kb_cache.c heartbeat.c openvas-krb5.c credentials.c) # On windows we are always PIC and stack-protector is replaces by DEP # Also stack protection needs a shared library to work @@ -105,7 +106,7 @@ set_target_properties (openvas_misc_shared PROPERTIES VERSION "${PROJECT_VERSION target_link_libraries (openvas_misc_shared LINK_PRIVATE ${GNUTLS_LDFLAGS} ${UUID_LDFLAGS} ${GLIB_LDFLAGS} ${GLIB_JSON_LDFLAGS} ${MAGIC_LDFLAGS} - ${PCAP_LDFLAGS} ${LIBGVM_BOREAS_LDFLAGS} ${CURL_LDFLAGS} ${KRB5_LDFLAGS} ${KRB5_GSSAPI_LDFLAGS} + ${PCAP_LDFLAGS} ${LIBGVM_BOREAS_LDFLAGS} ${CURL_LDFLAGS} ${CJSON_LDFLAGS} ${KRB5_LDFLAGS} ${KRB5_GSSAPI_LDFLAGS} ${LINKER_HARDENING_FLAGS}) if (OPENVAS_STATE_DIR) From 31b1b31c01b23a133fc2491bb1c4c61fd75b07e8 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:46:28 -0300 Subject: [PATCH 05/20] Add credentials list into the globals struct --- misc/scanneraux.c | 4 ++++ misc/scanneraux.h | 1 + 2 files changed, 5 insertions(+) diff --git a/misc/scanneraux.c b/misc/scanneraux.c index abb86a7c84..0e069d8ca5 100644 --- a/misc/scanneraux.c +++ b/misc/scanneraux.c @@ -10,6 +10,8 @@ #include "scanneraux.h" +#include "credentials.h" + void destroy_scan_globals (struct scan_globals *globals) { @@ -24,6 +26,8 @@ destroy_scan_globals (struct scan_globals *globals) if (globals->files_size_translation) g_hash_table_destroy (globals->files_size_translation); + destroy_credentials (&globals->credentials); + g_free (globals); globals = NULL; } diff --git a/misc/scanneraux.h b/misc/scanneraux.h index e70578aa03..ccc6f45d17 100644 --- a/misc/scanneraux.h +++ b/misc/scanneraux.h @@ -19,6 +19,7 @@ struct scan_globals { GHashTable *files_translation; GHashTable *files_size_translation; + GSList *credentials; char *scan_id; pid_t host_pid; }; From b0d84c9b1c0b129b17a08fc487e80510d2170212 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:47:27 -0300 Subject: [PATCH 06/20] parse received credential --- src/openvas.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/openvas.c b/src/openvas.c index 6b67ed250a..13f54d536a 100644 --- a/src/openvas.c +++ b/src/openvas.c @@ -22,6 +22,7 @@ #include "openvas.h" +#include "../misc/credentials.h" /* for process_credentials_json */ #include "../misc/kb_cache.h" #include "../misc/plugutils.h" /* nvticache_free */ #include "../misc/scan_id.h" /* to manage global scan_id */ @@ -118,7 +119,7 @@ static openvas_option openvas_defaults[] = { * @brief Set the prefs from the openvas_defaults array. */ static void -set_default_openvas_prefs () +set_default_openvas_prefs (void) { for (int i = 0; openvas_defaults[i].option != NULL; i++) prefs_set (openvas_defaults[i].option, openvas_defaults[i].value); @@ -252,6 +253,20 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) g_strfreev (pref); res = res->next; } + + const char *json = prefs_get ("multi-credentials"); + if (json != NULL && json[0] != '\0') + { + char *err = NULL; + + if (process_credentials_json (json, &globals, &err) < 0) + { + g_warning ("It was not possible to process multiple credentials: %s", + err); + g_free (err); + } + } + kb_del_items (kb, key); snprintf (key, sizeof (key), "internal/%s", globals->scan_id); kb_item_set_str_with_main_kb_check (kb, key, "ready", 0); @@ -259,7 +274,6 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) kb_lnk_reset (kb); g_debug ("End loading scan preferences."); - kb_item_free (res); return 0; } @@ -270,7 +284,7 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) * @return 0 on success, -1 on error. */ static int -init_logging () +init_logging (void) { static gchar *log_config_file_name = NULL; int err; @@ -309,7 +323,7 @@ gcrypt_init (void) * @brief Check TLS. */ static void -check_tls () +check_tls (void) { #if GNUTLS_VERSION_NUMBER < 0x030300 if (openvas_SSL_init () < 0) @@ -330,7 +344,7 @@ check_tls () * @brief Print start message. */ static void -openvas_print_start_msg () +openvas_print_start_msg (void) { #ifdef OPENVAS_GIT_REVISION g_message ("openvas %s (GIT revision %s) started", OPENVAS_VERSION, From 61efe11225f31a4fe3fa5437ae33b9e6be0d1b31 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 10:49:34 -0300 Subject: [PATCH 07/20] accept multiple credentials for the same service --- rust/src/openvasd/api/error.rs | 9 +-------- rust/src/openvasd/api/routes/scans.rs | 19 +------------------ 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/rust/src/openvasd/api/error.rs b/rust/src/openvasd/api/error.rs index 7f6ad84eef..2dbc2a5c5c 100644 --- a/rust/src/openvasd/api/error.rs +++ b/rust/src/openvasd/api/error.rs @@ -11,7 +11,7 @@ use axum::{ http::{StatusCode, header::CONTENT_TYPE}, response::{IntoResponse, Response}, }; -use scannerlib::{models, notus::NotusError}; +use scannerlib::notus::NotusError; use serde::Serialize; use thiserror::Error; use tokio::sync::mpsc::error::SendError; @@ -25,8 +25,6 @@ pub enum ApiError { Notus(#[from] NotusError), #[error("scheduling error: {0}")] Scheduling(#[from] SendError), - #[error("duplicate credentials")] - DuplicateCredentials(models::Service), #[error("feed is not synced")] FeedNotSynced, #[error("scan still running")] @@ -60,7 +58,6 @@ impl ApiError { _ => StatusCode::INTERNAL_SERVER_ERROR, }, ApiError::Scheduling(_) => StatusCode::INTERNAL_SERVER_ERROR, - ApiError::DuplicateCredentials(_) => StatusCode::BAD_REQUEST, ApiError::FeedNotSynced => StatusCode::SERVICE_UNAVAILABLE, ApiError::ScanRunning => StatusCode::CONFLICT, ApiError::Json(_) => StatusCode::BAD_REQUEST, @@ -102,10 +99,6 @@ impl From for ErrorJson { | ApiError::VtsError(_) | ApiError::Notus(_) | ApiError::JoinError(_) => ("Internal server error".to_string(), None), - ApiError::DuplicateCredentials(service) => ( - "Duplicate service credentials".to_string(), - Some(service.to_string()), - ), ApiError::FeedNotSynced => ("Feed is not synced".to_string(), None), ApiError::ScanRunning => ( "Unable to perform action".to_string(), diff --git a/rust/src/openvasd/api/routes/scans.rs b/rust/src/openvasd/api/routes/scans.rs index dc365a76a1..c47ad8eccd 100644 --- a/rust/src/openvasd/api/routes/scans.rs +++ b/rust/src/openvasd/api/routes/scans.rs @@ -1,7 +1,7 @@ //! `/scans` routes. #![allow(clippy::result_large_err)] -use std::{collections::HashSet, num::ParseIntError, sync::Arc}; +use std::{num::ParseIntError, sync::Arc}; use axum::{ Extension, Json, Router, @@ -104,10 +104,6 @@ async fn post_scans( ) -> Result { let mut scan = scan?; - if let Some(dup) = get_duplicate_credential_service(&scan.target.credentials) { - return Err(ApiError::DuplicateCredentials(dup)); - } - // generate a random scan id if none was given if scan.scan_id.is_empty() { scan.scan_id = uuid::Uuid::new_v4().into(); @@ -305,19 +301,6 @@ async fn get_scans_id_status( Ok(Json(scanner.get_scan_status(&client_id, &scan_id).await?)) } -/// Finds and returns duplicate credentials for a single service. -pub(super) fn get_duplicate_credential_service( - credentials: &[models::Credential], -) -> Option { - let mut map: HashSet<_> = HashSet::default(); - for c in credentials.iter() { - if !map.insert(c.service.to_string()) { - return Some(c.service.clone()); - } - } - None -} - /// Parses a string either containing either a single number or into an inclusive numerical range. /// /// ## Examples From ae000e1d3cf16e198cc389e8f0bfe2ab54d2bbe0 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 11:34:23 -0300 Subject: [PATCH 08/20] accept hosts field in credential struct, which tied the credential to an specific host list --- rust/src/models/credential.rs | 4 ++++ .../container_image_scanner/scheduling/db/sqlite/scan.rs | 1 + rust/src/openvasd/scans/mod.rs | 1 + rust/src/scannerctl/osp/start_scan.rs | 1 + 4 files changed, 7 insertions(+) diff --git a/rust/src/models/credential.rs b/rust/src/models/credential.rs index 0f741f6249..d1ccb31444 100644 --- a/rust/src/models/credential.rs +++ b/rust/src/models/credential.rs @@ -12,6 +12,9 @@ pub struct Credential { #[serde(flatten)] /// Type of the credential to get access. Different services support different types. pub credential_type: CredentialType, + /// Use this credential only for listed hosts + #[serde(default, flatten, skip_serializing_if = "Option::is_none")] + pub hosts: Option>, } impl Default for Credential { @@ -19,6 +22,7 @@ impl Default for Credential { Self { service: Service::SSH, port: Default::default(), + hosts: None, credential_type: CredentialType::UP { username: "root".to_string(), password: "".to_string(), diff --git a/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs b/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs index 1393251947..314f9f7a44 100644 --- a/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs +++ b/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs @@ -212,6 +212,7 @@ impl<'o> Fetch for DBScan<'o, String> { }, service: models::Service::Generic, port: None, + hosts: None, }) .collect(), ..Default::default() diff --git a/rust/src/openvasd/scans/mod.rs b/rust/src/openvasd/scans/mod.rs index 27a866c916..3420a9c867 100644 --- a/rust/src/openvasd/scans/mod.rs +++ b/rust/src/openvasd/scans/mod.rs @@ -183,6 +183,7 @@ pub mod tests { service, port, credential_type, + hosts: None, }) .collect() } diff --git a/rust/src/scannerctl/osp/start_scan.rs b/rust/src/scannerctl/osp/start_scan.rs index a168614cd4..1fe778a29b 100644 --- a/rust/src/scannerctl/osp/start_scan.rs +++ b/rust/src/scannerctl/osp/start_scan.rs @@ -73,6 +73,7 @@ impl From for Vec { service: (&x.service as &str).try_into().ok().unwrap_or(Service::SSH), port: x.port.and_then(|x| x.parse().ok()), credential_type: kind, + hosts: None, //multiple credentials not supported by ospd } }) .collect() From 5ef383f8783d88b872252cb3a403c567f15763a9 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 11:42:58 -0300 Subject: [PATCH 09/20] use as default credetial of each service, the first one found without hosts --- rust/src/models/credential.rs | 2 +- rust/src/openvas/pref_handler.rs | 130 ++++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/rust/src/models/credential.rs b/rust/src/models/credential.rs index d1ccb31444..14a2e8f7d6 100644 --- a/rust/src/models/credential.rs +++ b/rust/src/models/credential.rs @@ -13,7 +13,7 @@ pub struct Credential { /// Type of the credential to get access. Different services support different types. pub credential_type: CredentialType, /// Use this credential only for listed hosts - #[serde(default, flatten, skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub hosts: Option>, } diff --git a/rust/src/openvas/pref_handler.rs b/rust/src/openvas/pref_handler.rs index 6fafa3385d..55370ad8ab 100644 --- a/rust/src/openvas/pref_handler.rs +++ b/rust/src/openvas/pref_handler.rs @@ -353,12 +353,27 @@ where fn prepare_credentials_for_openvas(&mut self) -> RedisStorageResult<()> { let credentials = self.scan_config.target.credentials.clone(); + // To mark that a default credential was already set for a service. + // In case of multiple credentials for the same service, the first one + // in the list is set as default. + let mut default_ssh_set = false; + let mut default_smb_set = false; + let mut default_snmp_set = false; + let mut default_krb5_set = false; + let mut default_esxi_set = false; let mut credential_preferences: Vec = vec![]; - for credential in credentials { + for credential in credentials.clone() { // TODO: refactor that match credential.service { Service::KRB5 => { + // Only the first credential without hosts is set as default. + // Multiple KRB5 candidates are later handed and could override + // the default one. + if credential.hosts.is_some() || default_krb5_set { + continue; + } + if let Some(port) = credential.port { credential_preferences.push(format!("auth_port_krb5|||{port}")); } else { @@ -377,9 +392,16 @@ where .push(format!("{OID_KRB5_AUTH}:2:password::|||{password}")); credential_preferences.push(format!("{OID_KRB5_AUTH}:3:entry::|||{realm}")); credential_preferences.push(format!("{OID_KRB5_AUTH}:4:entry::|||{kdc}")); + default_krb5_set = true; } } Service::SSH => { + // Only the first credential whithout hosts is set as default. + // Multiple SSH candidates are later handed and could override + // the default one. + if credential.hosts.is_some() || default_ssh_set { + continue; + } if let Some(port) = credential.port { credential_preferences.push(format!("auth_port_ssh|||{port}")); } else { @@ -408,6 +430,7 @@ where p.password )); } + default_ssh_set = true; }; if let CredentialType::USK { username, @@ -436,9 +459,17 @@ where p.password )); } + default_ssh_set = true; }; } Service::SMB => { + // Only the first credential is set as default. + // Multiple SMB candidates are later handed and could override + // the default one. + if credential.hosts.is_some() || default_smb_set { + continue; + } + if let CredentialType::UP { username, password, .. } = credential.credential_type.clone() @@ -448,9 +479,17 @@ where credential_preferences.push(format!( "{OID_SMB_AUTH}:2:password:SMB password:|||{password}" )); + default_smb_set = true; }; } Service::ESXi => { + // Only the first credential is set as default. + // Multiple ESXi candidates are later handed and could override + // the default one. + if credential.hosts.is_some() || default_esxi_set { + continue; + } + if let CredentialType::UP { username, password, .. } = credential.credential_type.clone() @@ -461,9 +500,17 @@ where credential_preferences.push(format!( "{OID_ESXI_AUTH}:2:password:ESXi login password:|||{password}" )); + default_esxi_set = true; }; } Service::SNMP => { + // Only the first credential is set as default. + // Multiple SNMP candidates are later handed and could override + // the default one. + if credential.hosts.is_some() || default_snmp_set { + continue; + } + if let CredentialType::SNMP { username, password, @@ -517,6 +564,7 @@ where credential_preferences.push(format!( "{OID_SNMP_AUTH}:6:radio:SNMPv3 Privacy Algorithm:|||{privacy_algorithm}" )); + default_snmp_set = true; } } @@ -526,6 +574,12 @@ where ), } } + if !credentials.is_empty() { + credential_preferences.push(format!( + "multi-credentials|||{}", + serde_json::to_string(&credentials).unwrap() + )); + } if !credential_preferences.is_empty() { self.redis_connector.push_kb_item( @@ -726,6 +780,7 @@ mod tests { Credential { service: Service::SSH, port: Some(22), + hosts: None, credential_type: CredentialType::UP { username: "user".to_string(), password: "pass".to_string(), @@ -735,6 +790,7 @@ mod tests { Credential { service: Service::KRB5, port: None, + hosts: None, credential_type: CredentialType::KRB5 { username: "user".to_string(), password: "pass".to_string(), @@ -770,6 +826,78 @@ mod tests { )); } + #[test] + fn test_multiple_ssh_credentials_first_is_default_and_all_are_candidates() { + let mut scan = scan_config(); + scan.target.credentials = vec![ + Credential { + service: Service::SSH, + port: Some(2222), + hosts: Some(vec!["localhost".to_string()]), // does not apply as default, it has a host + credential_type: CredentialType::UP { + username: "zero".to_string(), + password: "zero-pass".to_string(), + privilege: None, + }, + }, + Credential { + service: Service::SSH, + port: Some(22), + hosts: None, // first without hosts + credential_type: CredentialType::UP { + username: "first".to_string(), + password: "first-pass".to_string(), + privilege: None, + }, + }, + Credential { + service: Service::SSH, + port: Some(22022), + hosts: None, // first without hosts + credential_type: CredentialType::UP { + username: "second".to_string(), + password: "second-pass".to_string(), + privilege: None, + }, + }, + + ]; + let expected = format!( + "multi-credentials|||{}", + serde_json::to_string(&scan.target.credentials).unwrap() + ); + let mut rc = fake_redis(); + let mut prefh = handler(scan, &mut rc); + + assert!(prefh.prepare_credentials_for_openvas().is_ok()); + assert!( + prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", &expected) + ); + // Only the first credential is set as default. + for preference in [ + "auth_port_ssh|||22", + "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||first", + ] { + assert!( + prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", preference) + ); + } + for preference in [ + "auth_port_ssh|||2222", + "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||second", + ] { + assert!( + !prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", preference) + ); + } + } + #[test] fn test_plugins() { let mut scan = scan_config(); From 7beffa7f06ecbd387394ac4f96a633f2b36f4d3a Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 17 Nov 2026 15:12:16 -0300 Subject: [PATCH 10/20] parse the crendetials host item and store it in the credential struct as gvm_hosts_t type. Create the override_host_credentials() function and call it from attack.c before starting to scan the host. It passes the gvm_host_t to be compared with the host list in credentials. A matching host in credential with the current scan host, stores the credential to be used for the rest of the host scan. --- misc/credentials.c | 410 +++++++++++++++++++++++++-------------------- misc/credentials.h | 74 +------- src/attack.c | 4 +- 3 files changed, 234 insertions(+), 254 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index feafd7729b..4d1f99cfb3 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -122,13 +122,161 @@ struct esxi_credential_type gvm_hosts_t *host_list; }; +static int +get_ssh_credential_port (credential_t *credential) +{ + return credential->ssh_credential->port; +} + +static char * +get_ssh_credential_username (credential_t *credential) +{ + return credential->ssh_credential->username; +} + +static char * +get_ssh_credential_password (credential_t *credential) +{ + return credential->ssh_credential->password; +} + +static char * +get_ssh_credential_private_key (credential_t *credential) +{ + return credential->ssh_credential->private_key; +} + +static char * +get_ssh_credential_private_key_uuid (credential_t *credential) +{ + return credential->ssh_credential->private_key_uuid; +} + +static char * +get_ssh_credential_privilege_username (credential_t *credential) +{ + return credential->ssh_credential->privilege_username; +} + +static char * +get_ssh_credential_privilege_password (credential_t *credential) +{ + return credential->ssh_credential->privilege_password; +} + +static char * +get_smb_credential_username (credential_t *credential) +{ + return credential->smb_credential->username; +} + +static char * +get_smb_credential_password (credential_t *credential) +{ + return credential->smb_credential->password; +} + +static char * +get_krb5_credential_username (credential_t *credential) +{ + return credential->krb5_credential->username; +} +static char * +get_krb5_credential_password (credential_t *credential) +{ + return credential->krb5_credential->password; +} + +static char * +get_krb5_credential_realm (credential_t *credential) +{ + return credential->krb5_credential->realm; +} + +static char * +get_krb5_credential_kdc (credential_t *credential) +{ + return credential->krb5_credential->kdc; +} + +static char * +get_snmp_credential_username (credential_t *credential) +{ + return credential->snmp_credential->username; +} + +static char * +get_snmp_credential_password (credential_t *credential) +{ + return credential->snmp_credential->password; +} + +static char * +get_snmp_credential_community (credential_t *credential) +{ + return credential->snmp_credential->community; +} + +static char * +get_snmp_credential_privacy_password (credential_t *credential) +{ + return credential->snmp_credential->privacy_password; +} + +static char * +get_snmp_credential_auth_proto (credential_t *credential) +{ + return credential->snmp_credential->auth_proto; +} + +static char * +get_snmp_credential_privacy_proto (credential_t *credential) +{ + return credential->snmp_credential->privacy_proto; +} + +static char * +get_esxi_credential_username (credential_t *credential) +{ + return credential->esxi_credential->username; +} + +static char * +get_esxi_credential_password (credential_t *credential) +{ + return credential->esxi_credential->password; +} + +static void +create_host_list (cJSON *hosts, gvm_hosts_t **host_list) +{ + if (hosts && cJSON_IsArray (hosts)) + { + for (int i = 0; i < cJSON_GetArraySize (hosts); i++) + { + cJSON *host_item = cJSON_GetArrayItem (hosts, i); + gvm_host_t *host = + gvm_host_from_str (cJSON_GetStringValue (host_item)); + + gvm_hosts_add(*host_list, host); + } + } + +} + static int credential_ssh_new (cJSON *service, credential_t **credential) { ssh_credential_t *cred = g_malloc0 (sizeof (ssh_credential_t)); cJSON *item; + gvm_hosts_t *host_list = gvm_hosts_new(""); + + item = cJSON_GetObjectItem(service,"hosts"); + create_host_list(item, &host_list); + cred->host_list = host_list; cred->port = gvm_json_obj_int (service, "port"); + item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) { @@ -170,6 +318,11 @@ credential_smb_new (cJSON *service, credential_t **credential) { smb_credential_t *cred = g_malloc0 (sizeof (smb_credential_t)); cJSON *item; + gvm_hosts_t *host_list = gvm_hosts_new(""); + + item = cJSON_GetObjectItem(service,"hosts"); + create_host_list(item, &host_list); + cred->host_list = host_list; item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) @@ -195,6 +348,11 @@ credential_esxi_new (cJSON *service, credential_t **credential) { esxi_credential_t *cred = g_malloc0 (sizeof (esxi_credential_t)); cJSON *item; + gvm_hosts_t *host_list = gvm_hosts_new(""); + + item = cJSON_GetObjectItem(service,"hosts"); + create_host_list(item, &host_list); + cred->host_list = host_list; item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) @@ -220,6 +378,11 @@ credential_snmp_new (cJSON *service, credential_t **credential) { snmp_credential_t *cred = g_malloc0 (sizeof (snmp_credential_t)); cJSON *item; + gvm_hosts_t *host_list = gvm_hosts_new(""); + + item = cJSON_GetObjectItem(service,"hosts"); + create_host_list(item, &host_list); + cred->host_list = host_list; item = cJSON_GetObjectItem (service, "snmp"); if (item && cJSON_IsObject (item)) @@ -250,6 +413,11 @@ credential_krb5_new (cJSON *service, credential_t **credential) { krb5_credential_t *cred = g_malloc0 (sizeof (krb5_credential_t)); cJSON *item; + gvm_hosts_t *host_list = gvm_hosts_new(""); + + item = cJSON_GetObjectItem(service,"hosts"); + create_host_list(item, &host_list); + cred->host_list = host_list; item = cJSON_GetObjectItem (service, "krb5"); if (item && cJSON_IsObject (item)) @@ -281,31 +449,31 @@ reset_credential_counters (void) esxi_credential_count = 0; } -int +static int get_ssh_credential_count (void) { return ssh_credential_count; } -int +static int get_smb_credential_count (void) { return smb_credential_count; } -int +static int get_snmp_credential_count (void) { return snmp_credential_count; } -int +static int get_krb5_credential_count (void) { return krb5_credential_count; } -int +static int get_esxi_credential_count (void) { return esxi_credential_count; @@ -419,18 +587,27 @@ free_credential (gpointer data) g_free (credential->ssh_credential->private_key_uuid); g_free (credential->ssh_credential->privilege_username); g_free (credential->ssh_credential->privilege_password); + + if (credential->ssh_credential->host_list != NULL) + gvm_hosts_free (credential->ssh_credential->host_list); + g_free (credential->ssh_credential); } else if (credential->type == SMB) { g_free (credential->smb_credential->username); g_free (credential->smb_credential->password); + if (credential->smb_credential->host_list != NULL) + gvm_hosts_free (credential->smb_credential->host_list); g_free (credential->smb_credential); } else if (credential->type == ESXi) { g_free (credential->esxi_credential->username); g_free (credential->esxi_credential->password); + if (credential->esxi_credential->host_list != NULL) + gvm_hosts_free (credential->esxi_credential->host_list); + g_free (credential->esxi_credential); } else if (credential->type == KRB5) @@ -439,6 +616,8 @@ free_credential (gpointer data) g_free (credential->krb5_credential->password); g_free (credential->krb5_credential->realm); g_free (credential->krb5_credential->kdc); + if (credential->krb5_credential->host_list != NULL) + gvm_hosts_free (credential->krb5_credential->host_list); g_free (credential->krb5_credential); } else if (credential->type == SNMP) @@ -457,146 +636,6 @@ destroy_credentials (GSList **credentials) g_slist_free_full (*credentials, (GDestroyNotify) free_credential); } -credential_type_t -get_credential_type (credential_t *credential) -{ - return credential->type; -} - -int -get_ssh_credential_port (credential_t *credential) -{ - return credential->ssh_credential->port; -} - -char * -get_ssh_credential_username (credential_t *credential) -{ - return credential->ssh_credential->username; -} - -char * -get_ssh_credential_password (credential_t *credential) -{ - return credential->ssh_credential->password; -} - -char * -get_ssh_credential_private_key (credential_t *credential) -{ - return credential->ssh_credential->private_key; -} - -char * -get_ssh_credential_private_key_uuid (credential_t *credential) -{ - return credential->ssh_credential->private_key_uuid; -} - -char * -get_ssh_credential_privilege_username (credential_t *credential) -{ - return credential->ssh_credential->privilege_username; -} - -char * -get_ssh_credential_privilege_password (credential_t *credential) -{ - return credential->ssh_credential->privilege_password; -} - -char * -get_smb_credential_username (credential_t *credential) -{ - return credential->smb_credential->username; -} - -char * -get_smb_credential_password (credential_t *credential) -{ - return credential->smb_credential->password; -} - -char * -get_krb5_credential_username (credential_t *credential) -{ - return credential->krb5_credential->username; -} -char * -get_krb5_credential_password (credential_t *credential) -{ - return credential->krb5_credential->password; -} - -char * -get_krb5_credential_realm (credential_t *credential) -{ - return credential->krb5_credential->realm; -} - -char * -get_krb5_credential_kdc (credential_t *credential) -{ - return credential->krb5_credential->kdc; -} - -char * -get_snmp_credential_username (credential_t *credential) -{ - return credential->snmp_credential->username; -} - -char * -get_snmp_credential_password (credential_t *credential) -{ - return credential->snmp_credential->password; -} - -char * -get_snmp_credential_community (credential_t *credential) -{ - return credential->snmp_credential->community; -} -char * -get_snmp_credential_privacy_password (credential_t *credential) -{ - return credential->snmp_credential->privacy_password; -} - -char * -get_snmp_credential_auth_proto (credential_t *credential) -{ - return credential->snmp_credential->auth_proto; -} - -char * -get_snmp_credential_privacy_proto (credential_t *credential) -{ - return credential->snmp_credential->privacy_proto; -} - -char * -get_esxi_credential_username (credential_t *credential) -{ - return credential->esxi_credential->username; -} - -char * -get_esxi_credential_password (credential_t *credential) -{ - return credential->esxi_credential->password; -} - - - -static int -is_match_host (const char *host, gvm_hosts_t *host_list) -{ - (void) host; - (void) host_list; - return 0; -} - static void store_smb_credential (credential_t *credential) { @@ -609,7 +648,7 @@ store_smb_credential (credential_t *credential) static int match_smb_host_credential (credential_t *credential, - const char *host) + const gvm_host_t *host) { const char *username = get_smb_credential_username (credential); const char *password = get_smb_credential_password (credential); @@ -617,7 +656,7 @@ match_smb_host_credential (credential_t *credential, if (!username || !*username || !password) return 0; - if (!is_match_host (host, credential->smb_credential->host_list)) + if (!gvm_host_in_hosts(host, NULL, credential->smb_credential->host_list)) return 0; store_smb_credential (credential); @@ -636,9 +675,9 @@ store_krb5_credential (credential_t *credential) static int match_krb5_host_credential (credential_t *cred, - const char *host) + const gvm_host_t *host) { - if (!is_match_host (host, cred->krb5_credential->host_list)) + if (!gvm_host_in_hosts(host, NULL, cred->krb5_credential->host_list)) return 0; store_krb5_credential (cred); @@ -662,9 +701,9 @@ struct data static int match_esxi_host_credential (credential_t *cred, - const char *host) + const gvm_host_t *host) { - if (!is_match_host (host, cred->esxi_credential->host_list)) + if (!gvm_host_in_hosts(host, NULL, cred->esxi_credential->host_list)) return 0; store_esxi_credential (cred); @@ -688,9 +727,9 @@ store_snmp_credential (credential_t *credential) static int match_snmp_host_credential (credential_t *credential, - const char *host_target) + const gvm_host_t *host) { - if (!is_match_host (host_target, credential->snmp_credential->host_list)) + if (!gvm_host_in_hosts(host, NULL, credential->snmp_credential->host_list)) return 0; store_snmp_credential (credential); @@ -732,9 +771,9 @@ store_ssh_credential (credential_t *credential) static int match_ssh_host_credential (credential_t *credential, - const char *host) + const gvm_host_t *host) { - if (!is_match_host (host, credential->ssh_credential->host_list)) + if (!gvm_host_in_hosts(host, NULL, credential->ssh_credential->host_list)) return 0; store_ssh_credential (credential); @@ -743,7 +782,7 @@ match_ssh_host_credential (credential_t *credential, struct credential_check_data { - const char *host; + const gvm_host_t *host; credential_type_t type; gboolean ssh_selected; gboolean smb_selected; @@ -785,7 +824,7 @@ set_host_credential (gpointer credential, gpointer user_data) static void set_host_credentials (GSList *credentials, - const char *host, credential_type_t type) + const gvm_host_t *host, credential_type_t type) { if (host == NULL) return; @@ -800,50 +839,59 @@ set_host_credentials (GSList *credentials, g_free (user_data); } -void -select_host_ssh_credential (struct scan_globals *globals, const char *host) +static void +select_host_ssh_credential (GSList *credentials, const gvm_host_t *host) { - if (globals == NULL || host == NULL - || get_ssh_credential_count () <= 1) + if (get_ssh_credential_count () < 1) return; - set_host_credentials (globals->credentials, host, SSH); + set_host_credentials (credentials, host, SSH); } -void -select_host_smb_credential (struct scan_globals *globals, const char *host) +static void +select_host_smb_credential (GSList *credentials, const gvm_host_t *host) { - if (globals == NULL || host == NULL - || get_smb_credential_count () <= 1) + if (get_smb_credential_count () < 1) return; - set_host_credentials ( globals->credentials, host, SMB); + set_host_credentials ( credentials, host, SMB); } -void -select_host_snmp_credential (struct scan_globals *globals, const char *host) +static void +select_host_snmp_credential (GSList *credentials, const gvm_host_t *host) { - if (globals == NULL || host == NULL - || get_snmp_credential_count () <= 1) + if (get_snmp_credential_count () < 1) return; - set_host_credentials ( globals->credentials, host, SNMP); + set_host_credentials ( credentials, host, SNMP); } -void -select_host_krb5_credential (struct scan_globals *globals, const char *host) +static void +select_host_krb5_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_krb5_credential_count () < 1) + return; + set_host_credentials (credentials, host, KRB5); +} + +static void +select_host_esxi_credential (GSList *credentials, const gvm_host_t *host) { - if (globals == NULL || host == NULL - || get_krb5_credential_count () <= 1) + if (get_esxi_credential_count () < 1) return; - set_host_credentials (globals->credentials, host, KRB5); + + set_host_credentials (credentials, host, ESXi); } void -select_host_esxi_credential (struct scan_globals *globals, const char *host) +override_host_credentials (struct scan_globals *globals, const gvm_host_t *host) { - if (globals == NULL || host == NULL - || get_esxi_credential_count () <= 1) + if (globals == NULL || globals->credentials == NULL || host == NULL) return; - set_host_credentials (globals->credentials, host, ESXi); + select_host_ssh_credential (globals->credentials, host); + select_host_smb_credential (globals->credentials, host); + select_host_krb5_credential (globals->credentials, host); + select_host_snmp_credential (globals->credentials, host); + select_host_esxi_credential (globals->credentials, host); + } diff --git a/misc/credentials.h b/misc/credentials.h index 85aa62dd43..988335b901 100644 --- a/misc/credentials.h +++ b/misc/credentials.h @@ -11,6 +11,7 @@ #ifndef MISC_CREDENTIALS_H #define MISC_CREDENTIALS_H +#include "base/hosts.h" #include "scanneraux.h" #include @@ -54,83 +55,12 @@ process_credentials_json (const char *, struct scan_globals **, char **); void destroy_credentials (GSList **); -credential_type_t -get_credential_type (credential_t *); -int -get_ssh_credential_port (credential_t *); -char * -get_ssh_credential_username (credential_t *); -char * -get_ssh_credential_password (credential_t *); -char * -get_ssh_credential_private_key (credential_t *); -char * -get_ssh_credential_private_key_uuid (credential_t *); -char * -get_ssh_credential_privilege_username (credential_t *); -char * -get_ssh_credential_privilege_password (credential_t *); -int -get_ssh_credential_count (void); - -char * -get_smb_credential_username (credential_t *); -char * -get_smb_credential_password (credential_t *); -int -get_smb_credential_count (void); - -char * -get_krb5_credential_username (credential_t *); -char * -get_krb5_credential_password (credential_t *); -char * -get_krb5_credential_realm (credential_t *); -char * -get_krb5_credential_kdc (credential_t *); -int -get_krb5_credential_count (void); - -char * -get_snmp_credential_username (credential_t *); -char * -get_snmp_credential_password (credential_t *); -char * -get_snmp_credential_community (credential_t *); -char * -get_snmp_credential_privacy_password (credential_t *); -char * -get_snmp_credential_auth_proto (credential_t *); -char * -get_snmp_credential_privacy_proto (credential_t *); -int -get_snmp_credential_count (void); - -char * -get_esxi_credential_username (credential_t *); -char * -get_esxi_credential_password (credential_t *); -int -get_esxi_credential_count (void); struct scan_globals; /* Select and publish one type credential in the per-host scanner process per * service. */ void -select_host_ssh_credential (struct scan_globals *, const char *); - -void -select_host_smb_credential (struct scan_globals *, const char *); - -void -select_host_snmp_credential (struct scan_globals *, const char *); - -void -select_host_krb5_credential (struct scan_globals *, const char *); - -void -select_host_esxi_credential (struct scan_globals *, const char *); - +override_host_credentials (struct scan_globals *, const gvm_host_t *); #endif // MISC_CREDENTIALS_H diff --git a/src/attack.c b/src/attack.c index 977fc6ddb1..89101e3a37 100644 --- a/src/attack.c +++ b/src/attack.c @@ -12,7 +12,7 @@ #include "attack.h" -#include "../nasl/nasl_credentials.h" +#include "../misc/credentials.h" #include "../misc/ipc_openvas.h" #include "../misc/kb_cache.h" #include "../misc/network.h" /* for auth_printf */ @@ -564,6 +564,8 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip, struct scheduler_plugin *plugin; pid_t parent; + override_host_credentials (globals, args->host); + addr6_to_str (ip, ip_str); host_kb = args->host_kb; host_vhosts = args->host->vhosts; From fb0b582fd239e4858e2b593c9a996e9c30a64131 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 11:59:59 -0300 Subject: [PATCH 11/20] Check for empty host list too --- rust/src/openvas/pref_handler.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rust/src/openvas/pref_handler.rs b/rust/src/openvas/pref_handler.rs index 55370ad8ab..8010d8f2d1 100644 --- a/rust/src/openvas/pref_handler.rs +++ b/rust/src/openvas/pref_handler.rs @@ -364,13 +364,14 @@ where let mut credential_preferences: Vec = vec![]; for credential in credentials.clone() { - // TODO: refactor that + let host_is_set = credential.hosts.map_or(false, |c| !c.is_empty()); + match credential.service { Service::KRB5 => { // Only the first credential without hosts is set as default. // Multiple KRB5 candidates are later handed and could override // the default one. - if credential.hosts.is_some() || default_krb5_set { + if host_is_set || default_krb5_set { continue; } @@ -399,7 +400,7 @@ where // Only the first credential whithout hosts is set as default. // Multiple SSH candidates are later handed and could override // the default one. - if credential.hosts.is_some() || default_ssh_set { + if host_is_set || default_ssh_set { continue; } if let Some(port) = credential.port { @@ -466,7 +467,8 @@ where // Only the first credential is set as default. // Multiple SMB candidates are later handed and could override // the default one. - if credential.hosts.is_some() || default_smb_set { + + if host_is_set || default_smb_set { continue; } @@ -486,7 +488,7 @@ where // Only the first credential is set as default. // Multiple ESXi candidates are later handed and could override // the default one. - if credential.hosts.is_some() || default_esxi_set { + if host_is_set || default_esxi_set { continue; } @@ -507,7 +509,7 @@ where // Only the first credential is set as default. // Multiple SNMP candidates are later handed and could override // the default one. - if credential.hosts.is_some() || default_snmp_set { + if host_is_set || default_snmp_set { continue; } @@ -860,7 +862,6 @@ mod tests { privilege: None, }, }, - ]; let expected = format!( "multi-credentials|||{}", From 58840435593e28519c574093c5de28e22aeddb6c Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 12:03:17 -0300 Subject: [PATCH 12/20] Create a comma separated string with credential hosts and init the gvm hosts list with gvm_hosts_new. Then hostnames are correctly handled. Also, free credentials and host list to fix some leaks. --- misc/credentials.c | 105 ++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 40 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index 4d1f99cfb3..6c6e72e9e1 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -11,6 +11,7 @@ #include "credentials.h" #include "base/hosts.h" +#include "glibconfig.h" #include "scanneraux.h" #include @@ -247,21 +248,30 @@ get_esxi_credential_password (credential_t *credential) return credential->esxi_credential->password; } -static void -create_host_list (cJSON *hosts, gvm_hosts_t **host_list) +static gvm_hosts_t * +create_host_list (cJSON *hosts) { + gvm_hosts_t *host_list = NULL; + if (hosts && cJSON_IsArray (hosts)) { + GString *hosts_s = NULL; for (int i = 0; i < cJSON_GetArraySize (hosts); i++) { cJSON *host_item = cJSON_GetArrayItem (hosts, i); - gvm_host_t *host = - gvm_host_from_str (cJSON_GetStringValue (host_item)); - - gvm_hosts_add(*host_list, host); + char *host_value = cJSON_GetStringValue (host_item); + if (hosts_s == NULL) + hosts_s = g_string_new (host_value); + else + { + g_string_append(hosts_s, ","); + g_string_append(hosts_s, host_value); + } } + host_list = gvm_hosts_new (hosts_s->str); + g_string_free(hosts_s, TRUE); } - + return host_list; } static int @@ -269,11 +279,14 @@ credential_ssh_new (cJSON *service, credential_t **credential) { ssh_credential_t *cred = g_malloc0 (sizeof (ssh_credential_t)); cJSON *item; - gvm_hosts_t *host_list = gvm_hosts_new(""); + gvm_hosts_t *host_list = NULL; item = cJSON_GetObjectItem(service,"hosts"); - create_host_list(item, &host_list); - cred->host_list = host_list; + if (item) + { + host_list = create_host_list(item); + cred->host_list = host_list; + } cred->port = gvm_json_obj_int (service, "port"); @@ -303,6 +316,7 @@ credential_ssh_new (cJSON *service, credential_t **credential) } if (item == NULL) { + gvm_hosts_free(host_list); g_free (cred); return -1; } @@ -318,12 +332,14 @@ credential_smb_new (cJSON *service, credential_t **credential) { smb_credential_t *cred = g_malloc0 (sizeof (smb_credential_t)); cJSON *item; - gvm_hosts_t *host_list = gvm_hosts_new(""); + gvm_hosts_t *host_list = NULL; item = cJSON_GetObjectItem(service,"hosts"); - create_host_list(item, &host_list); - cred->host_list = host_list; - + if (item) + { + host_list = create_host_list(item); + cred->host_list = host_list; + } item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) { @@ -332,6 +348,7 @@ credential_smb_new (cJSON *service, credential_t **credential) } if (item == NULL) { + gvm_hosts_free(host_list); g_free (cred); return -1; }; @@ -348,12 +365,14 @@ credential_esxi_new (cJSON *service, credential_t **credential) { esxi_credential_t *cred = g_malloc0 (sizeof (esxi_credential_t)); cJSON *item; - gvm_hosts_t *host_list = gvm_hosts_new(""); - - item = cJSON_GetObjectItem(service,"hosts"); - create_host_list(item, &host_list); - cred->host_list = host_list; + gvm_hosts_t *host_list = NULL; + item = cJSON_GetObjectItem (service, "hosts"); + if (item) { + host_list = create_host_list(item); + cred->host_list = host_list; + } + item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) { @@ -362,6 +381,7 @@ credential_esxi_new (cJSON *service, credential_t **credential) } if (item == NULL) { + gvm_hosts_free(host_list); g_free (cred); return -1; } @@ -378,12 +398,14 @@ credential_snmp_new (cJSON *service, credential_t **credential) { snmp_credential_t *cred = g_malloc0 (sizeof (snmp_credential_t)); cJSON *item; - gvm_hosts_t *host_list = gvm_hosts_new(""); - - item = cJSON_GetObjectItem(service,"hosts"); - create_host_list(item, &host_list); - cred->host_list = host_list; + gvm_hosts_t *host_list = NULL; + item = cJSON_GetObjectItem (service, "hosts"); + if (item) + { + host_list = create_host_list(item); + cred->host_list = host_list; + } item = cJSON_GetObjectItem (service, "snmp"); if (item && cJSON_IsObject (item)) { @@ -398,6 +420,7 @@ credential_snmp_new (cJSON *service, credential_t **credential) } if (item == NULL) { + gvm_hosts_free(host_list); g_free (cred); return -1; } @@ -413,12 +436,15 @@ credential_krb5_new (cJSON *service, credential_t **credential) { krb5_credential_t *cred = g_malloc0 (sizeof (krb5_credential_t)); cJSON *item; - gvm_hosts_t *host_list = gvm_hosts_new(""); - - item = cJSON_GetObjectItem(service,"hosts"); - create_host_list(item, &host_list); - cred->host_list = host_list; + gvm_hosts_t *host_list = NULL; + item = cJSON_GetObjectItem (service, "hosts"); + if (item) + { + host_list = create_host_list(item); + cred->host_list = host_list; + } + item = cJSON_GetObjectItem (service, "krb5"); if (item && cJSON_IsObject (item)) { @@ -429,6 +455,7 @@ credential_krb5_new (cJSON *service, credential_t **credential) } if (item == NULL) { + gvm_hosts_free (host_list); g_free (cred); return -1; } @@ -587,27 +614,21 @@ free_credential (gpointer data) g_free (credential->ssh_credential->private_key_uuid); g_free (credential->ssh_credential->privilege_username); g_free (credential->ssh_credential->privilege_password); - - if (credential->ssh_credential->host_list != NULL) - gvm_hosts_free (credential->ssh_credential->host_list); - + gvm_hosts_free (credential->ssh_credential->host_list); g_free (credential->ssh_credential); } else if (credential->type == SMB) { g_free (credential->smb_credential->username); g_free (credential->smb_credential->password); - if (credential->smb_credential->host_list != NULL) - gvm_hosts_free (credential->smb_credential->host_list); + gvm_hosts_free (credential->smb_credential->host_list); g_free (credential->smb_credential); } else if (credential->type == ESXi) { g_free (credential->esxi_credential->username); g_free (credential->esxi_credential->password); - if (credential->esxi_credential->host_list != NULL) - gvm_hosts_free (credential->esxi_credential->host_list); - + gvm_hosts_free (credential->esxi_credential->host_list); g_free (credential->esxi_credential); } else if (credential->type == KRB5) @@ -616,8 +637,7 @@ free_credential (gpointer data) g_free (credential->krb5_credential->password); g_free (credential->krb5_credential->realm); g_free (credential->krb5_credential->kdc); - if (credential->krb5_credential->host_list != NULL) - gvm_hosts_free (credential->krb5_credential->host_list); + gvm_hosts_free (credential->krb5_credential->host_list); g_free (credential->krb5_credential); } else if (credential->type == SNMP) @@ -626,14 +646,19 @@ free_credential (gpointer data) g_free (credential->snmp_credential->password); g_free (credential->snmp_credential->community); g_free (credential->snmp_credential->privacy_password); + g_free (credential->snmp_credential->auth_proto); + g_free (credential->snmp_credential->privacy_proto); + gvm_hosts_free (credential->snmp_credential->host_list); g_free (credential->snmp_credential); } + g_free (credential); } void destroy_credentials (GSList **credentials) { g_slist_free_full (*credentials, (GDestroyNotify) free_credential); + *credentials = NULL; } static void From a1c3982981fc10538266750b55201ff4a63f6278 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 12:06:40 -0300 Subject: [PATCH 13/20] set explicitely an empty string if the an optional credential value is NULL --- misc/credentials.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index 6c6e72e9e1..7c4444f5db 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -665,22 +665,14 @@ static void store_smb_credential (credential_t *credential) { // prefs set will replace the old values if any. - prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential)); - prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential)); - // SMB credentials carry no domain. Should I drop any value left by a previous one? - //prefs_set (OID_SMB_AUTH_DOMAIN, ""); -} + prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential) ? get_smb_credential_username (credential) : ""); + prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential) ? get_smb_credential_password (credential) : ""); + } static int match_smb_host_credential (credential_t *credential, const gvm_host_t *host) { - const char *username = get_smb_credential_username (credential); - const char *password = get_smb_credential_password (credential); - - if (!username || !*username || !password) - return 0; - if (!gvm_host_in_hosts(host, NULL, credential->smb_credential->host_list)) return 0; @@ -692,10 +684,10 @@ static void store_krb5_credential (credential_t *credential) { // prefs set will replace the old values if any. - prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential)); - prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential)); - prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential)); - prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential)); + prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential) ? get_krb5_credential_username (credential) : ""); + prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential) ? get_krb5_credential_password (credential) : ""); + prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential) ? get_krb5_credential_realm (credential) : ""); + prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential) ? get_krb5_credential_kdc (credential) : ""); } static int @@ -713,8 +705,8 @@ static void store_esxi_credential (credential_t *cred) { // prefs set will replace the old values if any. - prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred)); - prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred)); + prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred) ? get_esxi_credential_username (cred) : ""); + prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred) ? get_esxi_credential_password (cred) : ""); } struct data @@ -739,15 +731,15 @@ static void store_snmp_credential (credential_t *credential) { prefs_set (OID_SNMP_AUTH_COMMUNITY, - get_snmp_credential_community (credential)); - prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential)); - prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential)); + get_snmp_credential_community (credential) ? get_snmp_credential_community (credential) : ""); + prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential) ? get_snmp_credential_username (credential) : "" ); + prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential) ? get_snmp_credential_password (credential) : ""); prefs_set (OID_SNMP_AUTH_AUTH_ALGO, - get_snmp_credential_auth_proto (credential)); + get_snmp_credential_auth_proto (credential) ? get_snmp_credential_auth_proto (credential) : ""); prefs_set (OID_SNMP_AUTH_PRIV_PASS, - get_snmp_credential_privacy_password (credential)); + get_snmp_credential_privacy_password (credential) ? get_snmp_credential_privacy_password (credential) : ""); prefs_set (OID_SNMP_AUTH_PRIV_ALGO, - get_snmp_credential_privacy_proto (credential)); + get_snmp_credential_privacy_proto (credential) ? get_snmp_credential_privacy_proto (credential) : ""); } static int From d4e21c84bd1fcc6ca73cfc0e5bc54ab2480774e0 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 12:13:50 -0300 Subject: [PATCH 14/20] cleanup json object after use --- src/openvas.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/openvas.c b/src/openvas.c index 13f54d536a..87618e3e1f 100644 --- a/src/openvas.c +++ b/src/openvas.c @@ -265,6 +265,8 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) err); g_free (err); } + // Cleanup credential json data + prefs_set ("multi-credentials", ""); } kb_del_items (kb, key); From 5ccdb960de2b33c15a21142ff5e88f08d5d6fe35 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 13:41:49 -0300 Subject: [PATCH 15/20] fix format and other minor stuff --- misc/credentials.c | 194 ++++++++++++++++++++++++++++----------------- misc/credentials.h | 1 - src/attack.c | 4 +- 3 files changed, 122 insertions(+), 77 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index 7c4444f5db..8d9e7100e9 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -11,7 +11,6 @@ #include "credentials.h" #include "base/hosts.h" -#include "glibconfig.h" #include "scanneraux.h" #include @@ -69,7 +68,6 @@ #define OID_SNMP_AUTH_PRIV_ALGO \ "1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:" - int ssh_credential_count = 0; int smb_credential_count = 0; int snmp_credential_count = 0; @@ -93,7 +91,6 @@ struct smb_credential_type char *username; char *password; gvm_hosts_t *host_list; - }; struct krb5_credential_type @@ -103,7 +100,7 @@ struct krb5_credential_type char *realm; char *kdc; gvm_hosts_t *host_list; - }; +}; struct snmp_credential_type { @@ -252,7 +249,7 @@ static gvm_hosts_t * create_host_list (cJSON *hosts) { gvm_hosts_t *host_list = NULL; - + if (hosts && cJSON_IsArray (hosts)) { GString *hosts_s = NULL; @@ -260,16 +257,20 @@ create_host_list (cJSON *hosts) { cJSON *host_item = cJSON_GetArrayItem (hosts, i); char *host_value = cJSON_GetStringValue (host_item); + if (host_value == NULL || host_value[0] == '\0') + continue; + if (hosts_s == NULL) hosts_s = g_string_new (host_value); else { - g_string_append(hosts_s, ","); - g_string_append(hosts_s, host_value); + g_string_append (hosts_s, ","); + g_string_append (hosts_s, host_value); } } - host_list = gvm_hosts_new (hosts_s->str); - g_string_free(hosts_s, TRUE); + if (hosts_s && hosts_s->len > 0) + host_list = gvm_hosts_new (hosts_s->str); + g_string_free (hosts_s, TRUE); } return host_list; } @@ -281,11 +282,12 @@ credential_ssh_new (cJSON *service, credential_t **credential) cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem(service,"hosts"); + item = cJSON_GetObjectItem (service, "hosts"); if (item) { - host_list = create_host_list(item); - cred->host_list = host_list; + host_list = create_host_list (item); + if (host_list) + cred->host_list = host_list; } cred->port = gvm_json_obj_int (service, "port"); @@ -316,7 +318,7 @@ credential_ssh_new (cJSON *service, credential_t **credential) } if (item == NULL) { - gvm_hosts_free(host_list); + gvm_hosts_free (host_list); g_free (cred); return -1; } @@ -334,11 +336,12 @@ credential_smb_new (cJSON *service, credential_t **credential) cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem(service,"hosts"); + item = cJSON_GetObjectItem (service, "hosts"); if (item) { - host_list = create_host_list(item); - cred->host_list = host_list; + host_list = create_host_list (item); + if (host_list) + cred->host_list = host_list; } item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) @@ -348,7 +351,7 @@ credential_smb_new (cJSON *service, credential_t **credential) } if (item == NULL) { - gvm_hosts_free(host_list); + gvm_hosts_free (host_list); g_free (cred); return -1; }; @@ -368,11 +371,13 @@ credential_esxi_new (cJSON *service, credential_t **credential) gvm_hosts_t *host_list = NULL; item = cJSON_GetObjectItem (service, "hosts"); - if (item) { - host_list = create_host_list(item); - cred->host_list = host_list; - } - + if (item) + { + host_list = create_host_list (item); + if (host_list) + cred->host_list = host_list; + } + item = cJSON_GetObjectItem (service, "up"); if (item && cJSON_IsObject (item)) { @@ -381,7 +386,7 @@ credential_esxi_new (cJSON *service, credential_t **credential) } if (item == NULL) { - gvm_hosts_free(host_list); + gvm_hosts_free (host_list); g_free (cred); return -1; } @@ -403,8 +408,9 @@ credential_snmp_new (cJSON *service, credential_t **credential) item = cJSON_GetObjectItem (service, "hosts"); if (item) { - host_list = create_host_list(item); - cred->host_list = host_list; + host_list = create_host_list (item); + if (host_list) + cred->host_list = host_list; } item = cJSON_GetObjectItem (service, "snmp"); if (item && cJSON_IsObject (item)) @@ -420,7 +426,7 @@ credential_snmp_new (cJSON *service, credential_t **credential) } if (item == NULL) { - gvm_hosts_free(host_list); + gvm_hosts_free (host_list); g_free (cred); return -1; } @@ -441,10 +447,11 @@ credential_krb5_new (cJSON *service, credential_t **credential) item = cJSON_GetObjectItem (service, "hosts"); if (item) { - host_list = create_host_list(item); - cred->host_list = host_list; + host_list = create_host_list (item); + if (host_list) + cred->host_list = host_list; } - + item = cJSON_GetObjectItem (service, "krb5"); if (item && cJSON_IsObject (item)) { @@ -665,17 +672,23 @@ static void store_smb_credential (credential_t *credential) { // prefs set will replace the old values if any. - prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential) ? get_smb_credential_username (credential) : ""); - prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential) ? get_smb_credential_password (credential) : ""); - } + prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential) + ? get_smb_credential_username (credential) + : ""); + prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential) + ? get_smb_credential_password (credential) + : ""); +} static int -match_smb_host_credential (credential_t *credential, - const gvm_host_t *host) +match_smb_host_credential (credential_t *credential, const gvm_host_t *host) { - if (!gvm_host_in_hosts(host, NULL, credential->smb_credential->host_list)) + if (host == NULL || credential->smb_credential->host_list == NULL) return 0; - + + if (!gvm_host_in_hosts (host, NULL, credential->smb_credential->host_list)) + return 0; + store_smb_credential (credential); return 1; } @@ -684,17 +697,27 @@ static void store_krb5_credential (credential_t *credential) { // prefs set will replace the old values if any. - prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential) ? get_krb5_credential_username (credential) : ""); - prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential) ? get_krb5_credential_password (credential) : ""); - prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential) ? get_krb5_credential_realm (credential) : ""); - prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential) ? get_krb5_credential_kdc (credential) : ""); + prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential) + ? get_krb5_credential_username (credential) + : ""); + prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential) + ? get_krb5_credential_password (credential) + : ""); + prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential) + ? get_krb5_credential_realm (credential) + : ""); + prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential) + ? get_krb5_credential_kdc (credential) + : ""); } static int -match_krb5_host_credential (credential_t *cred, - const gvm_host_t *host) +match_krb5_host_credential (credential_t *cred, const gvm_host_t *host) { - if (!gvm_host_in_hosts(host, NULL, cred->krb5_credential->host_list)) + if (host == NULL || cred->krb5_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, cred->krb5_credential->host_list)) return 0; store_krb5_credential (cred); @@ -705,22 +728,21 @@ static void store_esxi_credential (credential_t *cred) { // prefs set will replace the old values if any. - prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred) ? get_esxi_credential_username (cred) : ""); - prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred) ? get_esxi_credential_password (cred) : ""); + prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred) + ? get_esxi_credential_username (cred) + : ""); + prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred) + ? get_esxi_credential_password (cred) + : ""); } -struct data -{ - char *memory; - size_t size; -}; - - static int -match_esxi_host_credential (credential_t *cred, - const gvm_host_t *host) +match_esxi_host_credential (credential_t *cred, const gvm_host_t *host) { - if (!gvm_host_in_hosts(host, NULL, cred->esxi_credential->host_list)) + if (host == NULL || cred->esxi_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, cred->esxi_credential->host_list)) return 0; store_esxi_credential (cred); @@ -731,22 +753,36 @@ static void store_snmp_credential (credential_t *credential) { prefs_set (OID_SNMP_AUTH_COMMUNITY, - get_snmp_credential_community (credential) ? get_snmp_credential_community (credential) : ""); - prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential) ? get_snmp_credential_username (credential) : "" ); - prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential) ? get_snmp_credential_password (credential) : ""); + get_snmp_credential_community (credential) + ? get_snmp_credential_community (credential) + : ""); + prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential) + ? get_snmp_credential_username (credential) + : ""); + prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential) + ? get_snmp_credential_password (credential) + : ""); prefs_set (OID_SNMP_AUTH_AUTH_ALGO, - get_snmp_credential_auth_proto (credential) ? get_snmp_credential_auth_proto (credential) : ""); + get_snmp_credential_auth_proto (credential) + ? get_snmp_credential_auth_proto (credential) + : ""); prefs_set (OID_SNMP_AUTH_PRIV_PASS, - get_snmp_credential_privacy_password (credential) ? get_snmp_credential_privacy_password (credential) : ""); + get_snmp_credential_privacy_password (credential) + ? get_snmp_credential_privacy_password (credential) + : ""); prefs_set (OID_SNMP_AUTH_PRIV_ALGO, - get_snmp_credential_privacy_proto (credential) ? get_snmp_credential_privacy_proto (credential) : ""); + get_snmp_credential_privacy_proto (credential) + ? get_snmp_credential_privacy_proto (credential) + : ""); } static int -match_snmp_host_credential (credential_t *credential, - const gvm_host_t *host) +match_snmp_host_credential (credential_t *credential, const gvm_host_t *host) { - if (!gvm_host_in_hosts(host, NULL, credential->snmp_credential->host_list)) + if (host == NULL || credential->snmp_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, credential->snmp_credential->host_list)) return 0; store_snmp_credential (credential); @@ -767,8 +803,14 @@ store_ssh_credential (credential_t *credential) g_snprintf (port_string, sizeof (port_string), "%d", port); prefs_set ("auth_port_ssh", port_string); } + else + // Let the nasl script set the default port. See kb_ssh_transport() in + // ssh_func.inc + prefs_set ("auth_port_ssh", "0"); - prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential)); + prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential) + ? get_ssh_credential_username (credential) + : ""); prefs_set (OID_SSH_AUTH_PASS, key_auth ? "" : (password ? password : "")); prefs_set (OID_SSH_AUTH_PASSPHRASE, key_auth && password ? password : ""); prefs_set (OID_SSH_AUTH_PRIV_USER, @@ -785,12 +827,13 @@ store_ssh_credential (credential_t *credential) : ""); } - static int -match_ssh_host_credential (credential_t *credential, - const gvm_host_t *host) +match_ssh_host_credential (credential_t *credential, const gvm_host_t *host) { - if (!gvm_host_in_hosts(host, NULL, credential->ssh_credential->host_list)) + if (host == NULL || credential->ssh_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, credential->ssh_credential->host_list)) return 0; store_ssh_credential (credential); @@ -808,6 +851,10 @@ struct credential_check_data gboolean esxi_selected; }; +/** + * @brief Stores the first matching credential to be used for the current + * scanned host + */ static void set_host_credential (gpointer credential, gpointer user_data) { @@ -840,8 +887,8 @@ set_host_credential (gpointer credential, gpointer user_data) } static void -set_host_credentials (GSList *credentials, - const gvm_host_t *host, credential_type_t type) +set_host_credentials (GSList *credentials, const gvm_host_t *host, + credential_type_t type) { if (host == NULL) return; @@ -871,7 +918,7 @@ select_host_smb_credential (GSList *credentials, const gvm_host_t *host) if (get_smb_credential_count () < 1) return; - set_host_credentials ( credentials, host, SMB); + set_host_credentials (credentials, host, SMB); } static void @@ -879,7 +926,7 @@ select_host_snmp_credential (GSList *credentials, const gvm_host_t *host) { if (get_snmp_credential_count () < 1) return; - set_host_credentials ( credentials, host, SNMP); + set_host_credentials (credentials, host, SNMP); } static void @@ -910,5 +957,4 @@ override_host_credentials (struct scan_globals *globals, const gvm_host_t *host) select_host_krb5_credential (globals->credentials, host); select_host_snmp_credential (globals->credentials, host); select_host_esxi_credential (globals->credentials, host); - } diff --git a/misc/credentials.h b/misc/credentials.h index 988335b901..ae74793bc0 100644 --- a/misc/credentials.h +++ b/misc/credentials.h @@ -55,7 +55,6 @@ process_credentials_json (const char *, struct scan_globals **, char **); void destroy_credentials (GSList **); - struct scan_globals; /* Select and publish one type credential in the per-host scanner process per diff --git a/src/attack.c b/src/attack.c index 89101e3a37..09d9f9a8c8 100644 --- a/src/attack.c +++ b/src/attack.c @@ -489,7 +489,7 @@ launch_plugin (struct scan_globals *globals, struct scheduler_plugin *plugin, plugin->running_state = PLUGIN_STATUS_DONE; goto finish_launch_plugin; } - + /* Do not launch NVT if mandatory key is missing (e.g. an important tool * was not found). */ if (!mandatory_requirements_met (args->host_kb, nvti)) @@ -565,7 +565,7 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip, pid_t parent; override_host_credentials (globals, args->host); - + addr6_to_str (ip, ip_str); host_kb = args->host_kb; host_vhosts = args->host->vhosts; From a729ad3476d5410399019e39512f7dc9fe2b7b16 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 14:22:28 -0300 Subject: [PATCH 16/20] fix auth_port_ssh. Set it as null if not set. --- misc/credentials.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/credentials.c b/misc/credentials.c index 8d9e7100e9..80ba5f3159 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -806,7 +806,7 @@ store_ssh_credential (credential_t *credential) else // Let the nasl script set the default port. See kb_ssh_transport() in // ssh_func.inc - prefs_set ("auth_port_ssh", "0"); + prefs_set ("auth_port_ssh", NULL); prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential) ? get_ssh_credential_username (credential) From c85afed61c42d8cee7c177902730e50d4a5fb4ea Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 18 Nov 2026 14:31:05 -0300 Subject: [PATCH 17/20] Doc: multiple credentials override --- doc/multiple-credentials.md | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 doc/multiple-credentials.md diff --git a/doc/multiple-credentials.md b/doc/multiple-credentials.md new file mode 100644 index 0000000000..920f911c49 --- /dev/null +++ b/doc/multiple-credentials.md @@ -0,0 +1,47 @@ +# Multiple credentials per service + +A scan target can carry several credentials for the same service (SSH, SMB, +SNMP, KRB5, ESXi). Each credential may be restricted to a list of hosts. +When a host is scanned, the credential matching that host is selected and +published as the scan preferences read by the NASL scripts. + +Credentials are part of the target in the scan JSON. The optional `hosts` +field ties a credential to specific hosts. + +```json +"credentials": [ + { "service": "ssh", "port": 22, + "up": { "username": "default", "password": "pass" } }, + { "service": "ssh", "port": 2222, + "hosts": ["192.168.1.0/24", "web.example.com"], + "usk": { "username": "admin", "password": "passphrase", + "private": "" } }, + { "service": "smb", "hosts": ["10.0.0.1-10"], + "up": { "username": "user", "password": "pass" } } +] +``` + +**openvasd** handles scan preferences. The first credential without +`hosts` field (missing or empty) of each service is written as the regular default +preferences (the samepreferences used before this feature). Credentials with +`hosts` and later credentials without `hosts` are not written as defaults. +The complete credential list is serialized into one scan preference, +`multi-credentials`, as a JSON string. +**openvas** gets the scan preferences and process them. If `multi-credentials` is set, +parses it and stores the credential list in `globals->credentials`. +Later, when a new host process is started and before any plugin +runs, a function for credential overriding is called. Then, for each +credential service, iterates over the list of host in a credential and +the first credential whose `hosts` list contains the current scanned host is stored +and will be used for the rest of host scan. +This only affects the forked per host process, so other hosts are unaffected. +If nothing matches, the default credential stays in place. + +Summarizing, this are the important rules: +- Matching is , in the order given in the JSON. +- A credential without `hosts` never matches in C; it can only be the default, + the first one without hosts, per service, set in **openvasd**. +- Each service is selected independently. E.g. an SSH match doesn't influence SMB. +- A matching credential replaces all values of that service. Missing + optional values are set to an empty NULL, so nothing leaks from the + default credential. From de2193ed52888408d1eddd4e5bc82430c680f52c Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 19 Nov 2026 10:29:52 -0300 Subject: [PATCH 18/20] resolve hostnames in the credentials host list --- misc/credentials.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/misc/credentials.c b/misc/credentials.c index 80ba5f3159..a0e416d5de 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -249,6 +249,7 @@ static gvm_hosts_t * create_host_list (cJSON *hosts) { gvm_hosts_t *host_list = NULL; + GSList *unresolved; if (hosts && cJSON_IsArray (hosts)) { @@ -272,6 +273,19 @@ create_host_list (cJSON *hosts) host_list = gvm_hosts_new (hosts_s->str); g_string_free (hosts_s, TRUE); } + + if (host_list == NULL) + return host_list; + + unresolved = gvm_hosts_resolve (host_list); + while (unresolved) + { + g_warning ("Couldn't resolve hostname '%s' tied to a credential", + (char *) unresolved->data); + unresolved = unresolved->next; + } + g_slist_free_full (unresolved, g_free); + return host_list; } From 410ff13542678e2e493b6a1898bab1077756d5dc Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 19 Nov 2026 10:31:22 -0300 Subject: [PATCH 19/20] missing optional fields in crdential are set to NULL instead of "" --- misc/credentials.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index a0e416d5de..4ebcf0a8a6 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -688,10 +688,10 @@ store_smb_credential (credential_t *credential) // prefs set will replace the old values if any. prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential) ? get_smb_credential_username (credential) - : ""); + : NULL); prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential) ? get_smb_credential_password (credential) - : ""); + : NULL); } static int @@ -713,16 +713,16 @@ store_krb5_credential (credential_t *credential) // prefs set will replace the old values if any. prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential) ? get_krb5_credential_username (credential) - : ""); + : NULL); prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential) ? get_krb5_credential_password (credential) - : ""); + : NULL); prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential) ? get_krb5_credential_realm (credential) - : ""); + : NULL); prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential) ? get_krb5_credential_kdc (credential) - : ""); + : NULL); } static int @@ -744,10 +744,10 @@ store_esxi_credential (credential_t *cred) // prefs set will replace the old values if any. prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred) ? get_esxi_credential_username (cred) - : ""); + : NULL); prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred) ? get_esxi_credential_password (cred) - : ""); + : NULL); } static int @@ -769,25 +769,25 @@ store_snmp_credential (credential_t *credential) prefs_set (OID_SNMP_AUTH_COMMUNITY, get_snmp_credential_community (credential) ? get_snmp_credential_community (credential) - : ""); + : NULL); prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential) ? get_snmp_credential_username (credential) - : ""); + : NULL); prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential) ? get_snmp_credential_password (credential) - : ""); + : NULL); prefs_set (OID_SNMP_AUTH_AUTH_ALGO, get_snmp_credential_auth_proto (credential) ? get_snmp_credential_auth_proto (credential) - : ""); + : NULL); prefs_set (OID_SNMP_AUTH_PRIV_PASS, get_snmp_credential_privacy_password (credential) ? get_snmp_credential_privacy_password (credential) - : ""); + : NULL); prefs_set (OID_SNMP_AUTH_PRIV_ALGO, get_snmp_credential_privacy_proto (credential) ? get_snmp_credential_privacy_proto (credential) - : ""); + : NULL); } static int @@ -824,21 +824,21 @@ store_ssh_credential (credential_t *credential) prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential) ? get_ssh_credential_username (credential) - : ""); - prefs_set (OID_SSH_AUTH_PASS, key_auth ? "" : (password ? password : "")); - prefs_set (OID_SSH_AUTH_PASSPHRASE, key_auth && password ? password : ""); + : NULL); + prefs_set (OID_SSH_AUTH_PASS, key_auth ? "" : (password ? password : NULL)); + prefs_set (OID_SSH_AUTH_PASSPHRASE, key_auth && password ? password : NULL); prefs_set (OID_SSH_AUTH_PRIV_USER, get_ssh_credential_privilege_username (credential) ? get_ssh_credential_privilege_username (credential) - : ""); + : NULL); prefs_set (OID_SSH_AUTH_PRIV_PASS, get_ssh_credential_privilege_password (credential) ? get_ssh_credential_privilege_password (credential) - : ""); + : NULL); prefs_set (OID_SSH_AUTH_PRIV_KEY, key_auth && get_ssh_credential_private_key_uuid (credential) ? get_ssh_credential_private_key_uuid (credential) - : ""); + : NULL); } static int @@ -847,7 +847,8 @@ match_ssh_host_credential (credential_t *credential, const gvm_host_t *host) if (host == NULL || credential->ssh_credential->host_list == NULL) return 0; - if (!gvm_host_in_hosts (host, NULL, credential->ssh_credential->host_list)) + if (!gvm_host_find_in_hosts (host, NULL, + credential->ssh_credential->host_list)) return 0; store_ssh_credential (credential); From 1373a8660cbab67228a4321ddd5509fc72b85b43 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 27 Nov 2026 13:24:48 -0300 Subject: [PATCH 20/20] Clone the credential for each target string --- misc/credentials.c | 204 ++++++++++++++++++++++++--------------------- src/openvas.c | 4 +- 2 files changed, 113 insertions(+), 95 deletions(-) diff --git a/misc/credentials.c b/misc/credentials.c index 4ebcf0a8a6..7b4cdb5f44 100644 --- a/misc/credentials.c +++ b/misc/credentials.c @@ -246,33 +246,13 @@ get_esxi_credential_password (credential_t *credential) } static gvm_hosts_t * -create_host_list (cJSON *hosts) +create_host_list (char *host) { gvm_hosts_t *host_list = NULL; GSList *unresolved; - if (hosts && cJSON_IsArray (hosts)) - { - GString *hosts_s = NULL; - for (int i = 0; i < cJSON_GetArraySize (hosts); i++) - { - cJSON *host_item = cJSON_GetArrayItem (hosts, i); - char *host_value = cJSON_GetStringValue (host_item); - if (host_value == NULL || host_value[0] == '\0') - continue; - - if (hosts_s == NULL) - hosts_s = g_string_new (host_value); - else - { - g_string_append (hosts_s, ","); - g_string_append (hosts_s, host_value); - } - } - if (hosts_s && hosts_s->len > 0) - host_list = gvm_hosts_new (hosts_s->str); - g_string_free (hosts_s, TRUE); - } + if (host && host[0] != '\0') + host_list = gvm_hosts_new (host); if (host_list == NULL) return host_list; @@ -290,16 +270,15 @@ create_host_list (cJSON *hosts) } static int -credential_ssh_new (cJSON *service, credential_t **credential) +credential_ssh_new (cJSON *service, credential_t **credential, char *target) { ssh_credential_t *cred = g_malloc0 (sizeof (ssh_credential_t)); cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem (service, "hosts"); - if (item) + if (target && target[0] != '\0') { - host_list = create_host_list (item); + host_list = create_host_list (target); if (host_list) cred->host_list = host_list; } @@ -344,16 +323,15 @@ credential_ssh_new (cJSON *service, credential_t **credential) } static int -credential_smb_new (cJSON *service, credential_t **credential) +credential_smb_new (cJSON *service, credential_t **credential, char *target) { smb_credential_t *cred = g_malloc0 (sizeof (smb_credential_t)); cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem (service, "hosts"); - if (item) + if (target && target[0] != '\0') { - host_list = create_host_list (item); + host_list = create_host_list (target); if (host_list) cred->host_list = host_list; } @@ -378,16 +356,15 @@ credential_smb_new (cJSON *service, credential_t **credential) } static int -credential_esxi_new (cJSON *service, credential_t **credential) +credential_esxi_new (cJSON *service, credential_t **credential, char *target) { esxi_credential_t *cred = g_malloc0 (sizeof (esxi_credential_t)); cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem (service, "hosts"); - if (item) + if (target && target[0] != '\0') { - host_list = create_host_list (item); + host_list = create_host_list (target); if (host_list) cred->host_list = host_list; } @@ -413,19 +390,19 @@ credential_esxi_new (cJSON *service, credential_t **credential) } static int -credential_snmp_new (cJSON *service, credential_t **credential) +credential_snmp_new (cJSON *service, credential_t **credential, char *target) { snmp_credential_t *cred = g_malloc0 (sizeof (snmp_credential_t)); cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem (service, "hosts"); - if (item) + if (target && target[0] != '\0') { - host_list = create_host_list (item); + host_list = create_host_list (target); if (host_list) cred->host_list = host_list; } + item = cJSON_GetObjectItem (service, "snmp"); if (item && cJSON_IsObject (item)) { @@ -452,16 +429,15 @@ credential_snmp_new (cJSON *service, credential_t **credential) } static int -credential_krb5_new (cJSON *service, credential_t **credential) +credential_krb5_new (cJSON *service, credential_t **credential, char *target) { krb5_credential_t *cred = g_malloc0 (sizeof (krb5_credential_t)); cJSON *item; gvm_hosts_t *host_list = NULL; - item = cJSON_GetObjectItem (service, "hosts"); - if (item) + if (target && target[0] != '\0') { - host_list = create_host_list (item); + host_list = create_host_list (target); if (host_list) cred->host_list = host_list; } @@ -527,13 +503,82 @@ get_esxi_credential_count (void) return esxi_credential_count; } +static int +credential_new (cJSON *service_obj, char *service, char *host_value, + struct scan_globals **globals) +{ + credential_t *credential = g_malloc0 (sizeof (credential_t)); + + int ret = 0; + if (!g_strcmp0 (service, "ssh")) + { + ret = credential_ssh_new (service_obj, &credential, host_value); + if (ret == 0 && credential->ssh_credential->private_key != NULL + && credential->ssh_credential->private_key[0] != '\0') + { + char *file_uuid = gvm_uuid_make (); + if (store_file (*globals, credential->ssh_credential->private_key, + file_uuid)) + { + g_free (file_uuid); + g_debug ("%s: Failed to parse file tipe private key.", + + __func__); + } + else + { + credential->ssh_credential->private_key_uuid = file_uuid; + } + } + } + else if (!g_strcmp0 (service, "smb")) + { + ret = credential_smb_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "esxi")) + { + ret = credential_esxi_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "snmp")) + { + ret = credential_snmp_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "krb5")) + { + ret = credential_krb5_new (service_obj, &credential, host_value); + } + else + { + g_free (credential); + g_warning ("%s: Unknown credential service type %s", __func__, service); + ret = 1; + } + if (ret == 0) + (*globals)->credentials = + g_slist_append ((*globals)->credentials, credential); + else + { + g_free (credential); + g_warning ("%s: not possible to parse a credential. Missing information", + __func__); + } + + if (ret != 0) + return 1; + + return ret; +} + +/** @brief Parse a string in json format and stored the credentials + * Returns 0 on success, -1 on error. + */ int process_credentials_json (const char *json_credentials, struct scan_globals **globals, char **err) { cJSON *parser; cJSON *service_obj = NULL; - int ret = 0; + int total_err = 0, ret = 0; reset_credential_counters (); @@ -557,62 +602,28 @@ process_credentials_json (const char *json_credentials, if (service_obj != NULL) { char *service = gvm_json_obj_str (service_obj, "service"); - credential_t *credential = g_malloc0 (sizeof (credential_t)); + cJSON *target = NULL; ret = 0; - if (!g_strcmp0 (service, "ssh")) + + target = cJSON_GetObjectItem (service_obj, "hosts"); + if (target && cJSON_IsArray (target)) { - ret = credential_ssh_new (service_obj, &credential); - if (ret == 0 && credential->ssh_credential->private_key != NULL - && credential->ssh_credential->private_key[0] != '\0') + for (int i = 0; i < cJSON_GetArraySize (target); i++) { - char *file_uuid = gvm_uuid_make (); - if (store_file (*globals, - credential->ssh_credential->private_key, - file_uuid)) - { - g_free (file_uuid); - g_debug ("%s: Failed to parse file tipe private key.", - - __func__); - } - else - { - credential->ssh_credential->private_key_uuid = file_uuid; - } + cJSON *host_item = cJSON_GetArrayItem (target, i); + char *host_value = cJSON_GetStringValue (host_item); + + if (host_value == NULL || host_value[0] == '\0') + continue; + ret = + credential_new (service_obj, service, host_value, globals); + total_err += ret; } } - else if (!g_strcmp0 (service, "smb")) - { - ret = credential_smb_new (service_obj, &credential); - } - else if (!g_strcmp0 (service, "esxi")) - { - ret = credential_esxi_new (service_obj, &credential); - } - else if (!g_strcmp0 (service, "snmp")) - { - ret = credential_snmp_new (service_obj, &credential); - } - else if (!g_strcmp0 (service, "krb5")) - { - ret = credential_krb5_new (service_obj, &credential); - } else { - g_free (credential); - g_warning ("%s: Unknown credential service type %s", __func__, - service); - continue; - } - if (ret == 0) - (*globals)->credentials = - g_slist_append ((*globals)->credentials, credential); - else - { - g_free (credential); - g_warning ( - "%s: not possible to parse a credential. Missing information", - __func__); + ret = credential_new (service_obj, service, NULL, globals); + total_err += ret; } } } @@ -620,6 +631,13 @@ process_credentials_json (const char *json_credentials, res_cleanup: cJSON_Delete (parser); + if (total_err > 0) + { + g_warning ("%s: %d errors were found during credential parsing", __func__, + total_err); + ret = -1; + } + return ret; } diff --git a/src/openvas.c b/src/openvas.c index 87618e3e1f..70a41b15be 100644 --- a/src/openvas.c +++ b/src/openvas.c @@ -261,8 +261,8 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) if (process_credentials_json (json, &globals, &err) < 0) { - g_warning ("It was not possible to process multiple credentials: %s", - err); + g_warning ( + "There were some errors processing multiple credentials: %s", err); g_free (err); } // Cleanup credential json data