Skip to content

Commit 03eb434

Browse files
suttonbradleySutton Bradley
andauthored
pe: Add ParseOptions field to toggle parsing Attribute Certificates for PEs loaded into memory (#377)
* Add in_memory feature to prevent parsing attribute certificates (not pulled into memory by loader) --------- Co-authored-by: Sutton Bradley <suttonb@microsoft.com>
1 parent 5bddac6 commit 03eb434

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/pe/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,23 @@ impl<'a> PE<'a> {
222222
}
223223
}
224224

225-
let certtable = if let Some(certificate_table) =
226-
*optional_header.data_directories.get_certificate_table()
227-
{
228-
certificates = certificate_table::enumerate_certificates(
229-
bytes,
230-
certificate_table.virtual_address,
231-
certificate_table.size,
232-
)?;
225+
// Parse attribute certificates unless opted out of
226+
let certtable = if opts.parse_attribute_certificates {
227+
if let Some(certificate_table) =
228+
*optional_header.data_directories.get_certificate_table()
229+
{
230+
certificates = certificate_table::enumerate_certificates(
231+
bytes,
232+
certificate_table.virtual_address,
233+
certificate_table.size,
234+
)?;
233235

234-
let start = certificate_table.virtual_address as usize;
235-
let end = start + certificate_table.size as usize;
236-
Some(start..end)
236+
let start = certificate_table.virtual_address as usize;
237+
let end = start + certificate_table.size as usize;
238+
Some(start..end)
239+
} else {
240+
None
241+
}
237242
} else {
238243
None
239244
};

src/pe/options.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
pub struct ParseOptions {
44
/// Wether the parser should resolve rvas or not. Default: true
55
pub resolve_rva: bool,
6+
/// Whether or not to parse attribute certificates.
7+
/// Set to false for in-memory representation, as the [loader does not map this info into
8+
/// memory](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#other-contents-of-the-file).
9+
/// For on-disk representations, leave as true. Default: true
10+
pub parse_attribute_certificates: bool,
611
}
712

813
impl ParseOptions {
914
/// Returns a parse options structure with default values
1015
pub fn default() -> Self {
11-
ParseOptions { resolve_rva: true }
16+
ParseOptions {
17+
resolve_rva: true,
18+
parse_attribute_certificates: true,
19+
}
1220
}
1321
}

0 commit comments

Comments
 (0)