icoextract 0.1.0

Simple library to extract icons from a Windows executable (PE32 and PE32+). Very barebones, do not use in anything important yet.
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#![feature(slice_ptr_get)]

use core::panic;
use std::cmp::Reverse;
use std::ffi::CStr;
use std::fs::File;
use std::io::{Read, Write};
use std::mem::size_of;
use std::path::PathBuf;
use std::ptr::{read_unaligned, slice_from_raw_parts};
use std::slice::from_raw_parts;

use libc::c_char;
use num_traits::FromPrimitive;

mod pe;

const DOS_HEADER_MAGIC: u16 = 0x5A4D;
const NT_HEADER_SIGNATURE: u32 = 0x0004550;

/// Simple wrapper used by IconExtractor
#[derive(Clone, Default, Debug)]
pub struct IconBuffer {
    icon: Vec<u8>,
}

impl IconBuffer {
    /// Create empty buffer
    pub fn new() -> Self {
        Self { icon: Vec::new() }
    }

    /// Create buffer from existing buffer
    pub fn from_vec(icon: Vec<u8>) -> Self {
        Self { icon }
    }

    /// Unwrap underlying Vec
    pub fn unwrap(self) -> Vec<u8> {
        self.icon
    }

    /// Write buffer to file specified by `path`
    pub fn to_file(self, path: PathBuf) -> std::io::Result<File> {
        let mut file = match std::fs::File::create(path) {
            Ok(f) => f,
            Err(e) => return Err(e),
        };

        match file.write_all(&self.icon) {
            Ok(_) => Ok(file),
            Err(e) => Err(e),
        }
    }
}

pub struct IconExtractor {
    bytes: Vec<u8>,
}

impl IconExtractor {
    /// Use to specify file by its path
    pub fn from_path(path: PathBuf) -> Self {
        Self::from_file(File::open(path).unwrap())
    }

    /// Use to specify file from an already-opened file
    pub fn from_file(mut file: File) -> Self {
        let mut vec: Vec<u8> = Vec::new();
        file.read(&mut vec).unwrap();
        Self::from_slice(&vec)
    }

    /// Use to specify file from a slice (i.e., if the file has already been read into a buffer)
    pub fn from_slice(slice: &[u8]) -> Self {
        Self::new(slice.to_vec())
    }

