use malwaredb_client::MdbClient;
use std::path::PathBuf;
use std::process::ExitCode;
use anyhow::{anyhow, Result};
use clap::Parser;
use uuid::Uuid;
#[derive(Debug, Clone, Eq, PartialEq, Parser)]
pub struct SearchRequest {
#[clap(long)]
pub hash: Option<String>,
#[clap(long, default_value = "sha256")]
pub hash_type: String,
#[clap(long)]
pub file_name: Option<String>,
#[clap(long)]
pub labels: Option<Vec<String>>,
#[clap(long = "type")]
pub file_type: Option<String>,
#[clap(long = "magic")]
pub magic: Option<String>,
#[clap(long, default_value = "sha256")]
pub response_type: String,
#[clap(long, default_value_t = 100)]
pub limit: u32,
}
impl SearchRequest {
pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
let hash_search = if let Some(hash) = &self.hash {
let hash_type = self
.hash_type
.as_str()
.try_into()
.map_err(|e: String| anyhow!(e))?;
Some((hash_type, hash.clone()))
} else {
None
};
let response = self
.response_type
.as_str()
.try_into()
.map_err(|e: String| anyhow!(e))?;
let response = config
.partial_search_labels_type(
hash_search,
self.file_name.clone(),
response,
self.labels.clone(),
self.file_type.clone(),
self.magic.clone(),
self.limit,
)
.await?;
if response.hashes.is_empty() {
println!("No results!");
} else {
for result in response.hashes {
println!("{result}");
}
}
Ok(ExitCode::SUCCESS)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Parser)]
pub struct YaraSearch {
pub yara_rule: PathBuf,
}
impl YaraSearch {
pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
let contents = std::fs::read_to_string(&self.yara_rule)?;
let response = config.yara_search(&contents).await?;
println!("Yara rule submitted as job: {}", response.uuid);
Ok(ExitCode::SUCCESS)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Parser)]
pub struct YaraResult {
pub uuid: Uuid,
}
impl YaraResult {
pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
let result = config.yara_result(self.uuid).await?;
for (rule, hashes) in result.results {
println!("{rule}");
for hash in hashes {
println!("\t{hash}");
}
}
Ok(ExitCode::SUCCESS)
}
}