Skip to content

Commit f82b1f8

Browse files
authored
[Binja] Fix custom hook persistence and tests (#99)
1 parent 6a3fa01 commit f82b1f8

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

plugins/binaryninja/mui/hook_manager.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def load_existing_hooks(self) -> None:
121121
json.loads(settings.get_string(f"{BINJA_HOOK_SETTINGS_PREFIX}avoid", bv))
122122
)
123123
bv.session_data.mui_custom_hooks = {
124-
key: item
124+
CustomHookIdentity.from_name(key): item
125125
for key, item in json.loads(
126126
settings.get_string(f"{BINJA_HOOK_SETTINGS_PREFIX}custom", bv)
127127
).items()
@@ -168,9 +168,12 @@ def save_hooks(self) -> None:
168168
scope=SettingsScope.SettingsResourceScope,
169169
)
170170

171+
custom_hooks = {
172+
key.to_name(): item for key, item in bv.session_data.mui_custom_hooks.items()
173+
}
171174
settings.set_string(
172175
f"{BINJA_HOOK_SETTINGS_PREFIX}custom",
173-
json.dumps(bv.session_data.mui_custom_hooks),
176+
json.dumps(custom_hooks),
174177
view=bv,
175178
scope=SettingsScope.SettingsResourceScope,
176179
)
Binary file not shown.

plugins/binaryninja/tests/test_native_plugin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from manticore.native import Manticore
77
from mui.native_plugin import RebaseHooksPlugin
8-
from mui.hook_manager import NativeHookManager
8+
from mui.hook_manager import NativeHookManager, CustomHookIdentity
99

1010

1111
class FakeHookManager:
@@ -14,7 +14,7 @@ def __init__(
1414
filename: str,
1515
find_hooks: Set[int] = set(),
1616
avoid_hooks: Set[int] = set(),
17-
custom_hooks: Dict[str, str] = {},
17+
custom_hooks: Dict[CustomHookIdentity, str] = {},
1818
global_hooks: Dict[str, str] = {},
1919
):
2020
self.bv = MagicMock()
@@ -30,7 +30,7 @@ def list_find_hooks(self) -> Set[int]:
3030
def list_avoid_hooks(self) -> Set[int]:
3131
return self.avoid_hooks
3232

33-
def list_custom_hooks(self) -> Dict[str, str]:
33+
def list_custom_hooks(self) -> Dict[CustomHookIdentity, str]:
3434
return self.custom_hooks
3535

3636
def list_global_hooks(self) -> Dict[str, str]:
@@ -104,7 +104,9 @@ def test_rebase_custom(self) -> None:
104104

105105
mgr = cast(
106106
NativeHookManager,
107-
FakeHookManager(self.LIB_PATH, custom_hooks={f"{self.FOO}_00": custom_code}),
107+
FakeHookManager(
108+
self.LIB_PATH, custom_hooks={CustomHookIdentity(self.FOO, 0): custom_code}
109+
),
108110
)
109111
m.register_plugin(RebaseHooksPlugin(mgr, self.find_f, self.avoid_f))
110112
m.run()

plugins/binaryninja/tests/test_persistence.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import unittest
3-
from mui.hook_manager import NativeHookManager
3+
from mui.hook_manager import NativeHookManager, CustomHookIdentity
44
from binaryninja import open_view
55

66

@@ -13,7 +13,9 @@ def test_hook_loading(self) -> None:
1313
mgr.load_existing_hooks()
1414
self.assertEqual(mgr.list_find_hooks(), set([0x401152]))
1515
self.assertEqual(mgr.list_avoid_hooks(), set([0x401153]))
16-
self.assertEqual(mgr.list_custom_hooks(), {"0x40114D": "# custom hook code"})
16+
self.assertEqual(
17+
mgr.list_custom_hooks(), {CustomHookIdentity(0x40114D, 0): "# custom hook code"}
18+
)
1719
self.assertEqual(mgr.list_global_hooks(), {"global_00": "# global hook code"})
1820

1921

0 commit comments

Comments
 (0)