long-910/BinSleuth
GitHub: long-910/BinSleuth
一款零依赖的静态二进制安全分析 CLI 工具,用于检查 ELF/PE 文件的安全加固标志、熵值分析和危险符号检测。
Stars: 0 | Forks: 0
# 🔍 BinSleuth
**一个快速、零依赖的静态二进制安全分析 CLI 工具。**
在几毫秒内检查 ELF 和 PE 二进制文件的安全加固标志,并检测加壳/加密区段。
[](https://crates.io/crates/binsleuth)
[](https://crates.io/crates/binsleuth)
[](https://docs.rs/binsleuth)
[](https://github.com/long-910/BinSleuth/actions/workflows/ci.yml)
[](https://github.com/long-910/BinSleuth/actions/workflows/release.yml)
[](LICENSE)
[](https://www.rust-lang.org)
[](#)
**Language / 言语 / 语言:**
[English](README.md) · [日本語](README.ja.md) · [中文](README.zh.md)
## 什么是 BinSleuth?
BinSleuth 是一个用 Rust 编写的**专注于安全的静态二进制分析器**。
它充当编译后可执行文件的快速健康检查工具——回答:
- *"这个二进制文件是否启用了现代安全保护?"*
- *"这个区段是否可能是加壳或加密的恶意软件?"*
- *"这个二进制文件是否导入了危险的操作系统级函数?"*
它专为**安全工程师、恶意软件研究人员和开发人员**设计,无需启动完整的逆向工程套件即可获得即时答案。
## 功能
### 1. 安全加固检查
| 标志 | 描述 | ELF | PE |
|------|-------------|-----|----|
| **NX** | 不可执行栈/数据 —— 防止代码注入 | `PT_GNU_STACK` | `NX_COMPAT` |
| **PIE** | 位置无关可执行文件 —— 启用 ASLR | `ET_DYN` | `DYNAMIC_BASE` |
| **RELRO** | 只读重定位 —— 防止 GOT 覆盖 | `PT_GNU_RELRO` + `BIND_NOW` | N/A |
| **Stack Canary** | 缓冲区溢出探针符号存在 | `__stack_chk_fail` | `__security_cookie` |
| **FORTIFY_SOURCE** | 加固的 libc 包装器 (`__*_chk`) —— 对不安全的字符串/内存调用进行编译时边界检查 | `__memcpy_chk`, … | `__memcpy_chk`, … |
| **No RPATH/RUNPATH** | 不存在嵌入式库搜索路径 —— 防止通过可写/相对 RPATH 进行库注入 | `DT_RPATH` / `DT_RUNPATH` | N/A |
| **Stripped** | 调试符号 / DWARF 信息不存在 —— 限制逆向工程 | `.debug_*` sections | Debug directory |
每次检查报告以下状态之一:**Enabled** / **Partial** / **Disabled** / **N/A**
### 2. 区段熵分析
BinSleuth 计算每个区段的 [香农熵](https://en.wikipedia.org/wiki/Entropy_(information_theory)):
```
H = -Σ P(x) · log₂(P(x)) range: [0.0 – 8.0]
```
| 熵值范围 | 解释 |
|---------------|----------------|
| 0.0 – 4.0 | 正常代码 / 数据 |
| 4.0 – 7.0 | 压缩资源(正常) |
| **> 7.0** | **⚠ 加壳 / 加密 —— 需调查** |
### 3. 危险符号检测
BinSleuth 标记通常出现在恶意或不安全二进制文件中的符号:
| 类别 | 示例 |
|----------|---------|
| **代码执行** | `system`, `execve`, `popen`, `WinExec`, `CreateProcess` |
| **网络** | `connect`, `socket`, `gethostbyname`, `WinHttpOpen` |
| **内存操作** | `mprotect`, `mmap`, `VirtualAlloc`, `VirtualProtect` |
## 安装
### 从 crates.io 安装(推荐)
```
cargo install binsleuth
```
### 从源码安装
```
git clone https://github.com/long-910/BinSleuth.git
cd BinSleuth
cargo build --release
# Binary 输出:./target/release/binsleuth
```
### 需求
- Rust **1.85** 或更高版本
- 无需系统库 —— 纯 Rust
## 使用方法
```
binsleuth [OPTIONS]
Arguments:
Path to the ELF or PE binary to analyze
Options:
-v, --verbose Show all sections, even those with normal entropy
--json Output results as JSON instead of the colored terminal report
--strict Exit with code 2 if any hardening protection is missing or
dangerous symbols are found (useful in CI pipelines)
-h, --help Print help
-V, --version Print version
```
### 基本分析
```
binsleuth /usr/bin/ls
binsleuth ./myapp.exe
binsleuth ./suspicious_binary
```
### 显示所有区段(包括低熵区段)
```
binsleuth --verbose /usr/bin/python3
```
### JSON 输出(用于脚本 / CI 集成)
```
binsleuth --json /usr/bin/ls | jq '.hardening.nx'
```
### CI 流水线 —— 发现加固问题时失败
```
binsleuth --strict ./myapp && echo "Hardening OK" || echo "Hardening FAILED"
# Exit 0 = 一切正常,Exit 2 = 发现加固问题,Exit 1 = 解析错误
```
### 示例输出 —— 已加固的二进制文件
```
╔══════════════════════════════════════════════════════╗
║ BinSleuth — Binary Analyzer ║
╚══════════════════════════════════════════════════════╝
File: /usr/bin/ls
Format: ELF
Arch: X86_64
── Security Hardening ──────────────────────────────────
[ ENABLED ] NX (Non-Executable Stack)
[ ENABLED ] PIE (ASLR-compatible)
[ ENABLED ] RELRO (Read-Only Relocations)
[ ENABLED ] Stack Canary
[ ENABLED ] FORTIFY_SOURCE
[ ENABLED ] No RPATH/RUNPATH
[ ENABLED ] Debug Symbols Stripped
── Section Entropy ─────────────────────────────────────
Section Size (B) Entropy Status
──────────────────────────────────────────────────────────────────────
All sections within normal entropy range.
(run with --verbose to show all sections)
── Dangerous Symbol Usage ──────────────────────────────
No dangerous symbols detected.
────────────────────────────────────────────────────────
Analysis complete.
```
### 示例输出 —— 可疑 / 加壳二进制文件
```
── Section Entropy ─────────────────────────────────────
Section Size (B) Entropy Status
──────────────────────────────────────────────────────────────────────
UPX0 491520 7.9981 ⚠ Packed/Encrypted suspected
UPX1 32768 7.9912 ⚠ Packed/Encrypted suspected
2 section(s) with entropy > 7.0 detected!
── Dangerous Symbol Usage ──────────────────────────────
3 dangerous symbol(s) found:
▶ execve
▶ mprotect
▶ socket
```
## 库用法
除了作为 CLI 使用外,`binsleuth` 还可以用作 Rust library crate。
添加到您的 `Cargo.toml`:
```
[dependencies]
binsleuth = "0.2"
```
然后使用公共 API:
```
use binsleuth::analyzer::hardening::HardeningInfo;
use binsleuth::analyzer::entropy::SectionEntropy;
let data = std::fs::read("path/to/binary")?;
let hardening = HardeningInfo::analyze(&data)?;
println!("PIE: {:?}", hardening.pie);
let entropies = SectionEntropy::analyze(&data)?;
for sec in &entropies {
println!("{}: entropy={:.4}", sec.name, sec.entropy);
}
```
请参阅 [docs.rs 上的 API 文档](https://docs.rs/binsleuth) 和 [`examples/basic.rs`](examples/basic.rs) 获取完整的可运行示例。
## 项目结构
```
BinSleuth/
├── Cargo.toml
├── README.md ← English (default)
├── README.ja.md ← Japanese
├── README.zh.md ← Chinese (Simplified)
├── CHANGELOG.md
├── LICENSE
├── examples/
│ └── basic.rs # Library usage example
└── src/
├── lib.rs # Library crate root (public API)
├── main.rs # CLI entry point (clap)
├── analyzer/
│ ├── mod.rs
│ ├── entropy.rs # Shannon entropy + SectionEntropy
│ └── hardening.rs # NX / PIE / RELRO / Canary / symbols
└── report/
├── mod.rs
├── terminal.rs # Colored terminal renderer
└── json.rs # JSON output serializer
```
### 关键类型
| 类型 | 位置 | 角色 |
|------|----------|------|
| `HardeningInfo` | `analyzer/hardening.rs` | 汇总的加固检查结果 |
| `CheckResult` | `analyzer/hardening.rs` | `Enabled` / `Partial(msg)` / `Disabled` / `N/A` |
| `SectionEntropy` | `analyzer/entropy.rs` | 区段名称 + 熵值 + 字节大小 |
| `TerminalReporter` | `report/terminal.rs` | 彩色终端输出渲染器 |
## 支持的格式
| 格式 | 架构 | NX | PIE | RELRO | Canary | FORTIFY | RPATH |
|--------|---------------|----|-----|-------|--------|---------|-------|
| ELF 32-bit | x86, ARM, MIPS, … | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| ELF 64-bit | x86-64, AArch64, … | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| PE 32-bit (PE32) | x86 | ✅ | ✅ | N/A | ✅ | ✅ | N/A |
| PE 64-bit (PE32+) | x86-64 | ✅ | ✅ | N/A | ✅ | ✅ | N/A |
## 退出代码
| 代码 | 含义 |
|------|---------|
| `0` | 分析成功完成 |
| `1` | 文件未找到、解析错误或不支持的格式 |
| `2` | `--strict` 模式:分析成功但发现加固问题 |
## 测试
```
# 所有测试(Unit + Integration)
cargo test
# 仅 Unit 测试
cargo test --lib
# 仅 Integration 测试(需要已编译的 binary)
cargo test --test cli
# Lint
cargo clippy -- -D warnings
# Format 检查
cargo fmt --check
```
测试套件包括 **24 个单元测试**、**20 个集成测试**和 **1 个文档测试**:
| 模块 | 测试 | 覆盖范围 |
|--------|-------|---------|
| `analyzer::entropy` | 9 | 香农公式、边界情况、单调性 |
| `analyzer::hardening` | 15 | PE 头解析、RELRO 状态、FORTIFY_SOURCE、RPATH、ELF 自分析 |
| `tests::cli` | 20 | CLI 标志、JSON 输出、严格模式、剥离检测、错误处理 |
| `lib.rs` (doc test) | 1 | 库 API 冒烟测试 |
## 路线图
- [x] JSON 输出模式 (`--json`)
- [x] DWARF / PDB 调试信息 / 剥离检测
- [x] CI 流水线的严格模式 (`--strict`,退出代码 2)
- [x] FORTIFY_SOURCE 检测 (`__*_chk` 符号扫描)
- [x] RPATH/RUNPATH 检测(库注入风险)
- [ ] SARIF 输出格式
- [ ] macOS Mach-O 支持
- [ ] 两个二进制文件之间的导入表差异对比 (`binsleuth diff a.out b.out`)
- [ ] Yara 规则风格的字节模式匹配
## 许可证
本项目采用 **MIT 许可证** 授权 —— 详情请参阅 [LICENSE](LICENSE)。
## 致谢
- [object](https://crates.io/crates/object) —— 跨平台二进制解析
- [clap](https://crates.io/crates/clap) —— CLI 参数解析
- [anyhow](https://crates.io/crates/anyhow) —— 符合人体工程学的错误处理
- [colored](https://crates.io/crates/colored) —— 终端彩色输出
Made with ❤️ and Rust
标签:ASLR, DeepSeek, DEP/NX, DNS 反向解析, ELF分析, GitHub Advanced Security, PE分析, PIE, Rust, 二进制分析, 二进制安全, 云安全监控, 云安全运维, 云资产清单, 加壳检测, 可视化界面, 安全加固, 文档结构分析, 漏洞缓解措施, 熵值分析, 网络安全, 网络流量审计, 逆向工程, 通知系统, 隐私保护, 静态分析