    /// Extract the icons
    pub fn extract(self) -> Vec<IconBuffer> {
        println!("parsing DOS header...");
        let dos_header = Self::get_dos_header(&self.bytes)
            .expect("failed to retrieve DOS header.");
        let e_magic = dos_header.e_magic;
        let e_lfanew = dos_header.e_lfanew;
        println!(
            "DOS header found and signature validated (e_magic={:#06X}, e_lfanew={:#010X}).",
            e_magic, e_lfanew
        );

        println!("attempting to locate NT header.");
        let nt_header = Self::get_nt_header(&dos_header, &self.bytes)
            .expect("failed to retrieve NT header");
        let signature = nt_header.signature;
        println!("NT header found (signature={:#010X}).", signature);

        if nt_header.optional_header_size == 0 {
            panic!("executable has no optional header (optional_header_size == 0).");
        }

        let nt_optional_header =
            Self::get_nt_optional_header(&dos_header, &self.bytes)
                .expect("failed to retrieve NT optional header");

        match nt_optional_header {
            pe::nt::OptionalHeader::H32(h) => {
                let magic = h.magic;
                println!(
                    "NT optional header found (magic={:#06X}, 32-bit).",
                    magic
                );
            }
            pe::nt::OptionalHeader::H64(h) => {
                let magic = h.magic;
                println!(
                    "NT optional header found (magic={:#06X}, 64-bit).",
                    magic
                );
            }
        }

        let sections =
            Self::get_section_table(&dos_header, &nt_header, &self.bytes);
        println!("Located section table (num_sections={:?}).", sections.len());

        for (i, s) in sections.iter().enumerate() {
            unsafe {
                println!(
                    "Found section {:?} ({:?}).",
                    CStr::from_ptr(s.name.as_ptr() as *const c_char),
                    i
                );
            }
        }

        let (resource_directory_root, rsrc_section_header) =
            Self::get_resource_directory_root(
                &nt_optional_header,
                &sections,
                &self.bytes,
            )
            .expect("failed to acquire resource section.");
        let resource_section_bytes = unsafe {
            &*core::ptr::slice_from_raw_parts(
                self.bytes
                    .as_ptr()
                    .offset(rsrc_section_header.raw_data_offset as isize),
                rsrc_section_header.raw_data_size as usize,
            )
        };
        let resource_directory_entries_bytes = unsafe {
            &*core::ptr::slice_from_raw_parts(
                resource_section_bytes
                    .as_ptr()
                    .add(size_of::<pe::nt::ResourceDirectoryRaw>()),
                resource_directory_root.num_id_entries as usize
                    * size_of::<pe::nt::ResourceDataEntryRaw>()
                    + resource_directory_root.num_named_entries as usize
                        * size_of::<pe::nt::ResourceDataEntryRaw>(),
            )
        };

        let resource_directory_root_entries =
            Self::get_resource_directory_entries(
                &resource_directory_root,
                resource_section_bytes,
                resource_directory_entries_bytes,
            );
        let icons_resource_directory_root_entry =
            resource_directory_root_entries
                .iter()
                .find(|e| match e.data_type {
                    pe::nt::ResourceDirectoryEntryDataType::Id(3, _) => true,
                    _ => false,
                })
                .unwrap();

        let icons_resource_directory_ptr =
            match icons_resource_directory_root_entry.data_type {
                pe::nt::ResourceDirectoryEntryDataType::Id(3, offset) => {
                    match offset {
                        pe::nt::ResourceDirectoryEntryOffsetType::Leaf(_) => {
                            panic!("icons resource directory is a leaf")
                        }
                        pe::nt::ResourceDirectoryEntryOffsetType::Stem(
                            offset,
                        ) => unsafe {
                            resource_section_bytes.as_ptr().add(offset as usize)
                        },
                    }
                }
                _ => panic!("where the fuck i am"),
            };

        let icons_resource_directory: pe::nt::ResourceDirectory = unsafe {
            (*(icons_resource_directory_ptr
                as *const pe::nt::ResourceDirectoryRaw))
                .into()
        };
        let icons_resource_data_entries_bytes = unsafe {
            &*slice_from_raw_parts(
                icons_resource_directory_ptr
                    .add(size_of::<pe::nt::ResourceDirectoryRaw>()),
                (icons_resource_directory.num_id_entries as usize
                    * size_of::<pe::nt::ResourceDirectoryEntryRaw>())
                    as usize
                    + (icons_resource_directory.num_named_entries as usize
                        * size_of::<pe::nt::ResourceDirectoryEntryRaw>())
                        as usize,
            )
        };

        let r = Self::get_resource_data_entries(
            &rsrc_section_header,
            &icons_resource_directory,
            resource_section_bytes,
            icons_resource_data_entries_bytes,
        );
        let mut icons: Vec<IconBuffer> = Vec::new();
        for d in r {
            unsafe {
                icons.push(IconBuffer::from_vec(
                    from_raw_parts(
                        self.bytes.as_ptr().offset(d.offset as isize),
                        d.size as usize,
                    )
                    .to_vec(),
                ))
            };
        }

        icons
    }

    fn new(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }

