Skip to content

Commit aacb73c

Browse files
authored
Merge pull request #19 from WhatTheFuzz/feature/C
Feature/c
2 parents 599192f + 964caad commit aacb73c

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
Integrates OpenAI's GPT3 with Binary Ninja via a plugin. Creates a query asking
66
"What does this function do?" followed by the instructions in the High Level IL
7-
function. Returns the response to the user in Binary Ninja's console.
7+
function or the decompiled pseudo-C. Returns the response to the user in Binary
8+
Ninja's console.
89

910
## Installation
1011

__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
"saved under the environment variable "
1212
"OPENAI_API_KEY or modify the path in entry.py.",
1313
check_function)
14+
15+
PluginCommand.register_for_function("OpenAI\What Does this Function Do (Pseudo-C)?",
16+
"Checks OpenAI to see what this pseudo-C function does." \
17+
"Requires an internet connection and an API key "
18+
"saved under the environment variable "
19+
"OPENAI_API_KEY or modify the path in entry.py.",
20+
check_function)

plugin.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
"Linux"
2020
],
2121
"installinstructions": {
22-
"Darwin": "pip3 install --user openai. Under your OpenAI account, create an API key and set it to the environment variable OPENAI_API_KEY inside Binary Ninja's Python console.",
23-
"Linux": "pip3 install --user openai. Under your OpenAI account, create an API key and set it to the environment variable OPENAI_API_KEY inside Binary Ninja's Python console."
22+
"Darwin": "`pip3 install --user openai`.\n Add your OpenAI API key to the OpenAI preferences in Binary Ninja.",
23+
"Linux": "`pip3 install --user openai`.\n Add your OpenAI API key to the OpenAI preferences in Binary Ninja."
2424
},
2525
"dependencies": {
2626
"pip": [
2727
"openai"
2828
]
2929
},
30-
"version": "1.3.0",
30+
"version": "1.4.0",
3131
"minimumbinaryninjaversion": 3200
3232
}

src/agent.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,36 @@
66
from openai.api_resources.model import Model
77
from openai.error import APIError
88

9+
from binaryninja.function import Function
910
from binaryninja.lowlevelil import LowLevelILFunction
1011
from binaryninja.mediumlevelil import MediumLevelILFunction
1112
from binaryninja.highlevelil import HighLevelILFunction
1213
from binaryninja.settings import Settings
13-
from binaryninja import log
14+
from binaryninja import log, BinaryView
1415

1516
from . query import Query
17+
from . c import Pseudo_C
1618

1719

1820
class Agent:
1921

2022
question: str = '''
2123
This is a function that was decompiled with Binary Ninja.
22-
It is in Binary Ninja's IL_FORM. What does this function do?
24+
It is in IL_FORM. What does this function do?
2325
'''
2426

2527
# A mapping of IL forms to their names.
2628
il_name: dict[type, str] = {
2729
LowLevelILFunction: 'Low Level Intermediate Language',
2830
MediumLevelILFunction: 'Medium Level Intermediate Language',
29-
HighLevelILFunction: 'High Level Intermediate Language'
31+
HighLevelILFunction: 'High Level Intermediate Language',
32+
Function: 'decompiled C code'
3033
}
3134

3235
def __init__(self,
33-
function: Union[LowLevelILFunction, MediumLevelILFunction, HighLevelILFunction],
36+
bv: BinaryView,
37+
function: Union[Function, LowLevelILFunction,
38+
MediumLevelILFunction, HighLevelILFunction],
3439
path_to_api_key: Optional[Path]=None) -> None:
3540

3641
# Read the API key from the environment variable.
@@ -39,12 +44,16 @@ def __init__(self,
3944
# Ensure that a function type was passed in.
4045
if not isinstance(
4146
function,
42-
(LowLevelILFunction, MediumLevelILFunction, HighLevelILFunction)):
47+
(Function, LowLevelILFunction, MediumLevelILFunction,
48+
HighLevelILFunction)):
4349
raise TypeError(f'Expected a BNIL function of type '
44-
f'LowLevelILFunction, MediumLevelILFunction, or '
45-
f'HighLevelILFunction, got {type(function)}.')
50+
f'Function, LowLevelILFunction, '
51+
f'MediumLevelILFunction, or HighLevelILFunction, '
52+
f'got {type(function)}.')
4653

54+
assert bv is not None, 'BinaryView is None. Check how you called this function.'
4755
# Set instance attributes.
56+
self.bv = bv
4857
self.function = function
4958
self.model = self.get_model()
5059

@@ -124,14 +133,17 @@ def instruction_list(self, function: Union[LowLevelILFunction,
124133
'''Generates a list of instructions in string representation given a
125134
BNIL function.
126135
'''
136+
if isinstance(function, Function):
137+
return Pseudo_C(self.bv, function).get_c_source()
127138
instructions: list[str] = []
128139
for instruction in function.instructions:
129140
instructions.append(str(instruction))
130141
return instructions
131142

132-
def generate_query(self, function: Union[LowLevelILFunction,
133-
MediumLevelILFunction,
134-
HighLevelILFunction]) -> str:
143+
def generate_query(self, function: Union[Function,
144+
LowLevelILFunction,
145+
MediumLevelILFunction,
146+
HighLevelILFunction]) -> str:
135147
'''Generates a query string given a BNIL function. Reads the file
136148
prompt.txt and replaces the IL form with the name of the IL form.
137149
'''

src/entry.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
API_KEY_PATH = Path.home() / Path('.openai/api_key.txt')
66

7-
# We don't use the bv argument, but it gets passed in by the PluginCommand.
8-
# pylint: disable=unused-argument
97
def check_function(bv: BinaryView, func: Function) -> bool:
108
agent: Agent = Agent(
9+
bv=bv,
1110
function=func,
1211
path_to_api_key=API_KEY_PATH
1312
)
1413
query: str = agent.generate_query(func)
15-
response: str = agent.send_query(query)
16-
print(response)
14+
agent.send_query(query)

0 commit comments

Comments
 (0)