Skip to content

Commit

Permalink
添加CPE匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Jul 26, 2023
1 parent 68b9a99 commit 9ff2d3b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ opt-level = 3

[dependencies]
cpe = { path = "cpe" }
cve = { path = "cve"}
cve = { path = "cve" }

[dev-dependencies]
serde = { version = "1", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions cpe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
percent-encoding = "2.1"
version-compare = "0.1"
language-tags = {version="0.3",features=["serde"]}
thiserror = "1.0"
15 changes: 12 additions & 3 deletions cpe/src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{parse_uri_attribute, CPEAttributes};
use chrono::{DateTime, Utc};
use serde::{de, Deserialize, Deserializer, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::marker::PhantomData;

Expand Down Expand Up @@ -86,7 +86,8 @@ pub struct Reference {
pub struct CPE23Item {
#[serde(
rename(serialize = "name", deserialize = "@name"),
deserialize_with = "uri_to_attribute"
deserialize_with = "uri_to_attribute",
serialize_with = "attribute_to_uri"
)]
pub name: CPEAttributes,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -105,7 +106,8 @@ pub struct Deprecation {
pub struct DeprecatedInfo {
#[serde(
rename(serialize = "name", deserialize = "@name"),
deserialize_with = "uri_to_attribute"
deserialize_with = "uri_to_attribute",
serialize_with = "attribute_to_uri"
)]
pub name: CPEAttributes,
#[serde(rename(serialize = "type", deserialize = "@type"))]
Expand Down Expand Up @@ -181,3 +183,10 @@ where
}
deserializer.deserialize_any(UriToAttribute(PhantomData))
}

pub fn attribute_to_uri<S>(cpe: &CPEAttributes, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(&cpe.to_string())
}
43 changes: 43 additions & 0 deletions cpe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,46 @@ fn parse_uri_attribute(value: &str) -> Result<String> {
let value = strip_slashes(value.as_str());
Ok(value)
}

pub fn version_cmp(a: &str, b: &str, operator: &str) -> bool {
if let Ok(op) = version_compare::Cmp::from_sign(operator) {
if let Ok(res) = version_compare::compare_to(a, b, op) {
return res;
}
}
false
}

impl CPEAttributes {
// 匹配指定版本是否存在漏洞
pub fn match_version(&self, version: &str) -> bool {
if self.version.is_any() {
return true;
} else if self.version.is_na() {
return false;
}
let my_version = if self.update.is_value() {
format!("{} {}", self.version, self.update)
} else {
self.version.to_string()
};
version_cmp(version, &my_version, "==")
}
// 是否匹配指定产品
pub fn match_product(&self, product: &str) -> bool {
if self.product.is_any() {
return true;
} else if self.product.is_na() {
return false;
}
product == self.normalize_target_software()
}
// 规范化目标软件,
fn normalize_target_software(&self) -> String {
if let Component::Value(software) = &self.target_sw {
format!("{}-{}", software, self.product)
} else {
self.product.to_string()
}
}
}
6 changes: 2 additions & 4 deletions cve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ edition = "2021"

[dependencies]
serde = { version = "1", features = ["derive"] }
cpe = { path = "../cpe", optional = true}
thiserror = "1.0"
[features]
cpe = ["dep:cpe"]
cpe = { path = "../cpe"}
thiserror = "1.0"
8 changes: 8 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}

0 comments on commit 9ff2d3b

Please sign in to comment.