| 1 | //! Attack Requirements (MAT) |
| 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 Requirements (MAT) - CVSS v4.0 Environmental Metric Group |
| 11 | /// |
| 12 | /// Described in CVSS v4.0 Specification: Section 4.2 |
| 13 | /// |
| 14 | /// > This metric captures the prerequisite **deployment and execution |
| 15 | /// > conditions or variables** of the vulnerable system that enable the attack. |
| 16 | /// > These differ from security-enhancing techniques/technologies (ref _Attack |
| 17 | /// > Complexity_) as the primary purpose of these conditions is **not** to |
| 18 | /// > explicitly mitigate attacks, but rather, emerge naturally as a consequence |
| 19 | /// > of the deployment and execution of the vulnerable system. If the attacker |
| 20 | /// > does not take action to overcome these conditions, the attack may succeed |
| 21 | /// > only occasionally or not succeed at all. |
| 22 | #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 23 | pub enum ModifiedAttackRequirements { |
| 24 | /// Not Defined (X) |
| 25 | /// |
| 26 | /// > The metric has not been evaluated. |
| 27 | NotDefined, |
| 28 | |
| 29 | /// Present (P) |
| 30 | /// |
| 31 | /// > The successful attack depends on the presence of specific deployment |
| 32 | /// > and execution conditions of the vulnerable system that enable the |
| 33 | /// > attack. These include: A **race condition** must be won to |
| 34 | /// > successfully exploit the vulnerability. The successfulness of the |
| 35 | /// > attack is conditioned on execution conditions that are not under full |
| 36 | /// > control of the attacker. The attack may need to be launched multiple |
| 37 | /// > times against a single target before being successful. Network |
| 38 | /// > injection. The attacker must inject themselves into the logical |
| 39 | /// > network path between the target and the resource requested by the |
| 40 | /// > victim (e.g. vulnerabilities requiring an on-path attacker). |
| 41 | Present, |
| 42 | |
| 43 | /// None (N) |
| 44 | /// |
| 45 | /// > The successful attack does not depend on the deployment and execution |
| 46 | /// > conditions of the vulnerable system. The attacker can expect to be |
| 47 | /// > able to reach the vulnerability and execute the exploit under all or |
| 48 | /// > most instances of the vulnerability. |
| 49 | None, |
| 50 | } |
| 51 | |
| 52 | impl Default for ModifiedAttackRequirements { |
| 53 | fn default() -> Self { |
| 54 | Self::NotDefined |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl Metric for ModifiedAttackRequirements { |
| 59 | const TYPE: MetricType = MetricType::MAT; |
| 60 | |
| 61 | fn as_str(self) -> &'static str { |
| 62 | match self { |
| 63 | ModifiedAttackRequirements::NotDefined => "X", |
| 64 | ModifiedAttackRequirements::Present => "P", |
| 65 | ModifiedAttackRequirements::None => "N", |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl fmt::Display for ModifiedAttackRequirements { |
| 71 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 72 | write!(f, "{}:{}", Self::name(), self.as_str()) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl FromStr for ModifiedAttackRequirements { |
| 77 | type Err = Error; |
| 78 | |
| 79 | fn from_str(s: &str) -> Result<Self> { |
| 80 | match s { |
| 81 | "X" => Ok(ModifiedAttackRequirements::NotDefined), |
| 82 | "P" => Ok(ModifiedAttackRequirements::Present), |
| 83 | "N" => Ok(ModifiedAttackRequirements::None), |
| 84 | _ => Err(Error::InvalidMetricV4 { |
| 85 | metric_type: Self::TYPE, |
| 86 | value: s.to_owned(), |
| 87 | }), |
| 88 | } |
| 89 | } |
| 90 | } |