| 1 |
//! A PE32 and PE32+ parser |
| 2 |
//! |
| 3 |
|
| 4 |
// TODO: panics with unwrap on None for apisetschema.dll, fhuxgraphics.dll and some others |
| 5 |
|
| 6 |
use core::cmp::max; |
| 7 |
|
| 8 |
use alloc::borrow::Cow; |
| 9 |
use alloc::string::String; |
| 10 |
use alloc::vec::Vec; |
| 11 |
use log::warn; |
| 12 |
|
| 13 |
pub mod authenticode; |
| 14 |
pub mod certificate_table; |
| 15 |
pub mod characteristic; |
| 16 |
pub mod data_directories; |
| 17 |
pub mod debug; |
| 18 |
pub mod dll_characteristic; |
| 19 |
pub mod exception; |
| 20 |
pub mod export; |
| 21 |
pub mod header; |
| 22 |
pub mod import; |
| 23 |
pub mod optional_header; |
| 24 |
pub mod options; |
| 25 |
pub mod relocation; |
| 26 |
pub mod section_table; |
| 27 |
pub mod subsystem; |
| 28 |
pub mod symbol; |
| 29 |
pub mod tls; |
| 30 |
pub mod utils; |
| 31 |
|
| 32 |
use crate::container; |
| 33 |
use crate::error; |
| 34 |
use crate::pe::utils::pad; |
| 35 |
use crate::strtab; |
| 36 |
|
| 37 |
use scroll::{ctx, Pwrite}; |
| 38 |
|
| 39 |
use log::debug; |
| 40 |
|
| 41 |
#[derive(Debug)] |
| 42 |
/// An analyzed PE32/PE32+ binary |
| 43 |
pub struct PE<'a> { |
| 44 |
/// Underlying bytes |
| 45 |
bytes: &'a [u8], |
| 46 |
authenticode_excluded_sections: Option<authenticode::ExcludedSections>, |
| 47 |
/// The PE header |
| 48 |
pub header: header::Header, |
| 49 |
/// A list of the sections in this PE binary |
| 50 |
pub sections: Vec<section_table::SectionTable>, |
| 51 |
/// The size of the binary |
| 52 |
pub size: usize, |
| 53 |
/// The name of this `dll`, if it has one |
| 54 |
pub name: Option<&'a str>, |
| 55 |
/// Whether this is a `dll` or not |
| 56 |
pub is_lib: bool, |
| 57 |
/// Whether the binary is 64-bit (PE32+) |
| 58 |
pub is_64: bool, |
| 59 |
/// the entry point of the binary |
| 60 |
pub entry: usize, |
| 61 |
/// The binary's RVA, or image base - useful for computing virtual addreses |
| 62 |
pub image_base: usize, |
| 63 |
/// Data about any exported symbols in this binary (e.g., if it's a `dll`) |
| 64 |
pub export_data: Option<export::ExportData<'a>>, |
| 65 |
/// Data for any imported symbols, and from which `dll`, etc., in this binary |
| 66 |
pub import_data: Option<import::ImportData<'a>>, |
| 67 |
/// The list of exported symbols in this binary, contains synthetic information for easier analysis |
| 68 |
pub exports: Vec<export::Export<'a>>, |
| 69 |
/// The list symbols imported by this binary from other `dll`s |
| 70 |
pub imports: Vec<import::Import<'a>>, |
| 71 |
/// The list of libraries which this binary imports symbols from |
| 72 |
pub libraries: Vec<&'a str>, |
| 73 |
/// Debug information, if any, contained in the PE header |
| 74 |
pub debug_data: Option<debug::DebugData<'a>>, |
| 75 |
/// TLS information, if any, contained in the PE header |
| 76 |
pub tls_data: Option<tls::TlsData<'a>>, |
| 77 |
/// Exception handling and stack unwind information, if any, contained in the PE header |
| 78 |
pub exception_data: Option<exception::ExceptionData<'a>>, |
| 79 |
/// Certificates present, if any, described by the Certificate Table |
| 80 |
pub certificates: certificate_table::CertificateDirectoryTable<'a>, |
| 81 |
} |
| 82 |
|
| 83 |
impl<'a> PE<'a> { |
| 84 |
/// Reads a PE binary from the underlying `bytes` |
| 85 |
pub fn parse(bytes: &'a [u8]) -> error::Result<Self> { |
| 86 |
Self::parse_with_opts(bytes, &options::ParseOptions::default()) |
| 87 |
} |
| 88 |
|
| 89 |
/// Reads a PE binary from the underlying `bytes` |
| 90 |
pub fn parse_with_opts(bytes: &'a [u8], opts: &options::ParseOptions) -> error::Result<Self> { |
| 91 |
let header = header::Header::parse(bytes)?; |
| 92 |
let mut authenticode_excluded_sections = None; |
| 93 |
|
| 94 |
debug!("{:#?}", header); |
| 95 |
let optional_header_offset = header.dos_header.pe_pointer as usize |
| 96 |
+ header::SIZEOF_PE_MAGIC |
| 97 |
+ header::SIZEOF_COFF_HEADER; |
| 98 |
let offset = |
| 99 |
&mut (optional_header_offset + header.coff_header.size_of_optional_header as usize); |
| 100 |
|
| 101 |
let sections = header.coff_header.sections(bytes, offset)?; |
| 102 |
let is_lib = characteristic::is_dll(header.coff_header.characteristics); |
| 103 |
let mut entry = 0; |
| 104 |
let mut image_base = 0; |
| 105 |
let mut exports = vec![]; |
| 106 |
let mut export_data = None; |
| 107 |
let mut name = None; |
| 108 |
let mut imports = vec![]; |
| 109 |
let mut import_data = None; |
| 110 |
let mut libraries = vec![]; |
| 111 |
let mut debug_data = None; |
| 112 |
let mut tls_data = None; |
| 113 |
let mut exception_data = None; |
| 114 |
let mut certificates = Default::default(); |
| 115 |
let mut is_64 = false; |
| 116 |
if let Some(optional_header) = header.optional_header { |
| 117 |
// Sections we are assembling through the parsing, eventually, it will be passed |
| 118 |
// to the authenticode_sections attribute of `PE`. |
| 119 |
let (checksum, datadir_entry_certtable) = match optional_header.standard_fields.magic { |
| 120 |
optional_header::MAGIC_32 => { |
| 121 |
let standard_field_offset = |
| 122 |
optional_header_offset + optional_header::SIZEOF_STANDARD_FIELDS_32; |
| 123 |
let checksum_field_offset = |
| 124 |
standard_field_offset + optional_header::OFFSET_WINDOWS_FIELDS_32_CHECKSUM; |
| 125 |
( |
| 126 |
checksum_field_offset..checksum_field_offset + 4, |
| 127 |
optional_header_offset + 128..optional_header_offset + 136, |
| 128 |
) |
| 129 |
} |
| 130 |
optional_header::MAGIC_64 => { |
| 131 |
let standard_field_offset = |
| 132 |
optional_header_offset + optional_header::SIZEOF_STANDARD_FIELDS_64; |
| 133 |
let checksum_field_offset = |
| 134 |
standard_field_offset + optional_header::OFFSET_WINDOWS_FIELDS_64_CHECKSUM; |
| 135 |
|
| 136 |
( |
| 137 |
checksum_field_offset..checksum_field_offset + 4, |
| 138 |
optional_header_offset + 144..optional_header_offset + 152, |
| 139 |
) |
| 140 |
} |
| 141 |
magic => { |
| 142 |
return Err(error::Error::Malformed(format!( |
| 143 |
"Unsupported header magic ({:#x})", |
| 144 |
magic |
| 145 |
))) |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
entry = optional_header.standard_fields.address_of_entry_point as usize; |
| 150 |
image_base = optional_header.windows_fields.image_base as usize; |
| 151 |
is_64 = optional_header.container()? == container::Container::Big; |
| 152 |
debug!( |
| 153 |
"entry {:#x} image_base {:#x} is_64: {}", |
| 154 |
entry, image_base, is_64 |
| 155 |
); |
| 156 |
let file_alignment = optional_header.windows_fields.file_alignment; |
| 157 |
if let Some(&export_table) = optional_header.data_directories.get_export_table() { |
| 158 |
if let Ok(ed) = export::ExportData::parse_with_opts( |
| 159 |
bytes, |
| 160 |
export_table, |
| 161 |
§ions, |
| 162 |
file_alignment, |
| 163 |
opts, |
| 164 |
) { |
| 165 |
debug!("export data {:#?}", ed); |
| 166 |
exports = export::Export::parse_with_opts( |
| 167 |
bytes, |
| 168 |
&ed, |
| 169 |
§ions, |
| 170 |
file_alignment, |
| 171 |
opts, |
| 172 |
)?; |
| 173 |
name = ed.name; |
| 174 |
debug!("name: {:#?}", name); |
| 175 |
export_data = Some(ed); |
| 176 |
} |
| 177 |
} |
| 178 |
debug!("exports: {:#?}", exports); |
| 179 |
if let Some(&import_table) = optional_header.data_directories.get_import_table() { |
| 180 |
let id = if is_64 { |
| 181 |
import::ImportData::parse_with_opts::<u64>( |
| 182 |
bytes, |
| 183 |
import_table, |
| 184 |
§ions, |
| 185 |
file_alignment, |
| 186 |
opts, |
| 187 |
)? |
| 188 |
} else { |
| 189 |
import::ImportData::parse_with_opts::<u32>( |
| 190 |
bytes, |
| 191 |
import_table, |
| 192 |
§ions, |
| 193 |
file_alignment, |
| 194 |
opts, |
| 195 |
)? |
| 196 |
}; |
| 197 |
debug!("import data {:#?}", id); |
| 198 |
if is_64 { |
| 199 |
imports = import::Import::parse::<u64>(bytes, &id, §ions)? |
| 200 |
} else { |
| 201 |
imports = import::Import::parse::<u32>(bytes, &id, §ions)? |
| 202 |
} |
| 203 |
libraries = id |
| 204 |
.import_data |
| 205 |
.iter() |
| 206 |
.map(|data| data.name) |
| 207 |
.collect::<Vec<&'a str>>(); |
| 208 |
libraries.sort(); |
| 209 |
libraries.dedup(); |
| 210 |
import_data = Some(id); |
| 211 |
} |
| 212 |
debug!("imports: {:#?}", imports); |
| 213 |
if let Some(&debug_table) = optional_header.data_directories.get_debug_table() { |
| 214 |
debug_data = Some(debug::DebugData::parse_with_opts( |
| 215 |
bytes, |
| 216 |
debug_table, |
| 217 |
§ions, |
| 218 |
file_alignment, |
| 219 |
opts, |
| 220 |
)?); |
| 221 |
} |
| 222 |
|
| 223 |
if let Some(tls_table) = optional_header.data_directories.get_tls_table() { |
| 224 |
tls_data = if is_64 { |
| 225 |
tls::TlsData::parse_with_opts::<u64>( |
| 226 |
bytes, |
| 227 |
image_base, |
| 228 |
tls_table, |
| 229 |
§ions, |
| 230 |
file_alignment, |
| 231 |
opts, |
| 232 |
)? |
| 233 |
} else { |
| 234 |
tls::TlsData::parse_with_opts::<u32>( |
| 235 |
bytes, |
| 236 |
image_base, |
| 237 |
&tls_table, |
| 238 |
§ions, |
| 239 |
file_alignment, |
| 240 |
opts, |
| 241 |
)? |
| 242 |
}; |
| 243 |
debug!("tls data: {:#?}", tls_data); |
| 244 |
} |
| 245 |
|
| 246 |
if header.coff_header.machine == header::COFF_MACHINE_X86_64 { |
| 247 |
// currently only x86_64 is supported |
| 248 |
debug!("exception data: {:#?}", exception_data); |
| 249 |
if let Some(&exception_table) = |
| 250 |
optional_header.data_directories.get_exception_table() |
| 251 |
{ |
| 252 |
exception_data = Some(exception::ExceptionData::parse_with_opts( |
| 253 |
bytes, |
| 254 |
exception_table, |
| 255 |
§ions, |
| 256 |
file_alignment, |
| 257 |
opts, |
| 258 |
)?); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// Parse attribute certificates unless opted out of |
| 263 |
let certificate_table_size = if opts.parse_attribute_certificates { |
| 264 |
if let Some(&certificate_table) = |
| 265 |
optional_header.data_directories.get_certificate_table() |
| 266 |
{ |
| 267 |
certificates = certificate_table::enumerate_certificates( |
| 268 |
bytes, |
| 269 |
certificate_table.virtual_address, |
| 270 |
certificate_table.size, |
| 271 |
)?; |
| 272 |
|
| 273 |
certificate_table.size as usize |
| 274 |
} else { |
| 275 |
0 |
| 276 |
} |
| 277 |
} else { |
| 278 |
0 |
| 279 |
}; |
| 280 |
|
| 281 |
authenticode_excluded_sections = Some(authenticode::ExcludedSections::new( |
| 282 |
checksum, |
| 283 |
datadir_entry_certtable, |
| 284 |
certificate_table_size, |
| 285 |
optional_header.windows_fields.size_of_headers as usize, |
| 286 |
)); |
| 287 |
} |
| 288 |
Ok(PE { |
| 289 |
bytes, |
| 290 |
authenticode_excluded_sections, |
| 291 |
header, |
| 292 |
sections, |
| 293 |
size: 0, |
| 294 |
name, |
| 295 |
is_lib, |
| 296 |
is_64, |
| 297 |
entry, |
| 298 |
image_base, |
| 299 |
export_data, |
| 300 |
import_data, |
| 301 |
exports, |
| 302 |
imports, |
| 303 |
libraries, |
| 304 |
debug_data, |
| 305 |
tls_data, |
| 306 |
exception_data, |
| 307 |
certificates, |
| 308 |
}) |
| 309 |
} |
| 310 |
|
| 311 |
pub fn write_sections( |
| 312 |
&self, |
| 313 |
bytes: &mut [u8], |
| 314 |
offset: &mut usize, |
| 315 |
file_alignment: Option<usize>, |
| 316 |
ctx: scroll::Endian, |
| 317 |
) -> Result<usize, error::Error> { |
| 318 |
// sections table and data |
| 319 |
debug_assert!( |
| 320 |
self.sections |
| 321 |
.iter() |
| 322 |
.flat_map(|section_a| { |
| 323 |
self.sections |
| 324 |
.iter() |
| 325 |
.map(move |section_b| (section_a, section_b)) |
| 326 |
}) |
| 327 |
// given sections = (s_1, …, s_n) |
| 328 |
// for all (s_i, s_j), i != j, verify that s_i does not overlap with s_j and vice versa. |
| 329 |
.all(|(section_i, section_j)| section_i == section_j |
| 330 |
|| !section_i.overlaps_with(section_j)), |
| 331 |
"Overlapping sections were found, this is not supported." |
| 332 |
); |
| 333 |
|
| 334 |
for section in &self.sections { |
| 335 |
let section_data = section.data(&self.bytes)?.ok_or_else(|| { |
| 336 |
error::Error::Malformed(format!( |
| 337 |
"Section data `{}` is malformed", |
| 338 |
section.name().unwrap_or("unknown name") |
| 339 |
)) |
| 340 |
})?; |
| 341 |
let file_section_offset = |
| 342 |
usize::try_from(section.pointer_to_raw_data).map_err(|_| { |
| 343 |
error::Error::Malformed(format!( |
| 344 |
"Section `{}`'s pointer to raw data does not fit in platform `usize`", |
| 345 |
section.name().unwrap_or("unknown name") |
| 346 |
)) |
| 347 |
})?; |
| 348 |
let vsize: usize = section.virtual_size.try_into()?; |
| 349 |
let ondisk_size: usize = section.size_of_raw_data.try_into()?; |
| 350 |
let section_name = String::from(section.name().unwrap_or("unknown name")); |
| 351 |
|
| 352 |
let mut file_offset = file_section_offset; |
| 353 |
// `file_section_offset` is a on-disk offset which can be anywhere in the file. |
| 354 |
// Write section data first to avoid the final consumption. |
| 355 |
match section_data { |
| 356 |
Cow::Borrowed(borrowed) => bytes.gwrite(borrowed, &mut file_offset)?, |
| 357 |
Cow::Owned(owned) => bytes.gwrite(owned.as_slice(), &mut file_offset)?, |
| 358 |
}; |
| 359 |
|
| 360 |
// Section tables follows the header. |
| 361 |
bytes.gwrite_with(section, offset, ctx)?; |
| 362 |
|
| 363 |
// for size size_of_raw_data |
| 364 |
// if < virtual_size, pad with 0 |
| 365 |
// Pad with zeros if necessary |
| 366 |
if file_offset < vsize { |
| 367 |
bytes.gwrite(vec![0u8; vsize - file_offset].as_slice(), &mut file_offset)?; |
| 368 |
} |
| 369 |
|
| 370 |
// Align on a boundary as per file alignement field. |
| 371 |
if let Some(pad) = pad(file_offset - file_section_offset, file_alignment) { |
| 372 |
debug!( |
| 373 |
"aligning `{}` {:#x} -> {:#x} bytes'", |
| 374 |
section_name, |
| 375 |
file_offset - file_section_offset, |
| 376 |
file_offset - file_section_offset + pad.len() |
| 377 |
); |
| 378 |
bytes.gwrite(pad.as_slice(), &mut file_offset)?; |
| 379 |
} |
| 380 |
|
| 381 |
let written_data_size = file_offset - file_section_offset; |
| 382 |
if ondisk_size != written_data_size { |
| 383 |
warn!("Original PE is inefficient or bug (on-disk data size in PE: {:#x}), we wrote {:#x} bytes", |
| 384 |
ondisk_size, |
| 385 |
written_data_size); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
Ok(*offset) |
| 390 |
} |
| 391 |
|
| 392 |
pub fn write_certificates( |
| 393 |
&self, |
| 394 |
bytes: &mut [u8], |
| 395 |
ctx: scroll::Endian, |
| 396 |
) -> Result<usize, error::Error> { |
| 397 |
let opt_header = self |
| 398 |
.header |
| 399 |
.optional_header |
| 400 |
.ok_or(error::Error::Malformed(format!( |
| 401 |
"This PE binary has no optional header; it is required to write certificates" |
| 402 |
)))?; |
| 403 |
let mut max_offset = 0; |
| 404 |
|
| 405 |
if let Some(certificate_directory) = opt_header.data_directories.get_certificate_table() { |
| 406 |
let mut certificate_start = certificate_directory.virtual_address.try_into()?; |
| 407 |
for certificate in &self.certificates { |
| 408 |
bytes.gwrite_with(certificate, &mut certificate_start, ctx)?; |
| 409 |
max_offset = max(certificate_start, max_offset); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
Ok(max_offset) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
impl<'a> ctx::TryIntoCtx<scroll::Endian> for PE<'a> { |
| 418 |
type Error = error::Error; |
| 419 |
|
| 420 |
fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> { |
| 421 |
let mut offset = 0; |
| 422 |
// We need to maintain a `max_offset` because |
| 423 |
// we could be writing sections in the wrong order (i.e. not an increasing order for the |
| 424 |
// pointer on raw disk) |
| 425 |
// and there could be holes between sections. |
| 426 |
// If we don't re-layout sections, we cannot fix that ourselves. |
| 427 |
// Same can be said about the certificate table, there could be a hole between sections |
| 428 |
// and the certificate data. |
| 429 |
// To avoid those troubles, we maintain the max over all offsets we see so far. |
| 430 |
let mut max_offset = 0; |
| 431 |
let file_alignment: Option<usize> = match self.header.optional_header { |
| 432 |
Some(opt_header) => { |
| 433 |
debug_assert!( |
| 434 |
opt_header.windows_fields.file_alignment.count_ones() == 1, |
| 435 |
"file alignment should be a power of 2" |
| 436 |
); |
| 437 |
Some(opt_header.windows_fields.file_alignment.try_into()?) |
| 438 |
} |
| 439 |
_ => None, |
| 440 |
}; |
| 441 |
bytes.gwrite_with(self.header, &mut offset, ctx)?; |
| 442 |
max_offset = max(offset, max_offset); |
| 443 |
self.write_sections(bytes, &mut offset, file_alignment, ctx)?; |
| 444 |
// We want the section offset for which we have the highest pointer on disk. |
| 445 |
// The next offset is reserved for debug tables (outside of sections) and/or certificate |
| 446 |
// tables. |
| 447 |
max_offset = max( |
| 448 |
self.sections |
| 449 |
.iter() |
| 450 |
.max_by_key(|section| section.pointer_to_raw_data as usize) |
| 451 |
.map(|section| (section.pointer_to_raw_data + section.size_of_raw_data) as usize) |
| 452 |
.unwrap_or(offset), |
| 453 |
max_offset, |
| 454 |
); |
| 455 |
|
| 456 |
// COFF Symbol Table |
| 457 |
// Auxiliary Symbol Records |
| 458 |
// COFF String Table |
| 459 |
assert!( |
| 460 |
self.header.coff_header.pointer_to_symbol_table == 0, |
| 461 |
"Symbol tables in PE are deprecated and not supported to write" |
| 462 |
); |
| 463 |
|
| 464 |
// The following data directories are |
| 465 |
// taken care inside a section: |
| 466 |
// - export table (.edata) |
| 467 |
// - import table (.idata) |
| 468 |
// - bound import table |
| 469 |
// - import address table |
| 470 |
// - delay import tables |
| 471 |
// - resource table (.rsrc) |
| 472 |
// - exception table (.pdata) |
| 473 |
// - base relocation table (.reloc) |
| 474 |
// - debug table (.debug) <- this one is special, it can be outside of a |
| 475 |
// section. |
| 476 |
// - load config table |
| 477 |
// - tls table (.tls) |
| 478 |
// - architecture (reserved, 0 for now) |
| 479 |
// - global ptr is a "empty" data directory (header-only) |
| 480 |
// - clr runtime header (.cormeta is object-only) |
| 481 |
// |
| 482 |
// Nonetheless, we need to write the attribute certificate table one. |
| 483 |
max_offset = max(max_offset, self.write_certificates(bytes, ctx)?); |
| 484 |
|
| 485 |
// TODO: we would like to support debug table outside of a section. |
| 486 |
// i.e. debug tables that are never mapped in memory |
| 487 |
// See https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only |
| 488 |
// > The debug directory can be in a discardable .debug section (if one exists), or it can be included in any other section in the image file, or not be in a section at all. |
| 489 |
// In case it's not in a section at all, we need to find a way |
| 490 |
// to rewrite it again. |
| 491 |
// and we need to respect the ordering between attribute certificates |
| 492 |
// and debug table. |
| 493 |
|
| 494 |
Ok(max_offset) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
/// An analyzed TE binary |
| 499 |
/// |
| 500 |
/// A TE binary is a PE/PE32+ binary that has had it's header stripped and |
| 501 |
/// re-formatted to the TE specification. This presents a challenge for |
| 502 |
/// parsing, as all relative addresses (RVAs) are not updated to take this into |
| 503 |
/// account, and are thus incorrect. The parsing of a TE must take this into |
| 504 |
/// account by using the [header::TeHeader::stripped_size`] field of the TE |
| 505 |
/// header to adjust the RVAs during parsing. |
| 506 |
#[cfg(feature = "te")] |
| 507 |
#[derive(Debug)] |
| 508 |
pub struct TE<'a> { |
| 509 |
/// The TE header |
| 510 |
pub header: header::TeHeader, |
| 511 |
/// A list of the sections in this TE binary |
| 512 |
pub sections: Vec<section_table::SectionTable>, |
| 513 |
/// Debug information, contained in the PE header |
| 514 |
pub debug_data: debug::DebugData<'a>, |
| 515 |
/// The offset to apply to addresses not parsed by the TE parser |
| 516 |
/// itself: [header::TeHeader::stripped_size] - size_of::<[header::TeHeader]>() |
| 517 |
pub rva_offset: usize, |
| 518 |
} |
| 519 |
|
| 520 |
#[cfg(feature = "te")] |
| 521 |
impl<'a> TE<'a> { |
| 522 |
/// Reads a TE binary from the underlying `bytes` |
| 523 |
pub fn parse(bytes: &'a [u8]) -> error::Result<Self> { |
| 524 |
let opts = &options::ParseOptions { |
| 525 |
resolve_rva: false, |
| 526 |
parse_attribute_certificates: false, |
| 527 |
}; |
| 528 |
|
| 529 |
let mut offset = 0; |
| 530 |
|
| 531 |
// Parse the TE header and adjust the offsets |
| 532 |
let header = header::TeHeader::parse(bytes, &mut offset)?; |
| 533 |
let rva_offset = header.stripped_size as usize - core::mem::size_of::<header::TeHeader>(); |
| 534 |
|
| 535 |
// Parse the sections and adjust the offsets |
| 536 |
let sections = header.sections(bytes, &mut offset)?; |
| 537 |
|
| 538 |
// Parse the debug data. Must adjust offsets before parsing the image_debug_directory |
| 539 |
let mut debug_data = debug::DebugData::default(); |
| 540 |
debug_data.image_debug_directory = debug::ImageDebugDirectory::parse_with_opts( |
| 541 |
bytes, |
| 542 |
header.debug_dir, |
| 543 |
§ions, |
| 544 |
0, |
| 545 |
opts, |
| 546 |
)?; |
| 547 |
TE::fixup_debug_data(&mut debug_data, rva_offset as u32); |
| 548 |
debug_data.codeview_pdb70_debug_info = debug::CodeviewPDB70DebugInfo::parse_with_opts( |
| 549 |
bytes, |
| 550 |
&debug_data.image_debug_directory, |
| 551 |
opts, |
| 552 |
)?; |
| 553 |
|
| 554 |
Ok(TE { |
| 555 |
header, |
| 556 |
sections, |
| 557 |
debug_data, |
| 558 |
rva_offset, |
| 559 |
}) |
| 560 |
} |
| 561 |
|
| 562 |
/// Adjust all addresses in the TE binary debug data. |
| 563 |
fn fixup_debug_data(dd: &mut debug::DebugData, rva_offset: u32) { |
| 564 |
debug!( |
| 565 |
"ImageDebugDirectory address of raw data fixed up from: 0x{:X} to 0x{:X}", |
| 566 |
dd.image_debug_directory.address_of_raw_data, |
| 567 |
dd.image_debug_directory |
| 568 |
.address_of_raw_data |
| 569 |
.wrapping_sub(rva_offset), |
| 570 |
); |
| 571 |
dd.image_debug_directory.address_of_raw_data = dd |
| 572 |
.image_debug_directory |
| 573 |
.address_of_raw_data |
| 574 |
.wrapping_sub(rva_offset); |
| 575 |
|
| 576 |
debug!( |
| 577 |
"ImageDebugDirectory pointer to raw data fixed up from: 0x{:X} to 0x{:X}", |
| 578 |
dd.image_debug_directory.pointer_to_raw_data, |
| 579 |
dd.image_debug_directory |
| 580 |
.pointer_to_raw_data |
| 581 |
.wrapping_sub(rva_offset), |
| 582 |
); |
| 583 |
dd.image_debug_directory.pointer_to_raw_data = dd |
| 584 |
.image_debug_directory |
| 585 |
.pointer_to_raw_data |
| 586 |
.wrapping_sub(rva_offset); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/// An analyzed COFF object |
| 591 |
#[derive(Debug)] |
| 592 |
pub struct Coff<'a> { |
| 593 |
/// The COFF header |
| 594 |
pub header: header::CoffHeader, |
| 595 |
/// A list of the sections in this COFF binary |
| 596 |
pub sections: Vec<section_table::SectionTable>, |
| 597 |
/// The COFF symbol table, they are not guaranteed to exist. |
| 598 |
/// For an image, this is expected to be None as COFF debugging information |
| 599 |
/// has been deprecated. |
| 600 |
pub symbols: Option<symbol::SymbolTable<'a>>, |
| 601 |
/// The string table, they don't exist if COFF symbol table does not exist. |
| 602 |
pub strings: Option<strtab::Strtab<'a>>, |
| 603 |
} |
| 604 |
|
| 605 |
impl<'a> Coff<'a> { |
| 606 |
/// Reads a COFF object from the underlying `bytes` |
| 607 |
pub fn parse(bytes: &'a [u8]) -> error::Result<Self> { |
| 608 |
let offset = &mut 0; |
| 609 |
let header = header::CoffHeader::parse(bytes, offset)?; |
| 610 |
debug!("{:#?}", header); |
| 611 |
// TODO: maybe parse optional header, but it isn't present for Windows. |
| 612 |
*offset += header.size_of_optional_header as usize; |
| 613 |
let sections = header.sections(bytes, offset)?; |
| 614 |
let symbols = header.symbols(bytes)?; |
| 615 |
let strings = header.strings(bytes)?; |
| 616 |
Ok(Coff { |
| 617 |
header, |
| 618 |
sections, |
| 619 |
symbols, |
| 620 |
strings, |
| 621 |
}) |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
#[cfg(test)] |
| 626 |
mod tests { |
| 627 |
use super::Coff; |
| 628 |
use super::PE; |
| 629 |
|
| 630 |
static INVALID_DOS_SIGNATURE: [u8; 512] = [ |
| 631 |
0x3D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, |
| 632 |
0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 633 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 634 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 635 |
0x00, 0x01, 0x00, 0x00, 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, |
| 636 |
0x4C, 0xCD, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, |
| 637 |
0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, |
| 638 |
0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20, 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A, |
| 639 |
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x31, 0xE2, 0xB1, 0x67, 0x50, 0x8C, |
| 640 |
0xE2, 0x67, 0x50, 0x8C, 0xE2, 0x67, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x88, 0xE3, 0x6D, 0x50, |
| 641 |
0x8C, 0xE2, 0x3C, 0x38, 0x8F, 0xE3, 0x62, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x89, 0xE3, 0xE0, |
| 642 |
0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x89, 0xE3, 0x42, 0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x88, 0xE3, |
| 643 |
0x77, 0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x8F, 0xE3, 0x6E, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x8D, |
| 644 |
0xE3, 0x64, 0x50, 0x8C, 0xE2, 0x67, 0x50, 0x8D, 0xE2, 0x3F, 0x50, 0x8C, 0xE2, 0xE1, 0x20, |
| 645 |
0x85, 0xE3, 0x66, 0x50, 0x8C, 0xE2, 0xE1, 0x20, 0x73, 0xE2, 0x66, 0x50, 0x8C, 0xE2, 0xE1, |
| 646 |
0x20, 0x8E, 0xE3, 0x66, 0x50, 0x8C, 0xE2, 0x52, 0x69, 0x63, 0x68, 0x67, 0x50, 0x8C, 0xE2, |
| 647 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 648 |
0x00, 0x50, 0x45, 0x00, 0x00, 0x64, 0x86, 0x07, 0x00, 0x5F, 0x41, 0xFC, 0x5E, 0x00, 0x00, |
| 649 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x22, 0x00, 0x0B, 0x02, 0x0E, 0x1A, 0x00, |
| 650 |
0xFC, 0x00, 0x00, 0x00, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE4, 0x14, 0x00, 0x00, |
| 651 |
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, |
| 652 |
0x00, 0x00, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, |
| 653 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0xE0, |
| 654 |
0x68, 0x02, 0x00, 0x03, 0x00, 0x60, 0x81, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 655 |
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, |
| 656 |
0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, |
| 657 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xA3, 0x01, 0x00, 0x28, |
| 658 |
0x00, 0x00, 0x00, 0x00, 0xF0, 0x01, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x01, 0x00, |
| 659 |
0x60, 0x0F, 0x00, 0x00, 0x00, 0xC4, 0x01, 0x00, 0xF8, 0x46, 0x00, 0x00, 0x00, 0x00, 0x02, |
| 660 |
0x00, 0x54, 0x06, 0x00, 0x00, 0xF0, 0x91, 0x01, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 661 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 662 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x92, 0x01, 0x00, 0x30, 0x01, 0x00, 0x00, |
| 663 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x48, 0x02, 0x00, |
| 664 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 665 |
0x00, 0x00, |
| 666 |
]; |
| 667 |
|
| 668 |
static INVALID_PE_SIGNATURE: [u8; 512] = [ |
| 669 |
0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, |
| 670 |
0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 671 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 672 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 673 |
0x00, 0x01, 0x00, 0x00, 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, |
| 674 |
0x4C, 0xCD, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, |
| 675 |
0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, |
| 676 |
0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20, 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A, |
| 677 |
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x31, 0xE2, 0xB1, 0x67, 0x50, 0x8C, |
| 678 |
0xE2, 0x67, 0x50, 0x8C, 0xE2, 0x67, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x88, 0xE3, 0x6D, 0x50, |
| 679 |
0x8C, 0xE2, 0x3C, 0x38, 0x8F, 0xE3, 0x62, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x89, 0xE3, 0xE0, |
| 680 |
0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x89, 0xE3, 0x42, 0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x88, 0xE3, |
| 681 |
0x77, 0x50, 0x8C, 0xE2, 0xAC, 0x3F, 0x8F, 0xE3, 0x6E, 0x50, 0x8C, 0xE2, 0x3C, 0x38, 0x8D, |
| 682 |
0xE3, 0x64, 0x50, 0x8C, 0xE2, 0x67, 0x50, 0x8D, 0xE2, 0x3F, 0x50, 0x8C, 0xE2, 0xE1, 0x20, |
| 683 |
0x85, 0xE3, 0x66, 0x50, 0x8C, 0xE2, 0xE1, 0x20, 0x73, 0xE2, 0x66, 0x50, 0x8C, 0xE2, 0xE1, |
| 684 |
0x20, 0x8E, 0xE3, 0x66, 0x50, 0x8C, 0xE2, 0x52, 0x69, 0x63, 0x68, 0x67, 0x50, 0x8C, 0xE2, |
| 685 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 686 |
0x00, 0x50, 0x05, 0x00, 0x00, 0x64, 0x86, 0x07, 0x00, 0x5F, 0x41, 0xFC, 0x5E, 0x00, 0x00, |
| 687 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x22, 0x00, 0x0B, 0x02, 0x0E, 0x1A, 0x00, |
| 688 |
0xFC, 0x00, 0x00, 0x00, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE4, 0x14, 0x00, 0x00, |
| 689 |
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, |
| 690 |
0x00, 0x00, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, |
| 691 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0xE0, |
| 692 |
0x68, 0x02, 0x00, 0x03, 0x00, 0x60, 0x81, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 693 |
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, |
| 694 |
0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, |
| 695 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xA3, 0x01, 0x00, 0x28, |
| 696 |
0x00, 0x00, 0x00, 0x00, 0xF0, 0x01, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x01, 0x00, |
| 697 |
0x60, 0x0F, 0x00, 0x00, 0x00, 0xC4, 0x01, 0x00, 0xF8, 0x46, 0x00, 0x00, 0x00, 0x00, 0x02, |
| 698 |
0x00, 0x54, 0x06, 0x00, 0x00, 0xF0, 0x91, 0x01, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 699 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 700 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x92, 0x01, 0x00, 0x30, 0x01, 0x00, 0x00, |
| 701 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x48, 0x02, 0x00, |
| 702 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 703 |
0x00, 0x00, |
| 704 |
]; |
| 705 |
|
| 706 |
// The assembler program used to generate this string is as follows: |
| 707 |
// |
| 708 |
// bits 64 |
| 709 |
// default rel |
| 710 |
// segment .text |
| 711 |
// global main |
| 712 |
// extern ExitProcess |
| 713 |
// main: |
| 714 |
// xor rax, rax |
| 715 |
// call ExitProcess |
| 716 |
// |
| 717 |
// |
| 718 |
// The code can be compiled using nasm (https://nasm.us) with the command below: |
| 719 |
// nasm -f win64 <filename>.asm -o <filename>.obj |
| 720 |
static COFF_FILE_SINGLE_STRING_IN_STRING_TABLE: [u8; 220] = [ |
| 721 |
0x64, 0x86, 0x1, 0x0, 0xb5, 0x39, 0x91, 0x62, 0x4e, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x0, |
| 722 |
0x0, 0x0, 0x0, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 723 |
0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x3c, 0x0, 0x0, 0x0, 0x44, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 724 |
0x1, 0x0, 0x0, 0x0, 0x20, 0x0, 0x50, 0x60, 0x48, 0x31, 0xc0, 0xe8, 0x0, 0x0, 0x0, 0x0, 0x4, |
| 725 |
0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x4, 0x0, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x0, 0x0, 0x0, |
| 726 |
0x0, 0x0, 0x0, 0x0, 0xfe, 0xff, 0x0, 0x0, 0x67, 0x1, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 727 |
0x73, 0x2e, 0x61, 0x73, 0x6d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2e, 0x74, 0x65, 0x78, |
| 728 |
0x74, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x1, 0x8, 0x0, 0x0, 0x0, |
| 729 |
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2e, 0x61, 0x62, |
| 730 |
0x73, 0x6f, 0x6c, 0x75, 0x74, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, |
| 731 |
0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x6d, 0x61, |
| 732 |
0x69, 0x6e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x10, |
| 733 |
0x0, 0x0, 0x0, 0x45, 0x78, 0x69, 0x74, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x0, |
| 734 |
]; |
| 735 |
|
| 736 |
#[test] |
| 737 |
fn string_table_excludes_length() { |
| 738 |
let coff = Coff::parse(&&COFF_FILE_SINGLE_STRING_IN_STRING_TABLE[..]).unwrap(); |
| 739 |
let string_table = coff.strings.unwrap().to_vec().unwrap(); |
| 740 |
|
| 741 |
assert!(string_table == vec!["ExitProcess"]); |
| 742 |
} |
| 743 |
|
| 744 |
#[test] |
| 745 |
fn symbol_name_excludes_length() { |
| 746 |
let coff = Coff::parse(&COFF_FILE_SINGLE_STRING_IN_STRING_TABLE).unwrap(); |
| 747 |
let strings = coff.strings.unwrap(); |
| 748 |
let symbols = coff |
| 749 |
.symbols |
| 750 |
.unwrap() |
| 751 |
.iter() |
| 752 |
.filter(|(_, name, _)| name.is_none()) |
| 753 |
.map(|(_, _, sym)| sym.name(&strings).unwrap().to_owned()) |
| 754 |
.collect::<Vec<_>>(); |
| 755 |
assert_eq!(symbols, vec!["ExitProcess"]) |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn invalid_dos_header() { |
| 760 |
if let Ok(_) = PE::parse(&INVALID_DOS_SIGNATURE) { |
| 761 |
panic!("must not parse PE with invalid DOS header"); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
#[test] |
| 766 |
fn invalid_pe_header() { |
| 767 |
if let Ok(_) = PE::parse(&INVALID_PE_SIGNATURE) { |
| 768 |
panic!("must not parse PE with invalid PE header"); |
| 769 |
} |
| 770 |
} |
| 771 |
} |