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
use std::{cell::Cell, ffi::CStr};
use goblin::pe::{
PE,
section_table::{IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE},
};
use thiserror::Error;
use crate::{
Pod, Ptr, TypedView,
address::MappedAddressView,
scan::{BinaryView, CodeSpan, Offset, Scanner},
};
/// Error type returned by PE wrapper APIs.
#[derive(Debug, Error)]
pub enum PeError {
#[error("failed to parse PE: {0}")]
Parse(#[from] goblin::error::Error),
#[error("only 64-bit PE images are supported in this module")]
NotPe64,
}
/// Result alias for PE wrapper APIs.
pub type Result<T> = std::result::Result<T, PeError>;
/// Minimal PE64 wrapper exposing pelite-like scanner behavior.
#[derive(Debug)]
pub struct PeFile<'a> {
bytes: &'a [u8],
pe: PE<'a>,
code_spans: Vec<CodeSpan>,
section_lookup_cache: Cell<Option<usize>>,
}
impl<'a> PeFile<'a> {
/// Parses a PE64 image from bytes.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let pattern = goblin_sigscan::pattern::parse("90")?;
/// let mut matches = file.scanner().matches_code(&pattern);
/// let mut save = [0u64; 4];
/// let _ = matches.next(&mut save);
/// Ok(())
/// }
/// ```
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self> {
let pe = PE::parse(bytes)?;
if !pe.is_64 {
return Err(PeError::NotPe64);
}
let code_spans = pe
.sections
.iter()
.filter_map(|section| {
let is_code = (section.characteristics & IMAGE_SCN_MEM_EXECUTE) != 0
|| (section.characteristics & IMAGE_SCN_CNT_CODE) != 0;
if !is_code {
return None;
}
let start = u64::from(section.virtual_address);
let end = start.checked_add(u64::from(section.size_of_raw_data))?;
let file_start = usize::try_from(section.pointer_to_raw_data).ok()?;
let file_size = usize::try_from(section.size_of_raw_data).ok()?;
let file_end = file_start.checked_add(file_size)?;
Some(CodeSpan {
mapped: start..end,
file: file_start..file_end,
})
})
.collect();
Ok(Self {
bytes,
pe,
code_spans,
section_lookup_cache: Cell::new(None),
})
}
/// Returns scanner access.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let pattern = goblin_sigscan::pattern::parse("90")?;
/// let mut matches = file.scanner().matches_code(&pattern);
/// let mut save = [0u64; 4];
/// let _ = matches.next(&mut save);
/// Ok(())
/// }
/// ```
pub fn scanner(&'a self) -> Scanner<'a, Self> {
Scanner::new(self)
}
/// Returns the parsed underlying goblin PE object.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let _sections = file.pe().sections.len();
/// Ok(())
/// }
/// ```
#[inline]
pub fn pe(&self) -> &PE<'a> {
&self.pe
}
/// Returns the original image bytes.
#[inline]
pub fn image(&self) -> &'a [u8] {
self.bytes
}
/// Reads a borrowed POD reference from an RVA.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let Some(rva) = file.file_offset_to_rva(0x1000) else {
/// return Ok(());
/// };
/// let _value = file.deref_rva::<u32>(rva);
/// Ok(())
/// }
/// ```
#[inline]
pub fn deref_rva<T: Pod>(&self, rva: u64) -> Option<&T> {
self.deref(self.ptr_from_rva(rva))
}
/// Reads a copied POD value from an RVA.
#[inline]
pub fn deref_copy_rva<T: Pod>(&self, rva: u64) -> Option<T> {
self.deref_copy(self.ptr_from_rva(rva))
}
/// Reads a NUL-terminated C string at an RVA.
#[inline]
pub fn deref_c_str_rva(&self, rva: u64) -> Option<&CStr> {
self.deref_c_str(self.ptr_from_rva::<u8>(rva).cast())
}
/// Reads a borrowed POD reference from a virtual address.
#[inline]
pub fn deref_va<T: Pod>(&self, va: u64) -> Option<&T> {
self.deref_rva(self.va_to_rva(va)?)
}
/// Reads a copied POD value from a virtual address.
#[inline]
pub fn deref_copy_va<T: Pod>(&self, va: u64) -> Option<T> {
self.deref_copy_rva(self.va_to_rva(va)?)
}
/// Reads a NUL-terminated C string at a virtual address.
#[inline]
pub fn deref_c_str_va(&self, va: u64) -> Option<&CStr> {
self.deref_c_str_rva(self.va_to_rva(va)?)
}
/// Builds a typed pointer from an RVA.
#[inline]
pub fn ptr_from_rva<T: ?Sized>(&self, rva: u64) -> Ptr<T> {
Ptr::from_mapped(rva)
}
/// Builds a typed pointer from a virtual address.
#[inline]
pub fn ptr_from_va<T: ?Sized>(&self, va: u64) -> Option<Ptr<T>> {
self.va_to_rva(va).map(Ptr::from_mapped)
}
/// Converts an RVA into a virtual address.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let _va = file.rva_to_va(0x1000);
/// Ok(())
/// }
/// ```
pub fn rva_to_va(&self, rva: Offset) -> Option<Offset> {
let image_base = self
.pe
.header
.optional_header
.as_ref()
.map(|header| header.windows_fields.image_base)?;
image_base.checked_add(rva)
}
/// Converts a virtual address into an RVA.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let Some(base) = file.rva_to_va(0) else {
/// return Ok(());
/// };
/// let _rva = file.va_to_rva(base + 0x1000);
/// Ok(())
/// }
/// ```
pub fn va_to_rva(&self, va: Offset) -> Option<Offset> {
let image_base = self
.pe
.header
.optional_header
.as_ref()
.map(|header| header.windows_fields.image_base)?;
let rva = va.checked_sub(image_base)?;
self.rva_to_file_offset(rva).map(|_| rva)
}
/// Converts an RVA into a file offset.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let _file_offset = file.rva_to_file_offset(0x1000);
/// Ok(())
/// }
/// ```
pub fn rva_to_file_offset(&self, rva: Offset) -> Option<usize> {
let headers_end = self
.pe
.header
.optional_header
.as_ref()
.map(|oh| u64::from(oh.windows_fields.size_of_headers))
.unwrap_or(0);
if rva < headers_end {
return usize::try_from(rva).ok();
}
if let Some(index) = self.section_lookup_cache.get()
&& let Some(file_offset) = self.section_file_offset(index, rva)
{
return Some(file_offset);
}
for (index, _) in self.pe.sections.iter().enumerate() {
if let Some(file_offset) = self.section_file_offset(index, rva) {
self.section_lookup_cache.set(Some(index));
return Some(file_offset);
}
}
None
}
/// Converts a file offset into an RVA.
///
/// # Examples
///
/// ```no_run
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// // Use real module bytes in production code.
/// let bytes: &[u8] = &[];
/// let file = goblin_sigscan::pe64::PeFile::from_bytes(bytes)?;
/// let _rva = file.file_offset_to_rva(0x1000);
/// Ok(())
/// }
/// ```
pub fn file_offset_to_rva(&self, file_offset: usize) -> Option<Offset> {
let headers_end = self
.pe
.header
.optional_header
.as_ref()
.and_then(|header| usize::try_from(header.windows_fields.size_of_headers).ok())
.unwrap_or(0);
if file_offset < headers_end {
return Offset::try_from(file_offset).ok();
}
self.pe.sections.iter().find_map(|section| {
let raw_start = usize::try_from(section.pointer_to_raw_data).ok()?;
let raw_size = usize::try_from(section.size_of_raw_data).ok()?;
let raw_end = raw_start.checked_add(raw_size)?;
if !(raw_start..raw_end).contains(&file_offset) {
return None;
}
let delta = file_offset.checked_sub(raw_start)?;
let section_rva = Offset::from(section.virtual_address);
section_rva.checked_add(Offset::try_from(delta).ok()?)
})
}
fn section_file_offset(&self, index: usize, rva: Offset) -> Option<usize> {
let section = self.pe.sections.get(index)?;
let section_rva = u64::from(section.virtual_address);
let delta = rva.checked_sub(section_rva)?;
if delta >= u64::from(section.size_of_raw_data) {
return None;
}
let raw_start = usize::try_from(section.pointer_to_raw_data).ok()?;
let delta_usize = usize::try_from(delta).ok()?;
raw_start.checked_add(delta_usize)
}
}
impl MappedAddressView for PeFile<'_> {
#[inline]
fn image(&self) -> &[u8] {
self.bytes
}
#[inline]
fn mapped_to_file_offset(&self, mapped_offset: Offset) -> Option<usize> {
self.rva_to_file_offset(mapped_offset)
}
#[inline]
fn file_offset_to_mapped(&self, file_offset: usize) -> Option<Offset> {
self.file_offset_to_rva(file_offset)
}
}
impl BinaryView for PeFile<'_> {
fn image(&self) -> &[u8] {
self.bytes
}
fn code_spans(&self) -> &[CodeSpan] {
&self.code_spans
}
fn mapped_to_file_offset(&self, offset: Offset) -> Option<usize> {
self.rva_to_file_offset(offset)
}
#[inline]
fn follow_pointer_target(&self, raw: Offset) -> Option<Offset> {
let rva = self.va_to_rva(raw)?;
self.rva_to_file_offset(rva).map(|_| rva)
}
}