Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public utils + more examples #116

Merged
merged 11 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ Cargo.lock
*~
*#
*.#*
/cmake-build-debug/
/.idea/
**/*.rs.bk
Binary file added assets/dotnet_executable_example.dll
Binary file not shown.
81 changes: 81 additions & 0 deletions examples/dotnet_pe_analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/// Demonstrates how to read additional metadata (i.e. .Net runtime ones) from PE context

#[macro_use]
extern crate scroll;
extern crate goblin;

use goblin::container::Endian;
use goblin::pe::data_directories::DataDirectory;
use goblin::pe::PE;
use scroll::ctx::TryFromCtx;
use scroll::Pread;
use goblin::pe::utils::get_data;

#[repr(C)]
#[derive(Debug, Pread)]
pub struct CliHeader {
pub cb: u32,
pub major_version: u16,
pub minor_version: u16,
pub metadata: DataDirectory,
pub flags: u32,
pub entry_point_token: u32,
}

#[repr(C)]
#[derive(Debug)]
struct MetadataRoot<'a> {
pub signature: u32,
pub major_version: u16,
pub minor_version: u16,
_reserved: u32,
pub length: u32,
pub version: &'a str,
}

impl<'a> TryFromCtx<'a, Endian> for MetadataRoot<'a> {
type Error = scroll::Error;
type Size = usize;

fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, Self::Size), Self::Error> {
let offset = &mut 0;
let signature = src.gread_with(offset, endian)?;
let major_version = src.gread_with(offset, endian)?;
let minor_version = src.gread_with(offset, endian)?;
let reserved = src.gread_with(offset, endian)?;
let length = src.gread_with(offset, endian)?;
let version = src.gread(offset)?;
Ok((
Self {
signature,
major_version,
minor_version,
_reserved: reserved,
length,
version,
},
*offset,
))
}
}

fn main() {
let file = include_bytes!("../assets/dotnet_executable_example.dll");
let file = &file[..];
let pe = PE::parse(file).unwrap();
if pe.header.coff_header.machine != 0x14c {
panic!("Is not a .Net executable");
}
let optional_header = pe.header.optional_header.expect("No optional header");
let file_alignment = optional_header.windows_fields.file_alignment;
let cli_header = optional_header
.data_directories
.get_clr_runtime_header()
.expect("No CLI header");
let sections = &pe.sections;

let cli_header_value: CliHeader = get_data(file, sections, &cli_header, file_alignment).unwrap();
println!("{:#?}", cli_header_value);
let metadata_root: MetadataRoot = get_data(file, sections, &cli_header_value.metadata, file_alignment).unwrap();
println!("{:#?}", metadata_root);
}
2 changes: 1 addition & 1 deletion src/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod data_directories;
pub mod export;
pub mod import;
pub mod debug;
mod utils;
pub mod utils;

use error;
use container;
Expand Down
12 changes: 11 additions & 1 deletion src/pe/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use scroll::{Pread};
use scroll::{self, Pread};
use alloc::string::ToString;
use error;

use super::section_table;

use core::cmp;
use pe::data_directories::DataDirectory;

pub fn is_in_range (rva: usize, r1: usize, r2: usize) -> bool {
r1 <= rva && rva < r2
Expand Down Expand Up @@ -74,3 +75,12 @@ pub fn try_name<'a>(bytes: &'a [u8], rva: usize, sections: &[section_table::Sect
}
}
}

pub fn get_data<'a, T>(bytes: &'a [u8], sections: &[section_table::SectionTable], directory: &DataDirectory, file_alignment: u32) -> error::Result<T>
where T: scroll::ctx::TryFromCtx<'a, scroll::Endian, Size = usize, Error = scroll::Error> {
let rva = directory.virtual_address as usize;
let offset = find_offset(rva, sections, file_alignment)
.ok_or_else(||error::Error::Malformed(directory.virtual_address.to_string()))?;
let result: T = bytes.pread_with(offset, scroll::LE)?;
Ok(result)
}