vtparser.c
Go to the documentation of this file.
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "vtparser.h"
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "libgvm util"
static int
get_category_from_name (const gchar *cat)
{
if (!g_strcmp0 (cat, "init"))
return ACT_INIT;
else if (!g_strcmp0 (cat, "scanner"))
return ACT_SCANNER;
else if (!g_strcmp0 (cat, "settings"))
return ACT_SETTINGS;
else if (!g_strcmp0 (cat, "gather_info"))
return ACT_GATHER_INFO;
else if (!g_strcmp0 (cat, "attack"))
return ACT_ATTACK;
else if (!g_strcmp0 (cat, "mixed_attack"))
return ACT_MIXED_ATTACK;
else if (!g_strcmp0 (cat, "destructive_attack"))
return ACT_DESTRUCTIVE_ATTACK;
else if (!g_strcmp0 (cat, "denial"))
return ACT_DENIAL;
else if (!g_strcmp0 (cat, "kill_host"))
return ACT_KILL_HOST;
else if (!g_strcmp0 (cat, "flood"))
return ACT_FLOOD;
else if (!g_strcmp0 (cat, "end"))
return ACT_END;
return -1;
}
static int
add_tags_to_nvt (nvti_t *nvt, cJSON *tag_obj)
{
if (cJSON_IsObject (tag_obj))
{
gchar *severity_vector, *str;
if (!gvm_json_obj_check_str (tag_obj, "affected", &str))
nvti_set_affected (nvt, str);
nvti_set_creation_time (nvt,
gvm_json_obj_double (tag_obj, "creation_date"));
nvti_set_modification_time (
nvt, gvm_json_obj_double (tag_obj, "last_modification"));
if (!gvm_json_obj_check_str (tag_obj, "insight", &str))
nvti_set_insight (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "impact", &str))
nvti_set_impact (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "qod", &str))
nvti_set_qod (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "qod_type", &str))
nvti_set_qod_type (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "solution", &str))
{
nvti_set_solution (nvt, str);
if (gvm_json_obj_check_str (tag_obj, "solution_type", &str))
g_debug ("%s: SOLUTION: missing type for OID: %s", __func__,
nvti_oid (nvt));
else
nvti_set_solution_type (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "solution_method", &str))
nvti_set_solution_method (nvt, str);
}
if (!gvm_json_obj_check_str (tag_obj, "summary", &str))
nvti_set_summary (nvt, str);
if (!gvm_json_obj_check_str (tag_obj, "vuldetect", &str))
nvti_set_detection (nvt, str);
// Parse severity
severity_vector = gvm_json_obj_str (tag_obj, "severity_vector");
if (!severity_vector)
severity_vector = gvm_json_obj_str (tag_obj, "cvss_base_vector");
if (severity_vector)
{
gchar *severity_type, *cvss_base;
double cvss_base_dbl;
if (g_strrstr (severity_vector, "CVSS:3"))
severity_type = g_strdup ("cvss_base_v3");
else
severity_type = g_strdup ("cvss_base_v2");
cvss_base_dbl = get_cvss_score_from_base_metrics (severity_vector);
nvti_add_vtseverity (
nvt, vtseverity_new (severity_type,
gvm_json_obj_str (tag_obj, "severity_origin"),
gvm_json_obj_double (tag_obj, "severity_date"),
cvss_base_dbl, severity_vector));
nvti_add_tag (nvt, "cvss_base_vector", severity_vector);
cvss_base = g_strdup_printf (
"%.1f", get_cvss_score_from_base_metrics (severity_vector));
nvti_set_cvss_base (nvt, cvss_base);
g_free (cvss_base);
g_free (severity_type);
// end parsing severity
}
else
{
g_warning ("%s: SEVERITY missing value element", __func__);
return -1;
}
return 0;
}
g_warning ("%s: Tag is not an object", __func__);
return -1;
}
static void
parse_references (nvti_t *nvt, cJSON *vt_obj)
{
cJSON *item;
item = cJSON_GetObjectItem (vt_obj, "references");
if (item != NULL && cJSON_IsArray (item))
{
cJSON *ref_obj;
cJSON_ArrayForEach (ref_obj, item)
{
gchar *id, *class;
if (!cJSON_IsObject (ref_obj))
g_debug ("%s: Error reading VT/REFS reference object", __func__);
else if (gvm_json_obj_check_str (ref_obj, "class", &class))
g_warning ("%s: REF missing class attribute", __func__);
else if (gvm_json_obj_check_str (ref_obj, "id", &id))
g_warning ("%s: REF missing ID attribute", __func__);
else
nvti_add_vtref (nvt, vtref_new (class, id, NULL));
}
} // end references
}
static void
add_preferences_to_nvt (nvti_t *nvt, cJSON *vt_obj)
{
cJSON *item;
item = cJSON_GetObjectItem (vt_obj, "preferences");
if (item != NULL)
{
if (!cJSON_IsArray (item))
g_debug ("%s: Error reading VT/REFS array", __func__);
else
{
cJSON *prefs_obj = NULL;
cJSON_ArrayForEach (prefs_obj, item)
{
gchar *class, *name, *default_val;
int id;
if (!cJSON_IsObject (prefs_obj))
g_debug ("%s: Error reading VT/PREFS preference object",
__func__);
else if (gvm_json_obj_check_str (prefs_obj, "class", &class))
g_warning ("%s: PREF missing class attribute", __func__);
else if (gvm_json_obj_check_int (prefs_obj, "id", &id))
g_warning ("%s: PREF missing id attribute", __func__);
else if (gvm_json_obj_check_str (prefs_obj, "name", &name))
g_warning ("%s: PREF missing name attribute", __func__);
else if (gvm_json_obj_check_str (prefs_obj, "default",
&default_val))
g_warning ("%s: PREF missing default attribute", __func__);
else
nvti_add_pref (nvt, nvtpref_new (id, name, class, default_val));
} // end each prefs
} // end prefs array
} // end preferences
}
int
parse_vt_json (gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event,
nvti_t **nvt)
{
cJSON *vt_obj = NULL;
gchar *str, *error_message = NULL;
*nvt = NULL;
gvm_json_pull_parser_next (parser, event);
// Handle start/end of json array
gchar *path = gvm_json_path_to_string (event->path);
if (!g_strcmp0 (path, "$") && event->type == GVM_JSON_PULL_EVENT_ARRAY_START)
{
gvm_json_pull_parser_next (parser, event);
g_debug ("%s: Start parsing feed", __func__);
}
else if (!g_strcmp0 (path, "$")
&& (event->type == GVM_JSON_PULL_EVENT_ARRAY_END
|| event->type == GVM_JSON_PULL_EVENT_EOF))
{
g_debug ("%s: Finish parsing feed", __func__);
g_free (path);
return 1;
}
g_free (path);
// It is an NVT object
if (event->type != GVM_JSON_PULL_EVENT_OBJECT_START)
{
g_warning ("%s: Error reading VT object", __func__);
return -1;
}
vt_obj = gvm_json_pull_expand_container (parser, &error_message);
if (!cJSON_IsObject (vt_obj))
{
g_free (error_message);
cJSON_Delete (vt_obj);
return -1;
}
g_free (error_message);
*nvt = nvti_new ();
if (gvm_json_obj_check_str (vt_obj, "oid", &str))
{
g_warning ("%s: VT missing OID", __func__);
cJSON_Delete (vt_obj);
nvti_free (*nvt);
return -1;
}
nvti_set_oid (*nvt, str);
if (gvm_json_obj_check_str (vt_obj, "name", &str))
{
g_warning ("%s: VT missing NAME", __func__);
cJSON_Delete (vt_obj);
nvti_free (*nvt);
return -1;
}
nvti_set_name (*nvt, str);
if (gvm_json_obj_check_str (vt_obj, "family", &str))
{
g_warning ("%s: VT missing FAMILY", __func__);
cJSON_Delete (vt_obj);
nvti_free (*nvt);
return -1;
}
nvti_set_family (*nvt, str);
if (gvm_json_obj_check_str (vt_obj, "category", &str))
{
g_warning ("%s: VT missing CATEGORY", __func__);
cJSON_Delete (vt_obj);
nvti_free (*nvt);
return -1;
}
nvti_set_category (*nvt, get_category_from_name (str));
cJSON *tag_obj = cJSON_GetObjectItem (vt_obj, "tag");
if (tag_obj)
{
if (add_tags_to_nvt (*nvt, tag_obj))
{
g_warning ("%s: Error adding tags", __func__);
cJSON_Delete (vt_obj);
nvti_free (*nvt);
return -1;
}
}
parse_references (*nvt, vt_obj);
add_preferences_to_nvt (*nvt, vt_obj);
cJSON_Delete (vt_obj);
return 0;
}
get_cvss_score_from_base_metrics
double get_cvss_score_from_base_metrics(const char *cvss_str)
Calculate CVSS Score.
Definition cvss.c:585
gvm_json_obj_double
double gvm_json_obj_double(cJSON *obj, const gchar *key)
Get a double field from a JSON object.
Definition json.c:75
gvm_json_obj_check_int
int gvm_json_obj_check_int(cJSON *obj, const gchar *key, int *val)
Get an int field from a JSON object.
Definition json.c:97
gvm_json_obj_str
gchar * gvm_json_obj_str(cJSON *obj, const gchar *key)
Get a string field from a JSON object.
Definition json.c:165
gvm_json_obj_check_str
int gvm_json_obj_check_str(cJSON *obj, const gchar *key, gchar **val)
Get a string field from a JSON object.
Definition json.c:142
gvm_json_pull_parser_next
void gvm_json_pull_parser_next(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Get the next event from a JSON pull parser.
Definition jsonpull.c:668
gvm_json_path_to_string
gchar * gvm_json_path_to_string(GQueue *path)
Converts a path as used by a JSON pull parser to a JSONPath string.
Definition jsonpull.c:909
gvm_json_pull_expand_container
cJSON * gvm_json_pull_expand_container(gvm_json_pull_parser_t *parser, gchar **error_message)
Expands the current array or object of a JSON pull parser.
Definition jsonpull.c:745
GVM_JSON_PULL_EVENT_OBJECT_START
@ GVM_JSON_PULL_EVENT_OBJECT_START
Definition jsonpull.h:45
GVM_JSON_PULL_EVENT_EOF
@ GVM_JSON_PULL_EVENT_EOF
Definition jsonpull.h:51
GVM_JSON_PULL_EVENT_ARRAY_END
@ GVM_JSON_PULL_EVENT_ARRAY_END
Definition jsonpull.h:44
GVM_JSON_PULL_EVENT_ARRAY_START
@ GVM_JSON_PULL_EVENT_ARRAY_START
Definition jsonpull.h:43
nvti_new
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition nvti.c:559
nvti_set_qod
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1880
nvti_set_solution
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1472
nvti_set_solution_type
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1513
nvti_t
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
nvti_set_qod_type
int nvti_set_qod_type(nvti_t *n, const gchar *qod_type)
Set the QoD type of a NVT.
Definition nvti.c:1856
nvti_set_oid
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1214
nvti_set_cvss_base
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1648
nvti_set_impact
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1394
nvti_set_summary
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1274
nvti_oid
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:611
nvtpref_new
nvtpref_t * nvtpref_new(int id, const gchar *name, const gchar *type, const gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition nvti.c:463
nvti_add_pref
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition nvti.c:2180
nvti_set_insight
int nvti_set_insight(nvti_t *n, const gchar *insight)
Set the insight text of a NVT.
Definition nvti.c:1314
vtref_new
vtref_t * vtref_new(const gchar *type, const gchar *ref_id, const gchar *ref_text)
Create a new vtref structure filled with the given values.
Definition nvti.c:77
nvti_add_vtseverity
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:426
nvti_set_solution_method
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1534
nvti_set_name
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1234
vtseverity_new
vtseverity_t * vtseverity_new(const gchar *type, const gchar *origin, int date, double score, const gchar *value)
Create a new vtseverity structure filled with the given values.
Definition nvti.c:180
nvti_set_modification_time
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1453
nvti_set_creation_time
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1434
nvti_set_family
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1903
nvti_set_affected
int nvti_set_affected(nvti_t *n, const gchar *affected)
Set the affected text of a NVT.
Definition nvti.c:1354
nvti_add_tag
int nvti_add_tag(nvti_t *n, const gchar *name, const gchar *value)
Add a tag to the NVT tags. The tag names "severity_date", "last_modification" and "creation_date" are...
Definition nvti.c:1561
nvti_set_detection
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1815
nvti_set_category
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1943
nvti_free
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:570
nvti_add_vtref
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition nvti.c:408
gvm_json_pull_event_t
Event generated by the JSON pull parser.
Definition jsonpull.h:59
gvm_json_pull_event_t::path
GQueue * path
Path to the event value.
Definition jsonpull.h:61
gvm_json_pull_event_t::type
gvm_json_pull_event_type_t type
Type of event.
Definition jsonpull.h:60
gvm_json_pull_parser_t
A json pull parser.
Definition jsonpull.h:86
parse_vt_json
int parse_vt_json(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, nvti_t **nvt)
Parse a VT element given in json format.
Definition vtparser.c:239
add_preferences_to_nvt
static void add_preferences_to_nvt(nvti_t *nvt, cJSON *vt_obj)
Definition vtparser.c:185
get_category_from_name
static int get_category_from_name(const gchar *cat)
Get the VT category type given the category as string.
Definition vtparser.c:27
parse_references
static void parse_references(nvti_t *nvt, cJSON *vt_obj)
Definition vtparser.c:157
add_tags_to_nvt
static int add_tags_to_nvt(nvti_t *nvt, cJSON *tag_obj)
Add to the NVT Info structure.
Definition vtparser.c:64
vtparser.h
Simple JSON reader.
ACT_KILL_HOST
@ ACT_KILL_HOST
Definition vtparser.h:39
ACT_DESTRUCTIVE_ATTACK
@ ACT_DESTRUCTIVE_ATTACK
Definition vtparser.h:37
ACT_SCANNER
@ ACT_SCANNER
Definition vtparser.h:32
ACT_END
@ ACT_END
Definition vtparser.h:41
ACT_FLOOD
@ ACT_FLOOD
Definition vtparser.h:40
ACT_GATHER_INFO
@ ACT_GATHER_INFO
Definition vtparser.h:34
ACT_DENIAL
@ ACT_DENIAL
Definition vtparser.h:38
ACT_ATTACK
@ ACT_ATTACK
Definition vtparser.h:35
ACT_SETTINGS
@ ACT_SETTINGS
Definition vtparser.h:33
ACT_MIXED_ATTACK
@ ACT_MIXED_ATTACK
Definition vtparser.h:36
ACT_INIT
@ ACT_INIT
Definition vtparser.h:31