p8n-types 2.0.1

Basic types for representing binary programs
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
// Panopticon - A libre program analysis library for machine code
// Copyright (C) 2014-2018  The Panopticon Developers
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

//! Loader for 32 and 64-bit ELF, PE, and Mach-o files.

use std::fs::File;
use std::path::Path;

use memmap::{MmapOptions, Mmap};
use goblin::{self, Hint, pe, elf, mach};
use goblin::elf::program_header;
use goblin::pe::section_table::SectionTable;

use {Str, Region, Result};

/// CPU the binary file is intended for.
#[derive(Clone,Copy,Debug)]
pub enum Machine {
    /// 8-bit AVR
    Avr,
    /// AMD64
    Amd64,
    /// Intel x86
    Ia32,
}

/// Named pointer inside a region.
#[derive(Debug, Clone)]
pub struct Pointer {
    /// Region name
    pub segment: Str,
    /// Pointed-to name
    pub name: Str,
    /// Offset from the start of the region
    pub offset: u64,
}

/// Binary file meta information. Starting point for disassembler routines.
#[derive(Debug, Clone)]
pub struct Content {
    /// CPU ISA
    pub machine: Machine,
    /// Public functions
    pub entry_points: Vec<Pointer>,
    /// Regions
    pub segments: Vec<Region>,
    /// Imported functions
    pub symbolic: Vec<Pointer>,
}

impl Content {
    /// Load an ELF or PE file from disk and creates a `Project` from it. Returns the `Project` instance and
    /// the CPU its intended for.
    pub fn load(path: &Path) -> Result<Self> {
        Self::load_all(path).and_then(|x| match x.into_iter().next() {
            Some(x) => Ok(x),
            None => Err(format!("Not a supported file format").into()),
        })
    }

    /// Load all programs inside a ELF or PE file.
    pub fn load_all(path: &Path) -> Result<Vec<Self>> {
        let fd = File::open(path)?;
        let map = unsafe { Mmap::map(&fd)? };
        let mut magic = [0u8; 16];

        magic.copy_from_slice(&map[0..16]);

        match goblin::peek_bytes(&magic)? {
            Hint::Unknown(magic) => Err(format!("Tried to load an unknown file. Magic: {}", magic).into()),
            Hint::Elf(_) => Self::load_elf(&map, &fd, path).map(|x| vec![x]),
            Hint::PE => Self::load_pe(&map, &fd, path).map(|x| vec![x]),
            Hint::Mach(_) => Self::load_mach(&map, &fd, path).map(|x| vec![x]),
            Hint::MachFat(_) => {
                unimplemented!()
            }
            Hint::Archive => {
                unimplemented!()
            }
        }
    }

    fn load_mach(map: &Mmap, fd: &File, path: &Path) -> Result<Content> {
        let binary = mach::MachO::parse(&map[..], 0)?;
        debug!("mach: {:#?}", &binary);
        let mut base = 0x0;
        let cputype = binary.header.cputype;
        let (machine, addr_bits) = match cputype {
            mach::cputype::CPU_TYPE_X86 => {
                (Machine::Ia32, 32)
            }
            mach::cputype::CPU_TYPE_X86_64 => {
                (Machine::Amd64, 64)
            }
            machine => {
                return Err(
                    format!(
                        "Unsupported machine ({:#x})",
                        machine,
                        )
                    .into()
                    )
            }
        };
        let mut regs = Vec::default();
        let mut syms = Vec::default();
        let mut entries = Vec::default();

        for segment in &*binary.segments {
            let offset = segment.fileoff as usize;
            let filesize = segment.filesize as usize;
            if offset + filesize > map.len() {
                return Err(
                    format!(
                        "Failed to read segment: range {:?} greater than len {}",
                        offset..offset + filesize,
                        map.len()
                        )
                    .into()
                    );
            }
            let start = segment.vmaddr;
            let name = segment.name()?;

            debug!(
                "Load mach segment {:?}: {} bytes segment to {:#x}",
                name,
                segment.vmsize,
                start
                );

            let reg = if filesize > 0 {
                Self::load_section(name.to_string().into(), fd, path, filesize, addr_bits, offset, start)?
            } else {
                Region::undefined(name.to_string(), addr_bits, None)
            };
            regs.push(reg);

            if name == "__TEXT" {
                base = segment.vmaddr;
                debug!("Setting vm address base to {:#x}", base);
            }
        }

        let entry = binary.entry;

        if entry != 0 {
            match Self::resolve_reference(entry as u64, "(entry)", &regs) {
                Some(e) => { entries.push(e); }
                None => { /* do nothing */ }
            }
        }

        for export in binary.exports()? {
            if export.offset != 0 {
                debug!("adding: {:?}", &export);

                match Self::resolve_reference(export.offset as u64 + base, &export.name, &regs) {
                    Some(e) => { entries.push(e); }
                    None => { /* do nothing */ }
                }
            }
        }

        for import in binary.imports()? {
            debug!("Import {}: {:#x}", import.name, import.offset);

            match Self::resolve_reference(import.offset as u64, import.name, &regs) {
                Some(e) => { syms.push(e); }
                None => { /* do nothing */ }
            }
        }

        let c = Content{
            machine: machine,
            entry_points: entries,
            symbolic: syms,
            segments: regs,
        };
        Ok(c)
    }

