1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* @file
* @brief Simple JSON reader.
*/
#include "vtparser.h"
#undef G_LOG_DOMAIN
/**
* @brief GLib logging domain.
*/
#define G_LOG_DOMAIN "libgvm util"
/**
* @brief Get the VT category type given the category as string
*
* @param cat The category as string.
*
* @return Integer representing the category type.
*/
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;
}
/**
* @brief Add to the NVT Info structure.
*
* @param nvt The NVT Info structure.
* @param tag_obj The JSON object containing the tags.
*
* @return 0 on success, -1 on error.
*/
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
}
/**
* @brief Parse a VT element given in json format.
*
* @param[in] parser Json pull parser.
* @param[in] event Json pull event.
* @param[out] nvt The NVT Info structure to fill with the parsed data.
*
* @return 0 on success, 1 on end of feed, -1 on error.
* In case of success the nvti struct must be freed with nvti_free()
* by the caller.
*/
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;
}
|