vivisect 0.1.13

A cross-platform, ELF, Mach-o, and PE binary parsing and loading crate.
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
#![allow(unused)]

use crate::envi::constants::{Endianess, CC_REG, CC_STACK, CC_STACK_INF};
use crate::envi::memory::{MemoryDef, MemoryObject};
use crate::envi::operands::OpCode;
use crate::envi::registers::RegisterContext;
use crate::envi::GenericArchitectureModule;
use crate::envi::Result;
use crate::error::Error::{Generic, UnknownCallingConvention};
use std::cmp::max;
use std::rc::Rc;

#[derive(Clone, Default)]
pub struct EmulatorData {
    pub endian: Endianess,
    pub metadata: std::collections::HashMap<String, i32>,
    pub emu_opts: std::collections::HashMap<String, String>,
    pub emu_segments: Vec<(i32, i32)>,
    pub emu_calling_conventions: std::collections::HashMap<String, Rc<dyn CallingConvention>>,
    pub emu_opts_doc: std::collections::HashMap<String, String>,
    pub op_methods: std::collections::HashMap<String, String>,
}

pub trait Emulator: RegisterContext + MemoryObject {
    fn get_emulator_data_mut(&mut self) -> &mut EmulatorData;

    fn get_emulator_data(&self) -> &EmulatorData;

    fn get_arch_module(&self, arch: Option<i32>) -> &GenericArchitectureModule;

    fn get_arch_module_mut(&mut self, arch: Option<i32>) -> &mut GenericArchitectureModule;

    /// This is the core method for an emulator to do any running of instructions and
    /// setting of the program counter should an instruction require that.
    fn execute_op_code(&mut self, op: OpCode) -> Result<()>;
}

/// The emulator should be able to
/// be extended for the architecutures which are included
/// in the envi framework.  You *must* mix in
/// an instance of your architecture abstraction module.
///
/// (NOTE: Most users will just use an arch mod and call getEmulator())
///
/// The intention is for "light weight" emulation to be
/// implemented mostly for user-space emulation of
/// protected mode execution.
impl<'a> dyn Emulator + 'a {
    /// Initialize an emulator option used by the emulator type.
    /// Arch specific options should begin with <arch>: and platform
    /// options should begin with <platform>:
    fn init_emu_opts(&mut self, opt: &str, def_val: &str, doc: &str) {
        let data = self.get_emulator_data_mut();
        data.emu_opts.insert(opt.to_string(), def_val.to_string());
        data.emu_opts_doc.insert(opt.to_string(), doc.to_string());
    }

    fn set_emu_opt(&mut self, key: &str, val: &str) {
        let data = self.get_emulator_data_mut();
        data.emu_opts.insert(key.to_string(), val.to_string());
    }

    fn get_emu_opt(&self, key: &str) -> Option<&str> {
        let data = self.get_emulator_data();
        data.emu_opts.get(key).map(|s| s.as_str())
    }

    fn set_endian(&mut self, endian: Endianess) {
        let data = self.get_emulator_data_mut();
        data.endian = endian;
    }

    fn get_endian(&self) -> Endianess {
        let data = self.get_emulator_data();
        data.endian.clone()
    }

    fn get_meta(&self, name: &str, default: Option<i32>) -> i32 {
        let data = self.get_emulator_data();
        data.metadata
            .get(name)
            .map_or_else(|| default.unwrap_or(0), |val| *val)
    }

    fn set_meta(&mut self, name: &str, value: i32) {
        let data = self.get_emulator_data_mut();
        data.metadata.insert(name.to_string(), value);
    }

    /// Return the data needed to "snapshot" this emulator.  For most
    /// archs, this method will be enough (it takes the memory object,
    /// and register values with it)
    fn get_emu_snap(&self) -> (Vec<i32>, Vec<MemoryDef>) {
        let regs = self.get_register_snap();
        let mem = self.get_memory_snap();
        (regs, mem)
    }

    fn set_emu_snap(&mut self, snap: (Vec<i32>, Vec<MemoryDef>)) {
        let (regs, mem) = snap;
        self.set_register_snap(regs);
        self.set_memory_snap(mem);
    }

    /// Utility function to try something with an emulator, and then revert it.
    /// If we fail to get a valid snap, we raise a base EmuException. Otherwise,
    /// we yield out the snap we received.
    ///
    /// On close, we try to rollback the emulator using the snap.
    fn snap(&mut self) -> (Vec<i32>, Vec<MemoryDef>) {
        let snap = self.get_emu_snap();
        self.set_emu_snap(snap.clone());
        snap
    }

    /// Run the emulator until "something" happens.
    /// (breakpoint, segv, syscall, etc...)
    ///
    /// Set stepcount in order to run that many instructions before pausing emulation
    fn run(&mut self, step_count: Option<i32>) -> Result<()> {
        if let Some(count) = step_count {
            for _ in 0..count {
                self.stepi()?;
            }
        } else {
            loop {
                self.stepi()?;
            }
        }
        Ok(())
    }

