1
1
import os
2
2
3
+ import binaryninja as bn
3
4
from binaryninja import BinaryView , Function
4
5
from binaryninja import CoreSymbol , Logger
5
6
from binaryninja import PluginCommand
6
7
from binaryninja import SymbolType , SymbolBinding
7
8
from binaryninja import TypeLibrary
8
9
9
10
10
- def create_type_library (log : Logger , bv : BinaryView , func_list : list [Function ]) -> TypeLibrary :
11
+ def create_type_library (log : Logger , bv : BinaryView , func_list : list [Function ], config : dict ) -> TypeLibrary :
11
12
typelib = TypeLibrary .new (bv .arch , os .path .basename (bv .file .filename ))
12
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" ]
13
22
log .log_debug (f"Exporting { len (func_list )} functions to a type library" )
14
23
for func in func_list :
15
24
bv .export_object_to_library (typelib , func .name , func .function_type )
@@ -28,8 +37,23 @@ def get_funcs_from_syms(log: Logger, bv: BinaryView, func_syms: list[CoreSymbol]
28
37
return func_list
29
38
30
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
+
31
51
def export_functions (bv : BinaryView ):
32
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
33
57
34
58
func_list = bv .get_symbols_of_type (SymbolType .FunctionSymbol )
35
59
export_func_syms = [sym for sym in func_list
@@ -38,10 +62,10 @@ def export_functions(bv: BinaryView):
38
62
export_funcs = get_funcs_from_syms (log , bv , export_func_syms )
39
63
log .log_debug (f"Discovered { len (export_funcs )} exported functions" )
40
64
41
- typelib = create_type_library (log , bv , export_funcs )
65
+ typelib = create_type_library (log , bv , export_funcs , config )
42
66
typelib .finalize ()
43
- log .log_info (f"Exported { len (export_funcs )} functions to { os . path . basename ( bv . file . filename ) } .bntl " )
44
- typelib .write_to_file (f"~/ { os . path . basename ( bv . file . filename ) } .bntl" )
67
+ log .log_info (f"Exported { len (export_funcs )} functions to { config [ 'export_path' ] } " )
68
+ typelib .write_to_file (config [ 'export_path' ] )
45
69
return
46
70
47
71
0 commit comments