hexspell 1.0.0

Dependency-free Rust library for parsing and patching PE, ELF, and Mach-O executables with a 1:1 on-disk API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Definitions for the various headers that make up a PE file.
//!
//! The structs in this module map directly onto the layout described in
//! Microsoft's PE/COFF specification. Each numeric field is represented
//! as a [`Field`] so it can be patched without
//! recalculating offsets manually.

use core::fmt;

use crate::errors::FileParseError;
use crate::field::Field;
use crate::utils::{extract_u16, extract_u32, extract_u64};

/// PE optional header magic: `0x10B` (PE32).
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum PEType {
    /// 32-bit optional header (`IMAGE_NT_OPTIONAL_HDR32_MAGIC`).
    PE32,
    /// 64-bit optional header (`IMAGE_NT_OPTIONAL_HDR64_MAGIC`).
    PE32Plus,
}

/// Preferred load address as stored in the optional header (4 or 8 bytes on disk).
#[derive(Clone, Copy)]
pub enum ImageBase {
    /// PE32 image base (`u32`).
    Base32(u32),
    /// PE32+ image base (`u64`).
    Base64(u64),
}

/// Stack/heap reserve or commit size (4 bytes in PE32, 8 bytes in PE32+).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SizedU64 {
    /// PE32 on-disk width (`u32`).
    U32(u32),
    /// PE32+ on-disk width (`u64`).
    U64(u64),
}

/// `IMAGE_DATA_DIRECTORY` — RVA and size of a data table.
pub struct DataDirectoryEntry {
    /// RVA of the directory (`VirtualAddress`).
    pub virtual_address: Field<u32>,
    /// Size of the directory in bytes.
    pub size: Field<u32>,
}

