-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathserver.rs
More file actions
79 lines (66 loc) · 2.71 KB
/
server.rs
File metadata and controls
79 lines (66 loc) · 2.71 KB
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
use unicorn_engine::unicorn_const::{Arch, Mode, Prot};
use unicorn_engine::{RegisterARM, Unicorn};
const PORT: u16 = 1234;
const ARM_CODE32: &[u8] = &[
0x0f, 0x00, 0xa0, 0xe1, 0x14, 0x00, 0x80, 0xe2, 0x00, 0x10, 0x90, 0xe5, 0x14, 0x10, 0x81, 0xe2, 0x00, 0x10, 0x80, 0xe5, 0xfb, 0xff, 0xff, 0xea,
];
#[allow(dead_code)]
struct CustomData {
test: [u8; 256],
test2: String,
}
fn main() {
let mut uc = Unicorn::new(Arch::ARM, Mode::LITTLE_ENDIAN).expect("Failed to initialize Unicorn instance");
uc.mem_map(0x1000, 0x400, Prot::ALL).expect("Failed to map code page");
uc.mem_write(0x1000, &ARM_CODE32).expect("Failed to write instructions");
uc.reg_write(RegisterARM::PC as i32, 0x1000).expect("Failed write PC");
udbserver::udbserver(&mut uc, PORT, 0x1000).expect("Failed to start udbserver");
uc.emu_start(0x1000, 0x2000, 0, 1000).expect("Failed to start emu");
}
#[test]
fn test_with_data() {
use std::process::Command;
use std::thread;
let data = CustomData {
test: [0; 256],
test2: String::from("ffffffffffffff"),
};
let mut uc = Unicorn::new_with_data(Arch::ARM, Mode::LITTLE_ENDIAN, data).expect("Failed to initialize Unicorn instance");
uc.mem_map(0x1000, 0x400, Prot::ALL).expect("Failed to map code page");
uc.mem_write(0x1000, &ARM_CODE32).expect("Failed to write instructions");
uc.reg_write(RegisterARM::PC as i32, 0x1000).expect("Failed write PC");
udbserver::udbserver(&mut uc, PORT, 0x1000).expect("Failed to start udbserver");
thread::spawn(move || {
let gdb_binary = if Command::new("gdb-multiarch").output().is_ok() {
"gdb-multiarch"
} else {
"gdb"
};
let output = Command::new(gdb_binary)
.arg("-nx")
.arg("-batch")
.arg("-ex")
.arg(format!("target remote localhost:{}", PORT))
.arg("-ex")
.arg("set architecture arm")
.arg("-ex")
.arg("break *0x1004")
.arg("-ex")
.arg("continue")
.arg("-ex")
.arg("stepi")
.arg("-ex")
.arg("quit")
.output()
.expect("Failed to execute GDB");
if output.status.success() {
println!("GDB interaction success");
println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout));
} else {
eprintln!("GDB interaction failed with exit code: {:?}", output.status.code());
eprintln!("stdout:\n{}", String::from_utf8_lossy(&output.stdout));
eprintln!("stderr:\n{}", String::from_utf8_lossy(&output.stderr));
}
});
uc.emu_start(0x1000, 0x2000, 0, 1000).expect("Failed to start emu");
}