Metadata-Version: 2.4
Name: ida-batch-decompile
Version: 0.2.2
Summary: Batch decompiler for binary files using IDA Pro's idat command-line tool
Author-email: kamille-bidan123 <lometsj@live.com>
License: MIT
Project-URL: Homepage, https://github.com/kamille-bidan123/ida2c_auto
Project-URL: Repository, https://github.com/kamille-bidan123/ida2c_auto.git
Project-URL: Issues, https://github.com/kamille-bidan123/ida2c_auto/issues
Keywords: ida,idapro,decompiler,binary,reverse-engineering
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Disassemblers
Requires-Python: >=3.7
Description-Content-Type: text/markdown

# 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 安装

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

# 从源码安装
pip install .
```

## 使用方法

### 作为 Python 库使用

安装后，你可以将此工具作为 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

```python
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

```python
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]]` |

### 命令行方式

```bash
# 扫描目录并批量反编译
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` | 显示帮助信息 |

#### 示例

```bash
# 处理单个文件
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 文件格式如下：

```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;
}
```

输出文件以原二进制文件名命名，例如 `hello` → `hello.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 的使用协议。
