-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_binary.rs
132 lines (115 loc) · 3.15 KB
/
test_binary.rs
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
// Guillaume Valadon <guillaume@valadon.net>
// binutils - test_binary.rs
extern crate libc;
use libc::{c_ulong, uintptr_t};
extern crate binutils;
use binutils::bfd;
use binutils::instruction;
use binutils::opcodes::DisassembleInfo;
use binutils::utils;
extern "C" fn change_address(addr: c_ulong, _info: *const uintptr_t) {
// Example of C callback that modifies an address used by an instruction
// Format the address and copy it to the buffer
utils::opcode_buffer_append(&format!("0x{:x}", addr));
}
fn test_ls(max_instructions: Option<u32>) {
println!("From an ELF");
let bfd = match bfd::Bfd::openr("/bin/ls", "elf64-x86-64") {
Ok(b) => b,
Err(e) => {
println!("Error with openr() - {}", e);
return;
}
};
match bfd.check_format(bfd::BfdFormat::bfd_object) {
Ok(_) => (),
Err(e) => {
println!("Error with check_format() - {}", e);
return;
}
};
// Retrieve the .text code section
let section = match bfd.get_section_by_name(".text") {
Ok(s) => s,
Err(e) => {
println!("Error with get_section_by_name() - {}", e);
return;
}
};
// Construct disassembler_ftype class
let disassemble = match bfd.disassembler() {
Ok(d) => d,
Err(e) => {
println!("Error with disassembler() - {}", e);
return;
}
};
// Create a disassemble_info structure
let info = match DisassembleInfo::new() {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
// Configure the disassemble_info structure
match info.configure(section, bfd) {
Ok(_) => (),
Err(e) => {
println!("Error configure() - {}", e);
return;
}
};
match info.set_print_address_func(change_address) {
Ok(_) => (),
Err(e) => {
println!("Error set_print_address_func() - {}", e);
return;
}
};
match info.init() {
Ok(_) => (),
Err(e) => {
println!("Error init() - {}", e);
return;
}
};
// Disassemble the binary
let mut pc = match bfd.get_start_address() {
Ok(a) => a,
Err(e) => {
println!("Error with get_start_address() - {}", e);
return;
}
};
let section_size = match section.get_size() {
Ok(a) => a,
Err(e) => {
println!("Error with get_size() - {}", e);
return;
}
};
let mut counter = 0;
loop {
let length = disassemble(pc, &info);
let instruction = match instruction::get_instruction(pc, length) {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
println!("{}", instruction);
pc += length;
counter += 1;
if !(length > 0 && pc <= section_size) {
break;
}
if !max_instructions.is_none() && max_instructions.unwrap() <= counter {
break;
}
}
}
fn main() {
test_ls(Some(65535));
}