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
use alloc::vec::Vec;
use core::fmt::Debug;
use crate::read::{ByteString, Bytes, Error, ReadError, ReadRef, Result};
use crate::{pe, LittleEndian as LE, U16Bytes, U32Bytes};
#[derive(Clone, Copy)]
pub enum ExportTarget<'data> {
Address(u32),
ForwardByOrdinal(&'data [u8], u32),
ForwardByName(&'data [u8], &'data [u8]),
}
impl<'data> ExportTarget<'data> {
pub fn is_address(&self) -> bool {
match self {
ExportTarget::Address(_) => true,
_ => false,
}
}
pub fn is_forward(&self) -> bool {
!self.is_address()
}
}
#[derive(Clone, Copy)]
pub struct Export<'data> {
pub ordinal: u32,
pub name: Option<&'data [u8]>,
pub target: ExportTarget<'data>,
}
impl<'a> Debug for Export<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
f.debug_struct("Export")
.field("ordinal", &self.ordinal)
.field("name", &self.name.map(ByteString))
.field("target", &self.target)
.finish()
}
}
impl<'a> Debug for ExportTarget<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
match self {
ExportTarget::Address(address) => write!(f, "Address({:#x})", address),
ExportTarget::ForwardByOrdinal(library, ordinal) => write!(
f,
"ForwardByOrdinal({:?}.#{})",
ByteString(library),
ordinal
),
ExportTarget::ForwardByName(library, name) => write!(
f,
"ForwardByName({:?}.{:?})",
ByteString(library),
ByteString(name)
),
}
}
}
#[derive(Debug, Clone)]
pub struct ExportTable<'data> {
data: Bytes<'data>,
virtual_address: u32,
directory: &'data pe::ImageExportDirectory,
addresses: &'data [U32Bytes<LE>],
names: &'data [U32Bytes<LE>],
name_ordinals: &'data [U16Bytes<LE>],
}
impl<'data> ExportTable<'data> {
pub fn parse(data: &'data [u8], virtual_address: u32) -> Result<Self> {
let directory = Self::parse_directory(data)?;
let data = Bytes(data);
let mut addresses = &[][..];
let address_of_functions = directory.address_of_functions.get(LE);
if address_of_functions != 0 {
addresses = data
.read_slice_at::<U32Bytes<_>>(
address_of_functions.wrapping_sub(virtual_address) as usize,
directory.number_of_functions.get(LE) as usize,
)
.read_error("Invalid PE export address table")?;
}
let mut names = &[][..];
let mut name_ordinals = &[][..];
let address_of_names = directory.address_of_names.get(LE);
let address_of_name_ordinals = directory.address_of_name_ordinals.get(LE);
if address_of_names != 0 {
if address_of_name_ordinals == 0 {
return Err(Error("Missing PE export ordinal table"));
}
let number = directory.number_of_names.get(LE) as usize;
names = data
.read_slice_at::<U32Bytes<_>>(
address_of_names.wrapping_sub(virtual_address) as usize,
number,
)
.read_error("Invalid PE export name pointer table")?;
name_ordinals = data
.read_slice_at::<U16Bytes<_>>(
address_of_name_ordinals.wrapping_sub(virtual_address) as usize,
number,
)
.read_error("Invalid PE export ordinal table")?;
}
Ok(ExportTable {
data,
virtual_address,
directory,
addresses,
names,
name_ordinals,
})
}
pub fn parse_directory(data: &'data [u8]) -> Result<&'data pe::ImageExportDirectory> {
data.read_at::<pe::ImageExportDirectory>(0)
.read_error("Invalid PE export dir size")
}
pub fn directory(&self) -> &'data pe::ImageExportDirectory {
self.directory
}
pub fn ordinal_base(&self) -> u32 {
self.directory.base.get(LE)
}
pub fn addresses(&self) -> &'data [U32Bytes<LE>] {
self.addresses
}
pub fn name_pointers(&self) -> &'data [U32Bytes<LE>] {
self.names
}
pub fn name_ordinals(&self) -> &'data [U16Bytes<LE>] {
self.name_ordinals
}
pub fn name_iter(&self) -> impl Iterator<Item = (u32, u16)> + 'data {
self.names
.iter()
.map(|x| x.get(LE))
.zip(self.name_ordinals.iter().map(|x| x.get(LE)))
}
pub fn address_by_index(&self, index: u32) -> Result<u32> {
Ok(self
.addresses
.get(index as usize)
.read_error("Invalid PE export address index")?
.get(LE))
}
pub fn address_by_ordinal(&self, ordinal: u32) -> Result<u32> {
self.address_by_index(ordinal.wrapping_sub(self.ordinal_base()))
}
pub fn target_by_index(&self, index: u32) -> Result<ExportTarget<'data>> {
self.target_from_address(self.address_by_index(index)?)
}
pub fn target_by_ordinal(&self, ordinal: u32) -> Result<ExportTarget<'data>> {
self.target_from_address(self.address_by_ordinal(ordinal)?)
}
pub fn target_from_address(&self, address: u32) -> Result<ExportTarget<'data>> {
Ok(if let Some(forward) = self.forward_string(address)? {
let i = forward
.iter()
.position(|x| *x == b'.')
.read_error("Missing PE forwarded export separator")?;
let library = &forward[..i];
match &forward[i + 1..] {
[b'#', digits @ ..] => {
let ordinal =
parse_ordinal(digits).read_error("Invalid PE forwarded export ordinal")?;
ExportTarget::ForwardByOrdinal(library, ordinal)
}
[] => {
return Err(Error("Missing PE forwarded export name"));
}
name => ExportTarget::ForwardByName(library, name),
}
} else {
ExportTarget::Address(address)
})
}
fn forward_offset(&self, address: u32) -> Option<usize> {
let offset = address.wrapping_sub(self.virtual_address) as usize;
if offset < self.data.len() {
Some(offset)
} else {
None
}
}
pub fn is_forward(&self, address: u32) -> bool {
self.forward_offset(address).is_some()
}
pub fn forward_string(&self, address: u32) -> Result<Option<&'data [u8]>> {
if let Some(offset) = self.forward_offset(address) {
self.data
.read_string_at(offset)
.read_error("Invalid PE forwarded export address")
.map(Some)
} else {
Ok(None)
}
}
pub fn name_from_pointer(&self, name_pointer: u32) -> Result<&'data [u8]> {
let offset = name_pointer.wrapping_sub(self.virtual_address);
self.data
.read_string_at(offset as usize)
.read_error("Invalid PE export name pointer")
}
pub fn exports(&self) -> Result<Vec<Export<'data>>> {
let mut exports = Vec::new();
let ordinal_base = self.ordinal_base();
for (i, address) in self.addresses.iter().enumerate() {
let ordinal = ordinal_base.wrapping_add(i as u32);
let target = self.target_from_address(address.get(LE))?;
exports.push(Export {
ordinal,
target,
name: None,
});
}
for (name_pointer, ordinal_index) in self.name_iter() {
let name = self.name_from_pointer(name_pointer)?;
exports
.get_mut(ordinal_index as usize)
.read_error("Invalid PE export ordinal")?
.name = Some(name);
}
Ok(exports)
}
}
fn parse_ordinal(digits: &[u8]) -> Option<u32> {
if digits.is_empty() {
return None;
}
let mut result: u32 = 0;
for &c in digits {
let x = (c as char).to_digit(10)?;
result = result.checked_mul(10)?.checked_add(x)?;
}
Some(result)
}