-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathlib.rs
More file actions
466 lines (424 loc) · 15 KB
/
lib.rs
File metadata and controls
466 lines (424 loc) · 15 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
mod types;
use std::fs::File;
use std::io::BufReader;
use binaryninja::background_task::BackgroundTask;
use binaryninja::command::Command;
use binaryninja::string::BnStrCompatible;
use binaryninja::type_library::TypeLibrary;
use binaryninja::types::QualifiedName;
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, IDBParam1, IDBParam2};
use idb_rs::til::section::TILSection;
use idb_rs::til::TypeVariant as TILTypeVariant;
use log::{error, trace, warn, LevelFilter};
use anyhow::{anyhow, Context, 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 import_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 import_til_info_from_debug_file(debug_info, bv, debug_file, progress) {
Ok(()) => true,
Err(error) => {
error!("Unable to parse TIL file: {error}");
false
}
}
}
}
struct LoadTilFile;
impl Command for LoadTilFile {
fn action(&self, view: &BinaryView) {
if let Err(error) = background_import_til(view) {
error!("Unable to convert TIL file: {error}");
}
}
fn valid(&self, _view: &BinaryView) -> bool {
true
}
}
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) {
return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, ""));
}
let len = self.bv.read(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, ""))?;
if !self.bv.offset_valid(new_offset) {
return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, ""));
}
self.offset = new_offset;
Ok(new_offset)
}
}
fn import_idb_info<P: Fn(usize, usize) -> Result<(), ()>>(
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
progress: P,
) -> Result<()> {
trace!("Opening a IDB file");
let file = BufReader::new(BinaryViewReader {
bv: debug_file,
offset: 0,
});
trace!("Parsing a IDB file");
let mut parser = idb_rs::IDBParser::new(file)?;
if let Some(til_section) = parser.til_section_offset() {
// TODO handle dependency, create a function for that with closures
trace!("Parsing the TIL section");
let til = parser.read_til_section(til_section)?;
let filename = debug_file.file().filename();
// TODO progress 0%-50%
import_til_to_type_library(til, filename, bv, progress)?;
}
if let Some(id0_section) = parser.id0_section_offset() {
trace!("Parsing the ID0 section");
let id0 = parser.read_id0_section(id0_section)?;
// TODO progress 50%-100%
parse_id0_section_info(debug_info, bv, debug_file, &id0)?;
}
Ok(())
}
fn import_til_info_from_debug_file<P: Fn(usize, usize) -> Result<(), ()>>(
_debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
progress: P,
) -> Result<()> {
trace!("Opening a TIL file");
let file = BinaryViewReader {
bv: debug_file,
offset: 0,
};
let filename = debug_file.file().filename();
let mut file = std::io::BufReader::new(file);
let til = TILSection::read(&mut file, idb_rs::IDBSectionCompression::None)?;
import_til_to_type_library(til, filename, bv, progress)
}
fn background_import_til(view: &BinaryView) -> Result<()> {
let moved_view = view.to_owned();
binaryninja::worker_thread::execute_on_worker_thread_interactive(c"Til Import", move || {
if let Err(err) = interactive_import_til(&moved_view) {
error!("Unable to import TIL: {err}");
}
});
Ok(())
}
fn interactive_import_til(view: &BinaryView) -> Result<()> {
let bt = BackgroundTask::new("Import TIL", true);
let Some(file) =
binaryninja::interaction::get_open_filename_input("Select a .til file", "*.til")
else {
return Ok(());
};
let filename = file.file_name().unwrap().to_string_lossy();
let mut file = BufReader::new(File::open(&file)?);
let til = TILSection::read(&mut file, idb_rs::IDBSectionCompression::None)?;
let progress = |current, total| {
if bt.is_cancelled() {
return Err(());
}
bt.set_progress_text(format!(
"Import TIL progress: {}%",
((current as f32 / total as f32) * 100f32) as u32
));
Ok(())
};
import_til_to_type_library(til, filename, view, progress)?;
bt.finish();
Ok(())
}
fn import_til_to_type_library<S, P>(
til: TILSection,
type_lib_name: S,
view: &BinaryView,
progress: P,
) -> Result<()>
where
S: BnStrCompatible,
P: Fn(usize, usize) -> Result<(), ()>,
{
let default_arch = view
.default_arch()
.ok_or_else(|| anyhow!("Unable to get the default arch"))?;
let mut type_lib = TypeLibrary::new(default_arch, type_lib_name);
// TODO create a background task to not freeze bn, also create a progress
// user interface feedback
import_til_file(til, view, &mut type_lib, progress)?;
if !type_lib.finalize() {
return Err(anyhow!("Unable to finalize TypeLibrary"));
};
view.add_type_library(&type_lib);
Ok(())
}
fn import_til_file<P: Fn(usize, usize) -> Result<(), ()>>(
til: TILSection,
view: &BinaryView,
type_library: &mut TypeLibrary,
progress: P,
) -> Result<()> {
trace!("Parsing the TIL section");
// TODO
let mut tils = vec![til];
import_til_dependency(&mut tils, 0, &progress)?;
import_til_section(type_library, view, &tils, progress)
}
fn import_til_dependency<P: Fn(usize, usize) -> Result<(), ()>>(
tils: &mut Vec<TILSection>,
til_idx: usize,
_progress: &P,
) -> Result<()> {
let names: Vec<_> = tils[til_idx].header.dependencies.to_vec();
for name in names {
let name = name.as_utf8_lossy();
let message = format!("Select the dependency \"{name}.til\"",);
let Some(dep_file) = binaryninja::interaction::get_open_filename_input(&message, "*.til")
else {
return Err(anyhow!("Unable to get the dependency {name}"));
};
let mut dep_file = BufReader::new(File::open(&dep_file)?);
let dep_til = TILSection::read(&mut dep_file, idb_rs::IDBSectionCompression::None)?;
tils.push(dep_til);
}
if tils.len() > til_idx + 1 {
// add dependencies of dependencies
// TODO handle progress
// TODO identify ciclycal dependencies
import_til_dependency(tils, til_idx + 1, _progress)
.context("While importing dependency {name}")?;
}
Ok(())
}
fn import_til_section<P: Fn(usize, usize) -> Result<(), ()>>(
type_library: &mut TypeLibrary,
view: &BinaryView,
tils: &[TILSection],
progress: P,
) -> Result<()> {
let default_arch = view
.default_arch()
.ok_or_else(|| anyhow!("Unable to get the default arch"))?;
let types = types::translate_til_types(default_arch, tils, progress)?;
// print any errors
print_til_convertsion_errors(&types)?;
// add all type to the type library
for ty in &types {
if let TranslateTypeResult::Translated(bn_ty)
| TranslateTypeResult::PartiallyTranslated(bn_ty, _) = &ty.ty
{
let name = QualifiedName::new(vec![ty.name.as_utf8_lossy().to_string()]);
type_library.add_named_type(name, bn_ty);
}
}
Ok(())
}
fn print_til_convertsion_errors(types: &[TranslatesIDBType]) -> Result<()> {
for ty in types {
match &ty.ty {
TranslateTypeResult::NotYet => {
// NOTE this should be unreachable
error!(
"Unable to finish parsing type `{}`",
ty.name.as_utf8_lossy(),
);
}
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 correctly parse type `{}`: {error}",
ty.name.as_utf8_lossy(),
);
} else {
warn!(
"Type `{}` maybe not be fully translated",
ty.name.as_utf8_lossy(),
);
}
}
TranslateTypeResult::Translated(_) => {}
};
}
Ok(())
}
fn parse_id0_section_info(
debug_info: &mut DebugInfo,
bv: &BinaryView,
debug_file: &BinaryView,
id0: &ID0Section,
) -> Result<()> {
let version = match id0.ida_info()? {
idb_rs::id0::IDBParam::V1(IDBParam1 { version, .. })
| idb_rs::id0::IDBParam::V2(IDBParam2 { version, .. }) => version,
};
for (addr, info) in get_info(id0, version)? {
// 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"[..])).to_string(),
);
}
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
}
});
match (label, &ty, bnty) {
(_, 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(str::to_string),
bnty,
Some(addr),
None,
vec![],
vec![],
)) {
error!("Unable to add the function at {addr:#x}")
}
}
(_, Some(_ty), Some(bnty)) => {
if !debug_info.add_data_variable(addr, &bnty, label, &[]) {
error!("Unable to add the type at {addr:#x}")
}
}
(_, 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 associacted 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 associacted 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();
binaryninja::command::register_command(
c"Import TIL types",
c"Convert and import a TIL file into a TypeLibrary",
LoadTilFile,
);
DebugInfoParser::register(c"IDB Parser", IDBDebugInfoParser);
DebugInfoParser::register(c"TIL Parser", TILDebugInfoParser);
true
}