Skip to content

Commit

Permalink
i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Mar 1, 2024
1 parent 61a05b9 commit 2c2d949
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 77 deletions.
9 changes: 7 additions & 2 deletions helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
argh = "0.1.12"
mysqlclient-sys = "0.2.5"
diesel = { version = "2.1.0", features = ["r2d2", "mysql", "chrono", "uuid", "serde_json"] }
chrono = { version = "0.4", default-features = false, features = ["serde"] }
chrono = { version = "0.4", default-features = false, features = ["serde", "clock"] }
dotenvy = "0.15"
uuid = { version = "1.3.3", features = ["v4"] }
nvd-cwe = { version = "0.1.0" }
Expand All @@ -31,8 +31,13 @@ octocrab = "0.34.0"
openssl = { version = "0.10", features = ["vendored"] }
reqwest = { version = "0.11.11", features = ["json", "gzip", "native-tls", "socks"] }
nvd-model = { path = "../nvd-model", features = ["db"] }
attackerkb-api-rs = { version = "0.1.0" }
attackerkb-api-rs = { version = "0.1.2" }
thiserror = "1.0.50"
regex = { version = "1.10.3", default-features = false, features = [
"std",
"unicode",
] }
once_cell = "1.18.0"
[dev-dependencies]
serde = { version = "1", features = ["derive"] }
quick-xml = { version = "0.31.0", features = ["serde", "encoding_rs", "serialize"] }
Expand Down
106 changes: 106 additions & 0 deletions helper/src/kb/attackerkb.rs
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
use crate::init_db_pool;
use crate::kb::create_or_update_exploit;
use attackerkb_api_rs::pagination::{KBResponse, ListResponse};
use attackerkb_api_rs::v1::query::TopicsParameters;
use attackerkb_api_rs::v1::topic::Topic;
use attackerkb_api_rs::AttackKBApi;
use chrono::Utc;
use nvd_model::knowledge_base::db::{CreateKnowledgeBase, KBSource, KBTypes};
use nvd_model::types::{AnyValue, MetaData};
use once_cell::sync::Lazy;
use regex::Regex;
use std::future::Future;
use std::ops::DerefMut;
use std::pin::Pin;

static RE_CVE: Lazy<Regex> =
Lazy::new(|| -> Regex { Regex::new(r"(?m)\bCVE-\d{4}-\d{4,7}\b$").expect("RE_COMPILE_BY_CVE") });

fn is_cve(id: &str) -> bool {
RE_CVE.is_match(id)
}

pub fn import_attackerkb(topics: &ListResponse<Topic>) {
let connection_pool = init_db_pool();
for topic in &topics.data {
let meta = MetaData::default();
if topic.rapid7_analysis.is_some() && is_cve(&topic.name) {
let new_kb = CreateKnowledgeBase {
id: uuid::Uuid::new_v4().as_bytes().to_vec(),
name: topic.name.clone(),
description: topic.document.clone(),
source: KBSource::AttackerKB.to_string(),
path: format!("https://attackerkb.com/topics/{}", topic.name),
meta: AnyValue::new(meta),
verified: true as u8,
created_at: topic
.rapid7_analysis_created
.unwrap_or(Utc::now())
.naive_utc(),
updated_at: topic
.rapid7_analysis_revision_date
.unwrap_or(Utc::now())
.naive_utc(),
types: KBTypes::KnowledgeBase.to_string(),
};
if let Err(err) = create_or_update_exploit(
connection_pool.get().unwrap().deref_mut(),
&new_kb,
Some(topic.name.clone()),
) {
println!("import attackerkb err: {:?}", err);
}
} else {
println!("不是CVE:{}", topic.name);
}
// if let Some(credits) = &topic.metadata.credits {
// for module in credits.module {
// println!("同步metasploit插件:{}", module);
// let new_exp = CreateKnowledgeBase {
// id: uuid::Uuid::new_v4().as_bytes().to_vec(),
// name: topic.name.to_string(),
// description: topic.document.clone(),
// source: KBSource::Metasploit.to_string(),
// path: module,
// meta: AnyValue::new(meta.clone()),
// verified: true as u8,
// created_at: topic.created.naive_utc(),
// updated_at: topic.revision_date.naive_utc(),
// types: KBTypes::Exploit.to_string(),
// };
// if let Err(err) = create_or_update_exploit(
// connection_pool.get().unwrap().deref_mut(),
// &new_exp,
// Some(topic.name.clone()),
// ) {
// println!("同步metasploit 插件失败: {:?}", err);
// };
// }
// }
}
}

pub fn fetch_query(
api: AttackKBApi,
mut query: TopicsParameters,
) -> Pin<Box<dyn Future<Output = ()>>> {
Box::pin(async move {
let resp = api.topics(&query).await;
match resp {
Ok(KBResponse::Topics(topics)) => {
import_attackerkb(&topics);
if let Some(link) = topics.links {
if link.next.is_some() {
query.page += 1;
fetch_query(api, query).await;
}
}
}
Err(err) => {
println!("请求失败: {:?}", err);
}
_ => {
println!("未知:{:?}", resp)
}
}
})
}
85 changes: 12 additions & 73 deletions helper/src/kb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@ mod github;
mod nuclei;
mod serde_format;

use std::fs::File;
use std::ops::DerefMut;
use std::path::{Path, PathBuf};

