Skip to content

Commit

Permalink
pe(write): some debug! traces
Browse files Browse the repository at this point in the history
It can be hard to debug why your writer is not working as intended, here
are some `debug!` traces to help in that endeavor.
  • Loading branch information
RaitoBezarius committed Jan 23, 2024
1 parent ac83189 commit 977179f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/pe/certificate_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only
/// https://learn.microsoft.com/en-us/windows/win32/api/wintrust/ns-wintrust-win_certificate
use crate::error;
use crate::pe::debug;
use scroll::{ctx, Pread, Pwrite};

use alloc::string::ToString;
Expand Down Expand Up @@ -132,6 +133,7 @@ impl<'a> AttributeCertificate<'a> {
bytes: &'a [u8],
current_offset: &mut usize,
) -> Result<AttributeCertificate<'a>, error::Error> {
debug!("reading certificate header at {current_offset}");
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further.
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?;
let cert_size = usize::try_from(header.length.saturating_sub(CERTIFICATE_DATA_OFFSET))
Expand All @@ -141,6 +143,11 @@ impl<'a> AttributeCertificate<'a> {
)
})?;

debug!(
"parsing certificate header {:#?}, predicted certificate size: {}",
header, cert_size
);

if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) {
let attr = Self {
length: header.length,
Expand Down
2 changes: 2 additions & 0 deletions src/pe/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl CoffHeader {
let string_table_offset = self.pointer_to_symbol_table as usize
+ symbol::SymbolTable::size(self.number_of_symbol_table as usize);
for i in 0..nsections {
debug!("parsing section at offset {offset}");
let section =
section_table::SectionTable::parse(bytes, offset, string_table_offset as usize)?;
debug!("({}) {:#?}", i, section);
Expand Down Expand Up @@ -342,6 +343,7 @@ impl ctx::TryIntoCtx<scroll::Endian> for Header {
bytes.gwrite_with(self.dos_stub, offset, ctx)?;
bytes.gwrite_with(self.signature, offset, scroll::LE)?;
bytes.gwrite_with(self.coff_header, offset, ctx)?;
debug!("Non-optional header written, current offset: {}", offset);
if let Some(opt_header) = self.optional_header {
bytes.gwrite_with(opt_header, offset, ctx)?;
}
Expand Down
5 changes: 5 additions & 0 deletions src/pe/optional_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::container;
use crate::error;

use crate::pe::data_directories;
use crate::pe::debug;

use scroll::{ctx, Endian, LE};
use scroll::{Pread, Pwrite, SizeWith};
Expand Down Expand Up @@ -358,12 +359,16 @@ impl ctx::TryIntoCtx<scroll::Endian> for OptionalHeader {
match self.standard_fields.magic {
MAGIC_32 => {
bytes.gwrite_with::<StandardFields32>(self.standard_fields.into(), offset, ctx)?;
debug!("Wrote standard fields 32 bits (offset: {})", offset);
bytes.gwrite_with(WindowsFields32::try_from(self.windows_fields)?, offset, ctx)?;
debug!("Wrote windows fields 32 bits (offset: {})", offset);
bytes.gwrite_with(self.data_directories, offset, ctx)?;
}
MAGIC_64 => {
bytes.gwrite_with::<StandardFields64>(self.standard_fields.into(), offset, ctx)?;
debug!("Wrote standard fields 64 bits (offset: {})", offset);
bytes.gwrite_with(self.windows_fields, offset, ctx)?;
debug!("Wrote windows fields 64 bits (offset: {})", offset);
bytes.gwrite_with(self.data_directories, offset, ctx)?;
}
_ => panic!(),
Expand Down

0 comments on commit 977179f

Please sign in to comment.