    /// Parses an ELF 32/64-bit binary from `bytes` and creates a `Project` from it. Returns the `Project` instance and
    /// the CPU its intended for.
    fn load_elf(map: &Mmap, fd: &File, path: &Path) -> Result<Content> {
        use std::collections::HashSet;

        let binary = elf::Elf::parse(&map[..])?;
        let mut regs = vec![];
        let mut entries: Vec<Pointer> = vec![];
        let mut syms: Vec<Pointer> = vec![];

        debug!("elf: {:#?}", &binary);

        let (machine, addr_bits) = match binary.header.e_machine {
            elf::header::EM_X86_64 => {
                (Machine::Amd64, 64)
            }
            elf::header::EM_386 => {
                (Machine::Ia32, 32)
            }
            elf::header::EM_AVR => {
                (Machine::Avr, 22)
            }
            machine => return Err(format!("Unsupported machine: {}", machine).into()),
        };


        for (idx, ph) in binary.program_headers.iter().enumerate() {
            if ph.p_type == program_header::PT_LOAD {
                debug!("Load ELF {} bytes segment to {:#x}", ph.p_filesz, ph.p_vaddr);

                let reg = Self::load_section(format!("sec{}", idx).into(), fd, path, ph.p_filesz as usize, addr_bits, ph.p_offset as usize, ph.p_vaddr)?;
                regs.push(reg);
            }
        }
        let mut seen_syms = HashSet::<u64>::new();

        // add dynamic symbol information (non-strippable)
        for sym in &binary.dynsyms {
            let name = &binary.dynstrtab[sym.st_name];

            Self::add_elf_symbol(&sym, name, &regs, &mut syms, &mut entries);
            seen_syms.insert(sym.st_value);

            let name = &binary.dynstrtab[sym.st_name];
            if !Self::resolve_elf_import_address(&binary.pltrelocs, name, &regs, &binary, &mut syms) {
                if sym.is_function() {
                    if !Self::resolve_elf_import_address(&binary.dynrelas, name, &regs, &binary, &mut syms) {
                        Self::resolve_elf_import_address(&binary.dynrels, name, &regs, &binary, &mut syms);
                    }
                }
            }
        }

        // add strippable symbol information
        for sym in &binary.syms {
            let name = &binary.strtab[sym.st_name];
            if !seen_syms.contains(&sym.st_value) {
                Self::add_elf_symbol(&sym, &name, &regs, &mut syms, &mut entries);
            }
            seen_syms.insert(sym.st_value);
        }

        // binary entry point
        match Self::resolve_reference(binary.entry, "(entry)", &regs) {
            Some(e) => { entries.push(e); }
            None => { /* do nothing */ }
        }

        let c = Content{
            machine: machine,
            entry_points: entries,
            symbolic: syms,
            segments: regs,
        };
        Ok(c)
    }

    fn add_elf_symbol(sym: &elf::Sym, name: &str, regs: &[Region], syms: &mut Vec<Pointer>, entries: &mut Vec<Pointer>) {
        let name = name.to_string();
        let addr = sym.st_value;

        debug!("Symbol: {} @ 0x{:x}: {:?}", name, addr, sym);

        if sym.is_function() {
            match Self::resolve_reference(addr, &name, &regs) {
                Some(e) =>
                    if sym.is_import() { syms.push(e); }
                    else { entries.push(e); },
                None => { /* do nothing */ }
            }
        }
    }

    fn resolve_elf_import_address(relocs: &elf::RelocSection, name: &str, regs: &[Region], binary: &elf::Elf, syms: &mut Vec<Pointer>) -> bool {
        for reloc in relocs.iter() {
            if let Some(pltsym) = &binary.dynsyms.get(reloc.r_sym) {
                let pltname = &binary.dynstrtab[pltsym.st_name];
                if pltname == name {
                    debug!("Import match {}: {:#x} {:?}", name, reloc.r_offset, pltsym);

                    match Self::resolve_reference(reloc.r_offset as u64, name.into(), &regs) {
                        Some(e) => { syms.push(e); }
                        None => { /* do nothing */ }
                    }
                    return true;
                }
            }
        }
        false
    }

