Skip to content

Commit c957211

Browse files
committed
feat: modularize export functions
1 parent d309ae6 commit c957211

File tree

2 files changed

+73
-69
lines changed

2 files changed

+73
-69
lines changed

__init__.py

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,14 @@
1-
import os
1+
from binaryninja import BinaryView, PluginCommand
22

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
705

716

727
def is_valid(bv: BinaryView):
738
return bv.has_initial_analysis()
749

7510

7611
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)

export.py

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

0 commit comments

Comments
 (0)