您不能真正在 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 博客