PE

Struct PE 

Source
pub struct PE<'a> { /* private fields */ }
Expand description

PE struct.

Implementations§

Source§

impl<'a> PE<'a>

Source

pub fn from_path<P: AsRef<Path>>(filename: P) -> Result<Self, PeSignError>

Parse PE struct from disk path.

§Example
use pesign::{PE, structs::{ImageNtHeaders, HDR64_MAGIC}};

let mut image = PE::from_path("test/normal64.exe").unwrap();
let headers = image.get_nt_headers().unwrap();

let magic = match headers {
   ImageNtHeaders::ImageNTHeaders32(hdr32) => hdr32.optional_header.magic,
   ImageNtHeaders::ImageNTHeaders64(hdr64) => hdr64.optional_header.magic,
};

assert_eq!(magic, HDR64_MAGIC);
Source

pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, PeSignError>

Parse PE struct from memory bytes.

§Example
use pesign::{PE, structs::{ImageNtHeaders, HDR64_MAGIC}};

let bytes = std::fs::read("test/normal64.exe").unwrap();

let mut image = PE::from_bytes(&bytes).unwrap();
let headers = image.get_nt_headers().unwrap();

let magic = match headers {
   ImageNtHeaders::ImageNTHeaders32(hdr32) => hdr32.optional_header.magic,
   ImageNtHeaders::ImageNTHeaders64(hdr64) => hdr64.optional_header.magic,
};

assert_eq!(magic, HDR64_MAGIC);
Source

pub fn from_reader( reader: Box<dyn ReadAndSeek + 'a>, ) -> Result<Self, PeSignError>

Parse PE struct from reader.

The reader must implement both the Read and Seek traits.

§Example
use pesign::{PE, structs::{ImageNtHeaders, HDR64_MAGIC}};

let file = Box::new(std::fs::File::open("test/normal64.exe").unwrap());

let mut image = PE::from_reader(file).unwrap();
let headers = image.get_nt_headers().unwrap();

let magic = match headers {
   ImageNtHeaders::ImageNTHeaders32(hdr32) => hdr32.optional_header.magic,
   ImageNtHeaders::ImageNTHeaders64(hdr64) => hdr64.optional_header.magic,
};

assert_eq!(magic, HDR64_MAGIC);
Source

pub fn get_dos_header(&mut self) -> Result<ImageDOSHeader, PeSignError>

Get the DOS header of the PE file.

Source

pub fn e_lfanew(&mut self) -> Result<u32, PeSignError>

Get the offset of NTHeaders within the PE file.

Source

pub fn get_arch(&mut self) -> Result<Arch, PeSignError>

Get the architecture of the PE file.

Source

pub fn get_nt_headers(&mut self) -> Result<ImageNtHeaders, PeSignError>

Get the NT headers of this PE file, inferring from the content of the file which architecture it is.

§Example
use pesign::{PE, structs::{ImageNtHeaders, HDR64_MAGIC}};

let mut image = PE::from_path("test/normal64.exe").unwrap();
let headers = image.get_nt_headers().unwrap();

let magic = match headers {
   ImageNtHeaders::ImageNTHeaders32(hdr32) => hdr32.optional_header.magic,
   ImageNtHeaders::ImageNTHeaders64(hdr64) => hdr64.optional_header.magic,
};

assert_eq!(magic, HDR64_MAGIC);
Source

pub fn get_nt_headers_32(&mut self) -> Result<ImageNTHeaders32, PeSignError>

Get 32-bit NT Headers

Source

pub fn get_nt_headers_64(&mut self) -> Result<ImageNTHeaders64, PeSignError>

Get 64-bit NT Headers

Source

pub fn get_nt_magic(&mut self) -> Result<u16, PeSignError>

Get the NT magic from the optional header of the NT headers.

Source

pub fn get_data_directory_offset(&mut self) -> Result<u64, PeSignError>

Get the offset to the data directory within the PE file.

Source

pub fn get_data_directory_size(&mut self) -> Result<u64, PeSignError>

Get the size of the data directory.

Rounds down number_of_rva_and_sizes to 16, which is what the Windows loader does.

Source

pub fn get_data_directory_table( &mut self, ) -> Result<Vec<ImageDataDirectory>, PeSignError>

Get the data directory table.

Normally one would expect this to be a part of ImageOptionalHeader, but ImageOptionalHeader::number_of_rva_and_sizes controls the size of the array. Therefore, we can’t stick it in the optional header, because that would produce a variable-sized structure, which Rust doesn’t support.

Source

pub fn get_data_directory( &mut self, idx: ImageDirectoryEntry, ) -> Result<ImageDataDirectory, PeSignError>

Get the data directory reference represented by the ImageDirectoryEntry enum. Returns PeSignError if the given directory is inaccessible due to the directory size.

Source

pub fn get_section_table_offset(&mut self) -> Result<u64, PeSignError>

Get the offset to the section table within the PE file.

Source

pub fn get_section_table_size(&mut self) -> Result<u64, PeSignError>

Get the size of the section table within the PE file.

Source

pub fn get_section_table( &mut self, ) -> Result<Vec<ImageSectionHeader>, PeSignError>

Get the section table of the PE file.

Source

pub fn get_header_size(&mut self) -> Result<u64, PeSignError>

Get the size of the header within the PE file.

SizeOfHeaders > dosHeader + ntHeader + dataDirectory + sectionTable.

Source

pub fn get_size(&mut self) -> Result<u64, PeSignError>

Get the PE file size.

Source

pub fn get_security_data(&mut self) -> Result<Option<Vec<u8>>, PeSignError>

Get security data within the PE file.

Source

pub fn calc_authenticode( &mut self, algorithm: Algorithm, ) -> Result<String, PeSignError>

Calculate authenticode of the PE file.

Source

pub unsafe fn cast_c_struct<T: Castable>( &mut self, offset: u64, ) -> Result<T, PeSignError>

Cast c struct bytes to rust struct.

Source

pub unsafe fn cast_c_array<T: Castable>( &mut self, offset: u64, size: u64, ) -> Result<Vec<T>, PeSignError>

Cast c array bytes to rust struct.

Source

pub fn read_exact_at( &mut self, offset: u64, buf: &mut [u8], ) -> Result<(), PeSignError>

Read bytes into a fixed-size buffer.

If the end is reached and there is not enough data to fill the buffer, it will return an Err.

Auto Trait Implementations§

§

impl<'a> Freeze for PE<'a>

§

impl<'a> !RefUnwindSafe for PE<'a>

§

impl<'a> !Send for PE<'a>

§

impl<'a> !Sync for PE<'a>

§

impl<'a> Unpin for PE<'a>

§

impl<'a> !UnwindSafe for PE<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,