use boxxy::errors::*;
use clap::Parser;
use std::path::PathBuf;
#[derive(Parser)]
struct Args {
#[clap(short = 'x', long)]
hex: bool,
path: PathBuf,
}
#[derive(Debug)]
enum Encoding {
Hex,
Base64,
}
impl Encoding {
fn encode(&self, data: &[u8]) -> String {
match &self {
Encoding::Hex => data
.iter()
.fold(String::new(), |a, b| a + &format!("\\x{:02x}", b)),
Encoding::Base64 => base64::encode(data),
}
}
}
fn main() -> Result<()> {
let args = Args::parse();
let encoding = if args.hex {
Encoding::Hex
} else {
Encoding::Base64
};
let file = std::fs::File::open(&args.path)?;
let mut elf = elf::ElfStream::<elf::endian::AnyEndian, _>::open_stream(&file)?;
let section = *elf
.section_header_by_name(".text")?
.expect("Could not find .text section");
let section_data = match elf.section_data(§ion)? {
(section_data, None) => section_data,
_ => {
panic!("Cannot dump compressed .text section");
}
};
let shellcode = encoding.encode(section_data);
println!("{}", shellcode);
Ok(())
}