Debian Codegraph @

src/v4/metric/base/ac.rs

raw symbols SCIP
1 //! Attack Complexity (AC)
2
3 use crate::{
4 Error, Result,
5 v4::metric::{Metric, MetricType},
6 };
7 use alloc::borrow::ToOwned;
8 use core::{fmt, str::FromStr};
9
10 /// Attack Complexity (AC) - CVSS v4.0 Base Metric Group
11 ///
12 /// Described in CVSS v4.0 Specification: Section 2.1.2
13 ///
14 /// > This metric captures measurable actions that must be taken by the attacker
15 /// > to actively evade or circumvent **existing built-in security-enhancing
16 /// > conditions** in order to obtain a working exploit. These are conditions
17 /// > whose primary purpose is to increase security and/or increase exploit
18 /// > engineering complexity. A vulnerability exploitable without a
19 /// > target-specific variable has a lower complexity than a vulnerability that
20 /// > would require non-trivial customization. This metric is meant to capture
21 /// > security mechanisms utilized by the vulnerable system, and does not relate
22 /// > to the amount of time or attempts it would take for an attacker to
23 /// > succeed, e.g. a race condition. If the attacker does not take action to
24 /// > overcome these conditions, the attack will always fail.
25 /// >
26 /// > The evasion or satisfaction of authentication mechanisms or requisites is
27 /// > included in the Privileges Required assessment and is *not* considered
28 /// > here as a factor of relevance for Attack Complexity.
29 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
30 pub enum AttackComplexity {
31 /// High (H)
32 ///
33 /// > The successful attack depends on the evasion or circumvention of
34 /// > security-enhancing techniques in place that would otherwise hinder the
35 /// > attack. These include: Evasion of exploit mitigation techniques. The
36 /// > attacker must have additional methods available to bypass security
37 /// > measures in place. For example, circumvention of **address space
38 /// > randomization (ASLR) or data execution prevention (DEP)** must be
39 /// > performed for the attack to be successful. Obtaining target-specific
40 /// > secrets. The attacker must gather some **target-specific secret**
41 /// > before the attack can be successful. A secret is any piece of
42 /// > information that cannot be obtained through any amount of
43 /// > reconnaissance. To obtain the secret the attacker must perform
44 /// > additional attacks or break otherwise secure measures (e.g. knowledge
45 /// > of a secret key may be needed to break a crypto channel). This
46 /// > operation must be performed for each attacked target.
47 High,
48
49 /// Low (L)
50 ///
51 /// > The attacker must take no measurable action to exploit the
52 /// > vulnerability. The attack requires no target-specific circumvention to
53 /// > exploit the vulnerability. An attacker can expect repeatable success
54 /// > against the vulnerable system.
55 Low,
56 }
57
58 impl Default for AttackComplexity {
59 fn default() -> Self {
60 Self::Low
61 }
62 }
63
64 impl Metric for AttackComplexity {
65 const TYPE: MetricType = MetricType::AC;
66
67 fn as_str(self) -> &'static str {
68 match self {
69 AttackComplexity::High => "H",
70 AttackComplexity::Low => "L",
71 }
72 }
73 }
74
75 impl fmt::Display for AttackComplexity {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(f, "{}:{}", Self::name(), self.as_str())
78 }
79 }
80
81 impl FromStr for AttackComplexity {
82 type Err = Error;
83
84 fn from_str(s: &str) -> Result<Self> {
85 match s {
86 "H" => Ok(AttackComplexity::High),
87 "L" => Ok(AttackComplexity::Low),
88 _ => Err(Error::InvalidMetricV4 {
89 metric_type: Self::TYPE,
90 value: s.to_owned(),
91 }),
92 }
93 }
94 }
95
96 #[cfg(feature = "std")]
97 pub(crate) mod merge {
98 use super::*;
99 use crate::{
100 Error,
101 v4::{
102 MetricType,
103 metric::{MetricLevel, environmental::ModifiedAttackComplexity},
104 },
105 };
106 use alloc::borrow::ToOwned;
107 use core::str::FromStr;
108
109 /// Result of the merging of the base and modified metrics.
110 ///
111 /// Used in scoring.
112 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
113 pub(crate) enum MergedAttackComplexity {
114 High,
115 Low,
116 }
117
118 impl Default for MergedAttackComplexity {
119 fn default() -> Self {
120 Self::Low
121 }
122 }
123
124 impl FromStr for MergedAttackComplexity {
125 type Err = Error;
126
127 fn from_str(s: &str) -> Result<Self> {
128 match s {
129 "H" => Ok(MergedAttackComplexity::High),
130 "L" => Ok(MergedAttackComplexity::Low),
131 _ => Err(Error::InvalidMetricV4 {
132 metric_type: MetricType::AC,
133 value: s.to_owned(),
134 }),
135 }
136 }
137 }
138
139 impl MetricLevel for MergedAttackComplexity {
140 fn level(self) -> f64 {
141 // AC_levels = {'L': 0.0, 'H': 0.1}
142 match self {
143 Self::High => 0.1,
144 Self::Low => 0.0,
145 }
146 }
147 }
148
149 impl AttackComplexity {
150 pub(crate) fn merge(
151 self,
152 value: Option<ModifiedAttackComplexity>,
153 ) -> MergedAttackComplexity {
154 match value {
155 Some(ModifiedAttackComplexity::NotDefined) | None => match self {
156 Self::High => MergedAttackComplexity::High,
157 Self::Low => MergedAttackComplexity::Low,
158 },
159 Some(ModifiedAttackComplexity::High) => MergedAttackComplexity::High,
160 Some(ModifiedAttackComplexity::Low) => MergedAttackComplexity::Low,
161 }
162 }
163 }
164 }