|
1 |
| -import os |
| 1 | +from binaryninja import BinaryView, PluginCommand |
2 | 2 |
|
3 |
| -import binaryninja as bn |
4 |
| -from binaryninja import BinaryView, Function |
5 |
| -from binaryninja import CoreSymbol, Logger |
6 |
| -from binaryninja import PluginCommand |
7 |
| -from binaryninja import SymbolType, SymbolBinding |
8 |
| -from binaryninja import TypeLibrary |
9 |
| - |
10 |
| - |
11 |
| -def create_type_library(log: Logger, bv: BinaryView, func_list: list[Function], config: dict) -> TypeLibrary: |
12 |
| - typelib = TypeLibrary.new(bv.arch, os.path.basename(bv.file.filename)) |
13 |
| - typelib.add_platform(bv.platform) |
14 |
| - |
15 |
| - if "alternate_names" not in config: |
16 |
| - name_list = [name.strip() for name in config["alternate_names"].split(";")] |
17 |
| - for name in name_list: |
18 |
| - typelib.add_alternate_name(name) |
19 |
| - |
20 |
| - if "dependency_name" not in config: |
21 |
| - typelib.dependency_name = config["dependency_name"] |
22 |
| - log.log_debug(f"Exporting {len(func_list)} functions to a type library") |
23 |
| - for func in func_list: |
24 |
| - bv.export_object_to_library(typelib, func.name, func.function_type) |
25 |
| - return typelib |
26 |
| - |
27 |
| - |
28 |
| -def get_funcs_from_syms(log: Logger, bv: BinaryView, func_syms: list[CoreSymbol]) -> list[Function]: |
29 |
| - func_list = [] |
30 |
| - for sym in func_syms: |
31 |
| - res = bv.get_function_at(sym.address) |
32 |
| - if res is None: |
33 |
| - log.log_warn(f"Function: {sym.name} at address: {sym.address} does not exist in the current binary view") |
34 |
| - else: |
35 |
| - func_list.append(res) |
36 |
| - |
37 |
| - return func_list |
38 |
| - |
39 |
| - |
40 |
| -def get_config_options(bv: BinaryView): |
41 |
| - alternate_names = bn.TextLineField("Alternative Names (optional):", "lib_musl.so;lib_musl.so.5") |
42 |
| - export_path = bn.TextLineField("Path to store type library:", f"{bv.file.filename}.bntl") |
43 |
| - dependency_name = bn.TextLineField("Dependency Name (optional):") |
44 |
| - bn.get_form_input([alternate_names, export_path, dependency_name], "Export as Type Library Options") |
45 |
| - |
46 |
| - config = {"alternate_names": alternate_names.result, "export_path": export_path.result, |
47 |
| - "dependency_name": dependency_name.result} |
48 |
| - return config |
49 |
| - |
50 |
| - |
51 |
| -def export_functions(bv: BinaryView): |
52 |
| - log = bv.create_logger("TypeLib_Exporter") |
53 |
| - config = get_config_options(bv) |
54 |
| - if not os.path.exists(config['export_path']): |
55 |
| - log.log_error("Please specify a path to export the type library") |
56 |
| - return |
57 |
| - |
58 |
| - func_list = bv.get_symbols_of_type(SymbolType.FunctionSymbol) |
59 |
| - export_func_syms = [sym for sym in func_list |
60 |
| - if sym.binding == SymbolBinding.GlobalBinding or sym.binding == SymbolBinding.WeakBinding] |
61 |
| - |
62 |
| - export_funcs = get_funcs_from_syms(log, bv, export_func_syms) |
63 |
| - log.log_debug(f"Discovered {len(export_funcs)} exported functions") |
64 |
| - |
65 |
| - typelib = create_type_library(log, bv, export_funcs, config) |
66 |
| - typelib.finalize() |
67 |
| - log.log_info(f"Exported {len(export_funcs)} functions to {config['export_path']}") |
68 |
| - typelib.write_to_file(config['export_path']) |
69 |
| - return |
| 3 | +from . import apply |
| 4 | +from . import export |
70 | 5 |
|
71 | 6 |
|
72 | 7 | def is_valid(bv: BinaryView):
|
73 | 8 | return bv.has_initial_analysis()
|
74 | 9 |
|
75 | 10 |
|
76 | 11 | PluginCommand.register("Export As Type Library", "Compiles the exported function types into a type library",
|
77 |
| - export_functions, is_valid) |
| 12 | + export.export_functions, is_valid) |
| 13 | +PluginCommand.register("Apply Type Library", "Loads and applies the specified type library to the current binary view", |
| 14 | + apply.load_library, is_valid) |
0 commit comments