-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IDA: Add a simple action to generate spec files (#94)
* IDA: Add a simple action to generate spec files * docs: Update the example instructions
- Loading branch information
1 parent
b9d73b1
commit 7e2ac5f
Showing
2 changed files
with
96 additions
and
4 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
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,78 @@ | ||
# Copyright (c) 2021-present Trail of Bits, Inc. | ||
|
||
import ida_funcs | ||
import ida_kernwin | ||
import idautils | ||
|
||
import anvill | ||
import json | ||
|
||
class generate_anvill_spec_t(ida_kernwin.action_handler_t): | ||
def activate(self, ctx): | ||
user_input = ida_kernwin.ask_yn(ida_kernwin.ASKBTN_YES, "Would you like to export all functions?") | ||
if user_input == ida_kernwin.ASKBTN_CANCEL: | ||
return 1 | ||
|
||
output_file_name_hint = "" | ||
|
||
p = anvill.get_program() | ||
|
||
if user_input == ida_kernwin.ASKBTN_NO: | ||
screen_cursor = ida_kernwin.get_screen_ea() | ||
function_name = ida_funcs.get_func_name(screen_cursor) | ||
if function_name is None: | ||
print("ANVILL: The cursor is not located inside a function") | ||
return 1 | ||
|
||
output_file_name_hint = function_name + ".json" | ||
|
||
try: | ||
p.add_function_definition(screen_cursor) | ||
|
||
except: | ||
print("ANVILL: Failed to process the function at address {0:x}".format(screen_cursor)) | ||
return 1 | ||
|
||
else: | ||
function_address_list = idautils.Functions() | ||
for function_address in function_address_list: | ||
try: | ||
p.add_function_definition(function_address) | ||
|
||
except: | ||
print("ANVILL: Failed to process the function at address {0:x}".format(function_address)) | ||
|
||
output_file_name_hint = "program.json" | ||
|
||
output_path = ida_kernwin.ask_file(True, output_file_name_hint, "Select where to save the spec file") | ||
if not output_path: | ||
return 1 | ||
|
||
output = json.dumps(p.proto(), sort_keys=False, indent=2) | ||
|
||
print("ANVILL: Saving the spec file to {}".format(output_path)) | ||
with open(output_path, "w") as f: | ||
f.write(output) | ||
|
||
def update(self, ctx): | ||
if ctx.widget_type == ida_kernwin.BWN_DISASM: | ||
return ida_kernwin.AST_ENABLE_FOR_WIDGET | ||
|
||
return ida_kernwin.AST_DISABLE_FOR_WIDGET | ||
|
||
ACTION_NAME = "generate-anvill-spec-file" | ||
|
||
ida_kernwin.register_action( | ||
ida_kernwin.action_desc_t( | ||
ACTION_NAME, | ||
"Generate ANVILL spec file", | ||
generate_anvill_spec_t(), | ||
"Ctrl+H")) | ||
|
||
class popup_hooks_t(ida_kernwin.UI_Hooks): | ||
def finish_populating_widget_popup(self, w, popup): | ||
if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_DISASM: | ||
ida_kernwin.attach_action_to_popup(w, popup, ACTION_NAME, None) | ||
|
||
hooks = popup_hooks_t() | ||
hooks.hook() |