Greenbone Vulnerability Management Libraries  11.0.1
cvss.c File Reference

CVSS utility functions. More...

#include <glib.h>
#include <string.h>
Include dependency graph for cvss.c:

Go to the source code of this file.

Data Structures

struct  impact_item
 Describe a CVSS impact element. More...
 
struct  cvss
 Describe a CVSS metrics. More...
 

Macros

#define AV_NETWORK   1.0
 AccessVector (AV) Constants. More...
 
#define AV_ADJACENT_NETWORK   0.646
 
#define AV_LOCAL   0.395
 
#define AC_LOW   0.71
 AccessComplexity (AC) Constants. More...
 
#define AC_MEDIUM   0.61
 
#define AC_HIGH   0.35
 
#define Au_MULTIPLE_INSTANCES   0.45
 Authentication (Au) Constants. More...
 
#define Au_SINGLE_INSTANCE   0.56
 
#define Au_NONE   0.704
 
#define C_NONE   0.0
 ConfidentialityImpact (C) Constants. More...
 
#define C_PARTIAL   0.275
 
#define C_COMPLETE   0.660
 
#define I_NONE   0.0
 IntegrityImpact (I) Constants. More...
 
#define I_PARTIAL   0.275
 
#define I_COMPLETE   0.660
 
#define A_NONE   0.0
 AvailabilityImpact (A) Constants. More...
 
#define A_PARTIAL   0.275
 
#define A_COMPLETE   0.660
 

Enumerations

enum  base_metrics {
  A, I, C, Au,
  AC, AV
}
 Base metrics. More...
 

Functions

static int toenum (const char *str, enum base_metrics *res)
 Determine base metric enumeration from a string. More...
 
static double get_impact_subscore (const struct cvss *cvss)
 Calculate Impact Sub Score. More...
 
static double get_exploitability_subscore (const struct cvss *cvss)
 Calculate Exploitability Sub Score. More...
 
static int set_impact_from_str (const char *value, enum base_metrics metric, struct cvss *cvss)
 Set impact score from string representation. More...
 
static double __get_cvss_score (struct cvss *cvss)
 Final CVSS score computation helper. More...
 
double get_cvss_score_from_base_metrics (const char *cvss_str)
 Calculate CVSS Score. More...
 

Variables

static const struct impact_item impact_map [][3]
 

Detailed Description

CVSS utility functions.

This file contains utility functions for handling CVSS. Namels a calculator for the CVSS base score from a CVSS base vector.

The base equation is the foundation of CVSS scoring. The base equation is: BaseScore6 = round_to_1_decimal(((0.6*Impact)+(0.4*Exploitability)–1.5)*f(Impact))

Impact = 10.41*(1-(1-ConfImpact)*(1-IntegImpact)*(1-AvailImpact))

Exploitability = 20* AccessVector*AccessComplexity*Authentication

f(impact)= 0 if Impact=0, 1.176 otherwise AccessVector = case AccessVector of requires local access: 0.395 adjacent network accessible: 0.646 network accessible: 1.0 AccessComplexity = case AccessComplexity of high: 0.35 medium: 0.61 low: 0.71 Authentication = case Authentication of requires multiple instances of authentication: 0.45 requires single instance of authentication: 0.56 requires no authentication: 0.704 ConfImpact = case ConfidentialityImpact of none: 0.0 partial: 0.275 complete: 0.660 IntegImpact = case IntegrityImpact of none: 0.0 partial: 0.275 complete: 0.660 AvailImpact = case AvailabilityImpact of none: 0.0 partial: 0.275 complete: 0.660

Definition in file cvss.c.

Macro Definition Documentation

◆ A_COMPLETE

#define A_COMPLETE   0.660

Complete Availability Impact.

Definition at line 109 of file cvss.c.

◆ A_NONE

#define A_NONE   0.0

AvailabilityImpact (A) Constants.

No Availability Impact.

Definition at line 107 of file cvss.c.

◆ A_PARTIAL

#define A_PARTIAL   0.275

Partial Availability Impact.

Definition at line 108 of file cvss.c.

◆ AC_HIGH

#define AC_HIGH   0.35

Access Complexity High.

Definition at line 81 of file cvss.c.

◆ AC_LOW

#define AC_LOW   0.71

AccessComplexity (AC) Constants.

Access Complexity Low.

Definition at line 79 of file cvss.c.

◆ AC_MEDIUM

#define AC_MEDIUM   0.61

Access Complexity Medium.

Definition at line 80 of file cvss.c.

◆ Au_MULTIPLE_INSTANCES

#define Au_MULTIPLE_INSTANCES   0.45

Authentication (Au) Constants.

Authentication multiple instances.

Definition at line 86 of file cvss.c.

◆ Au_NONE

#define Au_NONE   0.704

No Authentication.

Definition at line 88 of file cvss.c.

◆ Au_SINGLE_INSTANCE

#define Au_SINGLE_INSTANCE   0.56

Authentication single instances.

Definition at line 87 of file cvss.c.

◆ AV_ADJACENT_NETWORK

#define AV_ADJACENT_NETWORK   0.646

