Skip to main content

Batch decompiler for binary files using IDA Pro's idat command-line tool

Project description

IDA Pro 批量反编译工具

使用 IDA Pro 的命令行工具 idat 配合 IDAPython 批量反编译二进制文件,生成干净的 C 代码。

功能特性

  • 扫描目录并自动识别可执行文件和共享库
  • 使用 Hex-Rays decompile_many API 生成高质量的 C 代码
  • 支持并行处理
  • 自动等待分析完成
  • 支持 Mach-O、ELF、PE 等格式

依赖

  • IDA Pro 6.9+(推荐 9.0+)
  • Python 3.x

安装

使用 pip 安装

# 从 PyPI 安装
pip install ida-batch-decompile

# 从源码安装
pip install .

使用方法

作为 Python 库使用

安装后,你可以将此工具作为 Python 库导入使用:

from ida_batch_decompile import IDABatchDecompiler, BinaryScanner

# 创建反编译器实例
decompiler = IDABatchDecompiler(
    idat_path="/Applications/IDA Professional 9.1.app/Contents/MacOS/idat",
    output_dir="./output",
    jobs=4,
    timeout=300,
    verbose=True
)

# 方式 1: 反编译单个文件
result = decompiler.decompile_binary("./binaries/hello")
if result[0]:
    print(f"成功: {result[1]}")
else:
    print(f"失败: {result[1]}")

# 方式 2: 反编译整个目录
results = decompiler.decompile_directory("./binaries")
for path, (success, msg) in results.items():
    print(f"{path}: {'成功' if success else '失败'} - {msg}")

# 方式 3: 批量反编译指定文件列表
files = ["./binaries/hello", "./binaries/libutil.so"]
results = decompiler.decompile_batch(files, parallel=True)

# 使用 BinaryScanner 扫描二进制文件
scanner = BinaryScanner()
binaries = scanner.scan_directory(Path("./binaries"))
print(f"找到 {len(binaries)} 个二进制文件")

API 文档

IDABatchDecompiler
IDABatchDecompiler(
    idat_path: str = "/Applications/IDA Professional 9.1.app/Contents/MacOS/idat",
    output_dir: str = "./output",
    jobs: int = 4,
    timeout: int = 300,
    verbose: bool = False
)

方法:

方法 说明 返回值
decompile_binary(path) 反编译单个文件 (bool, str)
decompile_directory(dir) 反编译目录中所有二进制文件 Dict[str, Tuple[bool, str]]
decompile_batch(paths, parallel) 批量反编译指定文件 Dict[str, Tuple[bool, str]]
BinaryScanner
BinaryScanner()

方法:

方法 说明 返回值
scan_directory(dir) 扫描目录中的二进制文件 List[Path]
find_binaries(dir) 查找二进制文件 List[Path]
is_binary_file(path) 检查是否为二进制文件 bool
detect_file_type(path) 检测文件类型 Optional[Tuple[str, str]]
scan_and_group(dir) 扫描并按类型分组 Dict[str, List[Path]]

命令行方式

# 扫描目录并批量反编译
ida-decompile -i <输入目录> -o <输出目录>

# 指定 idat 路径
ida-decompile \
  -i /path/to/binaries \
  -o ./output \
  -p /path/to/idat

# 详细输出
ida-decompile -i ./binaries -o ./output -v

完整参数

参数 说明
-i, --input-dir 要扫描的输入目录或单个二进制文件
-o, --output-dir C 文件输出目录
-p, --idat-path idat 可执行文件路径
-j, --jobs 并行处理的进程数 (默认: 4)
-t, --timeout 每个文件的超时时间,单位秒 (默认: 300)
-v, --verbose 显示详细输出
-h, --help 显示帮助信息

示例

# 处理单个文件
ida-decompile -i ./hello -o ./output

# 处理整个目录
ida-decompile -i ./binaries -o ./decompiled

# 使用 8 个并行进程
ida-decompile -i ./binaries -o ./output -j 8

# 增加超时时间(处理大型文件)
ida-decompile -i ./binaries -o ./output -t 600

输出格式

生成的 C 文件格式如下:

/* This file was generated by the Hex-Rays decompiler version 9.1.0.250226.
   Copyright (c) 2007-2021 Hex-Rays <info@hex-rays.com>

   Detected compiler: GNU C++
*/

#include <defs.h>


//-------------------------------------------------------------------------
// Function declarations

int __fastcall main(int argc, const char **argv, const char **envp);
int printf(const char *, ...);


//----- (0000000100000460) ----------------------------------------------------
int __fastcall main(int argc, const char **argv, const char **envp)
{
  printf("Hello, World!\n");
  return 0;
}

输出文件以原二进制文件名命名,例如 hellohello.c

实现原理

工具使用 ida_hexrays.decompile_many() API:

  1. 使用 idat -A 以自动模式加载二进制文件
  2. 等待 IDA 自动分析完成
  3. 调用 decompile_many() 导出所有函数的 C 代码
  4. 保存为 .c 文件

这与 IDA GUI 的"导出 C 代码"功能使用相同的底层 API,因此输出质量相同。

注意事项

  • 确保 IDA Pro 已正确安装且 idat 可执行文件存在
  • 某些保护/混淆的二进制文件可能无法完整反编译
  • 处理大型文件时可能需要增加超时时间
  • 并行处理时请注意系统资源限制
  • idat 路径需要根据你的 IDA Pro 安装位置调整

许可证

本工具仅供学习和研究使用。请遵守 IDA Pro 的使用协议。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ida_batch_decompile-0.2.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ida_batch_decompile-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file ida_batch_decompile-0.2.0.tar.gz.

File metadata

  • Download URL: ida_batch_decompile-0.2.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ida_batch_decompile-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fbbb8c5f211329f01423f721132c3770b394aa28426de40d8381e0a84e2eb0c2
MD5 c89c461b87b842ef9851c67e0a39306d
BLAKE2b-256 0699e8d796f02bb061f81c139d8cffe1e844bc7fca0f6687454969e963290bb3

See more details on using hashes here.

File details

Details for the file ida_batch_decompile-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ida_batch_decompile-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f45451b64d1adc0d28d0f1306df7574b8bcac326640579ce1c9dd5301094db93
MD5 471fa5a2a09825f36b72f5f3f0878e2d
BLAKE2b-256 90210a8d3c8e8630d50017eb5b8d338461b62a7c630393c3cefabd76a2ca3d98

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page