#![doc(html_logo_url = "https://raw.githubusercontent.com/0xdea/augur/master/.img/logo.png")]
#[cfg(not(unix))]
compile_error!("only the `unix` target family is currently supported");
use std::fs;
use std::ops::Deref;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Context;
use haruspex::{decompile_to_file, HaruspexError};
use idalib::decompiler::HexRaysErrorCode;
use idalib::func::FunctionFlags;
use idalib::idb::IDB;
use idalib::xref::{XRef, XRefQuery};
use idalib::{Address, IDAError};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
static STRING_MAX_LENGTH: usize = 64;
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct IDAString(String);
impl AsRef<str> for IDAString {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Deref for IDAString {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<String> for IDAString {
fn from(value: String) -> Self {
Self(value)
}
}
impl IDAString {
fn traverse_xrefs(
&self,
idb: &IDB,
xref: &XRef,
addr: Address,
dirpath: &Path,
) -> Result<(), HaruspexError> {
if let Some(f) = idb.function_at(xref.from()) {
if f.flags().contains(FunctionFlags::THUNK) {
return Ok(());
}
let string_name: String = self
.filter_printable_chars()
.replace(['.', '/', ' '], "_")
.chars()
.take(STRING_MAX_LENGTH)
.collect();
let output_dir = format!("{addr:X}_{string_name}");
let func_name = f
.name()
.unwrap_or_else(|| "<no name>".into())
.replace(['.', '/'], "_");
let output_file = format!("{func_name}@{:X}", f.start_address());
let dirpath_sub = dirpath.join(&output_dir);
let output_path = dirpath_sub.join(output_file).with_extension("c");
if !dirpath_sub.exists() {
fs::create_dir(&dirpath_sub)?;
}
decompile_to_file(idb, &f, &output_path)?;
println!("{:#X} in {func_name} -> {output_path:?}", xref.from());
COUNTER.fetch_add(1, Ordering::Relaxed);
} else {
println!("{:#X} in <unknown>", xref.from());
}
xref.next_to().map_or(Ok(()), |next| {
self.traverse_xrefs(idb, &next, addr, dirpath)
})
}
fn filter_printable_chars(&self) -> String {
self.chars()
.filter(|c| c.is_ascii_graphic() || *c == ' ')
.collect()
}
}
pub fn run(filepath: &Path) -> anyhow::Result<usize> {
println!("[*] Trying to analyze binary file {filepath:?}");
if !filepath.is_file() {
return Err(anyhow::anyhow!("invalid file path"));
}
let idb = IDB::open(filepath)?;
println!("[+] Successfully analyzed binary file");
println!();
println!("[-] Processor: {}", idb.processor().long_name(),);
println!("[-] Compiler: {:?}", idb.meta().cc_id());
println!("[-] File type: {:?}", idb.meta().filetype());
println!();
if !idb.decompiler_available() {
return Err(anyhow::anyhow!("decompiler is not available"));
}
let dirpath = filepath.with_extension("str");
println!("[*] Preparing output directory {dirpath:?}");
if dirpath.exists() {
fs::remove_dir(&dirpath).map_err(|_| anyhow::anyhow!("output directory already exists"))?;
}
fs::create_dir_all(&dirpath)?;
println!("[+] Output directory is ready");
println!();
println!("[*] Finding cross-references to strings...");
for i in 0..idb.strings().len() {
let string: IDAString = idb
.strings()
.get_by_index(i)
.context("cannot get string content")?
.into();
let addr = idb
.strings()
.get_address_by_index(i)
.context("cannot get string address")?;
println!("\n{addr:#X} {:?} ", string.as_ref());
idb.first_xref_to(addr, XRefQuery::ALL)
.map_or(Ok::<(), HaruspexError>(()), |xref| {
match string.traverse_xrefs(&idb, &xref, addr, &dirpath) {
Err(HaruspexError::DecompileFailed(IDAError::HexRays(e)))
if e.code() == HexRaysErrorCode::License =>
{
fs::remove_dir_all(&dirpath)?;
Err(IDAError::HexRays(e).into())
}
Err(HaruspexError::DecompileFailed(_)) | Ok(()) => Ok(()),
Err(e) => Err(e),
}
})?;
}
if COUNTER.load(Ordering::Relaxed) == 0 {
fs::remove_dir_all(&dirpath)?;
return Err(anyhow::anyhow!(
"no functions were decompiled, check your input file"
));
}
println!();
println!("[+] Found {COUNTER:?} string usages in functions, decompiled into {dirpath:?}");
println!("[+] Done processing binary file {filepath:?}");
Ok(COUNTER.load(Ordering::Relaxed))
}