Access Vector Adjacent Network.

Definition at line 73 of file cvss.c.

◆ AV_LOCAL

#define AV_LOCAL   0.395

Access Vector Local.

Definition at line 74 of file cvss.c.

◆ AV_NETWORK

#define AV_NETWORK   1.0

AccessVector (AV) Constants.

Access Vector Network.

Definition at line 72 of file cvss.c.

◆ C_COMPLETE

#define C_COMPLETE   0.660

Complete Confidentiality Impact.

Definition at line 95 of file cvss.c.

◆ C_NONE

#define C_NONE   0.0

ConfidentialityImpact (C) Constants.

No Confidentiality Impact.

Definition at line 93 of file cvss.c.

◆ C_PARTIAL

#define C_PARTIAL   0.275

Partial Confidentiality Impact.

Definition at line 94 of file cvss.c.

◆ I_COMPLETE

#define I_COMPLETE   0.660

Complete Integrity Impact.

Definition at line 102 of file cvss.c.

◆ I_NONE

#define I_NONE   0.0

IntegrityImpact (I) Constants.

No Integrity Impact.

Definition at line 100 of file cvss.c.

◆ I_PARTIAL

#define I_PARTIAL   0.275

Partial Integrity Impact.

Definition at line 101 of file cvss.c.

Enumeration Type Documentation

◆ base_metrics

Base metrics.

Enumerator

Availability Impact.

Integrity Impact.

Confidentiality Impact.

Au 

Authentication.

AC 

Access Complexity.

AV 

Access Vector.

Definition at line 115 of file cvss.c.

116 {
117  A,
118  I,
119  C,
120  Au,
121  AC,
122  AV
123 };

Function Documentation

◆ __get_cvss_score()

static double __get_cvss_score ( struct cvss cvss)
static

Final CVSS score computation helper.

Parameters
[in]cvssThe CVSS structure that contains the different metrics and associated scores.
Returns
the CVSS score, as a double.

Definition at line 318 of file cvss.c.

319 {
320  double impact = 1.176;
321  double impact_sub;
322  double exploitability_sub;
323 
324  impact_sub = get_impact_subscore (cvss);
325  exploitability_sub = get_exploitability_subscore (cvss);
326 
327  if (impact_sub < 0.1)
328  impact = 0.0;
329 
330  return (((0.6 * impact_sub) + (0.4 * exploitability_sub) - 1.5) * impact)
331  + 0.0;
332 }

References get_exploitability_subscore(), and get_impact_subscore().

Referenced by get_cvss_score_from_base_metrics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cvss_score_from_base_metrics()

double get_cvss_score_from_base_metrics ( const char *  cvss_str)

Calculate CVSS Score.

Parameters
cvss_strBase vector string from which to compute score.
Returns
The resulting score. -1 upon error during parsing.

Definition at line 342 of file cvss.c.

343 {
344  struct cvss cvss;
345  char *token, *base_str, *base_metrics;
346 
347  memset (&cvss, 0x00, sizeof (struct cvss));
348 
349  if (cvss_str == NULL)
350  return -1.0;
351 
352  base_str = base_metrics = g_strdup_printf ("%s/", cvss_str);
353 
354  while ((token = strchr (base_metrics, '/')) != NULL)
355  {
356  char *token2 = strtok (base_metrics, ":");
357  char *metric_name = token2;
358  char *metric_value;
359  enum base_metrics mval;
360  int rc;
361 
362  *token++ = '\0';
363 
364  if (metric_name == NULL)
365  goto ret_err;
366 
367  metric_value = strtok (NULL, ":");
368 
369  if (metric_value == NULL)
370  goto ret_err;
371 
372  rc = toenum (metric_name, &mval);
373  if (rc)
374  goto ret_err;
375 
376  if (set_impact_from_str (metric_value, mval, &cvss))
377  goto ret_err;
378 
379  base_metrics = token;
380  }
381 
382  g_free (base_str);
383  return __get_cvss_score (&cvss);
384 
385 ret_err:
386  g_free (base_str);
387  return (double) -1;
388 }

References __get_cvss_score(), set_impact_from_str(), and toenum().

Here is the call graph for this function:

◆ get_exploitability_subscore()

static double get_exploitability_subscore ( const struct cvss cvss)
static

Calculate Exploitability Sub Score.

Parameters
[in]cvssContains the subscores associated to the metrics.
Returns
The resulting subscore.

Definition at line 245 of file cvss.c.

246 {
247  return (20 * cvss->access_vector * cvss->access_complexity
248  * cvss->authentication);
249 }

References cvss::access_complexity, cvss::access_vector, and cvss::authentication.

Referenced by __get_cvss_score().

Here is the caller graph for this function:

◆ get_impact_subscore()

static double get_impact_subscore ( const struct cvss cvss)
static

Calculate Impact Sub Score.

Parameters
[in]cvssContains the subscores associated to the metrics.
Returns
The resulting subscore.

Definition at line 228 of file cvss.c.

229 {
230  return (10.41
231  * (1
232  - (1 - cvss->conf_impact) * (1 - cvss->integ_impact)
233  * (1 - cvss->avail_impact)));
234 }

