1//! PE rich header handling
23use core::mem;
45use crate::endian::{LittleEndian as LE, U32};
6use crate::pe;
7use crate::pod::bytes_of_slice;
8use crate::read::{Bytes, ReadRef};
910/// Parsed information about a Rich Header.
11#[derive(Debug, Clone, Copy)]
12pub struct RichHeaderInfo<'data> {
13/// The offset at which the rich header starts.
14pub offset: usize,
15/// The length (in bytes) of the rich header.
16 ///
17 /// This includes the payload, but also the 16-byte start sequence and the
18 /// 8-byte final "Rich" and XOR key.
19pub length: usize,
20/// The XOR key used to mask the rich header.
21 ///
22 /// Unless the file has been tampered with, it should be equal to a checksum
23 /// of the file header.
24pub xor_key: u32,
25 masked_entries: &'data [pe::MaskedRichHeaderEntry],
26}
2728/// A PE rich header entry after it has been unmasked.
29///
30/// See [`pe::MaskedRichHeaderEntry`].
31#[derive(Debug, Clone, Copy)]
32#[repr(C)]
33pub struct RichHeaderEntry {
34/// ID of the component.
35pub comp_id: u32,
36/// Number of times this component has been used when building this PE.
37pub count: u32,
38}
3940impl<'data> RichHeaderInfo<'data> {
41/// Try to locate a rich header and its entries in the current PE file.
42pub fn parse<R: ReadRef<'data>>(data: R, nt_header_offset: u64) -> Option<Self> {
43// Locate the rich header, if any.
44 // It ends with the "Rich" string and an XOR key, before the NT header.
45let data = data.read_bytes_at(0, nt_header_offset).map(Bytes).ok()?;
46let end_marker_offset = memmem(data.0, b"Rich", 4)?;
47let xor_key = *data.read_at::<U32<LE>>(end_marker_offset + 4).ok()?;
4849// It starts at the masked "DanS" string and 3 masked zeroes.
50let masked_start_marker = U32::new(LE, 0x536e_6144 ^ xor_key.get(LE));
51let start_header = [masked_start_marker, xor_key, xor_key, xor_key];
52let start_sequence = bytes_of_slice(&start_header);
53let start_marker_offset = memmem(&data.0[..end_marker_offset], start_sequence, 4)?;
5455// Extract the items between the markers.
56let items_offset = start_marker_offset + start_sequence.len();
57let items_len = end_marker_offset - items_offset;
58let item_count = items_len / mem::size_of::<pe::MaskedRichHeaderEntry>();
59let items = data.read_slice_at(items_offset, item_count).ok()?;
60Some(RichHeaderInfo {
61 offset: start_marker_offset,
62// Includes "Rich" marker and the XOR key.
63length: end_marker_offset - start_marker_offset + 8,
64 xor_key: xor_key.get(LE),
65 masked_entries: items,
66 })
67 }
6869/// Returns an iterator over the unmasked entries.
70pub fn unmasked_entries(&self) -> impl Iterator<Item = RichHeaderEntry> + 'data {
71let xor_key = self.xor_key;
72self.masked_entries
73 .iter()
74 .map(move |entry| RichHeaderEntry {
75 comp_id: entry.masked_comp_id.get(LE) ^ xor_key,
76 count: entry.masked_count.get(LE) ^ xor_key,
77 })
78 }
79}
8081/// Find the offset of the first occurrence of needle in the data.
82///
83/// The offset must have the given alignment.
84fn memmem(data: &[u8], needle: &[u8], align: usize) -> Option<usize> {
85let mut offset = 0;
86loop {
87if data.get(offset..)?.get(..needle.len())? == needle {
88return Some(offset);
89 }
90 offset += align;
91 }
92}