Skip to content

Commit 34ad154

Browse files
committed
stack params: emu.call32() regs+stack microsoft ABI: emu.call64() arm abi: emu.linux_call64()
1 parent 30dfaa2 commit 34ad154

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

crates/libmwemu/src/emu/initialization.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ impl Emu {
227227
.unwrap();
228228
}
229229

230+
230231
/// Initialize windows simulator, this does like init_cpu() but also setup the windows memory.
232+
/// This require having the map files in place, otherwise use just init_cpu() but emu32() and
233+
/// emu64() already call init_cpu()
231234
/// This is called from load_code if the code is a PE or shellcode.
232235
/// load_code_bytes() and other loading ways don't call this, if you need windows simulation call this.
233236
pub fn init(&mut self, clear_registers: bool, clear_flags: bool) {
@@ -381,6 +384,8 @@ impl Emu {
381384
pub fn init_mem32(&mut self) {
382385
log::info!("loading memory maps");
383386

387+
self.maps.is_64bits = false;
388+
384389
let orig_path = std::env::current_dir().unwrap();
385390
std::env::set_current_dir(self.cfg.maps_folder.clone());
386391

@@ -487,6 +492,7 @@ impl Emu {
487492
/// This is called from init(), this setup the 64bits windows memory simulation.
488493
pub fn init_mem64(&mut self) {
489494
log::info!("loading memory maps");
495+
self.maps.is_64bits = true;
490496

491497
/*
492498
let orig_path = std::env::current_dir().unwrap();

crates/libmwemu/src/tests/call32.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ use crate::tests::helpers;
22
use crate::*;
33

44
#[test]
5-
// this tests a linux 64bits flags
5+
// this tests the emu.call32()
66
pub fn call32() {
77
helpers::setup();
88

99
let mut emu = emu32();
10-
let opcodes: Vec<u8> = vec![
11-
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d, 0xfc, 0x89,
12-
0x75, 0xf8, 0x8b, 0x55, 0xfc, 0x8b, 0x45, 0xf8,
13-
0x01, 0xd0, 0x5d, 0xc3,
10+
let opcodes: Vec<u8> = vec![ //TODO: test it with 7 parameters
11+
0x55, 0x89, 0xe5, 0x83, 0xec, 0x50, 0xb8, 0x37, 0x13, 0x00, 0x00, 0x83, 0xf0, 0x7b, 0xc9, 0xc3
1412
];
15-
E emu.load_bytes(opcodes);
16-
17-
13+
emu.set_verbose(3);
14+
emu.linux = true; // otherwise I would need to set map files.
15+
emu.load_code_bytes(&opcodes);
16+
emu.regs_mut().rax = 0;
17+
let eax = emu.call32(emu.regs().rip, &[]).unwrap();
18+
assert_eq!(emu.regs().get_eax() as u32, eax);
19+
assert_eq!(eax, 0x134c);
20+
assert_eq!(emu.regs().rax, 0x134c);
1821
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::tests::helpers;
2+
use crate::*;
3+
4+
#[test]
5+
// this tests the emu.call64() Microsoft ABI
6+
pub fn call64() {
7+
helpers::setup();
8+
9+
let mut emu = emu64();
10+
let opcodes: Vec<u8> = vec![
11+
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d, 0xfc, 0x89,
12+
0x75, 0xf8, 0x8b, 0x55, 0xfc, 0x8b, 0x45, 0xf8,
13+
0x01, 0xd0, 0x5d, 0xc3,
14+
];
15+
emu.set_verbose(3);
16+
emu.linux = true; // otherwise I would need to set map files.
17+
emu.load_code_bytes(&opcodes);
18+
emu.regs_mut().rax = 0;
19+
let rax = emu.call64(emu.regs().rip, &[]).unwrap();
20+
assert_eq!(emu.regs().rip, 0x3c0013);
21+
// TODO: improve this test with something with microsoft ABI and more than 4 params.
22+
//assert_eq!(rax, 0x134c);
23+
//assert_eq!(emu.regs().rax, 0x134c);
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::tests::helpers;
2+
use crate::*;
3+
4+
#[test]
5+
// this tests the emu.linux_call64() ARM ABI (used in linux calls)
6+
pub fn linux_call64() {
7+
helpers::setup();
8+
9+
/*
10+
int test(int p1, int p2, int p3, int p4, int p5, int p6, int p7) {
11+
return p1+p2+p3+p4+p5+p6+p7;
12+
}
13+
*/
14+
15+
let mut emu = emu64();
16+
let opcodes: Vec<u8> = vec![
17+
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d, 0xfc, 0x89, 0x75, 0xf8, 0x89, 0x55,
18+
0xf4, 0x89, 0x4d, 0xf0, 0x44, 0x89, 0x45, 0xec, 0x44, 0x89, 0x4d, 0xe8,
19+
0x8b, 0x55, 0xfc, 0x8b, 0x45, 0xf8, 0x01, 0xc2, 0x8b, 0x45, 0xf4, 0x01,
20+
0xc2, 0x8b, 0x45, 0xf0, 0x01, 0xc2, 0x8b, 0x45, 0xec, 0x01, 0xc2, 0x8b,
21+
0x45, 0xe8, 0x01, 0xc2, 0x8b, 0x45, 0x10, 0x01, 0xd0, 0x5d, 0xc3
22+
];
23+
emu.set_verbose(0);
24+
emu.linux = true;
25+
emu.load_code_bytes(&opcodes);
26+
emu.regs_mut().rax = 0;
27+
let rax = emu.linux_call64(emu.regs().rip, &[1,2,3,4,5,6,7]).unwrap();
28+
assert_eq!(rax, emu.regs().rax);
29+
assert_eq!(emu.regs().rax, 1+2+3+4+5+6+7);
30+
}

crates/mwemu/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn main() {
9494
.arg(clap_arg!("trace_register", "R", "trace_register", "trace a specific register in every step, value and content", "REGISTER1,REGISTER2"))
9595
.arg(clap_arg!("console", "c", "console", "select in which moment will spawn the console to inspect.", "NUMBER"))
9696
.arg(clap_arg!("loops", "l", "loops", "show loop interations, it is slow."))
97-
.arg(clap_arg!("nocolors", "n", "nocolors", "print without colors for redirectin to a file >out"))
97+
.arg(clap_arg!("nocolors", "n", "nocolors", "print without colors for redirecting to a file >out"))
9898
.arg(clap_arg!("string", "s", "string", "monitor string on a specific address", "ADDRESS"))
9999
.arg(clap_arg!("inspect", "i", "inspect", "monitor memory like: -i 'dword ptr [ebp + 0x24]", "DIRECTION"))
100100
//.arg(clap_arg!("endpoint", "e", "endpoint", "perform communications with the endpoint, use tor or vpn!"))

0 commit comments

Comments
 (0)