debug IDApython script
· 1 分钟阅读 · @admin

使用pycharm调试
在网上搜索调试的方法也很久了,也没有找到方法。无意中在auto_re插件中发现如下这段代码。
RDEBUG = False
# adjust this value to be a full path to a debug egg
RDEBUG_EGG = r'c:\Program Files\JetBrains\PyCharm 2017.1.4\debug-eggs\pycharm-debug.egg'
RDEBUG_HOST = 'localhost'
RDEBUG_PORT = 12321
...
def run():
.....
if RDEBUG and RDEBUG_EGG:
if not os.path.isfile(RDEBUG_EGG):
idaapi.msg('AutoRE: Remote debug is enabled, but I cannot find the debug egg: %s' % RDEBUG_EGG)
else:
import sys
if RDEBUG_EGG not in sys.path:
sys.path.append(RDEBUG_EGG)
import pydevd
pydevd.settrace(RDEBUG_HOST, port=RDEBUG_PORT, stdoutToServer=True, stderrToServer=True)
这不和我之前调试kodi插件时一个套路嘛。
> + 安装pydevd模块 > + 用python charm打开<font color=“red”>run->Edit Configuration</font>新建一个<font color=“red”>python remote debug</font>调试配置,需要注意ip和端口 > + ida的python解释器默认是没有pydevd的,我们手动加载pycharm-debug.egg.
写一个hello测试下,RDEBUG_HOST和RDEBUG_PORT要和调试配置的一致。 加载脚本,命中断点时会在pydevd.settrace停下,ida会被挂起,然后 就可以像正常python脚本一样调试。
def debug():
import sys
RDEBUG_EGG = "c:\Program Files\JetBrains\PyCharm 2017.1.4\debug-eggs\pycharm-debug.egg" # 看你的pycharm安装路径
sys.path.append(RDEBUG_EGG)
pydevd.settrace(RDEBUG_HOST, port=RDEBUG_PORT, stdoutToServer=True, stderrToServer=True)
class MyPlugin_t(idaapi.plugin_t):
flags = idaapi.PLUGIN_HIDE
comment = "Test"
help = ""
wanted_name = "Test"
wanted_hotkey = ""
def init(self):
return idaapi.PLUGIN_KEEP
def run(self, arg):
debug()
print
"hello,hit break" # 在这下个断点
def term(self):
pass
def PLUGIN_ENTRY():
return MyPlugin_t()
使用wingIDE调试
在wingIDE目录找到<font color=“red”>wingdbstub.py</font>并拷贝到IDA plugin目录中。 我们需要在脚本中指定wingIDE的目录,找到下面这行,把None改成对应的路径。
WINGHOME = None
打开IDE,左下角有个小虫子图标,点击选择“Accept Debug Connections”.最后我们需要在在调试代码加上两行代码,还是拿前面的为例。 新建一个project,选择hex-rays IDA,在这个工程打开需要调试的脚本,下好断点,ida加载要调试的脚本,后面和用pycharm是一样的。
def debug():
import wingdbstub
wingdbstub.Ensure()
class MyPlugin_t(idaapi.plugin_t):
flags = idaapi.PLUGIN_HIDE
comment = "Test"
help = ""
wanted_name = "Test"
wanted_hotkey = ""
def init(self):
return idaapi.PLUGIN_KEEP
def run(self, arg):
debug()
print
"hello,hit break" # 在这下个断点
def term(self):
pass
def PLUGIN_ENTRY():
return MyPlugin_t()
其他
理论上支持python remote debug的ide都应该支持调试IDApython脚本。 vs code使用pydevd调试,没试过。vs studio也可以,调试时需要附加IDA进程,很麻烦。

