Skip to content

Commit 5087de1

Browse files
committed
init
1 parent a240464 commit 5087de1

File tree

12 files changed

+281
-187
lines changed

12 files changed

+281
-187
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,3 @@ opt-level = 3
3131

3232

3333
[dependencies]
34-
35-
[[bin]]
36-
name = "nvd-server"
37-
path = "nvd-server/src/main.rs"

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ COPY nvd-yew nvd-yew
1818
COPY nvd-api nvd-api
1919
COPY nvd-server nvd-server
2020
COPY helper helper
21+
COPY src src
2122
RUN cargo build --release
2223
RUN trunk build --release
2324

cve/src/v4/configurations.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,16 @@ impl Match {
145145
}
146146
// 什么都没有的
147147
if v_start.is_none() && v_end.is_none() {
148-
return format!(
149-
"{} {}",
150-
op_start.unwrap_or(""),
151-
version.unwrap_or_default()
152-
);
148+
format!("{} {}", op_start.unwrap_or(""), version.unwrap_or_default())
153149
} else {
154-
return format!(
150+
format!(
155151
"{}{} {} {}{}",
156152
v_start.unwrap_or(""),
157153
op_start.unwrap_or(""),
158154
version.unwrap_or_default(),
159155
op_end.unwrap_or(""),
160156
v_end.unwrap_or_default()
161-
);
157+
)
162158
}
163159
}
164160
pub fn match_version_range(&self, ver: &str) -> bool {

helper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
[dependencies]
88
mysqlclient-sys = "0.2.5"
99
diesel = { version = "2.1.0", features = ["r2d2", "mysql", "chrono", "uuid", "serde_json"] }
10+
chrono = { version = "0.4", default-features = false, features = ["serde"] }
1011
dotenvy = "0.15"
1112
uuid = { version = "1.3.3", features = ["v4"] }
1213
nvd-server = { path = "../nvd-server" }

helper/examples/cpe-api-example.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use nvd_api::ApiVersion;
12
use nvd_api::v2::products::{CpeMatchParameters, CpeParameters};
23
// https://cwe.mitre.org/data/downloads.html
34
// curl -s -k https://cwe.mitre.org/data/downloads.html |grep -Eo '(/[^"]*\.xml.zip)'|xargs -I % wget -c https://cwe.mitre.org%
45
#[tokio::main]
56
async fn main() {
6-
let api = nvd_api::NVDApi::new(None, "2.0").unwrap();
7+
let api = nvd_api::NVDApi::new(None, ApiVersion::default()).unwrap();
78
let cpe = api
89
.cpe(CpeParameters {
910
cpe_name_id: None,

helper/examples/cve-api-example.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use nvd_api::ApiVersion;
12
use nvd_api::v2::vulnerabilities::{CveHistoryParameters, CveParameters};
23
// https://cwe.mitre.org/data/downloads.html
34
// curl -s -k https://cwe.mitre.org/data/downloads.html |grep -Eo '(/[^"]*\.xml.zip)'|xargs -I % wget -c https://cwe.mitre.org%
45
#[tokio::main]
56
async fn main() {
6-
let api = nvd_api::NVDApi::new(None, "2.0").unwrap();
7+
let api = nvd_api::NVDApi::new(None, ApiVersion::default()).unwrap();
78
let cve = api
89
.cve(CveParameters {
910
cpe_name: None,

helper/examples/nvdcve/v.json

Lines changed: 0 additions & 100 deletions
This file was deleted.

helper/src/bin/api_to_db.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
use cached::proc_macro::cached;
2+
use cached::SizedCache;
3+
use chrono::{NaiveDateTime, Utc};
4+
use cve::v4::{CVEContainer, CVEItem};
5+
use diesel::mysql::MysqlConnection;
6+
use helper::init_db_pool;
7+
use nvd_api::v2::vulnerabilities::CveParameters;
8+
use nvd_api::v2::LastModDate;
9+
use nvd_api::ApiVersion;
10+
use nvd_server::error::DBResult;
11+
use nvd_server::modules::cve_db::CreateCve;
12+
use nvd_server::modules::cve_product_db::CreateCveProductByName;
13+
use nvd_server::modules::product_db::{CreateProduct, QueryProductById};
14+
use nvd_server::modules::vendor_db::CreateVendors;
15+
use nvd_server::modules::{Cve, CveProduct, Product, Vendor};
16+
use std::str::FromStr;
17+
18+
// https://cwe.mitre.org/data/downloads.html
19+
// curl -s -k https://cwe.mitre.org/data/downloads.html |grep -Eo '(/[^"]*\.xml.zip)'|xargs -I % wget -c https://cwe.mitre.org%
20+
21+
fn import_to_db(connection: &mut MysqlConnection, cve_item: CVEItem) -> DBResult<String> {
22+
let id = cve_item.cve.meta.id;
23+
let y = id.split('-').nth(1).unwrap_or_default();
24+
let new_post = CreateCve {
25+
id: id.clone(),
26+
created_at: cve_item.published_date,
27+
updated_at: cve_item.last_modified_date,
28+
references: serde_json::json!(cve_item.cve.references.reference_data),
29+
description: serde_json::json!(cve_item.cve.description.description_data),
30+
severity: cve_item.impact.severity(),
31+
metrics: serde_json::json!(cve_item.impact),
32+
assigner: cve_item.cve.meta.assigner,
33+
configurations: serde_json::json!(cve_item.configurations.nodes),
34+
year: i32::from_str(y).unwrap_or_default(),
35+
weaknesses: serde_json::json!(cve_item.cve.problem_type.problem_type_data),
36+
timeline: Default::default(),
37+
};
38+
// 插入到数据库
39+
match Cve::create(connection, &new_post) {
40+
Ok(cve_id) => {
41+
// 插入cpe_match关系表
42+
for node in cve_item.configurations.nodes {
43+
for vendor_product in node.vendor_product() {
44+
import_vendor_product_to_db(connection, vendor_product.clone());
45+
create_cve_product(
46+
connection,
47+
cve_id.id.clone(),
48+
vendor_product.vendor,
49+
vendor_product.product,
50+
);
51+
}
52+
}
53+
}
54+
Err(err) => {
55+
println!("Cve::create: {err:?}");
56+
}
57+
}
58+
Ok(new_post.id)
59+
}
60+
61+
pub fn create_cve_product(
62+
conn: &mut MysqlConnection,
63+
cve_id: String,
64+
vendor: String,
65+
product: String,
66+
) -> String {
67+
// 构建待插入对象
68+
let cp = CreateCveProductByName {
69+
cve_id,
70+
vendor,
71+
product,
72+
};
73+
// 插入到数据库
74+
match CveProduct::create_by_name(conn, &cp) {
75+
Ok(_cp) => {}
76+
Err(err) => {
77+
println!("create_cve_product: {err:?}:{cp:?}");
78+
}
79+
}
80+
String::new()
81+
}
82+
83+
#[cached(
84+
type = "SizedCache<String, Vec<u8>>",
85+
create = "{ SizedCache::with_size(100) }",
86+
convert = r#"{ format!("{:?}", product.to_owned()) }"#
87+
)]
88+
fn import_vendor_product_to_db(connection: &mut MysqlConnection, product: cpe::Product) -> Vec<u8> {
89+
let vendor_id = create_vendor(connection, product.vendor, None);
90+
create_product(connection, vendor_id, product.product, product.part)
91+
}
92+
93+
#[cached(
94+
type = "SizedCache<String, Vec<u8>>",
95+
create = "{ SizedCache::with_size(100) }",
96+
convert = r#"{ format!("{}", name.to_owned()) }"#
97+
)]
98+
pub fn create_vendor(
99+
conn: &mut MysqlConnection,
100+
name: String,
101+
description: Option<String>,
102+
) -> Vec<u8> {
103+
if let Ok(v) = Vendor::query_by_name(conn, &name) {
104+
return v.id;
105+
}
106+
// 构建待插入对象
107+
let new_post = CreateVendors {
108+
id: uuid::Uuid::new_v4().as_bytes().to_vec(),
109+
name,
110+
description,
111+
official: u8::from(true),
112+
homepage: None,
113+
};
114+
// 插入到数据库
115+
if let Err(err) = Vendor::create(conn, &new_post) {
116+
println!("create_vendor: {err:?}");
117+
}
118+
new_post.id
119+
}
120+
121+
#[cached(
122+
type = "SizedCache<String, Vec<u8>>",
123+
create = "{ SizedCache::with_size(100) }",
124+
convert = r#"{ format!("{}:{:?}", name.to_owned(),vendor.to_owned()) }"#
125+
)]
126+
pub fn create_product(
127+
conn: &mut MysqlConnection,
128+
vendor: Vec<u8>,
129+
name: String,
130+
part: String,
131+
) -> Vec<u8> {
132+
let q = QueryProductById {
133+
vendor_id: vendor.clone(),
134+
name: name.clone(),
135+
};
136+
if let Ok(v) = Product::query_by_id(conn, &q) {
137+
return v.id;
138+
}
139+
// 构建待插入对象
140+
let new_post = CreateProduct {
141+
id: uuid::Uuid::new_v4().as_bytes().to_vec(),
142+
vendor_id: vendor,
143+
name,
144+
description: None,
145+
official: u8::from(true),
146+
part,
147+
homepage: None,
148+
};
149+
// 插入到数据库
150+
if let Err(err) = Product::create(conn, &new_post) {
151+
println!("create_product: {err:?}");
152+
}
153+
new_post.id
154+
}
155+
156+
fn main() {
157+
// let connection_pool = init_db_pool();
158+
// let api = nvd_api::NVDApi::new(None, ApiVersion::default()).unwrap();
159+
let now = Utc::now();
160+
let two_h =
161+
println!("{:?}", now);
162+
// api.cve(CveParameters{
163+
// cpe_name: None,
164+
// cve_id: None,
165+
// cvss_v2_metrics: None,
166+
// cvss_v2_severity: None,
167+
// cvss_v3_metrics: None,
168+
// cvss_v3_severity: None,
169+
// cwe_id: None,
170+
// has_cert_alerts: None,
171+
// has_cert_notes: None,
172+
// has_kev: None,
173+
// has_oval: None,
174+
// is_vulnerable: None,
175+
// keyword: None,
176+
// last_mod: Some(LastModDate{ last_mod_start_date: "".to_string(), last_mod_end_date: "".to_string() }),
177+
// no_rejected: None,
178+
// pub_date: None,
179+
// limit_offset: None,
180+
// source_identifier: None,
181+
// virtual_match: None,
182+
// })
183+
}

helper/src/bin/cve_to_db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn import_to_db(connection: &mut MysqlConnection, cve_item: CVEItem) -> DBResult
2626
updated_at: cve_item.last_modified_date,
2727
references: serde_json::json!(cve_item.cve.references.reference_data),
2828
description: serde_json::json!(cve_item.cve.description.description_data),
29-
severity: cve_item.impact.severity().to_string(),
29+
severity: cve_item.impact.severity(),
3030
metrics: serde_json::json!(cve_item.impact),
3131
assigner: cve_item.cve.meta.assigner,
3232
configurations: serde_json::json!(cve_item.configurations.nodes),
@@ -154,7 +154,7 @@ pub fn create_product(
154154

155155
fn main() {
156156
let connection_pool = init_db_pool();
157-
for y in 2023..2024 {
157+
for y in 2002..2024 {
158158
let p = format!("helper/examples/nvdcve/nvdcve-1.1-{y}.json.gz");
159159
println!("{p}");
160160
let gz_open_file = File::open(p).unwrap();
@@ -164,6 +164,6 @@ fn main() {
164164
for w in c.CVE_Items {
165165
import_to_db(connection_pool.get().unwrap().deref_mut(), w).unwrap_or_default();
166166
}
167-
break;
167+
// break;
168168
}
169169
}

0 commit comments

Comments
 (0)