Yeti-Sec/capa-rs

GitHub: Yeti-Sec/capa-rs

Google Mandiant CAPA 工具的 Rust 高性能重实现,通过静态分析识别可执行文件中的恶意行为能力。

Stars: 0 | Forks: 0

capa-rs logo # capa-rs [CAPA](https://github.com/mandiant/capa) 的 Rust 重实现 —— Mandiant 的开源工具,用于识别可执行文件中的功能。 [![CI status](https://github.com/yeti-sec/capa-rs/workflows/CI/badge.svg)](https://github.com/yeti-sec/capa-rs/actions) [![Last release](https://img.shields.io/github/v/release/yeti-sec/capa-rs)](https://github.com/yeti-sec/capa-rs/releases) [![Downloads](https://img.shields.io/github/downloads/yeti-sec/capa-rs/total)](https://github.com/yeti-sec/capa-rs/releases) [![License](https://img.shields.io/badge/license-Apache--2.0-green.svg)](LICENSE) [![Rust](https://img.shields.io/badge/rust-stable-orange.svg)](https://www.rust-lang.org/) [![CAPA rules](https://img.shields.io/badge/capa--rules-1000%2B-blue.svg)](https://github.com/mandiant/capa-rules) 使用 CAPA 的 YAML 规则格式分析 PE、ELF、.NET 和 shellcode。
![capa-rs 演示](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/c34ee51bbc232747.gif)
分样本演示 | 样本 | 演示 | |--------|------| | .NET assembly | ![dotnet](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/d047118ad2232800.gif) | | ELF x86-64 | ![elf](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/b28f543f86232813.gif) | | Golang PE32 | ![golang](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/1c48cd3b42232837.gif) | | GraalVM PE64 | ![graalvm](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/b74403fed8232944.gif) | | Cobalt Strike beacon | ![beacon](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/001bc626e5232958.gif) | | Cobalt Strike beacon v2 | ![beacon2](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/79d7186e78233010.gif) |
## 快速开始 ``` # 克隆与构建 git clone https://github.com/yeti-sec/capa-rs.git cd capa-rs cargo build --release # 分析二进制文件 ./target/release/capa-rs -r capa-rules malware.exe ``` ## 功能特性 - **快速**:使用 iced-x86 反汇编器,比 Python CAPA 快 10-50 倍(吞吐量 >250 MB/s) - **单一二进制文件**:无需 Python 运行时(~5MB 静态链接) - **兼容**:使用现有的 CAPA YAML 规则(捆绑 1000+ 条) - **纯 Rust**:默认构建中无 C/C++ 依赖 - **跨平台**:支持在 Windows、Linux 和 macOS 上构建 - **.NET 支持**:通过 [dotscope](https://github.com/BinFlip/dotscope) 进行 CIL 字节码和元数据分析 - **集成就绪**:提供 JSON 输出以供下游工具使用 ## 安装说明 ### 前置条件 通过 [rustup](https://rustup.rs) 安装 Rust: ``` curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` ### 构建 ``` # 默认构建 (x86/x64 分析) cargo build --release # 带 .NET CIL 分析 cargo build --release --features dotnet ``` 二进制文件将位于 `target/release/capa-rs`(Windows 上为 `capa-rs.exe`)。 ### 使用 just(可选) [just](https://github.com/casey/just) 提供了便捷的构建命令: ``` cargo install just just build-release # Default release build just build-release-dotnet # With .NET support ``` ## 使用方法 ``` # 基础分析 (自动检测格式) capa-rs -r capa-rules malware.exe # JSON 输出 capa-rs -r capa-rules -j malware.exe # 详细输出 (显示匹配地址) capa-rs -r capa-rules -v malware.exe # 按 namespace 过滤 capa-rs -r capa-rules -n "anti-analysis" malware.exe # Shellcode 分析 (需要显式指定格式) capa-rs -f sc64 -r capa-rules shellcode.bin # 64-bit capa-rs -f sc32 -r capa-rules shellcode.bin # 32-bit # .NET 分析 (需要 dotnet 功能) capa-rs -f dotnet -r capa-rules assembly.exe # 导出提取的特征到 JSON capa-rs -r capa-rules --dump-features malware.exe > features.json # 使用预提取特征 (跳过反汇编) capa-rs -r capa-rules -F features.json ``` ### 支持的格式 | 格式 | 标志 | 描述 | |--------|------|-------------| | Auto | `-f auto` | 从文件头自动检测(默认) | | PE | `-f pe` | Windows PE 可执行文件 | | ELF | `-f elf` | Linux ELF 可执行文件 | | .NET | `-f dotnet` | .NET assembly(需要 `dotnet` feature) | | Shellcode 64 | `-f sc64` | 原始 64 位 x64 shellcode | | Shellcode 32 | `-f sc32` | 原始 32 位 x86 shellcode | ## 项目结构 ``` capa-rs/ ├── crates/ │ ├── capa-core/ # Rule parsing, matching engine, output formatting │ ├── capa-backend/ # Binary loading, disassembly, feature extraction │ ├── capa-cli/ # Command-line interface │ └── dotscope/ # .NET PE/CIL analysis library ├── capa-rules/ # CAPA YAML rules (1000+) ├── enhanced-dotnet-rules/ # Additional .NET-specific rules ├── docs/ # Rule-writing guides ├── scripts/ # Benchmarking tools ├── justfile # Build automation └── ARCHITECTURE.md # System design and diagrams ``` ### Crate 概览 | Crate | 用途 | |-------|---------| | **capa-core** | 规则 YAML 解析器、布尔匹配引擎、JSON/表格输出 | | **capa-backend** | PE/ELF 加载 (goblin)、x86/x64 反汇编 (iced-x86)、IR 提升、特征提取 | | **capa-cli** | CLI 参数解析、编排、格式检测 | | **dotscope** | .NET 元数据表、CIL 反汇编、类型解析 | ## Feature Flags | Feature | 构建命令 | 描述 | |---------|--------------|-------------| | default | `cargo build --release` | 通过 iced-x86 进行 x86/x64 分析(纯 Rust) | | dotnet | `cargo build --release --features dotnet` | 通过 dotscope 进行 .NET CIL 分析(纯 Rust) | ## 架构 ### 分析流水线 ``` Binary File | v +------------------+ +-----------------+ +------------------+ | Loader (goblin) | --> | Lifter (iced / | --> | Extractor | | PE/ELF parsing | | capstone) | | Feature harvest | +------------------+ +-----------------+ +------------------+ | +----------------------------------------------------+ | v +------------------+ +-----------------+ +------------------+ | Rule Parser | --> | Match Engine | --> | Output (JSON / | | YAML -> AST | | Boolean eval | | table / verbose)| +------------------+ +-----------------+ +------------------+ ``` ### 二进制加载 (`capa-backend::loader`) [goblin](https://github.com/m4b/goblin) 解析 PE 和 ELF 头,提取: - 导入/导出表(含转发导出解析) - 节区头和属性 - 入口点和架构检测 - .NET CLR 头检测(COM 描述符目录) - ASCII 和 UTF-16LE 字符串提取(最少 4 个字符) Shellcode 作为单个可执行节区加载,位于偏移量 0 处,无需解析头。 ### 反汇编与提升 (`capa-backend::lifter`) 两个反汇编后端,根据架构自动选择: | 后端 | 库 | 架构 | 纯 Rust | |---------|---------|---------------|-----------| | **iced-x86**(默认) | [iced-x86](https://github.com/icedland/iced) | x86, x86-64 | 是 | | **capstone**(后备) | [capstone-rs](https://github.com/capstone-rust/capstone-rs) | ARM, AArch64, MIPS, PPC | 否(C 绑定) | 提升器执行递归下降反汇编以生成中间表示: - **函数发现**:入口点、导出地址、调用目标分析、PE `.pdata` 异常表 (x64) 以及函数序言模式匹配 - **基本块构建**:在分支、调用和跳转目标处分割,具有后继/前驱边 - **循环检测**:对基本块 CFG 进行后向边分析以识别循环头 - **Thunk 解析**:识别单个 `jmp` 函数并将其解析为 IAT 目标 - **IAT 映射**:映射导入地址表条目以进行间接调用解析 (`call [rip+disp]`) - **地址处字符串映射**:按地址索引数据节区字符串,用于基于指针的字符串引用检测 每条指令被提升为 `ILOperation` 变体(`Assign`、`Store`、`Load`、`Branch`、`Xor`、`Other`),供特征提取器使用。 ### .NET 分析 (`capa-backend::dotnet_extractor` + `dotscope`) 当启用 `dotnet` feature 时,[dotscope](https://github.com/BinFlip/dotscope) 提供: - **元数据表**:TypeDef、MethodDef、MemberRef、AssemblyRef 解析 - **用户字符串堆** (`#US`):将 `ldstr` token 操作数解析为实际字符串字面量 - **CIL 反汇编**:从方法体解码 IL 指令流(tiny + fat 头) - **P/Invoke 导入**:通过 `DllImport` / `ImplMap` 的原生 DLL 导入 - **按方法特征**:IL 助记符、数字常量和字符串引用,范围限定于单个方法,用于函数级规则匹配 ### 特征提取 (`capa-backend::extractor`) 特征在文件、函数、基本块和指令范围内收集: | 特征 | 范围 | 来源 | |---------|-------|--------| | API 调用 | 基本块 | 导入表 + thunk 解析 + IAT 间接调用 | | 字符串 (ASCII/wide) | 基本块 | 数据节区字符串映射,内存操作数解引用 | | 数字常量 | 基本块 | 来自提升指令的立即操作数 | | 助记符 | 基本块 | 指令助记符 (`mov`、`xor`、`call` 等) | | 节区 | 文件 | PE/ELF 节区名称 | | 导入 | 文件 | 带有 A/W/Ex 后缀标准化的导入表 | | 导出 | 文件 | 导出表(包括转发目标) | | 特性 | 变化 | 见下表 | | 字节模式 | 指令 | 原始指令字节 | | 偏移量 | 基本块 | 内存引用位移值 | | .NET 类型/方法 | 文件 + 函数 | dotscope 元数据(带 `dotnet` feature) | **特性检测:** | 特性 | 检测方式 | |----------------|-------------| | `nzxor` | 不同操作数的 XOR(非清零,非 XOR 0) | | `loop` | 基本块 CFG 中的后向边 | | `tight loop` | 包含 <= 10 条指令的循环头块 | | `recursive call` | 函数调用自身地址 | | `stack string` | 4 次以上带可打印 ASCII 立即数的 `mov` 到栈 | | `indirect call` | 无解析目标的 `call` | | `embedded pe` | 在前 512 字节后找到 MZ 头 + 有效的 PE 签名 | | `cross section flow` | 位于不同 PE/ELF 节区的分支目标 | | `peb access` | FS/GS 段寄存器访问(Windows 特有) | | `call $+5` | Shellcode 获取 EIP/RIP 模式 | | `forwarded export` | 导出到另一个 DLL 的导出条目 | | `mixed mode` | .NET CLR 头 + 原生可执行节区 + 导出 | ### 规则解析与匹配 (`capa-core`) **解析器** (`capa-core::rule::parser`):将 CAPA YAML 规则解析为带有布尔运算符(`and`、`or`、`not`、`optional`)、计数约束(`count(api(...)): 2 or more`)和子范围运算符(`instruction:`、`basic block:`、`function:`)的 `FeatureNode` 树 AST。 **匹配引擎** (`capa-core::matcher::engine`): - 使用 [rayon](https://github.com/rayon-rs/rayon) 线程池进行并行规则评估 - 通过 [DashMap](https://github.com/xacrimon/dashmap) 进行无锁结果收集 - 预编译的 [Aho-Corasick](https://github.com/BurntSushi/aho-corasick) 自动机用于批量字符串匹配 - 范围感知评估:文件范围规则检查合并特征,函数/基本块范围规则逐函数迭代 - API 名称标准化:`CreateFileW` -> `CreateFile`,`CreateFileExA` -> `CreateFileEx` + `CreateFile` ### 支持的分析 #### 二进制格式 | 格式 | 32 位 | 64 位 | 加载器 | |--------|--------|--------|--------| | PE (Windows) | 是 | 是 | goblin | | ELF (Linux) | 是 | 是 | goblin | | .NET (CIL) | 是 | 是 | goblin + dotscope | | Shellcode | 是 | 是 | 原始字节(无头) | #### 架构支持 | 架构 | 反汇编器 | Feature Flag | 纯 Rust | |--------------|-------------|--------------|-----------| | x86 / x86-64 | iced-x86 | default | 是 | | .NET CIL | dotscope | `dotnet` | 是 | | ARM (32-bit) | capstone-rs | - | 否 | | ARM64 / AArch64 | capstone-rs | - | 否 | | MIPS | capstone-rs | - | 否 | | PowerPC 32/64 | capstone-rs | - | 否 | ## 输出格式 ``` { "matched_rules": 42, "total_rules": 1087, "capabilities": [ { "name": "check for debugger via API", "namespace": "anti-analysis/anti-debugging", "matches": 3, "attack": ["T1622"] } ], "mitre_attack": ["T1622", "T1497.001"], "namespaces": { "anti-analysis/anti-debugging": ["check for debugger via API"] } } ``` ## 测试 使用 `cargo test` 或 `just test` 运行所有测试。 ### capa-core (`crates/capa-core/tests/`) | 测试文件 | 描述 | |-----------|-------------| | `test_engine.rs` | 匹配引擎逻辑 | | `test_rules.rs` | 规则解析和加载 | | `test_rules_extended.rs` | 扩展规则解析 | | `test_rules_insn_scope.rs` | 指令范围规则匹配 | | `test_match.rs` | 匹配评估 | | `test_capabilities.rs` | 能力检测 | | `test_rule_features.rs` | 规则特征提取 | | `test_output.rs` | 输出格式化 (JSON/table) | | `test_serialization.rs` | 序列化和反序列化 | ### capa-backend (`crates/capa-backend/tests/`) | 测试文件 | 描述 | |-----------|-------------| | `test_loader.rs` | 二进制加载器 (PE/ELF) | | `test_extractor_hashing.rs` | 提取器哈希 | | `test_strings.rs` | 字符串提取 | | `test_helpers.rs` | 辅助实用程序 | ### dotscope (`crates/dotscope/tests/`) | 测试文件 | 描述 | |-----------|-------------| | `builders.rs` | 构建器模式 | | `crafted_1.rs` | 构造的 .NET 二进制解析 | | `fuzzer.rs` | 模糊测试 | | `modify_add.rs` | 元数据 (添加) | | `modify_basic.rs` | 元数据修改 (基础) | | `modify_heaps.rs` | 堆修改 | | `modify_impexp.rs` | 导入/导出修改 | | `modify_roundtrips_crafted2.rs` | 往返测试 (构造的二进制) | | `modify_roundtrips_method.rs` | 往返测试 (方法) | | `modify_roundtrips_wbdll.rs` | 往返测试 (DLL) | | `mono.rs` | Mono 运行时兼容性 | | `roundtrip_asm.rs` | Assembly 往返 | 此外,许多源文件包含内联单元测试(`#[cfg(test)]` 模块)。 ## Just 命令 运行 `just --list` 查看所有可用命令。 | 命令 | 描述 | |---------|-------------| | `just build` | 使用默认 features 构建 | | `just build-release` | 构建优化的 release 二进制 | | `just build-release-dotnet` | 带 .NET 支持的 release 构建 | | `just test` | 运行所有测试 | | `just run ` | 分析二进制文件 | | `just run-json ` | 使用 JSON 输出分析 | | `just run-dotnet ` | 分析 .NET assembly | | `just doc-open` | 生成并查看 API 文档 | | `just lint` | 运行 clippy linter | | `just fmt` | 格式化代码 | | `just check-all` | 格式检查 + lint + 测试 | | `just features` | 显示可用的 feature flags | ## 文档 编写 CAPA 规则的指南: | 指南 | 描述 | |-------|-------------| | [PE 规则指南](docs/pe-rules-guide.md) | 编写 Windows PE 二进制文件规则 | | [ELF 规则指南](docs/elf-rules-guide.md) | 编写 Linux ELF 二进制文件规则 | | [.NET 规则指南](docs/dotnet-rules-guide.md) | 编写 .NET assemblies 规则 | | [Shellcode 规则指南](docs/shellcode-rules-guide.md) | 编写原始 shellcode 规则 | | [架构](ARCHITECTURE.md) | 带 Mermaid 图表的系统设计 | ## 许可证 Apache-2.0 - 详情见 [LICENSE](LICENSE)。
标签:Caido项目解析, CAPA, Cobalt Strike, EDR, ELF分析, Findomain, Mandiant, .NET分析, PE分析, Rust, Windows日志分析, YARA替代, 二进制分析, 云安全监控, 云安全运维, 云计算, 云资产清单, 可执行文件, 可视化界面, 威胁情报, 开发者工具, 恶意代码分析, 攻击诱捕, 数据展示, 红队, 网络安全, 网络流量审计, 脆弱性评估, 自定义DNS解析器, 规则引擎, 逆向工程, 通知系统, 配置文件, 隐私保护, 静态分析