Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Apr 9, 2024
1 parent ebe825e commit e752d64
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 443 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ keywords = [
include = ["/Cargo.toml", "/LICENSE", "README.md", "/src/**", "/examples/**"]

[dependencies]
goblin = "0.8.0"
capstone = "0.12.0"
log = { version = "0.4.11", features = ["std"] }
clap = { version = "4.0.29", features = ["derive"] }
colored = "2"
bitflags = "2.4.2"
log = { version = "0.4.11", features = ["std"] }
capstone = "0.12.0"
goblin = "0.8.0"

[lib]
crate-type = ["dylib", "rlib"]
Expand Down
12 changes: 9 additions & 3 deletions examples/rp-rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Args {

/// The verbosity level
#[arg(short, long = "verbose", action = clap::ArgAction::Count)]
verbosity: usize,
verbosity: u8,

/// Unique gadgets
#[arg(short, long, action = ArgAction::SetTrue)]
Expand Down Expand Up @@ -69,7 +69,13 @@ pub struct Args {
fn main() -> GenericResult<()> {
let args = Args::parse();

let verbosity = LevelFilter::Debug; //from(args.verbosity);
let verbosity = match args.verbosity {
1 => LevelFilter::Warn,
2 => LevelFilter::Info,
3 => LevelFilter::Debug,
4 => LevelFilter::Trace,
_ => LevelFilter::Error,
};

let _output = match args.output_file {
None => RopGadgetOutput::Console,
Expand All @@ -80,7 +86,7 @@ fn main() -> GenericResult<()> {
.nb_thread(args.thread_num.into())
.output(_output)
.unique_only(args.unique)
// .verbosity(verbosity)
.verbosity(verbosity)
.use_color(!args.no_color);

info!("Created session: {:?}", sess);
Expand Down
37 changes: 0 additions & 37 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,3 @@ impl std::fmt::Debug for dyn Cpu {
.finish()
}
}

// impl From<&goblin::elf::header::Header> for CpuType {
// fn from(value: &goblin::elf::header::Header) -> Self {
// match value.e_machine {
// goblin::elf::header::EM_386 => CpuType::X86,
// goblin::elf::header::EM_X86_64 => CpuType::X64,
// goblin::elf::header::EM_ARM => CpuType::ARM,
// goblin::elf::header::EM_AARCH64 => CpuType::ARM64,
// _ => panic!("ELF machine format is unsupported"),
// }
// }
// }

// impl From<&goblin::mach::header::Header> for CpuType {
// fn from(value: &goblin::mach::header::Header) -> Self {
// match value.cputype {
// goblin::mach::constants::cputype::CPU_TYPE_X86 => CpuType::X86,
// goblin::mach::constants::cputype::CPU_TYPE_X86_64 => CpuType::X64,
// goblin::mach::constants::cputype::CPU_TYPE_ARM => CpuType::ARM,
// goblin::mach::constants::cputype::CPU_TYPE_ARM64 => CpuType::ARM64,
// _ => panic!("MachO is corrupted"),
// }
// }
// }

// impl From<&goblin::pe::header::CoffHeader> for CpuType {
// fn from(obj: &goblin::pe::header::CoffHeader) -> Self {
// match obj.machine {
// goblin::pe::header::COFF_MACHINE_X86 => CpuType::X86,
// goblin::pe::header::COFF_MACHINE_X86_64 => CpuType::X64,
// goblin::pe::header::COFF_MACHINE_ARM => CpuType::ARM,
// goblin::pe::header::COFF_MACHINE_ARMNT => CpuType::ARM,
// goblin::pe::header::COFF_MACHINE_ARM64 => CpuType::ARM64,
// _ => panic!("Unsupported format"),
// }
// }
// }
1 change: 0 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ impl DisassemblyEngine {
DisassemblyEngineType::Capstone => Self {
disassembler: Box::new(CapstoneDisassembler::new(cpu)),
},
_ => panic!("invalid disassembler"),
}
}
}
Expand Down
201 changes: 7 additions & 194 deletions src/format/elf.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// use colored::Colorize;
// use goblin;
use log::debug;

use std::convert::TryInto;
// use std::fs::File;
// use std::io::{BufReader, Read, Seek, SeekFrom};
use std::mem;
// use std::path::PathBuf;

use crate::common::GenericResult;
use crate::cpu::{self, CpuType};
use crate::cpu::{self};
use crate::error;
use crate::section::Permission;
use crate::{format::FileFormat, section::Section};
Expand Down Expand Up @@ -91,122 +91,6 @@ struct ElfSectionHeader64 {
sh_entsize: u64,
}

// #[derive(Default)]
// pub struct ElfParser<'a> {
// bytes: &'a [u8],
// machine: CpuType,
// number_of_sections: usize,
// section_table_offset: usize,
// entry_point: u64,
// }

// impl<'a> FileFormatParser<'a> for Parser<'a, Elf> {
// fn parse(bytes: &'a [u8]) -> GenericResult<Self> {
// let elf_header: &[u8] = bytes.as_ref();

// match elf_header.get(0..ELF_HEADER_MAGIC.len()) {
// Some(ELF_HEADER_MAGIC) => {}
// _ => return Err(error::Error::InvalidMagicParsingError),
// };

// let is_64b = {
// let ei_class_off = mem::offset_of!(ElfIdentHeader, ei_class);
// match elf_header.get(ei_class_off) {
// Some(val) => match *val {
// ELF_CLASS_32 => false,
// ELF_CLASS_64 => true,
// _ => {
// return Err(error::Error::InvalidFileError);
// }
// },
// None => {
// return Err(error::Error::InvalidFileError);
// }
// }
// };

// let machine = {
// let ei_class_off = mem::offset_of!(ElfHeader64, e_machine);
// let machine = {
// let mut dst = [0u8; 2];
// dst.clone_from_slice(elf_header.get(ei_class_off..ei_class_off + 2).unwrap());
// u16::from_le_bytes(dst)
// };

// match machine {
// ELF_MACHINE_386 => Ok(cpu::CpuType::X86),
// ELF_MACHINE_AMD64 => Ok(cpu::CpuType::X64),
// ELF_MACHINE_ARM => match is_64b {
// true => Ok(cpu::CpuType::ARM64),
// false => Ok(cpu::CpuType::ARM),
// },

// _ => Err(error::Error::UnsupportedCpuError),
// }
// }?;

// let entrypoint = {
// match is_64b {
// true => {
// let e_entry_off = mem::offset_of!(ElfHeader64, e_entry);
// u64::from_le_bytes(elf_header[e_entry_off..e_entry_off + 8].try_into().unwrap())
// }
// false => {
// let e_entry_off = mem::offset_of!(ElfHeader32, e_entry);
// u32::from_le_bytes(elf_header[e_entry_off..e_entry_off + 4].try_into().unwrap())
// as u64
// }
// }
// };

// let number_of_sections = {
// let e_shnum_off = match is_64b {
// true => mem::offset_of!(ElfHeader64, e_shnum),
// false => mem::offset_of!(ElfHeader32, e_shnum),
// };
// u16::from_le_bytes(elf_header[e_shnum_off..e_shnum_off + 2].try_into().unwrap())
// } as usize;

// let section_table_offset = {
// match is_64b {
// true => {
// let e_shoff_off = mem::offset_of!(ElfHeader64, e_shoff);
// u64::from_le_bytes(elf_header[e_shoff_off..e_shoff_off + 8].try_into().unwrap())
// as usize
// }
// false => {
// let e_shoff_off = mem::offset_of!(ElfHeader32, e_shoff);
// u32::from_le_bytes(elf_header[e_shoff_off..e_shoff_off + 4].try_into().unwrap())
// as usize
// }
// }
// };

// Ok(Parser::<Elf> {
// bytes: elf_header,
// machine: machine,
// number_of_sections: number_of_sections,
// entry_point: entrypoint,
// section_table_offset: section_table_offset,
// image_base: 0,
// })
// }

// fn sections(&self) -> GenericResult<Vec<Section>> {
// Ok(ElfSectionIterator {
// index: 0,
// elf: self,
// }
// .into_iter()
// .collect())
// }
// }

// pub struct ElfSectionIterator<'a> {
// index: usize,
// elf: &'a Parser<'a, Elf>,
// }

type ElfSectionIterator<'a> = SectionIterator<'a, Elf>;

pub type ElfCharacteristics = u64;
Expand Down Expand Up @@ -240,7 +124,7 @@ impl<'a> Iterator for ElfSectionIterator<'a> {
u64::from_le_bytes(current_section[0x18..0x20].try_into().unwrap()) as usize;

Some(Section {
start_address: start_address,
start_address,
end_address: start_address.checked_add(section_size as u64)?,
name: Some(section_name),
permission: Permission::from(flags),
Expand All @@ -261,81 +145,11 @@ pub struct Elf {
number_of_sections: usize,
section_table_offset: usize,
entry_point: u64,
image_base: u64,
// image_base: u64,
}

impl Elf {
pub fn new(bytes: Vec<u8>) -> GenericResult<Self> {
// let filepath = path.to_str().unwrap();
// let elf = Parser::<Elf>::parse(buf)?;
// let executable_sections = elf
// .sections()?
// .into_iter()
// .filter(|s| s.is_executable())
// .collect();
// debug!("{:?}", &executable_sections);
// debug!(
// "looking for executable sections in ELF: '{}'",
// filepath.bold()
// );

// let file = File::open(&path).unwrap();
// let mut reader = BufReader::new(file);

// for current_section in &obj.section_headers {
// trace!("Testing section {:?}", s);

// //
// // disregard non executable section
// //
// if !s.is_executable() {
// continue;
// }

// debug!("Importing section {:?}", s);

// let mut section = Section::from(s);
// section.name = Some(String::from(&obj.shdr_strtab[s.sh_name]));

// let mut sect =
// Section::from(current_section).name(&obj.shdr_strtab[current_section.sh_name]);

// if !sect.permission.contains(Permission::EXECUTABLE) {
// continue;
// }

// if reader
// .seek(SeekFrom::Start(current_section.sh_addr))
// .is_err()
// {
// panic!("Invalid offset {}", current_section.sh_addr,)
// }

// match reader.read_exact(&mut sect.data) {
// Ok(_) => {}
// Err(e) => panic!(
// "Failed to extract section '{}' (size={:#x}) at offset {:#x}: {:?}",
// &sect.name.clone().unwrap_or_default(),
// &sect.size(),
// sect.start_address,
// e
// ),
// };

// debug!("Adding {}", sect);
// executable_sections.push(sect);
// }

// let cpu_type = match obj.header.e_machine {
// goblin::elf::header::EM_386 => cpu::CpuType::X86,
// goblin::elf::header::EM_X86_64 => cpu::CpuType::X64,
// goblin::elf::header::EM_ARM => cpu::CpuType::ARM,
// goblin::elf::header::EM_AARCH64 => cpu::CpuType::ARM64,
// _ => {
// panic!("ELF machine format is unsupported")
// }
// };

let elf_header: &[u8] = bytes.as_ref();

match elf_header.get(0..ELF_HEADER_MAGIC.len()) {
Expand Down Expand Up @@ -425,7 +239,7 @@ impl Elf {
cpu_type: machine,
number_of_sections,
section_table_offset,
image_base: 0,
// image_base: 0,
entry_point: entrypoint,
})
}
Expand All @@ -442,16 +256,15 @@ impl ExecutableFileFormat for Elf {
// &self.path
// }

fn format(&self) -> FileFormat {
FileFormat::Elf
fn format(&self) -> &str {
"ELF"
}

fn executable_sections(&self) -> Vec<Section> {
ElfSectionIterator {
index: 0,
obj: self,
}
.into_iter()
.collect()
}

Expand Down
13 changes: 0 additions & 13 deletions src/format/mach.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// use std::fs::File;
// use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::PathBuf;

use colored::Colorize;
use goblin;
use log::debug;

use crate::cpu;
use crate::{format::FileFormat, section::Permission, section::Section};

use super::ExecutableFileFormat;

pub const MACHO_HEADER_MAGIC32: &[u8] = b"\xce\xfa\xed\xfe"; // 0xfeedface
pub const MACHO_HEADER_MAGIC64: &[u8] = b"\xcf\xfa\xed\xfe"; // 0xfeedfacf

Expand Down
Loading

0 comments on commit e752d64

Please sign in to comment.