References cvss::avail_impact, cvss::conf_impact, and cvss::integ_impact.

Referenced by __get_cvss_score().

Here is the caller graph for this function:

◆ set_impact_from_str()

static int set_impact_from_str ( const char *  value,
enum base_metrics  metric,
struct cvss cvss 
)
inlinestatic

Set impact score from string representation.

Parameters
[in]valueThe literal value associated to the metric.
[in]metricThe enumeration constant identifying the metric.
[out]cvssThe structure to update with the score.
Returns
0 on success, -1 on error.

Definition at line 261 of file cvss.c.

263 {
264  int i;
265 
266  for (i = 0; i < 3; i++)
267  {
268  const struct impact_item *impact;
269 
270  impact = &impact_map[metric][i];
271 
272  if (g_strcmp0 (impact->name, value) == 0)
273  {
274  switch (metric)
275  {
276  case A:
277  cvss->avail_impact = impact->nvalue;
278  break;
279 
280  case I:
281  cvss->integ_impact = impact->nvalue;
282  break;
283 
284  case C:
285  cvss->conf_impact = impact->nvalue;
286  break;
287 
288  case Au:
289  cvss->authentication = impact->nvalue;
290  break;
291 
292  case AV:
293  cvss->access_vector = impact->nvalue;
294  break;
295 
296  case AC:
297  cvss->access_complexity = impact->nvalue;
298  break;
299 
300  default:
301  return -1;
302  }
303  return 0;
304  }
305  }
306  return -1;
307 }

References A, AC, cvss::access_complexity, cvss::access_vector, Au, cvss::authentication, AV, cvss::avail_impact, C, cvss::conf_impact, I, impact_map, cvss::integ_impact, impact_item::name, and impact_item::nvalue.

Referenced by get_cvss_score_from_base_metrics().

Here is the caller graph for this function:

◆ toenum()

static int toenum ( const char *  str,
enum base_metrics res 
)
static

Determine base metric enumeration from a string.

Parameters
[in]strBase metric in string form, for example "A".
[out]resWhere to write the desired value.
Returns
0 on success, -1 on error.

Definition at line 195 of file cvss.c.

196 {
197  int rc = 0; /* let's be optimistic */
198 
199  if (g_strcmp0 (str, "A") == 0)
200  *res = A;
201  else if (g_strcmp0 (str, "I") == 0)
202  *res = I;
203  else if (g_strcmp0 (str, "C") == 0)
204  *res = C;
205  else if (g_strcmp0 (str, "Au") == 0)
206  *res = Au;
207  else if (g_strcmp0 (str, "AU") == 0)
208  *res = Au;
209  else if (g_strcmp0 (str, "AV") == 0)
210  *res = AV;
211  else if (g_strcmp0 (str, "AC") == 0)
212  *res = AC;
213  else
214  rc = -1;
215 
216  return rc;
217 }

References A, AC, Au, AV, C, and I.

Referenced by get_cvss_score_from_base_metrics().

Here is the caller graph for this function:

Variable Documentation

◆ impact_map

const struct impact_item impact_map[][3]
static

Definition at line 147 of file cvss.c.

Referenced by set_impact_from_str().

__get_cvss_score
static double __get_cvss_score(struct cvss *cvss)
Final CVSS score computation helper.
Definition: cvss.c:318
impact_map
static const struct impact_item impact_map[][3]
Definition: cvss.c:147
toenum
static int toenum(const char *str, enum base_metrics *res)
Determine base metric enumeration from a string.
Definition: cvss.c:195
cvss::avail_impact
double avail_impact
Definition: cvss.c:141
C
@ C
Definition: cvss.c:119
get_exploitability_subscore
static double get_exploitability_subscore(const struct cvss *cvss)
Calculate Exploitability Sub Score.
Definition: cvss.c:245
A
@ A
Definition: cvss.c:117
impact_item::nvalue
double nvalue
Definition: cvss.c:131
I
@ I
Definition: cvss.c:118
cvss
Describe a CVSS metrics.
Definition: cvss.c:137
AC
@ AC
Definition: cvss.c:121
get_impact_subscore
static double get_impact_subscore(const struct cvss *cvss)
Calculate Impact Sub Score.
Definition: cvss.c:228
impact_item::name
const char * name
Definition: cvss.c:130
impact_item
Describe a CVSS impact element.
Definition: cvss.c:128
cvss::authentication
double authentication
Definition: cvss.c:144
Au
@ Au
Definition: cvss.c:120
AV
@ AV
Definition: cvss.c:122
base_metrics
base_metrics
Base metrics.
Definition: cvss.c:115
cvss::access_complexity
double access_complexity
Definition: cvss.c:143
set_impact_from_str
static int set_impact_from_str(const char *value, enum base_metrics metric, struct cvss *cvss)
Set impact score from string representation.
Definition: cvss.c:261
cvss::conf_impact
double conf_impact
Definition: cvss.c:139
cvss::access_vector
double access_vector
Definition: cvss.c:142
cvss::integ_impact
double integ_impact
Definition: cvss.c:140