Skip to content

Commit a154d46

Browse files
committed
Remove function parameter from agent.
The function is passed in to generate the query; it does not need to be known at initialization time. Additionally, having that parameter prevents us from registering PluginCommands that don't work on Function (HLIL or otherwise) objects. In this case, we want to register an instruction, not a function.
1 parent aacb73c commit a154d46

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from binaryninja import PluginCommand
22
from . src.settings import OpenAISettings
33
from . src.entry import check_function
4+
from . src.entry import rename_expression
45

56
# Register the settings group in Binary Ninja to store the API key and model.
67
OpenAISettings()
@@ -18,3 +19,11 @@
1819
"saved under the environment variable "
1920
"OPENAI_API_KEY or modify the path in entry.py.",
2021
check_function)
22+
23+
PluginCommand.register_for_high_level_il_instruction("OpenAI\Rename Variable",
24+
"If the current expression is a HLIL Initialization " \
25+
"(HighLevelILVarInit), then query OpenAI to rename the " \
26+
"variable to what it believes is correct. If the expression" \
27+
"is not an HighLevelILVarInit, then do nothing. Requires " \
28+
"an internet connection and an API key. ",
29+
rename_expression)

src/agent.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,14 @@ class Agent:
3434

3535
def __init__(self,
3636
bv: BinaryView,
37-
function: Union[Function, LowLevelILFunction,
38-
MediumLevelILFunction, HighLevelILFunction],
3937
path_to_api_key: Optional[Path]=None) -> None:
4038

4139
# Read the API key from the environment variable.
4240
openai.api_key = self.read_api_key(path_to_api_key)
4341

44-
# Ensure that a function type was passed in.
45-
if not isinstance(
46-
function,
47-
(Function, LowLevelILFunction, MediumLevelILFunction,
48-
HighLevelILFunction)):
49-
raise TypeError(f'Expected a BNIL function of type '
50-
f'Function, LowLevelILFunction, '
51-
f'MediumLevelILFunction, or HighLevelILFunction, '
52-
f'got {type(function)}.')
53-
5442
assert bv is not None, 'BinaryView is None. Check how you called this function.'
5543
# Set instance attributes.
5644
self.bv = bv
57-
self.function = function
5845
self.model = self.get_model()
5946

6047
def read_api_key(self, filename: Optional[Path]=None) -> str:
@@ -133,6 +120,15 @@ def instruction_list(self, function: Union[LowLevelILFunction,
133120
'''Generates a list of instructions in string representation given a
134121
BNIL function.
135122
'''
123+
124+
# Ensure that a function type was passed in.
125+
if not isinstance(function, (Function, LowLevelILFunction,
126+
MediumLevelILFunction, HighLevelILFunction)):
127+
raise TypeError(f'Expected a BNIL function of type '
128+
f'Function, LowLevelILFunction, '
129+
f'MediumLevelILFunction, or HighLevelILFunction, '
130+
f'got {type(function)}.')
131+
136132
if isinstance(function, Function):
137133
return Pseudo_C(self.bv, function).get_c_source()
138134
instructions: list[str] = []

src/entry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from pathlib import Path
22
from binaryninja import BinaryView, Function
3+
from binaryninja.highlevelil import HighLevelILInstruction
34
from . agent import Agent
45

56
API_KEY_PATH = Path.home() / Path('.openai/api_key.txt')
67

78
def check_function(bv: BinaryView, func: Function) -> bool:
89
agent: Agent = Agent(
910
bv=bv,
10-
function=func,
1111
path_to_api_key=API_KEY_PATH
1212
)
1313
query: str = agent.generate_query(func)
1414
agent.send_query(query)
15+
16+
def rename_expression(bv: BinaryView, instruction: HighLevelILInstruction) -> bool:
17+
pass

0 commit comments

Comments
 (0)