    /// Parses a PE32/PE32+ file from `bytes` and create a project from it.
    fn load_pe(map: &Mmap, fd: &File, path: &Path) -> Result<Self> {
        let pe = pe::PE::parse(&map[..])?;
        let image_base = pe.image_base as u64;
        let mut regs = vec![];
        let mut entries = vec![];
        let entry = (pe.image_base + pe.entry) as u64;
        let size = fd.metadata()?.len();
        let address_bits = if pe.is_64 { 64 } else { 32 };

        debug!("loading PE: {:#?}", &pe);

        // alloc. segment for each section
        for section in &pe.sections {
            debug!("loading PE section: {:?}", section.name);

            let ret = Self::load_pe_section(section, fd, path, size as usize, image_base, address_bits)?;
            let addr = section.virtual_address as u64 + pe.image_base as u64;

            if entry >= addr && entry < addr + section.virtual_size as u64 {
                let ent = Pointer{
                    segment: ret.name().clone(),
                    name: "main".into(),
                    offset: entry,
                };
                entries.push(ent);
            }

            regs.push(ret);
        }

        debug!("PE file entry at {:#x}", entry);

        // add exported functions as entry points
        for (i, export) in pe.exports.iter().enumerate() {
            debug!("adding PE export: {:?}", &export);

            let nam = export.name.map(|x| x.to_string()).unwrap_or(format!("exp{}", i));
            match Self::resolve_reference(export.rva as u64 + image_base, &nam, &regs) {
                Some(e) => {
                    entries.push(e);
                }
                None => {
                    error!("PE export {:?} at {:#x} not mapped", export.name, export.rva);
                }
            }
        }

        let mut syms = vec![];

        // add imports as symbolic functions
        for import in pe.imports {
            debug!("adding PE import: {:?}", &import);

            match Self::resolve_reference(import.rva as u64 + image_base, &import.name, &regs) {
                Some(e) => {
                    syms.push(e);
                }
                None => {
                    error!("PE import {} at {:#x} not mapped", import.name, import.rva);
                }
            }
        }

        let c = Content{
            machine: Machine::Ia32,
            entry_points: entries,
            symbolic: syms,
            segments: regs,
        };
        Ok(c)
    }

    fn load_pe_section(sec: &SectionTable, fd: &File, path: &Path, fsize: usize, image_base: u64, address_bits: usize) -> Result<Region> {
        let voffset = sec.virtual_address as u64 + image_base;
        let vsize = sec.virtual_size as u64;
        let offset = sec.pointer_to_raw_data as usize;
        let size = sec.size_of_raw_data as usize;
        let name = String::from_utf8(sec.name[..].to_vec())?;

        if size > 0 {
            if offset + size > fsize {
                return Err(format!("PE section out of range: {:#x} + {:#x} >= {:#x}",offset,size,fsize).into());
            }

            // XXX: implemented larger vsize in Region
            debug!("PE section {} mapped from {:?} to {:?}", name, offset..offset + size, voffset..voffset + size as u64);
            Self::load_section(name.into(), fd, path, size, address_bits, offset, voffset)
        } else {
            debug!("PE section {} mapped to {:?}", name, voffset..voffset + vsize);
            Ok(Region::undefined(name, address_bits, None))
        }
    }

    fn resolve_reference(addr: u64, name: &str, regs: &[Region]) -> Option<Pointer> {
        for r in regs {
            if r.in_range(addr..addr + 1) {
                return Some(Pointer{
                    segment: r.name().clone(),
                    name: name.to_string().into(),
                    offset: addr,
                });
            }
        }

        None
    }

    fn load_section(name: Str, fd: &File, path: &Path, size: usize, address_bits: usize, file_offset: usize, load_offset: u64) -> Result<Region> {
        let mmap = unsafe {
            MmapOptions::new()
                .len(size)
                .offset(file_offset)
                .map(fd)?
        };
        Ok(Region::from_mmap(name, address_bits, mmap, path.to_path_buf(), file_offset as u64, load_offset, None))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pe() {
        use std::path::Path;

        let p = format!("{}/tests/data/test.exe", env!("CARGO_MANIFEST_DIR"));
        let c = Content::load(Path::new(&p)).unwrap();

        println!("{:?}", c);
    }

    #[test]
    fn elf() {
        use std::path::Path;

        let p = format!("{}/tests/data/dynamic-32", env!("CARGO_MANIFEST_DIR"));
        let c = Content::load(Path::new(&p)).unwrap();

        println!("{:?}", c);
    }

    #[test]
    fn mach() {
        use std::path::Path;

        let p = format!("{}/tests/data/deadbeef.mach", env!("CARGO_MANIFEST_DIR"));
        let c = Content::load(Path::new(&p)).unwrap();

        println!("{:?}", c);
    }

}