插件框架

字段名 功能
flags 插件的状态
comment 描述信息
help 帮助信息
wanted_name 菜单中显示的名字
wanted_hotkey 希望注册的快捷键
  1. class UnknowClass(idaapi.plugin_t):
  2. '''
  3. 给插件接口,实例的类定义
  4. '''
  5. flags = idaapi.PLUGIN_KEEP # 插件的状态, 当前状态保持在Plugin菜单中
  6. comment = "XXX" # 描述信息
  7. help = "" # 帮助信息
  8. wanted_name = "XXX" # 菜单中显示的名字
  9. #wanted_hotkey = "Ctrl+Alt+Shift+F12" # 希望注册的快捷键
  10. wanted_hotkey = ""
  11. #_PREFIX_NAME = 'carveSelectedBytes'
  12. #_MIN_MAX_MATH_OPS_TO_ALLOW_RENAME = 11
  13. def __init__(self):
  14. '''
  15. 构造基类,一般没实名操作,
  16. 记得给Super第一个参数更正为 当前类的名称
  17. '''
  18. super(UnknowClass, self).__init__()
  19. self._data = None
  20. def term(self):
  21. '''
  22. Destory函数, 同析构函数, 留待释放资源
  23. '''
  24. pass
  25. def init(self):
  26. '''
  27. 进行初始化操作,可在此输出一些描述信息
  28. '''
  29. self.view = None
  30. # self._cfg = None
  31. # print("=" * 80)
  32. # print("carveSelectedBytes")
  33. # print(u"保存所选的 HexData 到文件")
  34. # print("=" * 80)
  35. return idaapi.PLUGIN_OK
  36. def run(self, arg):
  37. '''
  38. 每次运行插件时, 执行的具体操作
  39. 功能代码在此编写
  40. '''
  41. pass
  42. def PLUGIN_ENTRY():
  43. '''
  44. 插件入口,用于实例对象
  45. 返回的就是插件的功能等
  46. '''
  47. return carveSelectedBytes()

增加菜单

  1. #IDA7 定义 菜单的Handle, 其他版本直接传入函数均可
  2. class Kp_MC_Patcher(Kp_Menu_Context): //继承
  3. def activate(self, ctx):
  4. self.plugin.patcher()
  5. return 1
  1. class Kp_Menu_Context(ida_kernwin.action_handler_t)
  2. | Method resolution order:
  3. | Kp_Menu_Context
  4. | ida_kernwin.action_handler_t
  5. | __builtin__.object
  6. |
  7. | Class methods defined here:
  8. |
  9. | activate(self, ctx) from __builtin__.type # 当菜单激活时 Handle处理函数
  10. |
  11. | get_label(self) from __builtin__.type
  12. |
  13. | get_name(self) from __builtin__.type # 获取Handle对象
  14. |
  15. | register(self, plugin, label) from __builtin__.type # 注册菜单
  16. |
  17. | unregister(self) from __builtin__.type
  18. | Unregister the action.
  19. | After unregistering the class cannot be used.
  20. |
  21. | update(self, ctx) from __builtin__.type
  1. # 一般在插件主类的 Init 中 注册菜单
  2. if idaapi.IDA_SDK_VERSION >= 700:
  3. # Add menu IDA >= 7.0
  4. # attach_action_to_menu(menupath, name, flags) -> bool
  5. # 参数1: 要增加在哪个菜单路径下
  6. # 参数2: 继承重写的类对象
  7. # 参数3: 显示出来为 什么属性
  8. idaapi.attach_action_to_menu("Edit/Keypatch/Patcher", Kp_MC_Patcher.get_name(), idaapi.SETMENU_APP)
  9. else:
  10. # add Keypatch menu
  11. # IDA 7 以下
  12. menu = idaapi.add_menu_item("Edit/Keypatch/", "Patcher (Ctrl-Alt-K)", "", 1, self.patcher, None)
  13. if menu is not None:
  14. #add_menu_item(menupath, name, hotkey, flags, pyfunc, args) -> PyObject *
  15. # 菜单路径, 显示名称, 热键, 排序, 处理的Handle, 传入参数
  16. idaapi.add_menu_item("Edit/Keypatch/", "About", "", 1, self.about, None)
  17. elif idaapi.IDA_SDK_VERSION < 680:
  18. # older IDAPython (such as in IDAPro 6.6) does add new submenu.
  19. # in this case, put Keypatch menu in menu Edit \ Patch program
  20. # not sure about v6.7, so to be safe we just check against v6.8
  21. # 68以下 暂不关心
  22. idaapi.add_menu_item("Edit/Patch program/", "-", "", 0, self.menu_null, None)

弹出菜单

  1. # 在插件Init函数中 注册弹出菜单
  2. # register popup menu handlers
  3. try:
  4. # register(self, plugin, label) method of __builtin__.type instance
  5. Kp_MC_Patcher.register(self, "Patcher (Ctrl-Alt-K)")
  6. except:
  7. pass
  8. # setup popup menu
  9. self.hooks = Hooks()
  10. self.hooks.hook()
  11. # hooks for popup menu
  12. class Hooks(idaapi.UI_Hooks): # 名为HOOK
  13. def finish_populating_tform_popup(self, form, popup): #重写虚函数, popup 自传入
  14. # We'll add our action to all "IDA View-*"s.
  15. # If we wanted to add it only to "IDA View-A", we could
  16. # also discriminate on the widget's title:
  17. #
  18. # if idaapi.get_tform_title(form) == "IDA View-A":
  19. # ...
  20. #
  21. if idaapi.get_tform_type(form) == idaapi.BWN_DISASM:
  22. try:
  23. # attach_action_to_popup(widget, popup_handle, name, popuppath=None, flags=0) -> bool
  24. # 目标窗口, 弹出处理Handle, 名称, 弹出菜单的位置
  25. idaapi.attach_action_to_popup(form, popup, Kp_MC_Patcher.get_name(), 'Keypatch/')
  26. except:
  27. pass

IDA 界面库

IDA7 支持了PyQt5
而IDA6 却只支持PySide
则需兼容代码

  1. if idaapi.IDA_SDK_VERSION >= 700:
  2. from PyQt5.Qt import QApplication
  3. else:
  4. from PySide.QtGui import QApplication