Skip to content

Instantly share code, notes, and snippets.

@trietptm
Forked from NeatMonster/copy_stuff.py
Created January 6, 2021 11:48
Show Gist options
  • Save trietptm/a5d86a94b1a525874be259381217e9dc to your computer and use it in GitHub Desktop.
Save trietptm/a5d86a94b1a525874be259381217e9dc to your computer and use it in GitHub Desktop.
Contextual menu to enable copying as UUID/bytes list/hex string
import uuid
import ida_bytes
import ida_hexrays
import ida_idaapi
import ida_kernwin
from PyQt5.Qt import QApplication
class Plugin(ida_idaapi.plugin_t):
flags = ida_idaapi.PLUGIN_HIDE
comment = ""
help = ""
wanted_name = "CopyStuff"
wanted_hotkey = ""
class Handler(ida_kernwin.action_handler_t):
def __init__(self, type):
ida_kernwin.action_handler_t.__init__(self)
self.type = type
def highlight(self, ctx):
vu = ida_hexrays.get_widget_vdui(ctx.widget)
vu.get_current_item(ida_hexrays.USE_KEYBOARD)
return vu.item.e if vu.item.is_citem() else None
def activate(self, ctx):
if ctx.widget_type == ida_kernwin.BWN_PSEUDOCODE:
highlight = self.highlight(ctx)
if not highlight or highlight.op != ida_hexrays.cot_obj:
return 1
start_ea = ida_bytes.get_item_head(highlight.obj_ea)
end_ea = ida_bytes.get_item_end(highlight.obj_ea)
else:
select, start_ea, end_ea = ida_kernwin.read_range_selection(None)
if not select:
item_ea = ida_kernwin.get_screen_ea()
start_ea = ida_bytes.get_item_head(item_ea)
end_ea = ida_bytes.get_item_end(item_ea)
if ida_idaapi.BADADDR in [start_ea, end_ea]:
return 1
if end_ea == start_ea + 1:
end_ea = start_ea + 16
if self.type == "uuid":
data = str(uuid.UUID(bytes_le=ida_bytes.get_bytes(start_ea, 16)))
if self.type in ["bytes", "hexstr"]:
data =ida_bytes.get_bytes(start_ea, end_ea - start_ea)
if self.type == "bytes":
data = " ".join(["%02x" % b for b in data])
if self.type == "hexstr":
data = data.hex()
QApplication.clipboard().setText(data)
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_FOR_WIDGET
class IDAHooks(ida_kernwin.UI_Hooks):
def finish_populating_widget_popup(self, widget, popup):
widget_type = ida_kernwin.get_widget_type(widget)
if widget_type == ida_kernwin.BWN_DISASMS:
Plugin.attach(widget, popup)
return 0
class HRHooks(ida_hexrays.Hexrays_Hooks):
def populating_popup(self, widget, popup, vu):
Plugin.attach(widget, popup)
return 0
@staticmethod
def attach(widget, popup):
ida_kernwin.attach_action_to_popup(
widget, popup, Plugin.uuid_action.name)
ida_kernwin.attach_action_to_popup(
widget, popup, Plugin.bytes_action.name)
ida_kernwin.attach_action_to_popup(
widget, popup, Plugin.hexstr_action.name)
def init(self):
Plugin.uuid_action = ida_kernwin.action_desc_t(
":copy_uuid", "Copy as UUID/GUID", Plugin.Handler("uuid"))
ida_kernwin.register_action(Plugin.uuid_action)
Plugin.bytes_action = ida_kernwin.action_desc_t(
":copy_bytes", "Copy as bytes list", Plugin.Handler("bytes"))
ida_kernwin.register_action(Plugin.bytes_action)
Plugin.hexstr_action = ida_kernwin.action_desc_t(
":copy_hexstr", "Copy as hex string", Plugin.Handler("hexstr"))
ida_kernwin.register_action(Plugin.hexstr_action)
self.ida_hooks = Plugin.IDAHooks()
self.ida_hooks.hook()
self.hr_hooks = Plugin.HRHooks()
self.hr_hooks.hook()
return ida_idaapi.PLUGIN_KEEP
def run(self):
return False
def term(self):
ida_kernwin.unregister_action(Plugin.uuid_action.name)
ida_kernwin.unregister_action(Plugin.bytes_action.name)
ida_kernwin.unregister_action(Plugin.hexstr_action.name)
self.ida_hooks.unhook()
self.hr_hooks.unhook()
def PLUGIN_ENTRY():
return Plugin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment