server/ (state, ingestion, API, CSV exports, embedded assets), scanner, feeds_cmd, config_cmd and commands. Code moved as is; no behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.3 KiB
Rust
148 lines
4.3 KiB
Rust
//! `xexeu feeds import` and `xexeu feeds sync`.
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::ExitCode;
|
|
|
|
use crate::fail;
|
|
use xexeu_core::vuln::{import_kev, import_nvd, sync_feeds, KevCatalog, SyncOptions, VulnDatabase};
|
|
|
|
fn find_matching_file(dir: &Path, keyword: &str, ext: &str) -> Option<PathBuf> {
|
|
let entries = fs::read_dir(dir).ok()?;
|
|
for entry in entries.flatten() {
|
|
let path = entry.path();
|
|
if path.is_file() {
|
|
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
|
|
let lower = name.to_lowercase();
|
|
if lower.contains(keyword) && lower.ends_with(ext) {
|
|
return Some(path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub(crate) fn run_feeds_import(
|
|
directory: Option<PathBuf>,
|
|
nvd_path: Option<PathBuf>,
|
|
kev_path: Option<PathBuf>,
|
|
data_dir: &Path,
|
|
) -> ExitCode {
|
|
let nvd_file = nvd_path.or_else(|| {
|
|
directory.as_ref().map(|d| {
|
|
let direct = d.join("nvd.json");
|
|
if direct.exists() {
|
|
direct
|
|
} else {
|
|
find_matching_file(d, "nvd", "json").unwrap_or(direct)
|
|
}
|
|
})
|
|
});
|
|
|
|
let kev_file = kev_path.or_else(|| {
|
|
directory.as_ref().map(|d| {
|
|
let direct_json = d.join("kev.json");
|
|
if direct_json.exists() {
|
|
direct_json
|
|
} else {
|
|
let direct_csv = d.join("kev.csv");
|
|
if direct_csv.exists() {
|
|
direct_csv
|
|
} else {
|
|
find_matching_file(d, "kev", "json")
|
|
.or_else(|| find_matching_file(d, "kev", "csv"))
|
|
.unwrap_or(direct_json)
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
let nvd_file = match nvd_file {
|
|
Some(f) if f.exists() => f,
|
|
Some(f) => return fail(&format!("arquivo NVD não encontrado em: {}", f.display())),
|
|
None => return fail("informe o diretório com os feeds ou use --nvd <arquivo>"),
|
|
};
|
|
|
|
let nvd = match import_nvd(&nvd_file) {
|
|
Ok(c) => c,
|
|
Err(e) => return fail(&format!("falha ao importar feed NVD: {e}")),
|
|
};
|
|
|
|
let kev = if let Some(kf) = &kev_file {
|
|
if kf.exists() {
|
|
match import_kev(kf) {
|
|
Ok(c) => c,
|
|
Err(e) => return fail(&format!("falha ao importar feed CISA KEV: {e}")),
|
|
}
|
|
} else {
|
|
KevCatalog::default()
|
|
}
|
|
} else {
|
|
KevCatalog::default()
|
|
};
|
|
|
|
let db = VulnDatabase { nvd, kev };
|
|
let db_path = data_dir.join("vuln_db.json");
|
|
if let Err(e) = db.save_to_file(&db_path) {
|
|
return fail(&format!(
|
|
"falha ao salvar base de vulnerabilidades em {}: {e}",
|
|
db_path.display()
|
|
));
|
|
}
|
|
|
|
println!(
|
|
"Base de vulnerabilidades importada com sucesso: {} CVEs ({} no KEV) salvos em {}",
|
|
db.cve_count(),
|
|
db.kev_count(),
|
|
db_path.display()
|
|
);
|
|
ExitCode::SUCCESS
|
|
}
|
|
|
|
pub(crate) async fn run_feeds_sync(data_dir: &Path) -> ExitCode {
|
|
let db_path = data_dir.join("vuln_db.json");
|
|
let mut db = if db_path.exists() {
|
|
match VulnDatabase::load_from_file(&db_path) {
|
|
Ok(loaded) => loaded,
|
|
Err(e) => {
|
|
eprintln!(
|
|
"aviso: não foi possível carregar base anterior em {}: {e}; iniciando base nova",
|
|
db_path.display()
|
|
);
|
|
VulnDatabase::default()
|
|
}
|
|
}
|
|
} else {
|
|
VulnDatabase::default()
|
|
};
|
|
|
|
let sync_options = SyncOptions::default();
|
|
println!("Sincronizando feeds de vulnerabilidade (NVD e CISA KEV)...");
|
|
|
|
let downloaded = match sync_feeds(&sync_options).await {
|
|
Ok(d) => d,
|
|
Err(e) => {
|
|
eprintln!("falha na sincronização dos feeds: {e}");
|
|
return ExitCode::FAILURE;
|
|
}
|
|
};
|
|
|
|
db.merge(downloaded);
|
|
|
|
if let Err(e) = db.save_to_file(&db_path) {
|
|
return fail(&format!(
|
|
"falha ao salvar base atualizada em {}: {e}",
|
|
db_path.display()
|
|
));
|
|
}
|
|
|
|
println!(
|
|
"Sincronização de feeds concluída: {} CVEs totais ({} no KEV) gravados em {}",
|
|
db.cve_count(),
|
|
db.kev_count(),
|
|
db_path.display()
|
|
);
|
|
ExitCode::SUCCESS
|
|
}
|