2015-10-06 53 views
0

IDApython在命令行中可以在IDA pro中正常工作。然而,IDA Pro的在正常的編輯,當我編譯進口idautis Python程序外,我得到的錯誤:如何使用python導入ida pro之外的idautils

「無模塊名爲_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親以外的地方執行Python代碼?

回答

3

你真的不能執行IDAPython腳本IDA之外,但你可以把它使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

六角射線博客