use attackerkb_api_rs::pagination::KBResponse;
use attackerkb_api_rs::v1::query::TopicsParametersBuilder;
use attackerkb_api_rs::AttackKBApi;
use chrono::Utc;
use chrono::{Duration, Utc};
use diesel::MysqlConnection;

use nvd_model::cve_knowledge_base::db::CreateCveKB;
use nvd_model::cve_knowledge_base::CveKnowledgeBase;
use nvd_model::error::DBResult;
use nvd_model::knowledge_base::db::{CreateKnowledgeBase, KBSource, KBTypes};
use nvd_model::knowledge_base::KnowledgeBase;
use nvd_model::types::{AnyValue, MetaData};
use std::fs::File;
use std::ops::DerefMut;
use std::path::{Path, PathBuf};

use crate::error::HelperResult;
use crate::kb::attackerkb::fetch_query;
use crate::kb::exploit_db::ExploitDB;
use crate::kb::github::GitHubCommit;
use crate::kb::nuclei::Template;
use crate::{init_db_pool, Connection};
use nvd_model::cve_knowledge_base::db::CreateCveKB;
use nvd_model::cve_knowledge_base::CveKnowledgeBase;
use nvd_model::error::DBResult;
use nvd_model::knowledge_base::db::CreateKnowledgeBase;
use nvd_model::knowledge_base::KnowledgeBase;

// 绑定cve和exploit,也许是先有了exp,cve还没更新进来
pub fn associate_cve_and_exploit(conn: &mut Connection, id: &str) {
Expand Down Expand Up @@ -131,68 +128,10 @@ pub async fn akb_sync() -> HelperResult<()> {
};
if let Ok(api) = AttackKBApi::new(token) {
let query = TopicsParametersBuilder::default()
.q(Some("cve-2023-46805".into()))
.rapid7_analysis_revised_after(Some((Utc::now() - Duration::days(3)).date_naive()))
.build()
.unwrap_or_default();
let resp = api.topics(query).await;
if let Ok(KBResponse::Topics(topics)) = resp {
let connection_pool = init_db_pool();
let meta = MetaData::default();
for topic in topics.data {
if topic.rapid7_analysis.is_some() {
let new_kb = CreateKnowledgeBase {
id: uuid::Uuid::new_v4().as_bytes().to_vec(),
name: topic.name.clone(),
description: topic.document,
source: KBSource::AttackerKB.to_string(),
path: format!("https://attackerkb.com/topics/{}", topic.name),
meta: AnyValue::new(meta),
verified: true as u8,
created_at: topic
.rapid7_analysis_created
.unwrap_or(Utc::now())
.naive_utc(),
updated_at: topic
.rapid7_analysis_revision_date
.unwrap_or(Utc::now())
.naive_utc(),
types: KBTypes::KnowledgeBase.to_string(),
};
if let Err(err) = create_or_update_exploit(
connection_pool.get().unwrap().deref_mut(),
&new_kb,
Some(topic.name),
) {
println!("import attackerkb err: {:?}", err);
}
break;
}
if let Some(credits) = topic.metadata.credits {
for module in credits.module {
println!("同步metasploit插件:{}", module);
let new_exp = CreateKnowledgeBase {
id: uuid::Uuid::new_v4().as_bytes().to_vec(),
name: topic.name.to_string(),
description: topic.document.clone(),
source: KBSource::Metasploit.to_string(),
path: module,
meta: AnyValue::new(meta.clone()),
verified: true as u8,
created_at: topic.created.naive_utc(),
updated_at: topic.revision_date.naive_utc(),
types: KBTypes::Exploit.to_string(),
};
if let Err(err) = create_or_update_exploit(
connection_pool.get().unwrap().deref_mut(),
&new_exp,
Some(topic.name.clone()),
) {
println!("同步metasploit 插件失败: {:?}", err);
};
}
}
}
}
fetch_query(api, query).await;
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub async fn sync_mode(config: SyncCommand) {
if config.kb {
update_from_rss().await;
update_from_github().await;
let _ = akb_sync().await;
}
}

Expand Down
7 changes: 5 additions & 2 deletions nvd-yew/src/component/kb_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl KBRow {
"https://github.com/projectdiscovery/nuclei-templates/blob/main/{}",
path
),
_ => String::new(),
_ => source.to_string(),
};
html! {<div><a href={kb_url} class="text-reset text-nowrap" target="_blank" rel="noreferrer"><i class="ti ti-external-link"></i>{path}</a></div>}
}
Expand All @@ -118,9 +118,12 @@ impl KBRow {
"nuclei-templates" => {
html! {<div><span class="badge bg-azure"><i class="ti ti-storm"></i>{source}</span></div>}
}
_ => {
"attackerkb" => {
html! {<div><span class="badge bg-green"><i class="ti ti-check"></i>{source}</span></div>}
}
_ => {
html! {<div><span class="badge bg-google"><i class="ti ti-check"></i>{source}</span></div>}
}
}
}
}
1 change: 1 addition & 0 deletions nvd-yew/src/routes/cve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl CVEDetails {
fn description(&self, description_data: &[nvd_cves::v4::Description]) -> Html {
let description = description_data
.iter()
.filter(|d| d.lang == "en")
.map(|d| d.value.clone())
.collect::<String>();
let mut description = description.chars();
Expand Down

0 comments on commit 2c2d949

Please sign in to comment.