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
use std::collections::BTreeMap;
use anyhow::Result;
use log::debug;
use crate::{
arch::Arch,
emu::{mmu::MMU, plat, Emulator},
loader::pe::PE,
VA,
};
use super::WindowsEmulator;
pub struct Win32Emulator {
pub inner: Emulator,
imports: BTreeMap<VA, String>,
}
impl Default for Win32Emulator {
fn default() -> Self {
Win32Emulator {
inner: Emulator::with_arch(Arch::X32),
imports: Default::default(),
}
}
}
impl WindowsEmulator for Win32Emulator {
fn load_pe(&mut self, pe: &PE) -> Result<()> {
debug!("emu: plat: win32: load pe");
self.inner.load_module(&pe.module)?;
let imports = plat::win::link_imports(&mut self.inner, pe)?;
self.imports.extend(imports);
Ok(())
}
fn mem(&mut self) -> &mut MMU {
&mut self.inner.mem
}
fn set_pc(&mut self, addr: VA) {
self.inner.reg.set_eip(addr as u32);
}
fn pc(&self) -> VA {
self.inner.reg.eip() as VA
}
fn set_sp(&mut self, addr: VA) {
self.inner.reg.set_esp(addr as u32);
}
fn sp(&self) -> VA {
self.inner.reg.esp() as VA
}
/// note: truncates `value` to u32.
///
/// Errors:
/// - WriteError::AddressNotMapped when stack is not mapped.
/// - WriteError::AccessViolation when stack is not writable.
fn push(&mut self, value: u64) -> Result<()> {
let old_sp = self.sp();
let new_sp = old_sp - 4;
// write to mem first, since it could fail.
self.mem().write_u32(new_sp, value as u32)?;
self.set_sp(new_sp);
Ok(())
}
/// note: returns u32 value.
///
/// Errors:
/// - ReadError::AddressNotMapped when stack is not mapped.
/// - ReadError::AccessViolation when stack is not readable.
fn pop(&mut self) -> Result<u64> {
let old_sp = self.sp();
let new_sp = old_sp + 4;
// write to mem first, since it could fail.
let value = self.mem().read_u32(old_sp)? as u64;
self.set_sp(new_sp);
Ok(value)
}
fn set_bp(&mut self, addr: VA) {
self.inner.reg.set_ebp(addr as u32);
}
fn bp(&self) -> VA {
self.inner.reg.esp() as VA
}
fn set_fsbase(&mut self, addr: VA) {
self.inner.set_fsbase(addr);
}
// TODO: sketching this out
fn resolve_address(&self, addr: VA) -> Option<String> {
self.imports.get(&addr).cloned()
}
}
impl Win32Emulator {
pub fn handle_api(&mut self) -> Result<()> {
use super::api::CallingConvention;
if let Some(symbol) = self.resolve_address(self.pc()) {
if let Some(api) = super::api::API.get(&symbol) {
let ra = self.pop()?;
self.set_pc(ra);
if let CallingConvention::Stdcall = api.calling_convention {
for _ in 0..api.arguments.len() {
let _ = self.pop()?;
}
}
Ok(())
} else {
// we dont know anything about the API
// its probably stdcall, but we dont know how many arguments.
// TODO
unimplemented!("unknown API");
}
} else {
// we don't know what API this is.
// TODO
unimplemented!("unresolved API");
}
}
}
#[cfg(test)]
mod tests {
use crate::{
emu::{plat::win::win32::*, *},
rsrc::*,
};
/// Errors:
/// - FetchError::InvalidInstruction for instructions that cannot be
/// decoded.
/// - FetchError::AddressNotMapped when the instruction address is not
/// mapped.
/// - FetchError::AccessViolation when the instruction address is not
/// executable.
/// - WriteError::AddressNotMapped when a memory address is not mapped.
/// - WriteError::AccessViolation when a memory address is not executable.
/// - ReadError::AddressNotMapped when a memory address is not mapped.
/// - ReadError::AccessViolation when a memory address is not readable.
///
/// TODO: this routine should not be part of the base emulator.
/// there are too many customizations to handle, like how to deal with
/// imports, styles of breakpoints, etc.
/// so, provide documentation/examples of `step` routines and remove this.
fn step(emu: &mut Win32Emulator) -> Result<()> {
let insn = emu.inner.fetch()?;
emu.inner.execute(&insn)?;
// example: https://github.com/williballenthin/viv-utils/blob/master/viv_utils/emulator_drivers.py
Ok(())
}
#[test]
fn nop() -> Result<()> {
//init_logging();
let pe = crate::loader::pe::PE::from_bytes(&get_buf(Rsrc::NOP))?;
let mut emu: Win32Emulator = Default::default();
emu.load_pe(&pe)?;
let opt = pe.optional_header.unwrap();
let ep = opt.windows_fields.image_base + opt.standard_fields.address_of_entry_point;
emu.set_pc(ep);
emu.mem().mmap(0x5000, 0x2000, Permissions::RW)?;
emu.set_sp(0x6000);
emu.set_bp(0x6000);
emu.mem().mmap(0x7000, 0x1000, Permissions::RW)?;
emu.set_fsbase(0x7000);
// .text:00401081 push 18h
// .text:00401083 push offset stru_406160
// .text:00401088 call __SEH_prolog
assert_eq!(emu.pc(), 0x401081);
step(&mut emu)?; // push
step(&mut emu)?; // push
step(&mut emu)?; // call
// .text:004027A0 __SEH_prolog proc near
// .text:004027A0 push offset __except_handler3
// .text:004027A5 mov eax, large fs:0
// .text:004027AB push eax
// .text:004027AC mov eax, [esp+8+arg_4]
// .text:004027B0 mov [esp+8+arg_4], ebp
// .text:004027B4 lea ebp, [esp+8+arg_4]
// .text:004027B8 sub esp, eax
assert_eq!(emu.pc(), 0x4027A0);
step(&mut emu)?; // push
step(&mut emu)?; // mov
step(&mut emu)?; // push
step(&mut emu)?; // mov
step(&mut emu)?; // mov
step(&mut emu)?; // lea
step(&mut emu)?; // sub
while emu.pc() != 0x4027DA {
step(&mut emu)?;
}
// .text:004027DA retn
step(&mut emu)?; // retn
assert_eq!(emu.pc(), 0x40108D);
// .text:0040108D mov edi, 94h ; '”'
// .text:00401092 mov eax, edi
// .text:00401094 call __alloca_probe
step(&mut emu)?; // mov
step(&mut emu)?; // mov
step(&mut emu)?; // call
assert_eq!(emu.pc(), 0x402900);
// .text:00402900 cmp eax, 1000h
// .text:00402905 jnb short probesetup
step(&mut emu)?; // cmp
step(&mut emu)?; // jnb
assert_eq!(emu.pc(), 0x402907);
// .text:00402907 neg eax
// .text:00402909 add eax, esp
// .text:0040290B add eax, 4
// .text:0040290E test [eax], eax
// .text:00402910 xchg eax, esp
// .text:00402911 mov eax, [eax]
// .text:00402913 push eax
// .text:00402914 retn
step(&mut emu)?; // neg
step(&mut emu)?; // add
step(&mut emu)?; // add
step(&mut emu)?; // test
step(&mut emu)?; // xchg
step(&mut emu)?; // mov
step(&mut emu)?; // push
step(&mut emu)?; // ret
assert_eq!(emu.pc(), 0x401099);
// .text:00401099 mov [ebp+ms_exc.old_esp], esp
// .text:0040109C mov esi, esp
// .text:0040109E mov [esi], edi
// .text:004010A0 push esi ; lpVersionInformation
step(&mut emu)?; // mov
step(&mut emu)?; // mov
step(&mut emu)?; // mov
step(&mut emu)?; // push
// handling imports:
//
// next is call to import GetVersionExA.
// .text:004010A1 call ds:GetVersionExA
assert_eq!(emu.pc(), 0x4010A1);
step(&mut emu)?; // call ds:[0x406008]
// we've mapped the IAT entry to point to the OFT.
//
// FT OFT
// 0x406008 -> 0x406e88 (hint: 0x01DF, name: GetVersionExA)
assert_eq!(emu.pc(), 0x406e88);
// but this isn't a code segment,
// so if we try to execute it, it will fail with a FetchError
let e = step(&mut emu).unwrap_err(); // GetVersionExA impl
// demonstrate how to recover the called imported API.
if let Some(FetchError::AccessViolation { va, .. }) = e.downcast_ref::<FetchError>() {
assert_eq!(emu.resolve_address(*va).unwrap(), "kernel32.dll!GetVersionExA");
}
emu.handle_api()?;
assert_eq!(emu.pc(), 0x4010A7);
Ok(())
}
}