    fn stepi(&mut self) -> Result<()> {
        let pc = self.get_program_counter();
        let op = MemoryObject::parse_op_code(self, pc, None)?;
        self.execute_op_code(op)
    }

    fn get_segment_info(&self, op: OpCode) -> (i32, i32) {
        let indx = self.get_segment_index(op);
        let data = self.get_emulator_data();
        data.emu_segments[indx as usize]
    }

    /// The *default* segmentation is none (most arch's will over-ride).
    /// This method may be implemented to return a segment index based on either
    /// emulator state or properties of the particular instruction in question.
    fn get_segment_index(&self, _op: OpCode) -> i32 {
        0
    }

    /// Set a base and size for a given segment index
    fn set_segment_info(&mut self, indx: i32, base: i32, size: i32) {
        let data = self.get_emulator_data_mut();
        if (data.emu_segments.len() as i32 - indx) == 0 {
            data.emu_segments.push((base, size));
            return;
        }
        data.emu_segments[indx as usize] = (base, size);
    }

    /// Return the value for the operand at index idx for
    /// the given opcode reading memory and register states if necessary.
    ///
    /// In partially-defined emulation, this may return None
    fn get_oper_value(&self, op: OpCode, indx: i32) -> Result<Option<i32>> {
        let oper = op.get_operands()[indx as usize].clone();
        oper.get_oper_value(op, None)
    }

    /// Set the value of the target operand at index idx from
    /// opcode op.
    /// (obviously OM_IMMEDIATE *cannot* be set)
    fn set_oper_value(&self, op: OpCode, indx: i32, value: i32) -> Result<()> {
        let mut oper = op.get_operands()[indx as usize].clone();
        Rc::get_mut(&mut oper)
            .unwrap()
            .set_oper_value(op, Some(self), value)
    }

    /// Emulator implementors can implement this method to allow
    /// analysis modules a platform/architecture independant way
    /// to get stack/reg/whatever args.
    ///
    /// Usage: getCallArgs(3, "stdcall") -> (0, 32, 0xf00)
    fn get_call_args(&mut self, count: i32, calling_convention: String) -> Result<Vec<i32>> {
        let data = self.get_emulator_data().clone();
        if let Some(cc) = data.emu_calling_conventions.get(&calling_convention) {
            return Ok(cc.get_call_args(self, count));
        }
        Err(UnknownCallingConvention(calling_convention))
    }

    /// Emulator implementors can implement this method to allow
    /// analysis modules a platform/architecture independant way
    /// to set a function return value. (this should also take
    /// care of any argument cleanup or other return time tasks
    /// for the calling convention)
    fn exec_call_return(
        &mut self,
        value: i32,
        calling_convention: String,
        argc: Option<i32>,
    ) -> Result<i32> {
        let data = self.get_emulator_data().clone();
        if let Some(cc) = data.emu_calling_conventions.get(&calling_convention) {
            return Ok(cc.exec_call_return(self, value, argc));
        }
        Err(UnknownCallingConvention(calling_convention))
    }

    fn add_calling_convention(&mut self, name: String, cc: Rc<dyn CallingConvention>) {
        let data = self.get_emulator_data_mut();
        data.emu_calling_conventions.insert(name, cc);
    }

    fn has_calling_convention(&self, name: &str) -> bool {
        let data = self.get_emulator_data();
        data.emu_calling_conventions.contains_key(name)
    }

    fn get_calling_convention(&self, name: &str) -> Option<&Rc<dyn CallingConvention>> {
        let data = self.get_emulator_data();
        data.emu_calling_conventions.get(name)
    }

    fn get_calling_conventions(&self) -> Vec<String> {
        let data = self.get_emulator_data();
        data.emu_calling_conventions
            .keys()
            .map(|s| s.clone())
            .collect()
    }

    /// Returns the value of the bytes at the "addr" address, given the size (currently, power of 2 only)
    fn read_mem_value(&self, va: i32, size: i32) -> Result<Option<Vec<i32>>> {
        if let Ok(data) = MemoryObject::read_memory(self, va, size, None) {
            if data.len() as i32 != size {
                return Err(Generic(format!(
                    "Read gave wrong length at {} (va: {} wanted {} got {})",
                    self.get_program_counter(),
                    va,
                    size,
                    data.len() as i32
                )));
            }
            // parse_bytes()
        }
        Ok(None)
    }
}

#[derive(Clone, Debug)]
pub struct CallingConventionData {
    pub pad: i32,
    pub align: i32,
    pub delta: i32,
    pub flags: i32,
    pub arg_def: Vec<(i32, i32)>,
    pub ret_val_def: (i32, i32),
    pub ret_addr_def: (i32, i32),
}

