IDA 技巧(8) IDA 的批处理模式

admin 2024年4月19日03:18:57评论2 views字数 3923阅读13分4秒阅读模式

批处理模式

基础用法

总结一下,可以使用以下命令行来调用批处理模式:

ida -B -Lida.log <other switches> <filename>

IDA会加载文件,等待分析结束,并将完整的反汇编写入:

<filename>.asm

它是如何工作的

其实,-B 是 -A -Sanalysis.idc 的简写:

  • -A:启用自动模式(以默认选择回答所有查询)。
  • -Sanalysis.idc:加载文件后运行脚本 analysis.idc。

你可以在 IDA 安装的 idc 子目录中找到 analysis.idc 文件。在IDA 7.5中,它的位置如下所示:

static main()
{
 // turn on coagulation of data in the final pass of analysis
 set_inf_attr(INF_AF, get_inf_attr(INF_AF) | AF_DODATA | AF_FINAL);
 // .. and plan the entire address space for the final pass
 auto_mark_range(0, BADADDR, AU_FINAL);
 msg("Waiting for the end of the auto analysis...n");
 auto_wait();
 msg("nn------ Creating the output file.... --------n");
 auto file = get_idb_path()[0:-4] + ".asm";
 auto fhandle = fopen(file, "w");
 gen_file(OFILE_ASM, fhandle, 0, BADADDR, 0); // create the assembler
 file
 msg("All done, exiting...n")
;
 qexit(0); // exit to OS, error code 0 - success
}

因此,要修改批处理模式的行为,你可以:

  • 修改标准分析.idc
  • 使用-S<myscript.idc>指定不同的脚本

例如,要输出一个包含地址前缀的LST文件,请更改 gen_file 调用:

gen_file(OFILE_LST, fhandle, 0, BADADDR, 0);

批量反编译

如果你有目标文件架构的反编译器,你也可以批处理运行它。

例如,反编译整个文件:

ida -Ohexrays:outfile.c:ALL -A <filename>

只反编译 main 函数:

ida -Ohexrays:outfile.c:main -A <filename>

这使用了反编译插件内置的功能,类似于 analysis.idc 脚本(等待自动分析结束,然后将指定的函数反编译到 outfile.c)。

自定义批量反编译

如果默认功能不够,你可以编写一个插件,通过其C++ API驱动反编译器。然而,对于脚本编写,可能更方便使用Python。与IDC类似,可以使用Python脚本通过-S开关在文件加载后自动运行。

附带了一个示例脚本,请按以下方式使用:

ida -A -Sdecompile_entry_points.py -Llogfile.txt <filename>

decompile_entry_points.py 的代码如下

from __future__ import print_function

#
# This example tries to load a decompiler plugin corresponding to the current
# architecture (and address size) right after auto-analysis is performed,
# and then tries to decompile the function at the first entrypoint.
#
# It is particularly suited for use with the '-S' flag, for example:
# idat -Ldecompile.log -Sdecompile_entry_points.py -c file
#

import ida_ida
import ida_auto
import ida_loader
import ida_hexrays
import ida_idp
import ida_entry

# becsause the -S script runs very early, we need to load the decompiler
# manually if we want to use it
def init_hexrays():
    ALL_DECOMPILERS = {
        ida_idp.PLFM_386: "hexrays",
        ida_idp.PLFM_ARM: "hexarm",
        ida_idp.PLFM_PPC: "hexppc",
        ida_idp.PLFM_MIPS: "hexmips",
    }
    cpu = ida_idp.ph.id
    decompiler = ALL_DECOMPILERS.get(cpu, None)
    if not decompiler:
        print("No known decompilers for architecture with ID: %d" % ida_idp.ph.id)
        return False
    if ida_ida.inf_is_64bit():
        if cpu == ida_idp.PLFM_386:
            decompiler = "hexx64"
        else:
            decompiler += "64"
    if ida_loader.load_plugin(decompiler) and ida_hexrays.init_hexrays_plugin():
        return True
    else:
        print('Couldn't load or initialize decompiler: "%s"' % decompiler)
        return False

def decompile_func(ea, outfile):
    print("Decompiling at: %X..." % ea)
    cf = ida_hexrays.decompile(ea)
    if cf:
        print("OK.")
        outfile.write(str(cf) + "n")
    else:
        print("failed!")
        outfile.write("decompilation failure at %X!n" % ea)

def main():
    print("Waiting for autoanalysis...")
    ida_auto.auto_wait()
    if init_hexrays():
        eqty = ida_entry.get_entry_qty()
        if eqty:
            idbpath = idc.get_idb_path()
            cpath = idbpath[:-4] + ".c"
            with open(cpath, "w"as outfile:
                print("writing results to '%s'..." % cpath)
                for i in range(eqty):
                    ea = ida_entry.get_entry(ida_entry.get_entry_ordinal(i))
                    decompile_func(ea, outfile)
        else:
            print("No known entrypoint. Cannot decompile.")
    if idaapi.cvar.batch:
        print("All done, exiting.")
        ida_pro.qexit(0)

main()

加速批处理

到目前为止,我们一直在使用ida可执行文件,它是IDA的完整GUI版本。即使在批处理模式下实际上不显示UI,它仍然需要加载和初始化所有依赖的UI库,这可能需要相当长的时间。

这就是为什么通常最好使用文本模式可执行文件(idat),它使用轻量级文本模式UI。

但是,即使在批处理模式下,它仍然需要一个终端。如果您需要在没有终端的情况下运行它(例如,在后台运行或从守护程序运行),您可以使用以下方法:

  1. 将环境变量 TVHEADLESS 设置为 1
  2. 重定向输出

例子如下

TVHEADLESS=1 idat -A -Smyscript.idc file.bin >/dev/null &

原文始发于微信公众号(二进制磨剑):IDA 技巧(8) IDA 的批处理模式

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年4月19日03:18:57
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   IDA 技巧(8) IDA 的批处理模式https://cn-sec.com/archives/2668764.html

发表评论

匿名网友 填写信息