/// Data directory index (`IMAGE_DIRECTORY_ENTRY_*`).
pub const EXPORT: usize = 0;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_IMPORT`).
pub const IMPORT: usize = 1;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_RESOURCE`).
pub const RESOURCE: usize = 2;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_EXCEPTION`).
pub const EXCEPTION: usize = 3;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_SECURITY`).
pub const SECURITY: usize = 4;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_BASERELOC`).
pub const BASERELOC: usize = 5;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_DEBUG`).
pub const DEBUG: usize = 6;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_ARCHITECTURE`).
pub const ARCHITECTURE: usize = 7;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_GLOBALPTR`).
pub const GLOBAL_PTR: usize = 8;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_TLS`).
pub const TLS: usize = 9;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG`).
pub const LOAD_CONFIG: usize = 10;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT`).
pub const BOUND_IMPORT: usize = 11;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_IAT`).
pub const IAT: usize = 12;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT`).
pub const DELAY_IMPORT: usize = 13;
/// Data directory index (`IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR`).
pub const COM_DESCRIPTOR: usize = 14;

/// Target CPU derived from the COFF `machine` field.
pub enum Architecture {
    /// `IMAGE_FILE_MACHINE_I386` (`0x014c`).
    X86,
    /// `IMAGE_FILE_MACHINE_AMD64` (`0x8664`).
    X64,
    /// `IMAGE_FILE_MACHINE_ARMNT` (`0x01c4`) — ARM32 / CHPE.
    Armnt,
    /// `IMAGE_FILE_MACHINE_ARM64` (`0xAA64`).
    Arm64,
    /// `IMAGE_FILE_MACHINE_ARM64X` (`0xA64E`) — ARM64X hybrid.
    Arm64x,
    /// Any other machine value.
    Unknown,
}

impl fmt::Display for Architecture {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match *self {
            Architecture::X86 => "x86",
            Architecture::X64 => "x64",
            Architecture::Armnt => "armnt",
            Architecture::Arm64 => "arm64",
            Architecture::Arm64x => "arm64x",
            Architecture::Unknown => "Unknown",
        };
        write!(f, "{s}")
    }
}

impl Architecture {
    /// Maps a COFF `machine` constant to [`Architecture`].
    pub fn from_u16(value: u16) -> Self {
        match value {
            0x014c => Architecture::X86,
            0x8664 => Architecture::X64,
            0x01c4 => Architecture::Armnt,
            0xAA64 => Architecture::Arm64,
            0xA64E => Architecture::Arm64x,
            _ => Architecture::Unknown,
        }
    }
}

/// Optional header fields (PE32 / PE32+).
///
/// Field offsets follow the Microsoft PE/COFF specification. `base_of_data` is present only for
/// PE32; `image_base` is 4 bytes wide for PE32 and 8 bytes for PE32+.
pub struct OptionalHeader {
    /// `Magic` — `0x10B` (PE32) or `0x20B` (PE32+).
    pub magic: Field<u16>,
    /// Linker major version.
    pub major_linker_version: Field<u8>,
    /// Linker minor version.
    pub minor_linker_version: Field<u8>,
    /// Combined size of all code sections.
    pub size_of_code: Field<u32>,
    /// Combined size of initialized data sections.
    pub size_of_initialized_data: Field<u32>,
    /// Combined size of uninitialized data sections.
    pub size_of_uninitialized_data: Field<u32>,
    /// Relative virtual address of the entry point.
    pub entry_point: Field<u32>,
    /// RVA of the start of the code section.
    pub base_of_code: Field<u32>,
    /// RVA of the start of the data section (PE32 only).
    pub base_of_data: Option<Field<u32>>,
    /// Preferred image load address.
    pub image_base: Field<ImageBase>,
    /// Section alignment in memory.
    pub section_alignment: Field<u32>,
    /// File alignment for raw section data.
    pub file_alignment: Field<u32>,
    /// Required OS major version.
    pub major_operating_system_version: Field<u16>,
    /// Required OS minor version.
    pub minor_operating_system_version: Field<u16>,
    /// Image major version.
    pub major_image_version: Field<u16>,
    /// Image minor version.
    pub minor_image_version: Field<u16>,
    /// Subsystem major version.
    pub major_subsystem_version: Field<u16>,
    /// Subsystem minor version.
    pub minor_subsystem_version: Field<u16>,
    /// Reserved; must be zero.
    pub win32_version_value: Field<u32>,
    /// Size of the image in memory.
    pub size_of_image: Field<u32>,
    /// Combined size of headers and section table.
    pub size_of_headers: Field<u32>,
    /// PE checksum (see [`crate::pe::PE::calc_checksum`]).
    pub checksum: Field<u32>,
    /// Subsystem (`IMAGE_SUBSYSTEM_*`).
    pub subsystem: Field<u16>,
    /// DLL characteristics flags.
    pub dll_characteristics: Field<u16>,
    /// Default stack reserve (4 or 8 bytes on disk).
    pub size_of_stack_reserve: Field<SizedU64>,
    /// Default stack commit (4 or 8 bytes on disk).
    pub size_of_stack_commit: Field<SizedU64>,
    /// Default heap reserve (4 or 8 bytes on disk).
    pub size_of_heap_reserve: Field<SizedU64>,
    /// Default heap commit (4 or 8 bytes on disk).
    pub size_of_heap_commit: Field<SizedU64>,
    /// Loader flags; must be zero.
    pub loader_flags: Field<u32>,
    /// Number of data directory entries (typically 16).
    pub number_of_rva_and_sizes: Field<u32>,
    /// Data directory table (`IMAGE_DATA_DIRECTORY[16]`).
    pub data_directories: [DataDirectoryEntry; 16],
}

impl DataDirectoryEntry {
    /// Parses an `IMAGE_DATA_DIRECTORY` at `offset`.
    pub fn parse(buffer: &[u8], offset: usize) -> Result<Self, FileParseError> {
        Ok(DataDirectoryEntry {
            virtual_address: Field::new(extract_u32(buffer, offset)?, offset, 4),
            size: Field::new(extract_u32(buffer, offset + 4)?, offset + 4, 4),
        })
    }
}

impl OptionalHeader {
    /// Parses the optional header at `offset` (PE32 or PE32+).
    pub fn parse(buffer: &[u8], offset: usize) -> Result<Self, FileParseError> {
        let magic = extract_u16(buffer, offset)?;
        let pe_type = match magic {
            0x10B => PEType::PE32,
            0x20B => PEType::PE32Plus,
            _ => return Err(FileParseError::InvalidFileFormat),
        };

        let extract_u8 = |buf: &[u8], off: usize| -> Result<u8, FileParseError> {
            buf.get(off).copied().ok_or(FileParseError::BufferOverflow)
        };

        let parse_sized_u64 =
            |buf: &[u8], off: usize, size: usize| -> Result<SizedU64, FileParseError> {
                match size {
                    4 => Ok(SizedU64::U32(extract_u32(buf, off)?)),
                    8 => Ok(SizedU64::U64(extract_u64(buf, off)?)),
                    _ => Err(FileParseError::InvalidFileFormat),
                }
            };

        let (
            image_base,
            base_of_data,
            stack_size,
            heap_size,
            loader_flags_off,
            number_of_rva_off,
            data_dirs_off,
        ) = match pe_type {
            PEType::PE32 => (
                Field::new(
                    ImageBase::Base32(extract_u32(buffer, offset + 28)?),
                    offset + 28,
                    4,
                ),
                Some(Field::new(
                    extract_u32(buffer, offset + 24)?,
                    offset + 24,
                    4,
                )),
                4usize,
                4usize,
                offset + 88,
                offset + 92,
                offset + 96,
            ),
            PEType::PE32Plus => (
                Field::new(
                    ImageBase::Base64(extract_u64(buffer, offset + 24)?),
                    offset + 24,
                    8,
                ),
                None,
                8usize,
                8usize,
                offset + 104,
                offset + 108,
                offset + 112,
            ),
        };

        let stack_reserve_off = offset + 72;
        let stack_commit_off = stack_reserve_off + stack_size;
        let heap_reserve_off = stack_commit_off + stack_size;
        let heap_commit_off = heap_reserve_off + heap_size;

        let number_of_rva_and_sizes = extract_u32(buffer, number_of_rva_off)?;
        let active_directories = (number_of_rva_and_sizes as usize).min(16);
        let min_end = data_dirs_off + active_directories * 8;
        if buffer.len() < min_end {
            return Err(FileParseError::BufferOverflow);
        }

        let mut data_directories: [DataDirectoryEntry; 16] =
            std::array::from_fn(|i| DataDirectoryEntry {
                virtual_address: Field::new(0, data_dirs_off + i * 8, 4),
                size: Field::new(0, data_dirs_off + i * 8 + 4, 4),
            });
        for (i, dir) in data_directories
            .iter_mut()
            .enumerate()
            .take(active_directories)
        {
            *dir = DataDirectoryEntry::parse(buffer, data_dirs_off + i * 8)?;
        }

        Ok(OptionalHeader {
            magic: Field::new(magic, offset, 2),
            major_linker_version: Field::new(extract_u8(buffer, offset + 2)?, offset + 2, 1),
            minor_linker_version: Field::new(extract_u8(buffer, offset + 3)?, offset + 3, 1),
            size_of_code: Field::new(extract_u32(buffer, offset + 4)?, offset + 4, 4),
            size_of_initialized_data: Field::new(extract_u32(buffer, offset + 8)?, offset + 8, 4),
            size_of_uninitialized_data: Field::new(
                extract_u32(buffer, offset + 12)?,
                offset + 12,
                4,
            ),
            entry_point: Field::new(extract_u32(buffer, offset + 16)?, offset + 16, 4),
            base_of_code: Field::new(extract_u32(buffer, offset + 20)?, offset + 20, 4),
            base_of_data,
            image_base,
            section_alignment: Field::new(extract_u32(buffer, offset + 32)?, offset + 32, 4),
            file_alignment: Field::new(extract_u32(buffer, offset + 36)?, offset + 36, 4),
            major_operating_system_version: Field::new(
                extract_u16(buffer, offset + 40)?,
                offset + 40,
                2,
            ),
            minor_operating_system_version: Field::new(
                extract_u16(buffer, offset + 42)?,
                offset + 42,
                2,
            ),
            major_image_version: Field::new(extract_u16(buffer, offset + 44)?, offset + 44, 2),
            minor_image_version: Field::new(extract_u16(buffer, offset + 46)?, offset + 46, 2),
            major_subsystem_version: Field::new(extract_u16(buffer, offset + 48)?, offset + 48, 2),
            minor_subsystem_version: Field::new(extract_u16(buffer, offset + 50)?, offset + 50, 2),
            win32_version_value: Field::new(extract_u32(buffer, offset + 52)?, offset + 52, 4),
            size_of_image: Field::new(extract_u32(buffer, offset + 56)?, offset + 56, 4),
            size_of_headers: Field::new(extract_u32(buffer, offset + 60)?, offset + 60, 4),
            checksum: Field::new(extract_u32(buffer, offset + 64)?, offset + 64, 4),
            subsystem: Field::new(extract_u16(buffer, offset + 68)?, offset + 68, 2),
            dll_characteristics: Field::new(extract_u16(buffer, offset + 70)?, offset + 70, 2),
            size_of_stack_reserve: Field::new(
                parse_sized_u64(buffer, stack_reserve_off, stack_size)?,
                stack_reserve_off,
                stack_size,
            ),
            size_of_stack_commit: Field::new(
                parse_sized_u64(buffer, stack_commit_off, stack_size)?,
                stack_commit_off,
                stack_size,
            ),
            size_of_heap_reserve: Field::new(
                parse_sized_u64(buffer, heap_reserve_off, heap_size)?,
                heap_reserve_off,
                heap_size,
            ),
            size_of_heap_commit: Field::new(
                parse_sized_u64(buffer, heap_commit_off, heap_size)?,
                heap_commit_off,
                heap_size,
            ),
            loader_flags: Field::new(extract_u32(buffer, loader_flags_off)?, loader_flags_off, 4),
            number_of_rva_and_sizes: Field::new(number_of_rva_and_sizes, number_of_rva_off, 4),
            data_directories,
        })
    }

    /// Number of data directory slots present on disk (`min(number_of_rva_and_sizes, 16)`).
    pub fn active_data_directory_count(&self) -> usize {
        (self.number_of_rva_and_sizes.value as usize).min(16)
    }

    /// Returns `true` when `index` is within [`Self::active_data_directory_count`].
    pub fn has_data_directory(&self, index: usize) -> bool {
        index < self.active_data_directory_count()
    }

    /// Returns [`PEType`] from the optional header `magic` field.
    pub fn pe_type(&self) -> Result<PEType, FileParseError> {
        match self.magic.value {
            0x10B => Ok(PEType::PE32),
            0x20B => Ok(PEType::PE32Plus),
            _ => Err(FileParseError::InvalidFileFormat),
        }
    }
}

impl Field<ImageBase> {
    /// Updates the image base in the PE optional header (always little-endian).
    pub fn update(
        &mut self,
        buffer: &mut [u8],
        new_value: ImageBase,
    ) -> Result<(), FileParseError> {
        match (new_value, self.size) {
            (ImageBase::Base32(value), 4) => {
                buffer[self.offset..self.offset + 4].copy_from_slice(&value.to_le_bytes());
                self.value = new_value;
                Ok(())
            }
            (ImageBase::Base64(value), 8) => {
                buffer[self.offset..self.offset + 8].copy_from_slice(&value.to_le_bytes());
                self.value = new_value;
                Ok(())
            }
            _ => Err(FileParseError::InvalidFileFormat),
        }
    }
}

impl Field<SizedU64> {
    /// Updates a stack/heap size field (4 bytes for PE32, 8 for PE32+).
    pub fn update(&mut self, buffer: &mut [u8], new_value: SizedU64) -> Result<(), FileParseError> {
        match (new_value, self.size) {
            (SizedU64::U32(value), 4) => {
                buffer[self.offset..self.offset + 4].copy_from_slice(&value.to_le_bytes());
                self.value = new_value;
                Ok(())
            }
            (SizedU64::U64(value), 8) => {
                buffer[self.offset..self.offset + 8].copy_from_slice(&value.to_le_bytes());
                self.value = new_value;
                Ok(())
            }
            _ => Err(FileParseError::InvalidFileFormat),
        }
    }
}