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 3 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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ documentation = "https://docs.rs/goblin"
categories = ["parsing", "development-tools::debugging"]
include = ["src/**/*", "Cargo.toml", "CHANGELOG.md", "LICENSE", "README.md", "etc/*", "examples/*", "tests/*", "fuzz/**/*"]

[dev-dependencies]
failure = "0.1.3"
Pzixel marked this conversation as resolved.
Show resolved Hide resolved

[dependencies]
plain = "0.2.3"

Expand Down
Binary file added assets/dotnet_executable_example.dll
Binary file not shown.
89 changes: 89 additions & 0 deletions examples/dotnet_pe_analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/// Demonstrates how to read additional metadata (i.e. .Net runtime ones) from PE context

extern crate scroll;
extern crate goblin;
extern crate failure;

use failure::{bail, err_msg, Error};
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::find_offset;

#[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() -> Result<(), Error> {
let file = include_bytes!("../assets/dotnet_executable_example.dll");
let file = &file[..];
let pe = PE::parse(file)?;
if pe.header.coff_header.machine != 0x14c {
bail!("Is not a .Net executable");
}
let optional_header = pe.header.optional_header.ok_or_else(|| err_msg("No optional header"))?;
let file_alignment = optional_header.windows_fields.file_alignment;
let cli_header = optional_header
.data_directories
.get_cli_header()
.ok_or_else(|| err_msg("No CLI header"))?;
let sections = &pe.sections;

let rva = cli_header.virtual_address as usize;
let offset = find_offset(rva, sections, file_alignment).ok_or_else(|| err_msg("Cannot map rva into offset"))?;
let cli_header_value: CliHeader = file.pread_with(offset, scroll::LE)?;

println!("{:#?}", cli_header_value);
let rva = cli_header_value.metadata.virtual_address as usize;
let offset = find_offset(rva, sections, file_alignment).ok_or_else(|| err_msg("Cannot map rva into offset"))?;
let root: MetadataRoot = file.pread_with(offset, scroll::LE)?;
Pzixel marked this conversation as resolved.
Show resolved Hide resolved
println!("{:#?}", root);

Ok(())
}
2 changes: 1 addition & 1 deletion src/pe/data_directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl DataDirectories {
let idx = 13;
unsafe { self.data_directories.get_unchecked(idx) }
}
pub fn get_clr_runtime_header(&self) -> &Option<DataDirectory> {
pub fn get_cli_header(&self) -> &Option<DataDirectory> {
let idx = 14;
unsafe { self.data_directories.get_unchecked(idx) }
}
Expand Down
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