use std::path::Path;
#[cfg(feature = "yara")]
use std::collections::BTreeSet;
use crate::error::{Error, Result};
use crate::loader::LoadedBinary;
pub const MAX_YARA_RULES_BYTES: usize = 1 << 20;
pub const MAX_YARA_HITS: usize = 100_000;
pub(crate) fn file_off_of(va: u64, binary: &LoadedBinary) -> Option<u64> {
for sec in &binary.sections {
if sec.file_size == 0 || va < sec.vaddr {
continue;
}
let d = va - sec.vaddr;
if d < sec.file_size {
return sec.file_off.checked_add(d);
}
}
if binary.format == crate::loader::BinaryFormat::Raw {
for seg in &binary.segments {
if va < seg.vaddr {
continue;
}
let d = va - seg.vaddr;
if d < seg.data.len() as u64 {
return Some(d);
}
}
}
None
}
pub(crate) fn section_of(va: u64, binary: &LoadedBinary) -> Option<String> {
for sec in &binary.sections {
if sec.file_size == 0 || va < sec.vaddr {
continue;
}
let d = va - sec.vaddr;
if d < sec.file_size {
return Some(sec.name.clone());
}
}
None
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
pub struct YaraHit {
pub rule: String,
pub pattern: String,
pub addr: u64,
pub file_off: Option<u64>,
pub section: Option<String>,
pub len: usize,
#[serde(default)]
pub func: Option<u64>,
#[serde(default)]
pub func_name: Option<String>,
}
impl serde::Serialize for YaraHit {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut st = serializer.serialize_struct("YaraHit", 9)?;
st.serialize_field("rule", &self.rule)?;
st.serialize_field("pattern", &self.pattern)?;
st.serialize_field("addr", &self.addr)?;
st.serialize_field("addr_hex", &crate::addr_hex(self.addr))?;
st.serialize_field("file_off", &self.file_off)?;
st.serialize_field("section", &self.section)?;
st.serialize_field("len", &self.len)?;
st.serialize_field("func", &self.func)?;
st.serialize_field("func_hex", &self.func.map(crate::addr_hex))?;
st.serialize_field("func_name", &self.func_name)?;
st.end()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RawYaraMatch {
pub rule: String,
pub pattern: String,
pub addr: u64,
pub len: usize,
}
pub(crate) fn read_rules_file(path: &Path) -> Result<String> {
let meta = std::fs::metadata(path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => Error::NotFound(path.display().to_string()),
_ => Error::Io(format!("stat {}: {e}", path.display())),
})?;
if !meta.is_file() {
return Err(Error::Unsupported(format!(
"not a file: {}",
path.display()
)));
}
if meta.len() > MAX_YARA_RULES_BYTES as u64 {
return Err(Error::Limit(format!(
"yara rules of {} bytes exceed the {MAX_YARA_RULES_BYTES}-byte cap",
meta.len()
)));
}
let bytes =
std::fs::read(path).map_err(|e| Error::Io(format!("read {}: {e}", path.display())))?;
if bytes.len() > MAX_YARA_RULES_BYTES {
return Err(Error::Limit(format!(
"yara rules of {} bytes exceed the {MAX_YARA_RULES_BYTES}-byte cap",
bytes.len()
)));
}
String::from_utf8(bytes).map_err(|e| Error::Invalid(format!("yara rules are not UTF-8: {e}")))
}
#[cfg(feature = "yara")]
pub(crate) fn collect_raw(
binary: &LoadedBinary,
rules_text: &str,
rule: Option<&str>,
) -> Result<Vec<RawYaraMatch>> {
let mut compiler = yara_x::Compiler::new();
compiler
.add_source(rules_text)
.map_err(|e| Error::Invalid(format!("yara compile: {e}")))?;
let rules = compiler.build();
if let Some(want) = rule
&& !rules.iter().any(|r| r.identifier() == want)
{
return Err(Error::NotFound(format!("unknown yara rule {want:?}")));
}
let mut seen: BTreeSet<(u64, String, String)> = BTreeSet::new();
let mut out: Vec<RawYaraMatch> = Vec::new();
for seg in &binary.segments {
if seg.data.is_empty() {
continue;
}
let mut scanner = yara_x::Scanner::new(&rules);
let results = scanner.scan(&seg.data).map_err(|e| match e {
yara_x::ScanError::Timeout => Error::Limit("yara scan timeout".to_string()),
other => Error::Invalid(format!("yara scan: {other}")),
})?;
for matched in results.matching_rules() {
let rule_id = matched.identifier().to_string();
if rule.is_some_and(|want| want != rule_id) {
continue;
}
for pat in matched.patterns() {
let pat_id = pat.identifier().to_string();
for m in pat.matches() {
let range = m.range();
let len = range.len();
if len == 0 {
continue;
}
let Some(addr) = seg.vaddr.checked_add(range.start as u64) else {
continue;
};
if out.len() >= MAX_YARA_HITS {
return Err(Error::Limit(format!(
"yara matches exceed the {MAX_YARA_HITS}-hit cap"
)));
}
if seen.insert((addr, rule_id.clone(), pat_id.clone())) {
out.push(RawYaraMatch {
rule: rule_id.clone(),
pattern: pat_id.clone(),
addr,
len,
});
}
}
}
}
}
out.sort_by(|a, b| {
a.addr
.cmp(&b.addr)
.then_with(|| a.rule.cmp(&b.rule))
.then_with(|| a.pattern.cmp(&b.pattern))
});
Ok(out)
}
#[cfg(not(feature = "yara"))]
pub(crate) fn collect_raw(
_binary: &LoadedBinary,
_rules_text: &str,
_rule: Option<&str>,
) -> Result<Vec<RawYaraMatch>> {
Err(Error::Unsupported(
"yara scanning requires the `yara` cargo feature (rebuild with --features yara)"
.to_string(),
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rules_file_rejects_missing_dir_and_oversize_stat() {
assert!(matches!(
read_rules_file(Path::new("/nonexistent-yara-rules.yar")).unwrap_err(),
Error::NotFound(_)
));
assert!(matches!(
read_rules_file(Path::new("/tmp")).unwrap_err(),
Error::Unsupported(_)
));
}
#[test]
fn rules_file_rejects_non_utf8() {
let dir = std::env::temp_dir().join(format!(
"binscout-yara-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("time")
.as_nanos()
));
std::fs::create_dir_all(&dir).expect("temp dir");
let path = dir.join("bad.yar");
std::fs::write(&path, [0xffu8, 0xfe, 0x00]).expect("write");
assert!(matches!(
read_rules_file(&path).unwrap_err(),
Error::Invalid(_)
));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
#[cfg(not(feature = "yara"))]
fn collect_raw_without_feature_is_unsupported() {
let bytes = vec![0x90u8; 16];
let bin = crate::loader::load_raw(
&bytes,
crate::loader::RawOptions {
base: 0x1000,
arch: crate::disasm::Arch::X86_64,
entry: None,
},
)
.expect("raw loads");
assert!(matches!(
collect_raw(&bin, "rule t { condition: true }", None).unwrap_err(),
Error::Unsupported(_)
));
}
}