【问题标题】:How to import idautils outside ida pro using python如何使用 python 在 ida pro 之外导入 idautils
【发布时间】:2015-10-10 23:21:50
【问题描述】:

IDApython 在命令行中的 IDA pro 中运行良好。但是,在 IDA pro 之外的普通编辑器中,当我使用 import idautis 编译 python 程序时出现错误:

“没有名为_idaapi的模块”

from idautils import *
from idaapi import *

ea = BeginEA()
for funcea in Functions(SegStart(ea), SegEnd(ea)):
    functionName = GetFunctionName(funcea)
    functionStart = paddAddr(hex(funcea)[2:])
    functionEnd = paddAddr(hex(FindFuncEnd(funcea))[2:])
   <REST OF THE CODE>

如何在IDA pro之外执行python代码?

【问题讨论】:

    标签: python execute ida


    【解决方案1】:

    您不能真正在 IDA 之外执行 IDAPython 脚本,但您可以让 IDA 静默运行并且不显示其 GUI。

    在您的脚本中,您需要将stdout 重定向到一个文件,例如:

    import sys
    import idaapi
    import idc
    import os
    
    def stdout_to_file(output_file_name, output_dir=None):
        '''Set stdout to a file descriptor
    
        param: output_file_name: name of the file where standard output is written.
        param: output_dir: output directory for output file, default to script directory.
    
        Returns: output file descriptor, original stdout descriptor
        '''
        # obtain this script path and build output path
        if not output_dir:
            output_dir = os.path.dirname(os.path.realpath(__file__))
    
        output_file_path = os.path.join(output_dir, output_file_name)
    
        # save original stdout descriptor
        orig_stdout = sys.stdout
    
        # create output file
        f = file(output_file_path, "w")
    
        # set stdout to output file descriptor
        sys.stdout = f
    
        return f, orig_stdout
    
    def main(args):
        # get original stdout and output file descriptor
        f, orig_stdout = stdout_to_file("output.txt")
    
        if idc.ARGV:
            for i, arg in enumerate(idc.ARGV):
                print "[*] arg[{}]: {}".format(i, arg)
    
        # call something from IDA (get the original input file name from IDB)
        print "[*] filename from IDB: {}".format(idaapi.get_root_filename())
        print("[*] done, exiting.")
    
        # restore stdout, close output file
        sys.stdout = orig_stdout 
        f.close()
    
        # exit IDA
        idc.Exit(0)
    
    if __name__ == "__main__":
        main(sys.argv)
    

    然后您可以在命令行调用您的 IDAPython 脚本(假设 IDA 在您的 PATH 中):

    idaq.exe  -A -S"C:\tmp\test_script.py foo bar" "C:\tmp\mydatabase.idb"
    
    • -A 用于静默运行 IDA
    • -S 用于脚本路径和脚本参数
    • 最后一个参数是 idb 路径(或使用-t 生成临时 idb)

    查看 IDA 帮助文件以获取所有可用选项的完整列表。

    输出,在 output.txt 文件中(IDB 来自输入文件 'calc.exe'):

    [*] arg[0]: C:\tmp\test_script.py
    [*] arg[1]: foo
    [*] arg[2]: bar
    [*] filename from IDB: calc.exe
    [*] done, exiting.
    

    您还可以查看标题为“Running scripts from the command line with idascript”的 hex ray 博客

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2011-10-29
      • 2015-06-04
      • 1970-01-01
      • 2019-03-21
      相关资源
      最近更新 更多