forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
411 lines (380 loc) · 13.9 KB
/
Copy pathlib.rs
File metadata and controls
411 lines (380 loc) · 13.9 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
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
405
406
407
408
409
410
411
mod types;
use std::borrow::Cow;
use std::io::{BufRead, Cursor, Seek};
use idb_rs::id1::ID1Section;
use idb_rs::id2::{ID2Section, ID2SectionVariants};
use idb_rs::{IDAKind, IDAUsize, IDBFormat};
use types::*;
mod addr_info;
use addr_info::*;
use binaryninja::binary_view::{BinaryView, BinaryViewBase, BinaryViewExt};
use binaryninja::debuginfo::{
CustomDebugInfoParser, DebugFunctionInfo, DebugInfo, DebugInfoParser,
};
use idb_rs::id0::{ID0Section, ID0SectionVariants};
use idb_rs::til::section::TILSection;
use idb_rs::til::TypeVariant as TILTypeVariant;
use log::{error, trace, warn, LevelFilter};
use anyhow::{anyhow, Result};
use binaryninja::logger::Logger;
struct IDBDebugInfoParser;
impl CustomDebugInfoParser for IDBDebugInfoParser {
fn is_valid(&self, view: &BinaryView) -> bool {
if let Some(project_file) = view.file().project_file() {
project_file.name().as_str().ends_with(".i64")
|| project_file.name().as_str().ends_with(".idb")
} else {
view.file().filename().as_str().ends_with(".i64")
|| view.file().filename().as_str().ends_with(".idb")
}
}
fn parse_info(
&self,
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
) -> bool {
match parse_idb_info(debug_info, bv, debug_file, progress) {
Ok(()) => true,
Err(error) => {
error!("Unable to parse IDB file: {error}");
false
}
}
}
}
struct TILDebugInfoParser;
impl CustomDebugInfoParser for TILDebugInfoParser {
fn is_valid(&self, view: &BinaryView) -> bool {
if let Some(project_file) = view.file().project_file() {
project_file.name().as_str().ends_with(".til")
} else {
view.file().filename().as_str().ends_with(".til")
}
}
fn parse_info(
&self,
debug_info: &mut DebugInfo,
_bv: &BinaryView,
debug_file: &BinaryView,
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
) -> bool {
match parse_til_info(debug_info, debug_file, progress) {
Ok(()) => true,
Err(error) => {
error!("Unable to parse TIL file: {error}");
false
}
}
}
}
struct BinaryViewReader<'a> {
bv: &'a BinaryView,
offset: u64,
}
impl std::io::Read for BinaryViewReader<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if !self.bv.offset_valid(self.offset) {
// TODO check if this is truly a EoF hit, `self.bv.len()` is not
// reliable, it's returning a size bigger then the original file.
return Ok(0);
}
let len = BinaryView::read(self.bv, buf, self.offset);
self.offset += u64::try_from(len).unwrap();
Ok(len)
}
}
impl std::io::Seek for BinaryViewReader<'_> {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
let new_offset = match pos {
std::io::SeekFrom::Start(offset) => Some(offset),
std::io::SeekFrom::End(end) => self.bv.len().checked_add_signed(end),
std::io::SeekFrom::Current(next) => self.offset.checked_add_signed(next),
};
let new_offset = new_offset.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"Unable to calculate new offset in BinaryViewReader",
)
})?;
if !self.bv.offset_valid(new_offset) {
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"Try to set invalid offset in BinaryViewReader",
));
}
self.offset = new_offset;
Ok(new_offset)
}
}
fn parse_idb_info(
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
) -> Result<()> {
trace!("Opening a IDB file");
let file = BinaryViewReader {
bv: debug_file,
offset: 0,
};
trace!("Parsing a IDB file");
let mut file = std::io::BufReader::new(file);
let idb_kind = idb_rs::identify_idb_file(&mut file)?;
match idb_kind {
idb_rs::IDBFormats::Separated(sep) => {
parse_idb_info_format(debug_info, bv, debug_file, sep, file, progress)
}
idb_rs::IDBFormats::InlineUncompressed(inline) => {
parse_idb_info_format(debug_info, bv, debug_file, inline, file, progress)
}
idb_rs::IDBFormats::InlineCompressed(compressed) => {
let mut buf = vec![];
let inline = compressed.decompress_into_memory(&mut file, &mut buf)?;
parse_idb_info_format(
debug_info,
bv,
debug_file,
inline,
Cursor::new(&buf[..]),
progress,
)
}
}
}
fn parse_idb_info_format(
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
format: impl IDBFormat,
mut idb_data: impl BufRead + Seek,
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
) -> Result<()> {
let Some(id0_idx) = format.id0_location() else {
return Err(anyhow!("Unable to find the ID0 section"));
};
let Some(id1_idx) = format.id1_location() else {
return Err(anyhow!("Unable to find the ID1 section"));
};
let id2_idx = format.id2_location();
if let Some(til_idx) = format.til_location() {
trace!("Parsing the TIL section");
let til = format.read_til(&mut idb_data, til_idx)?;
// progress 0%-50%
import_til_section(debug_info, debug_file, &til, progress)?;
};
let id0 = format.read_id0(&mut idb_data, id0_idx)?;
let id1 = format.read_id1(&mut idb_data, id1_idx)?;
let id2 = id2_idx
.map(|id2_idx| format.read_id2(&mut idb_data, id2_idx))
.transpose()?;
match (id0, id2) {
(ID0SectionVariants::IDA32(id0), Some(ID2SectionVariants::IDA32(id2))) => {
parse_id0_section_info(debug_info, bv, debug_file, &id0, &id1, Some(&id2))?
}
(ID0SectionVariants::IDA32(id0), None) => {
parse_id0_section_info(debug_info, bv, debug_file, &id0, &id1, None)?
}
(ID0SectionVariants::IDA64(id0), Some(ID2SectionVariants::IDA64(id2))) => {
parse_id0_section_info(debug_info, bv, debug_file, &id0, &id1, Some(&id2))?
}
(ID0SectionVariants::IDA64(id0), None) => {
parse_id0_section_info(debug_info, bv, debug_file, &id0, &id1, None)?
}
_ => unreachable!(),
}
Ok(())
}
fn parse_til_info(
debug_info: &mut DebugInfo,
debug_file: &BinaryView,
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
) -> Result<()> {
trace!("Opening a TIL file");
let file = BinaryViewReader {
bv: debug_file,
offset: 0,
};
let mut file = std::io::BufReader::new(file);
trace!("Parsing the TIL section");
let til = TILSection::read(&mut file)?;
import_til_section(debug_info, debug_file, &til, progress)
}
pub fn import_til_section(
debug_info: &mut DebugInfo,
debug_file: &BinaryView,
til: &TILSection,
progress: impl Fn(usize, usize) -> Result<(), ()>,
) -> Result<()> {
let types = types::translate_til_types(debug_file.default_arch().unwrap(), til, progress)?;
// print any errors
for ty in &types {
match &ty.ty {
TranslateTypeResult::NotYet => {
panic!(
"type could not be processed `{}`: {:#?}",
ty.name.as_utf8_lossy(),
&ty.og_ty
);
}
TranslateTypeResult::Error(error) => {
error!(
"Unable to parse type `{}`: {error}",
ty.name.as_utf8_lossy(),
);
}
TranslateTypeResult::PartiallyTranslated(_, error) => {
if let Some(error) = error {
error!(
"Unable to parse type `{}` correctly: {error}",
ty.name.as_utf8_lossy(),
);
} else {
warn!(
"Type `{}` maybe not be fully translated",
ty.name.as_utf8_lossy(),
);
}
}
TranslateTypeResult::Translated(_) => {}
};
}
// add all type to binary ninja
for ty in &types {
if let TranslateTypeResult::Translated(bn_ty)
| TranslateTypeResult::PartiallyTranslated(bn_ty, _) = &ty.ty
{
if !debug_info.add_type(&ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
error!("Unable to add type `{}`", ty.name.as_utf8_lossy())
}
}
}
// add a second time to fix the references LOL
for ty in &types {
if let TranslateTypeResult::Translated(bn_ty)
| TranslateTypeResult::PartiallyTranslated(bn_ty, _) = &ty.ty
{
if !debug_info.add_type(&ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
error!("Unable to fix type `{}`", ty.name.as_utf8_lossy())
}
}
}
Ok(())
}
fn parse_id0_section_info<K: IDAKind>(
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
id0: &ID0Section<K>,
id1: &ID1Section,
id2: Option<&ID2Section<K>>,
) -> Result<()> {
let ida_info_idx = id0.root_node()?;
let ida_info = id0.ida_info(ida_info_idx)?;
let idb_baseaddr = ida_info.addresses.loading_base.into_u64();
let bv_baseaddr = bv.start();
let netdelta = ida_info.netdelta();
// just addr this value to the address to translate from ida to bn
// NOTE this delta could wrap here and while using translating
let addr_delta = bv_baseaddr.wrapping_sub(idb_baseaddr);
for (idb_addr, info) in get_info(id0, id1, id2, netdelta)? {
let addr = addr_delta.wrapping_add(idb_addr.into_raw().into_u64());
// just in case we change this struct in the future, this line will for us to review this code
// TODO merge this data with folder locations
let AddrInfo {
comments,
label,
ty,
} = info;
// TODO set comments to address here
for function in &bv.functions_containing(addr) {
function.set_comment_at(addr, &String::from_utf8_lossy(&comments.join(&b"\n"[..])));
}
let bnty = ty
.as_ref()
.and_then(|ty| match translate_ephemeral_type(debug_file, ty) {
TranslateTypeResult::Translated(result) => Some(result),
TranslateTypeResult::PartiallyTranslated(result, None) => {
warn!("Unable to fully translate the type at {addr:#x}");
Some(result)
}
TranslateTypeResult::NotYet => {
error!("Unable to translate the type at {addr:#x}");
None
}
TranslateTypeResult::PartiallyTranslated(_, Some(bn_type_error))
| TranslateTypeResult::Error(bn_type_error) => {
error!("Unable to translate the type at {addr:#x}: {bn_type_error}",);
None
}
});
let label: Option<Cow<'_, str>> =
label.as_ref().map(Cow::as_ref).map(String::from_utf8_lossy);
match (label, &ty, bnty) {
(label, Some(ty), bnty) if matches!(&ty.type_variant, TILTypeVariant::Function(_)) => {
if bnty.is_none() {
error!("Unable to convert the function type at {addr:#x}",)
}
if !debug_info.add_function(&DebugFunctionInfo::new(
None,
None,
label.map(Cow::into_owned),
bnty,
Some(addr),
None,
vec![],
vec![],
)) {
error!("Unable to add the function at {addr:#x}")
}
}
(label, Some(_ty), Some(bnty)) => {
if !debug_info.add_data_variable(addr, &bnty, label.as_ref().map(Cow::as_ref), &[])
{
error!("Unable to add the type at {addr:#x}")
}
}
(label, Some(_ty), None) => {
// TODO types come from the TIL sections, can we make all types be just NamedTypes?
error!("Unable to convert type {addr:#x}");
// TODO how to add a label without a type associated with it?
if let Some(name) = label {
if !debug_info.add_data_variable(
addr,
&binaryninja::types::Type::void(),
Some(&name),
&[],
) {
error!("Unable to add the label at {addr:#x}")
}
}
}
(Some(name), None, None) => {
// TODO how to add a label without a type associated with it?
if !debug_info.add_data_variable(
addr,
&binaryninja::types::Type::void(),
Some(&name),
&[],
) {
error!("Unable to add the label at {addr:#x}")
}
}
// just comments at this address
(None, None, None) => {}
(_, None, Some(_)) => unreachable!(),
}
}
Ok(())
}
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn CorePluginInit() -> bool {
Logger::new("IDB Import")
.with_level(LevelFilter::Error)
.init();
DebugInfoParser::register("IDB Parser", IDBDebugInfoParser);
DebugInfoParser::register("TIL Parser", TILDebugInfoParser);
true
}