    fn get_dos_header(v: &[u8]) -> Result<pe::dos::Header, &'static str> {
        let dos_header_bytes = &v[0..std::mem::size_of::<pe::dos::Header>()];
        let dos_header: pe::dos::Header = unsafe {
            std::ptr::read_unaligned(dos_header_bytes.as_ptr() as *const _)
        };
        match dos_header.e_magic {
            DOS_HEADER_MAGIC => Ok(dos_header),
            _ => Err("invalid magic value for DOS header (probably not MZ/PE executable)"),
        }
    }

    fn get_nt_header(
        dos_header: &pe::dos::Header,
        v: &[u8],
    ) -> Result<pe::nt::Header, &'static str> {
        let nt_header_offset = dos_header.e_lfanew as usize;
        let nt_header_bytes = &v[nt_header_offset
            ..nt_header_offset + std::mem::size_of::<pe::nt::Header>()];
        let nt_header: pe::nt::Header = unsafe {
            std::ptr::read_unaligned(nt_header_bytes.as_ptr() as *const _)
        };

        match nt_header.signature {
            NT_HEADER_SIGNATURE => Ok(nt_header),
            _ => Err("invalid magic value for NT header (probably not PE executable)"),
        }
    }

    fn get_nt_optional_header(
        dos_header: &pe::dos::Header,
        v: &[u8],
    ) -> Result<pe::nt::OptionalHeader, &'static str> {
        let nt_optional_header_offset =
            dos_header.e_lfanew as usize + size_of::<pe::nt::Header>() as usize;
        let nt_optional_header_bytes = &v[nt_optional_header_offset
            ..nt_optional_header_offset
                + std::mem::size_of::<pe::nt::OptionalHeader>()];
        let magic_raw: u16 = unsafe {
            std::ptr::read_unaligned(
                nt_optional_header_bytes.as_ptr() as *const _
            )
        };
        let magic: pe::nt::OptionalHeaderMagic =
            match FromPrimitive::from_u16(magic_raw) {
                Some(m) => m,
                None => return Err("invalid NT optional header magic"),
            };

        match magic {
            pe::nt::OptionalHeaderMagic::M32 => {
                let h32: pe::nt::OptionalHeader32 = unsafe {
                    std::ptr::read_unaligned(
                        nt_optional_header_bytes.as_ptr() as *const _
                    )
                };
                Ok(pe::nt::OptionalHeader::H32(h32))
            }
            pe::nt::OptionalHeaderMagic::M64 => {
                let h64: pe::nt::OptionalHeader64 = unsafe {
                    std::ptr::read_unaligned(
                        nt_optional_header_bytes.as_ptr() as *const _
                    )
                };
                Ok(pe::nt::OptionalHeader::H64(h64))
            }
            _ => panic!("where the fuck i am"), // other cases should've been handled above
        }
    }

    fn get_section_table(
        dos_header: &pe::dos::Header,
        nt_header: &pe::nt::Header,
        v: &[u8],
    ) -> Vec<pe::nt::SectionHeaderRaw> {
        let e_lfanew = dos_header.e_lfanew as usize;
        let nt_header_size = size_of::<pe::nt::Header>() as usize;
        let nt_optional_header_size = nt_header.optional_header_size as usize;
        let section_table_offset =
            e_lfanew + nt_header_size + nt_optional_header_size;
        let num_sections = nt_header.num_sections;
        let section_table_bytes = &v[section_table_offset
            ..section_table_offset
                + size_of::<pe::nt::SectionHeaderRaw>()
                    * num_sections as usize];
        let mut sections: Vec<pe::nt::SectionHeaderRaw> = Vec::new();

        unsafe {
            for i in 0..num_sections {
                sections.push(
                    *(section_table_bytes.as_ptr()
                        as *const pe::nt::SectionHeaderRaw)
                        .offset(i as isize),
                );
            }
        }

        sections
    }

    fn get_resource_section_rva(
        nt_optional_header: &pe::nt::OptionalHeader,
    ) -> usize {
        let rsrc_data_dir: pe::nt::DataDirectory = match nt_optional_header {
            pe::nt::OptionalHeader::H32(h) => h.data_directory[2],
            pe::nt::OptionalHeader::H64(h) => h.data_directory[2],
        };

        rsrc_data_dir.vaddr as usize
    }

    fn get_resource_section_header(
        nt_optional_header: &pe::nt::OptionalHeader,
        section_table: &Vec<pe::nt::SectionHeaderRaw>,
    ) -> Option<pe::nt::SectionHeaderRaw> {
        let resource_section_rva =
            Self::get_resource_section_rva(nt_optional_header);
        for a in section_table {
            if a.virtual_address as usize == resource_section_rva {
                return Some(*a);
            }
        }
        None
    }

    fn get_resource_directory_root(
        nt_optional_header: &pe::nt::OptionalHeader,
        section_table: &Vec<pe::nt::SectionHeaderRaw>,
        v: &[u8],
    ) -> Result<(pe::nt::ResourceDirectory, pe::nt::SectionHeader), &'static str>
    {
        let rsrc_section_header_raw = match Self::get_resource_section_header(
            nt_optional_header,
            section_table,
        ) {
            Some(h) => h,
            None => return Err("failed to find resource section header."),
        };
        let rsrc_section_header: pe::nt::SectionHeader =
            rsrc_section_header_raw.into();
        println!(
            "Found resource section (section={:?})",
            rsrc_section_header.name
        );

        let resource_section_offset =
            rsrc_section_header.raw_data_offset as usize;
        let resource_section_size = rsrc_section_header.raw_data_size as usize;
        let resource_section_bytes = &v[resource_section_offset
            ..resource_section_offset + resource_section_size];
        let resource_section_raw: pe::nt::ResourceDirectoryRaw = unsafe {
            read_unaligned(resource_section_bytes.as_ptr() as *const _)
        };
        Ok((resource_section_raw.into(), rsrc_section_header))
    }

    fn get_resource_directory_entries(
        resource_directory: &pe::nt::ResourceDirectory,
        resource_section_bytes: &[u8],
        resource_directory_entries_bytes: &[u8],
    ) -> Vec<pe::nt::ResourceDirectoryEntry> {
        let mut v: Vec<pe::nt::ResourceDirectoryEntry> = Vec::new();

        for i in 0..(resource_directory.num_named_entries as usize) {
            unsafe {
                let raw = read_unaligned(
                    (resource_directory_entries_bytes.as_ptr()
                        as *const pe::nt::ResourceDirectoryEntryRaw)
                        .add(i),
                );
                let len: usize = *(resource_section_bytes
                    .as_ptr()
                    .offset(raw.offset as isize & !(1 << 31) as isize)
                    as *const u32) as usize;
                let strbuf: &[u8] = from_raw_parts(
                    resource_section_bytes.as_ptr().offset(
                        raw.offset as isize
                            & ((!(1 << 31)) as isize
                                + size_of::<u32>() as isize),
                    ) as *const u8,
                    len * 2,
                );
                let string: pe::nt::ResourceDirectoryString =
                    pe::nt::ResourceDirectoryString::from_utf16(
                        strbuf.to_vec(),
                    )
                    .unwrap();
                v.push(pe::nt::ResourceDirectoryEntry::new(
                    (i + 1) as u16,
                    pe::nt::ResourceDirectoryEntryDataType::Named(
                        string,
                        raw.offset.into(),
                    ),
                ));
            };
        }

        for i in 0..(resource_directory.num_id_entries as usize) {
            unsafe {
                let raw = read_unaligned(
                    (resource_directory_entries_bytes.as_ptr()
                        as *const pe::nt::ResourceDirectoryEntryRaw)
                        .offset(
                            i as isize
                                + resource_directory.num_named_entries as isize,
                        ),
                );
                v.push(pe::nt::ResourceDirectoryEntry::new(
                    (i + 1) as u16,
                    pe::nt::ResourceDirectoryEntryDataType::Id(
                        raw.id,
                        raw.offset.into(),
                    ),
                ));
            };
        }

        v
    }

    #[allow(clippy::cast_slice_different_sizes)]
    fn get_resource_data_entries(
        resource_section_header: &pe::nt::SectionHeader,
        root_directory: &pe::nt::ResourceDirectory,
        resource_section_bytes: &[u8],
        resource_directory_entries_bytes: &[u8],
    ) -> Vec<pe::nt::ResourceDataEntry> {
        let v = Self::get_resource_directory_entries(
            root_directory,
            resource_section_bytes,
            resource_directory_entries_bytes,
        );
        let mut r: Vec<pe::nt::ResourceDataEntry> = Vec::new();
        for e in v {
            match e.data_type {
                pe::nt::ResourceDirectoryEntryDataType::Id(_, t) => match t {
                    pe::nt::ResourceDirectoryEntryOffsetType::Leaf(offset) => unsafe {
                        let p = resource_section_bytes
                            .as_ptr()
                            .offset(offset as isize)
                            as *const pe::nt::ResourceDataEntryRaw;
                        r.push(pe::nt::ResourceDataEntry::from_raw(
                            resource_section_header,
                            *p,
                        ));
                    },
                    pe::nt::ResourceDirectoryEntryOffsetType::Stem(offset) => {
                        let p = unsafe {
                            resource_section_bytes
                                .as_ptr()
                                .offset(offset as isize)
                                as *const pe::nt::ResourceDirectoryRaw
                        };
                        let raw = unsafe { *p };
                        let dir: pe::nt::ResourceDirectory = raw.into();
                        let resource_directory_entries_bytes = unsafe {
                            &*(core::ptr::slice_from_raw_parts(
                                (p as *const u8).add(size_of::<
                                    pe::nt::ResourceDirectoryRaw,
                                >(
                                ))
                                    as *const pe::nt::ResourceDirectoryEntryRaw,
                                dir.num_id_entries as usize
                                    + dir.num_named_entries as usize,
                            ) as *const [u8])
                        };
                        r.append(&mut Self::get_resource_data_entries(
                            resource_section_header,
                            &dir,
                            resource_section_bytes,
                            resource_directory_entries_bytes,
                        ));
                    }
                },
                pe::nt::ResourceDirectoryEntryDataType::Named(_, t) => {
                    match t {
                        pe::nt::ResourceDirectoryEntryOffsetType::Leaf(
                            offset,
                        ) => unsafe {
                            let p = resource_section_bytes
                                .as_ptr()
                                .offset(offset as isize)
                                as *const pe::nt::ResourceDataEntryRaw;
                            r.push(pe::nt::ResourceDataEntry::from_raw(
                                resource_section_header,
                                *p,
                            ));
                        },
                        pe::nt::ResourceDirectoryEntryOffsetType::Stem(
                            offset,
                        ) => {
                            let raw = unsafe {
                                *(resource_section_bytes
                                    .as_ptr()
                                    .offset(offset as isize)
                                    as *const pe::nt::ResourceDirectoryRaw)
                            };
                            let dir: pe::nt::ResourceDirectory = raw.into();
                            r.append(&mut Self::get_resource_data_entries(
                                resource_section_header,
                                &dir,
                                resource_section_bytes,
                                resource_directory_entries_bytes,
                            ));
                        }
                    }
                }
            }
        }

        r.sort_by_key(|w| Reverse(*w));
        r
    }
}