如何用idapython调用idc函数

逆向工程 艾达 蟒蛇
2021-06-25 12:43:45

我想用 IdaPython 编写一个 x86Emu 插件(一个 IDA 模拟器)。它是注册的 idc 函数。以下是插件源代码中的函数名称。

/* add IDC functions for interacting with the emulator
EmuRun();
EmuTrace();
EmuStepOne();
EmuTraceOne();
EmuSync();
EmuGetReg(regno);
EmuSetReg(regno, value);
EmuAddBpt(addr);
*/

此功能在 idc 中运行良好。我在idaapi.py.

class _IdcFunction(object):
"""
Internal class that calls pyw_call_idc_func() with a context
"""
def \_\_init\_\_(self, ctxptr):
    self.ctxptr = ctxptr
    # Take a reference to the ctypes callback
    # (note: this will create a circular reference)
    self.cb   = _IDCFUNC_CB_T(self)
fp_ptr = property(lambda self: ctypes.cast(self.cb, ctypes.c_void_p).value)
def \_\_call\_\_(self, args, res):
    return call_idc_func__(self.ctxptr, args, res)

当我输入这个时:

import ctypes
idaapi._IdcFuntion( ctypes ).\_\_call\_\_( 'EmuStepOne', ctypes.c_voidp ) 

我收到以下错误:

ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1

我不知道为什么会出现此错误,有人知道吗?

1个回答

之所以会出现这些错误,是因为您尝试使用ctypes,它使用从 python 对象到低级 C 类类型的转换从 python调用本机函数。

显然,python 异常不是 ctypes 可以转换为本机表示的东西,因此它失败了。

有一种非常简单的方法可以从 IDAPython 中调用 IDC 代码,使用idc.Eval,如下所示:

idc.Eval("EmuStepOne();")