diff --git a/kunai/src/bin/main.rs b/kunai/src/bin/main.rs index d06ae28..1778dab 100644 --- a/kunai/src/bin/main.rs +++ b/kunai/src/bin/main.rs @@ -753,22 +753,20 @@ impl<'s> EventConsumer<'s> { if let Some(ns) = ns { match self.cache.get_or_cache_in_ns(ns, p) { Ok(h) => h, - Err(e) => Hashes { - path: p.to_path_buf().clone(), - meta: FileMeta { + Err(e) => { + let meta = FileMeta { error: Some(format!("{e}")), ..Default::default() - }, - }, + }; + Hashes::with_meta(p.to_path_buf().clone(), meta) + } } } else { - Hashes { - path: p.to_path_buf().clone(), - meta: FileMeta { - error: Some("unknown namespace".into()), - ..Default::default() - }, - } + let meta = FileMeta { + error: Some("unknown namespace".into()), + ..Default::default() + }; + Hashes::with_meta(p.to_path_buf().clone(), meta) } } diff --git a/kunai/src/cache.rs b/kunai/src/cache.rs index 1f8db72..07146e4 100644 --- a/kunai/src/cache.rs +++ b/kunai/src/cache.rs @@ -52,6 +52,19 @@ pub struct FileMeta { pub error: Option, } +impl From for FileMeta { + fn from(value: Hashes) -> Self { + Self { + md5: value.md5, + sha1: value.sha1, + sha256: value.sha256, + sha512: value.sha512, + size: value.size, + error: value.error, + } + } +} + impl FileMeta { #[inline] pub(crate) fn iocs(&self) -> Vec> { @@ -67,11 +80,27 @@ impl FileMeta { #[derive(Debug, Default, Clone, FieldGetter, Serialize, Deserialize)] pub struct Hashes { pub path: PathBuf, - #[serde(flatten)] - pub meta: FileMeta, + pub md5: String, + pub sha1: String, + pub sha256: String, + pub sha512: String, + pub size: usize, + pub error: Option, } impl Hashes { + pub fn with_meta(p: PathBuf, meta: FileMeta) -> Self { + Self { + path: p, + md5: meta.md5, + sha1: meta.sha1, + sha256: meta.sha256, + sha512: meta.sha512, + size: meta.size, + error: meta.error, + } + } + #[inline(always)] pub fn from_path_ref>(p: T) -> Self { let path = p.as_ref(); @@ -95,13 +124,13 @@ impl Hashes { sha1.update(&buf[..n]); sha256.update(&buf[..n]); sha512.update(&buf[..n]); - h.meta.size += n; + h.size += n; } - h.meta.md5 = hex::encode(md5.finalize()); - h.meta.sha1 = hex::encode(sha1.finalize()); - h.meta.sha256 = hex::encode(sha256.finalize()); - h.meta.sha512 = hex::encode(sha512.finalize()); + h.md5 = hex::encode(md5.finalize()); + h.sha1 = hex::encode(sha1.finalize()); + h.sha256 = hex::encode(sha256.finalize()); + h.sha512 = hex::encode(sha512.finalize()); } h @@ -109,9 +138,13 @@ impl Hashes { #[inline(always)] pub(crate) fn iocs(&self) -> Vec> { - let mut v = vec![self.path.to_string_lossy()]; - v.extend(self.meta.iocs()); - v + vec![ + self.path.to_string_lossy(), + self.md5.as_str().into(), + self.sha1.as_str().into(), + self.sha256.as_str().into(), + self.sha512.as_str().into(), + ] } } diff --git a/kunai/src/events.rs b/kunai/src/events.rs index ec9eb1a..2f99e12 100644 --- a/kunai/src/events.rs +++ b/kunai/src/events.rs @@ -1015,9 +1015,10 @@ pub struct FileScanData { impl FileScanData { pub fn from_hashes(h: Hashes) -> Self { + let p = h.path.clone(); Self { - path: h.path, - meta: h.meta, + path: p, + meta: h.into(), ..Default::default() } }