-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport.py
More file actions
102 lines (82 loc) · 3.42 KB
/
export.py
File metadata and controls
102 lines (82 loc) · 3.42 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
import os
import binaryninja as bn
from binaryninja import BinaryView
from binaryninja import CoreSymbol, Logger
from binaryninja import SymbolType, SymbolBinding
from binaryninja import TypeLibrary, Function, Metadata
def create_type_library(
log: Logger, bv: BinaryView, func_list: list[Function], config: dict
) -> TypeLibrary:
typelib = TypeLibrary.new(bv.arch, config["lib_name"])
typelib.add_platform(bv.platform)
if "alternate_names" in config:
name_list = [name.strip() for name in config["alternate_names"].split(";")]
for name in name_list:
typelib.add_alternate_name(name)
if "dependency_name" in config:
typelib.dependency_name = config["dependency_name"]
log.log_info(f'Exporting {len(export_func_syms)} ordinals to type library')
ordinals = {sym.ordinal: sym.name for sym in export_func_syms}
typelib.store_metadata('ordinals', Metadata(ordinals))
func_list = get_funcs_from_syms(log, bv, export_func_syms)
log.log_debug(f"Exporting {len(func_list)} functions to type library")
for func in func_list:
bv.export_object_to_library(typelib, func.name, func.type)
log.log_info(f"Exported {len(func_list)} functions to {config['export_path']}")
return typelib
def get_funcs_from_syms(
log: Logger, bv: BinaryView, func_syms: list[CoreSymbol]
) -> list[Function]:
func_list = []
for sym in func_syms:
res = bv.get_function_at(sym.address)
if res is None:
log.log_warn(
f"Function: {sym.name} at address: {sym.address} does not exist in the current binary view"
)
else:
func_list.append(res)
return func_list
def get_config_options(bv: BinaryView):
lib_name = bn.TextLineField(
"Type Library Name:", f"{os.path.basename(bv.file.filename)}"
)
alternate_names = bn.TextLineField(
"Alternative Names (optional):", "lib_musl.so;lib_musl.so.5"
)
export_path = bn.TextLineField(
"Path to store type library:", f"~/{os.path.basename(bv.file.filename)}.bntl"
)
dependency_name = bn.TextLineField("Dependency Name (optional):")
bn.get_form_input(
[lib_name, alternate_names, export_path, dependency_name],
"Export as Type Library Options",
)
return {
"lib_name": lib_name.result,
"alternate_names": alternate_names.result,
"export_path": os.path.expanduser(export_path.result),
"dependency_name": dependency_name.result,
}
def export_functions(bv: BinaryView):
log = bv.create_logger("TypeLib_Exporter")
config = get_config_options(bv)
if not os.path.exists(os.path.dirname(config["export_path"])):
log.log_error(
f"Please specify a path to export the type library: {config['export_path']}"
)
return
if len(config["lib_name"]) == 0:
log.log_error(f"lib name: [{config['lib_name']}]")
log.log_error("Please specify a name for the type library")
return
func_list = bv.get_symbols_of_type(SymbolType.FunctionSymbol)
export_func_syms = [
sym
for sym in func_list
if sym.binding in (SymbolBinding.GlobalBinding, SymbolBinding.WeakBinding)
]
typelib = create_type_library(log, bv, export_func_syms, config)
typelib.finalize()
log.log_info(f"Exported {len(export_funcs)} functions to {config['export_path']}")
typelib.write_to_file(config["export_path"])