use eframe::{egui, Frame};
use memmap::Mmap;
use std::fs;
use iced_x86::{Decoder as IcedDecoder, DecoderOptions, Formatter, Instruction as IcedInstruction};
use goblin::{Object, pe, elf};
use std::sync::{Arc, Mutex};
use std::collections::{HashSet, HashMap};
use std::str;
use log::info;
use env_logger;
use rfd::FileDialog;
use std::thread;
use std::fs::File;
use std::io::Read;
use std::path::Path;
let app = ReaverApp::default();
let native_options = eframe::NativeOptions::default();
eframe::run_native(
"Reaver", // Replace with your app name
native_options,
Box::new(|cc| Ok(Box::new(app))),
);
pub fn disassemble(&self) -> Vec<Instruction> {
let mut decoder = IcedDecoder::with_ip(self.bitness, self.code, self.address, DecoderOptions::NONE);
let mut formatter = iced_x86::IntelFormatter::new();
let mut output = String::new();
let mut instructions = Vec::new();
while decoder.can_decode() {
let instr = decoder.decode();
output.clear();
formatter.format(&instr, &mut output);
let instruction = Instruction {
address: instr.ip(),
text: output.clone(),
instr,
};
instructions.push(instruction);
}
instructions
}
}
rust
impl BinaryLoader {
pub fn load(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
use goblin::{Object, pe, elf};
use goblin::pe::section_table::IMAGE_SCN_MEM_EXECUTE;
let file = fs::File::open(path)?;
let mmap = unsafe { Mmap::map(&file)? };
let buffer = &mmap[..];
let object = Object::parse(buffer)?;
match object {
Object::PE(pe) => {
// Process PE file
let image_base = pe.image_base as u64;
let entry_point = image_base + pe.entry as u64;
let mut sections = Vec::new();
for section in &pe.sections {
let name = section.name().unwrap_or_default().to_string();
let addr = image_base + section.virtual_address as u64;
let size = section.virtual_size as u64;
let is_executable = section.characteristics & IMAGE_SCN_MEM_EXECUTE != 0;
let raw_data_ptr = section.pointer_to_raw_data as usize;
let raw_data_size = section.size_of_raw_data as usize;
let data = if raw_data_ptr + raw_data_size <= buffer.len() {
buffer[raw_data_ptr..raw_data_ptr + raw_data_size].to_vec()
} else {
Vec::new()
};
sections.push(BinarySection {
name,
addr,
size,
data,
is_executable,
});
}
let bitness = if pe.is_64 { 64 } else { 32 };
Ok(BinaryLoader {
entry_point,
sections,
bitness,
mmap,
})
}
Object::Elf(elf) => {
// Process ELF file
let entry_point = elf.entry as u64;
let mut sections = Vec::new();
for header in &elf.section_headers {
let name = if let Some(Ok(name)) = elf.shdr_strtab.get(header.sh_name) {
name.to_string()
} else {
"".to_string()
};
let addr = header.sh_addr as u64;
let size = header.sh_size as u64;
let is_executable = header.is_executable();
let offset = header.sh_offset as usize;
let size = header.sh_size as usize;
let data = if offset + size <= buffer.len() {
buffer[offset..offset + size].to_vec()
} else {
Vec::new()
};
sections.push(BinarySection {
name,
addr,
size,
data,
is_executable,
});
}
let bitness = match elf.header.e_ident[elf::header::EI_CLASS] {
elf::header::ELFCLASS64 => 64,
elf::header::ELFCLASS32 => 32,
_ => return Err("Unknown ELF class".into()),
};
Ok(BinaryLoader {
entry_point,
sections,
bitness,
mmap,
})
}
_ => Err("Unsupported binary format".into()),
}
}
}
This implementation of BinaryLoader::load handles both PE (Windows) and ELF (Linux) binaries using the goblin library. It reads the binary file into memory using memory mapping, parses the binary, extracts the entry point, sections, and bitness, and populates the BinaryLoader struct accordingly.
Explanation:
Memory Mapping:
rust
let file = fs::File::open(path)?;
let mmap = unsafe { Mmap::map(&file)? };
let buffer = &mmap[..];
This code opens the binary file and memory-maps it for efficient reading.
Parsing the Binary:
rust
let object = Object::parse(buffer)?;
The goblin library parses the memory-mapped file and determines its format.
Handling PE Files:
Extracts the image base, entry point, and sections.
Each section's name, address, size, executable flag, and data are extracted and stored.
The bitness is determined based on whether it's a 64-bit binary.
Handling ELF Files:
Extracts the entry point and sections.
Each section's name, address, size, executable flag, and data are extracted and stored.
The bitness is determined based on the ELF class in the header.
Unsupported Formats:
rust
_ => Err("Unsupported binary format".into()),
If the binary is neither PE nor ELF, an error is returned.
Note:
The mmap field is stored in the BinaryLoader struct to ensure that the data referenced by the sections remains valid as long as the BinaryLoader exists.
The pe and elf fields were omitted for simplicity since they might introduce lifetime complexities due to references into mmap.
Ensure that the goblin, memmap, and other necessary crates are included in your Cargo.toml.