添加 analyzer: db

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-06-25 14:38:19 +08:00
parent 976d5016f0
commit 969f62f547
6 changed files with 67694 additions and 1 deletions

View File

@ -5,3 +5,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.0", features = ["derive"] }
cvrf-xmlparser = { git = "http://git.culinux.net/jiachao2130/cvrf-xmlparser.git", version = "0.1.0" }
updateinfo-xmlparser = { git = "http://git.culinux.net/jiachao2130/updateinfo-xmlparser.git", version = "0.1.0" }
serde = { version = "1", features = ["serde_derive"] }
toml = { version = "0.8" }

100
src/analyzer/db.rs Normal file
View File

@ -0,0 +1,100 @@
use std::collections::HashMap;
use cvrf_xmlparser::{SaInfo, CVE};
use serde::{Deserialize, Serialize};
use updateinfo_xmlparser::{UpdateInfoDb, RpmInfo};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PacakgeDb {
db: HashMap<String, Vec<RpmInfo>>,
}
impl PacakgeDb {
pub fn new() -> Self {
PacakgeDb {
db: HashMap::new(),
}
}
/// 从已有的 updateinfo 仓库文件中,获取所有与安全更新相关的软件包
pub fn load_from_updateinfodb(&mut self, updateinfodb: &UpdateInfoDb) {
for updateinfo in &updateinfodb.db {
for pkg in &updateinfo.pkglist {
if let Some(rpms) = self.db.get_mut(pkg.name()) {
rpms.push(pkg.clone());
} else {
let rpms = vec![pkg.clone()];
self.db.insert(pkg.name().to_string(), rpms);
}
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SaDb {
db: HashMap<String, SaInfo>,
}
impl SaDb {
pub fn new() -> Self {
SaDb {
db: HashMap::new(),
}
}
/// 一般来自对 cvrf 文件解析并转换为 SaInfo 的文本数据文件
pub fn load_from_file(&mut self, file: &str) -> crate::Result<()> {
let data = std::fs::read_to_string(file)?;
self.db = toml::from_str(&data)?;
Ok(())
}
/// 从 SaInfo 中提取出所有的 CVE 源
pub fn get_cvedb(&self) -> CveDb {
let mut cvedb = CveDb::new();
self.db.iter().for_each(|(_, sainfo)| {
sainfo.cves.iter().for_each(|cve| {
cvedb.db.insert(cve.id.clone(), cve.clone());
})
});
cvedb
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CveDb {
db: HashMap<String, CVE>,
}
impl CveDb {
#[allow(dead_code)]
pub fn new() -> Self {
CveDb {
db: HashMap::new(),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn pkgdb_works() {
assert!(true);
let updatexml = "test/updateinfo.xml";
let mut updatedb = UpdateInfoDb::new();
updatedb.load_xml(&updatexml).unwrap();
let mut pkgdb = PacakgeDb::new();
pkgdb.load_from_updateinfodb(&updatedb);
let bash_pkgs = pkgdb.db.get("bash").unwrap();
assert_eq!(bash_pkgs.len(), 2);
}
}

1
src/analyzer/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod db;

View File

@ -1,4 +1,4 @@
use clap::{Parser, command};
use clap::{command, Parser};
/// CULinux Vulnerability Analyze Tool (简称 CULinux-VAT) 是一个用于分析是一个用于分析和检测 Linux
/// 系统漏洞的工具。该工具旨在帮助系统管理员和安全专业人员识别和修复潜在的安全漏洞,以确保 CULinux

View File

@ -1,5 +1,7 @@
pub mod cli;
mod analyzer;
/// 定义 crate::Error
/// 大部分函数返回的错误
pub type Error = Box<dyn std::error::Error + Send + Sync>;

67586
test/updateinfo.xml Normal file

File diff suppressed because one or more lines are too long