forked from fkie-cad/dewolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
67 lines (52 loc) · 2.26 KB
/
__init__.py
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
"""Main plugin file registering plugin commands in bianryninja."""
from logging import info, warning
from os.path import dirname, realpath
from sys import path
from threading import Lock
from binaryninja import BackgroundTaskThread, BinaryView, Function, MessageBoxButtonSet, PluginCommand, core_ui_enabled, show_html_report
from binaryninja.interaction import show_message_box
# Add dewolf to the path in case it is not in the pythonpath already
current_dir = dirname(realpath(__file__))
path.append(current_dir)
from decompile import Decompiler
from decompiler.logger import configure_logging
from decompiler.util.decoration import DecoratedCode
from decompiler.util.options import Options
def decompile(bv: BinaryView, function: Function):
"""Decompile the target mlil_function."""
decompiler = Decompiler.from_raw(bv)
options = Options.from_gui()
task = decompiler.decompile(function, options)
show_html_report(
f"decompile {task.name}", DecoratedCode.generate_html_from_code(task.code, task.options.getstring("code-generator.style_plugin"))
)
class Decompile(BackgroundTaskThread):
"""Thread wrapper for GUI decompile"""
def __init__(self, bv, function):
BackgroundTaskThread.__init__(self, f"Decompiling {function.name}", True)
self.bv = bv
self.function = function
def run(self):
with gui_thread_lock:
info(f"[+] started decompilation: {self.function.name}")
decompile(self.bv, self.function)
def decompile_thread(bv: BinaryView, function: Function):
configure_logging() # reload settings
if gui_thread_lock.locked():
choice = show_message_box(
"Decompiler - Error",
"Only one instance of decompiler may run at a time.",
MessageBoxButtonSet.OKButtonSet,
)
thread = Decompile(bv, function)
thread.start()
if core_ui_enabled():
# register the plugin command
gui_thread_lock = Lock()
PluginCommand.register_for_function("Decompile", "decompile the current function", decompile_thread)
Options.from_gui() # register dewolf config in GUI
try:
from decompiler.util.widget import add_dock_widget
add_dock_widget()
except Exception as ex:
warning(f"failed to load widget: {ex}")