-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major Refactor: Restructure backend to depend on LibBS (#22)
* Running script in decompiler stubs * More changes * Working abstraction tested in IDA. Still need an `askKey` api * Fixed the Binja side code * Add some headless Ghidra code * Ghidra, IDA, Binja working. Ghidra still needs a way to start UI without terminal * Update the README, and GHIDRA WORKS!!!
- Loading branch information
Showing
25 changed files
with
666 additions
and
1,300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
*.pyc | ||
.idea/ | ||
*.egg-info/ | ||
testing/ | ||
*.o | ||
*.so | ||
*.a | ||
.gdb_history | ||
*.i64 | ||
*.idb | ||
*.id0 | ||
*.id1 | ||
*.id2 | ||
*.nam | ||
*.til | ||
*.swp | ||
*.dll | ||
*.obj | ||
*.lib | ||
*.exp | ||
*.pdb | ||
*.ilk | ||
angr/tests/*.png | ||
screenlog.0 | ||
angr/tests/screenlog.0 | ||
angr/screenlog.0 | ||
.idea | ||
*.egg-info | ||
/build | ||
/tags | ||
MANIFEST | ||
dist | ||
.eggs | ||
.vscode/ | ||
*.db | ||
.DS_Store | ||
.pytest_cache/ | ||
binsync/decompilers/ghidra/client/build/ | ||
binsync/decompilers/ghidra/client/dist/ | ||
binsync/decompilers/ghidra/client/bin/ | ||
binsync/decompilers/ghidra/client/.gradle/ | ||
binsync/decompilers/ghidra/client/.classpath | ||
binsync/decompilers/ghidra/client/.project | ||
binsync/decompilers/ghidra/client/Ghidra/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,31 @@ | ||
__version__ = "1.3.0" | ||
__version__ = "2.0.0" | ||
|
||
from .api import AIAPI, OpenAIAPI | ||
from libbs.api import DecompilerInterface | ||
|
||
|
||
def create_plugin(*args, **kwargs): | ||
|
||
ai_api = OpenAIAPI(delay_init=True) | ||
# create context menus for prompts | ||
gui_ctx_menu_actions = { | ||
f"DAILA/{prompt_name}": (prompt.desc, getattr(ai_api, prompt_name)) | ||
for prompt_name, prompt in ai_api.prompts_by_name.items() | ||
} | ||
# create context menus for others | ||
gui_ctx_menu_actions["DAILA/Update API Key"] = ("Update API Key", ai_api.ask_api_key) | ||
|
||
# create decompiler interface | ||
force_decompiler = kwargs.pop("force_decompiler", None) | ||
deci = DecompilerInterface.discover_interface( | ||
force_decompiler=force_decompiler, | ||
# decompiler-creation args | ||
plugin_name="DAILA", | ||
init_plugin=True, | ||
gui_ctx_menu_actions=gui_ctx_menu_actions, | ||
ui_init_args=args, | ||
ui_init_kwargs=kwargs | ||
) | ||
ai_api.init_decompiler_interface(decompiler_interface=deci) | ||
|
||
return deci.gui_plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .ai_api import AIAPI | ||
from .openai import OpenAIAPI, Prompt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from typing import Dict, Optional | ||
from functools import wraps | ||
|
||
from libbs.api import DecompilerInterface | ||
|
||
|
||
class AIAPI: | ||
def __init__( | ||
self, | ||
decompiler_interface: Optional[DecompilerInterface] = None, | ||
decompiler_name: Optional[str] = None, | ||
use_decompiler: bool = True, | ||
delay_init: bool = False, | ||
# size in bytes | ||
min_func_size: int = 0x10, | ||
max_func_size: int = 0xffff, | ||
model=None, | ||
): | ||
# useful for initing after the creation of a decompiler interface | ||
self._dec_interface: Optional[DecompilerInterface] = None | ||
self._dec_name = None | ||
if not delay_init: | ||
self.init_decompiler_interface(decompiler_interface, decompiler_name, use_decompiler) | ||
|
||
self._min_func_size = min_func_size | ||
self._max_func_size = max_func_size | ||
self.model = model or self.__class__.__name__ | ||
|
||
def init_decompiler_interface( | ||
self, | ||
decompiler_interface: Optional[DecompilerInterface] = None, | ||
decompiler_name: Optional[str] = None, | ||
use_decompiler: bool = True | ||
): | ||
self._dec_interface: DecompilerInterface = DecompilerInterface.discover_interface(force_decompiler=decompiler_name) \ | ||
if use_decompiler and decompiler_interface is None else decompiler_interface | ||
self._dec_name = decompiler_name if decompiler_interface is None else decompiler_interface.name | ||
if self._dec_interface is None and not self._dec_name: | ||
raise ValueError("You must either provide a decompiler name or a decompiler interface.") | ||
|
||
def info(self, msg): | ||
if self._dec_interface is not None: | ||
self._dec_interface.info(msg) | ||
|
||
def debug(self, msg): | ||
if self._dec_interface is not None: | ||
self._dec_interface.debug(msg) | ||
|
||
def warning(self, msg): | ||
if self._dec_interface is not None: | ||
self._dec_interface.warning(msg) | ||
|
||
def error(self, msg): | ||
if self._dec_interface is not None: | ||
self._dec_interface.error(msg) | ||
|
||
@property | ||
def has_decompiler_gui(self): | ||
return self._dec_interface is not None and not self._dec_interface.headless | ||
|
||
@staticmethod | ||
def requires_function(f): | ||
""" | ||
A wrapper function to make sure an API call has decompilation text to operate on and possibly a Function | ||
object. There are really two modes any API call operates in: | ||
1. Without Decompiler Backend: requires provided dec text | ||
2. With Decompiler Backend: | ||
2a. With UI: Function will be collected from the UI if not provided | ||
2b. Without UI: requires a FunctionA | ||
The Function collected from the UI is the one the use is currently looking at. | ||
""" | ||
@wraps(f) | ||
def _requires_function(*args, ai_api: "AIAPI" = None, **kwargs): | ||
function = kwargs.pop("function", None) | ||
dec_text = kwargs.pop("dec_text", None) | ||
use_dec = kwargs.pop("use_dec", True) | ||
|
||
if not dec_text and not use_dec: | ||
raise ValueError("You must provide decompile text if you are not using a dec backend") | ||
|
||
# two mode constructions: with decompiler and without | ||
# with decompiler backend | ||
if use_dec: | ||
if not ai_api.has_decompiler_gui and function is None: | ||
raise ValueError("You must provide a Function when using this with a decompiler") | ||
|
||
# we must have a UI if we have no func | ||
if function is None: | ||
function = ai_api._dec_interface.active_context() | ||
|
||
# get new text with the function that is present | ||
if dec_text is None: | ||
dec_text = ai_api._dec_interface.decompile(function.addr) | ||
|
||
return f(*args, function=function, dec_text=dec_text, use_dec=use_dec, **kwargs) | ||
|
||
return _requires_function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .openai_api import OpenAIAPI | ||
from .prompts import Prompt |
Oops, something went wrong.