use anyhow::{Context, Result};
use clap::Parser;
use std::io::{Read, Write};
use std::path::PathBuf;
use rustrip::analyzers::{
panics::PanicsAnalyzer, strings::StringsAnalyzer, symbols::SymbolsAnalyzer, Registry,
};
use rustrip::output::{
binja::Binja, ghidra::Ghidra, json::Json, table::Table, Format, OutputBackend,
};
#[derive(Parser, Debug)]
#[command(
name = "rustrip",
version,
about = "Make stripped Rust binaries readable again",
long_about = "Make stripped Rust binaries readable again.\n\n\
rustrip recovers `&str` slice boundaries, demangles Rust symbols, \
and reconstructs panic site source locations from release/stripped \
ELF, PE, and Mach-O binaries. Outputs to a table, JSON, or a Ghidra/\
Binary Ninja Python script that re-applies annotations."
)]
struct Cli {
path: PathBuf,
#[arg(long, short = 'f', default_value = "table")]
format: String,
#[arg(long, short = 'o')]
output: Option<PathBuf>,
#[arg(long)]
no_strings: bool,
#[arg(long)]
no_symbols: bool,
#[arg(long)]
no_panics: bool,
#[arg(long, default_value_t = 4096)]
max_string_len: usize,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let bytes = read_bytes(&cli.path)?;
let bin = rustrip::binary::Binary::parse(cli.path.to_str(), bytes).context("loading binary")?;
let mut registry = Registry::new();
if !cli.no_strings {
let lim = rustrip::analyzers::Limits {
max_string_len: cli.max_string_len,
..rustrip::analyzers::Limits::default()
};
registry = registry.with(Box::new(StringsAnalyzer::with_limits(lim)));
}
if !cli.no_symbols {
registry = registry.with(Box::new(SymbolsAnalyzer::new()));
}
if !cli.no_panics {
registry = registry.with(Box::new(PanicsAnalyzer::new()));
}
let anns = registry.run(&bin);
let fmt =
Format::parse(&cli.format).with_context(|| format!("unknown --format '{}'", cli.format))?;
let backend: Box<dyn OutputBackend> = match fmt {
Format::Table => Box::new(Table),
Format::Json => Box::new(Json),
Format::GhidraScript => Box::new(Ghidra),
Format::BinjaScript => Box::new(Binja),
};
let mut sink: Box<dyn Write> = match cli.output.as_ref() {
Some(p) => {
Box::new(std::fs::File::create(p).with_context(|| format!("creating {}", p.display()))?)
}
None => Box::new(std::io::stdout().lock()),
};
backend.render(&anns, &mut *sink)?;
Ok(())
}
fn read_bytes(path: &PathBuf) -> Result<Vec<u8>> {
let mut buf = Vec::new();
if path.to_str() == Some("-") {
std::io::stdin().read_to_end(&mut buf)?;
} else {
let mut f =
std::fs::File::open(path).with_context(|| format!("opening {}", path.display()))?;
f.read_to_end(&mut buf)?;
if buf.len() < 4 {
anyhow::bail!("binary too small: {} bytes", buf.len());
}
}
Ok(buf)
}