Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vt info to results #809

Merged
merged 4 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [middleware] (unreleased)

### Added
- Add MQTT and json support.
Original
[#739](https://github.com/greenbone/openvas/pull/739)
[#754](https://github.com/greenbone/openvas/pull/754)
[#743](https://github.com/greenbone/openvas/pull/743).
Reintroduction after Rebase with
[#788](https://github.com/greenbone/openvas/pull/788)
- Add nvti info to openvas results. [#809](https://github.com/greenbone/openvas/pull/809)

## [21.10] (unreleased)

### Added
Expand All @@ -14,13 +26,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add support for non-interactive shell to nasl_ssh_shell_open().
[#744](https://github.com/greenbone/openvas/pull/744)
[#757](https://github.com/greenbone/openvas/pull/757)
- Add MQTT and json support.
Original
[#739](https://github.com/greenbone/openvas/pull/739)
[#754](https://github.com/greenbone/openvas/pull/754)
[#743](https://github.com/greenbone/openvas/pull/743).
Reintroduction after Rebase with
[#788](https://github.com/greenbone/openvas/pull/788)
- Add message type validation for proto_post_wrapped. [#805](https://github.com/greenbone/openvas/pull/805)

### Changed
Expand Down
2 changes: 1 addition & 1 deletion misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ include_directories (${GLIB_INCLUDE_DIRS} ${GLIB_JSON_INCLUDE_DIRS}
# Library

set (FILES bpf_share.c ftp_funcs.c vendorversion.c network.c plugutils.c pcap.c
strutils.c)
nvt_qod.c strutils.c)


# On windows we are always PIC and stack-protector is replaces by DEP
Expand Down
78 changes: 78 additions & 0 deletions misc/nvt_qod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* Copyright (C) 2009-2021 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* @file
* @brief Converts a qod_type string into a QoD value.
*
*/

#include "nvt_qod.h"

#include <glib.h>

#undef G_LOG_DOMAIN
/**
* @brief GLib logging domain.
*/
#define G_LOG_DOMAIN "lib misc"

static const struct
{
qod_val val;
const char *type;
} qod_types[] = {
{EXPLOIT, "exploit"},
{REMOTE_VUL, "remote_vul"},
{REMOTE_APP, "remote_app"},
{PACKAGE, "package"},
{REGISTRY, "registry"},
{REMOTE_ACTIVE, "remote_active"},
{REMOTE_BANNER, "remote_banner"},
{EXECUTABLE_VERSION, "executable_version"},
{REMOTE_ANALYSIS, "remote_analysis"},
{REMOTE_PROBE, "remote_probe"},
{REMOTE_BANNER_UNRELIABLE, "remote_banner_unreliable"},
{EXECUTABLE_VERSION_UNRELIABLE, "executable_version_unreliable"},
{GENERAL_NOTE, "general_note"},
{DEFAULT, "default"}};

/**
* @brief Converts a qod_type string into int value
*
* @param[in] qod_type String containing the qod type
*
* @return The value corresponding to the given qod type. Defaults to 70 if
* the given type does not exist. -1 if a null pointer was given.
*/
int
qod_type2val (const char *qod_type)
{
unsigned int i;

if (!qod_type)
return -1;

for (i = 0; i < sizeof (qod_types) / sizeof (qod_types[i]); i++)
{
if (!g_strcmp0 (qod_types[i].type, qod_type))
return qod_types[i].val;
}
return DEFAULT;
}
51 changes: 51 additions & 0 deletions misc/nvt_qod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (C) 2009-2021 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* @file nvt_qod_types.h
* @brief QOD_TYPES definitions.
*
* This file contains defines for the QoD types of NVTs.
*/

#ifndef _NVT_QOD_H
#define _NVT_QOD_H

typedef enum
{
EXPLOIT = 100,
REMOTE_VUL = 99,
REMOTE_APP = 98,
PACKAGE = 97,
REGISTRY = 97,
REMOTE_ACTIVE = 95,
REMOTE_BANNER = 80,
EXECUTABLE_VERSION = 80,
REMOTE_ANALYSIS = 70,
REMOTE_PROBE = 50,
REMOTE_BANNER_UNRELIABLE = 30,
EXECUTABLE_VERSION_UNRELIABLE = 30,
GENERAL_NOTE = 1,
DEFAULT = 70,
} qod_val;

int
qod_type2val (const char *);

#endif /* _NVT_QOD_H */
110 changes: 110 additions & 0 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#include "plugutils.h"

#include "network.h" // for OPENVAS_ENCAPS_IP
#include "nvt_qod.h" // for qod_str2val

#include <errno.h> // for errno
#include <gvm/base/cvss.h> // for get_cvss_score_from_base_metrics
#include <gvm/base/hosts.h> // for g_vhost_t
#include <gvm/base/networking.h> // for port_protocol_t
#include <gvm/base/prefs.h> // for prefs_get_bool
Expand Down Expand Up @@ -405,6 +407,111 @@ make_table_driven_lsc_info_json_str (const char *scan_id, const char *ip_str,
return json_str;
}

/**
* @brief Get the nvti's severity vector and return the score as string.
*
* @param[in] nvti The nvti where to get the severity vector from.
*
* @return string representing a float value, with max 1 decimal. Eg. -1, 3.2.
* NULL otherwise. Must be free()'d by the caller.
*/
static gchar *
get_severity_score_from_vt (nvti_t *nvti)
{
gchar *vector;
gchar *score_str;
double score;

vector = nvti_severity_vector_from_tag (nvti);
if (!vector)
return NULL;

score = get_cvss_score_from_base_metrics (vector);
g_free (vector);

score_str = g_strdup_printf ("%.1f", score);

return score_str;
}

/**
* @brief Get the nvti's QoD and return the value as
* string.
*
ArnoStiefvater marked this conversation as resolved.
Show resolved Hide resolved
* We check first for qod_type in the nvti's tag list, which has
* priority. If it is not present. we check for 'qod' tag.
*
* @param[in] nvti The nvti where to get the QoD from.
*
* @return string representing an integer value, NULL if no
* QoD was set for the nvti, or on error.
* Must be free()'d by the caller.
*/
static gchar *
get_qod_from_vt (nvti_t *nvti)
{
int qod;
gchar *qod_str, *qod_type;

qod_str = NULL;
qod_type = nvti_get_tag (nvti, "qod_type");
if (qod_type)
{
qod = qod_type2val (qod_type);
qod_str = g_strdup_printf ("%i", qod);
g_free (qod_type);
return qod_str;
}

qod_str = nvti_get_tag (nvti, "qod");
return qod_str;
}

/**
* @brief Build a json representation of the part related to nvti information
* of a result.
*
* OpenVAS result contains also the nvti name, QoD and score, which need to be
* taken from redis and the score must be calculated from the vector
*
* @param[in] oid The oid of the NVT.
* @param[out] builder Json builder to add data to.
*
* @return Json builder, NULL on error.
*/
static JsonBuilder *
add_nvti_info_into_json_builder (JsonBuilder *builder, const char *oid)
{
nvti_t *nvti;
gchar *score_str;
gchar *qod;

nvti = nvticache_get_nvt (oid);

if (!nvti)
{
g_warning ("%s: Plugin '%s' missing from nvticache.", __func__, oid);
return NULL;
}

json_builder_set_member_name (builder, "name");
builder = json_builder_add_string_value (builder, nvti_name (nvti));

qod = get_qod_from_vt (nvti);
json_builder_set_member_name (builder, "qod");
builder = json_builder_add_string_value (builder, qod);
g_free (qod);

score_str = get_severity_score_from_vt (nvti);
json_builder_set_member_name (builder, "severity");
builder = json_builder_add_string_value (builder, score_str);
g_free (score_str);

nvti_free (nvti);

return builder;
}

/**
* @brief Build a json representation of a result.
*
Expand Down Expand Up @@ -458,6 +565,9 @@ make_result_json_str (const gchar *scan_id, const gchar *type,
json_builder_set_member_name (builder, "OID");
json_builder_add_string_value (builder, oid);

if (oid != NULL && oid[0] != '\0')
add_nvti_info_into_json_builder (builder, oid);

json_builder_set_member_name (builder, "value");
json_builder_add_string_value (builder, action_str);

Expand Down