impl Default for CallingConventionData {
    fn default() -> CallingConventionData {
        CallingConventionData {
            pad: 0,
            align: 4,
            delta: 0,
            flags: 0,
            arg_def: vec![],
            ret_val_def: (CC_STACK, 0),
            ret_addr_def: (CC_STACK, 0),
        }
    }
}

pub trait CallingConvention {
    fn get_data(&self) -> &CallingConventionData;

    /// Returns the number of stack arguments.
    fn get_num_stack_args(&self, _emulator: Option<&dyn Emulator>, argc: i32) -> i32 {
        let data = self.get_data();
        let rargs = data
            .arg_def
            .iter()
            .filter(|(key, _val)| *key == CC_REG)
            .collect::<Vec<_>>();
        max(argc - rargs.len() as i32, 0)
    }

    /// Returns the number of bytes from RET to the first Stack Arg
    fn get_stack_arg_offset(&self, _emulator: Option<&dyn Emulator>, _argc: i32) -> i32 {
        let data = self.get_data();
        data.pad + data.align
    }

    /// Returns a list of the arguments passed to the function.
    ///
    /// Expects to be called at call/jmp to function entrypoint.
    fn get_precall_args(&self, emulator: &dyn Emulator, mut argc: i32) -> Vec<i32> {
        let data = self.get_data();
        let mut args = vec![];
        let mut sp = emulator.get_stack_counter();
        sp += data.pad;
        for (arg_type, arg_val) in data.arg_def.iter() {
            if argc <= 0 {
                break;
            }
            match *arg_type {
                CC_REG => {
                    args.push(emulator.get_register(*arg_val));
                    argc -= 1;
                }
                CC_STACK => {
                    args.push(emulator.read_memory_format(sp, "<P")[0]);
                    argc -= 1;
                    sp += data.align;
                }
                CC_STACK_INF => {
                    let mut values = emulator.read_memory_format(sp, format!("<{argc}P").as_str());
                    args.append(&mut values);
                    argc -= values.len() as i32;
                    if argc != 0 {
                        panic!("Wrong number of args from read_memory_format!");
                    }
                }
                _ => {
                    panic!("Unknown argument type: {}", arg_type);
                }
            }
        }
        args
    }

    fn set_precall_args(&self, emulator: &mut dyn Emulator, mut args: Vec<i32>) {
        let data = self.get_data();
        let mut argc = args.len() as i32;
        let mut cur_arg = 0;
        let mut sp = emulator.get_stack_counter();
        sp += data.pad;
        for (arg_type, mut arg_val) in data.arg_def.iter() {
            if argc <= 0 {
                break;
            }
            match *arg_type {
                CC_REG => {
                    emulator.set_register(arg_val, args[cur_arg]);
                    cur_arg += 1;
                }
                CC_STACK => {
                    emulator.write_memory_format(sp, "<P".to_string(), &[args[cur_arg]]);
                    argc -= 1;
                    cur_arg += 1;
                    sp += data.align;
                }
                CC_STACK_INF => {
                    arg_val -= data.align;
                    emulator.write_memory_format(sp, format!("<{argc}P"), &args[cur_arg..]);
                    argc -= args[cur_arg..].len() as i32;
                    if argc != 0 {
                        panic!("Wrong number of args from write_memory_format!");
                    }
                }
                _ => {
                    panic!("Unknown argument type: {}", arg_type);
                }
            }
        }
    }

    ///Returns a list of the arguments passed to the function.
    ///
    ///Expects to be called at the function entrypoint.
    fn get_call_args(&self, emulator: &mut dyn Emulator, argc: i32) -> Vec<i32> {
        let data = self.get_data();
        let sp = emulator.get_stack_counter();
        emulator.set_stack_counter(sp + data.delta);
        let args = self.get_precall_args(emulator, argc);
        emulator.set_stack_counter(sp);
        args
    }

    /// Returns the return address.
    ///
    /// Expects to be called at the function entrypoint.
    fn get_return_address(&self, emulator: &dyn Emulator) -> i32 {
        let data = self.get_data();
        let sp = emulator.get_stack_counter();
        emulator.read_memory_format(sp + data.ret_addr_def.1, "<P")[0]
    }

    fn exec_call_return(&self, emulator: &mut dyn Emulator, value: i32, argc: Option<i32>) -> i32 {
        let data = self.get_data();
        let sp = emulator.get_stack_counter();
        let ret_addr = self.get_return_address(emulator);
        let mut sp = sp + data.ret_val_def.1;
        if let Some(count) = argc {
            sp += count * data.align;
        }
        emulator.write_memory_format(sp, "<P".to_string(), &[value]);
        ret_addr
    }
}