memN0ps/matrix-rs
GitHub: memN0ps/matrix-rs
一个基于 Rust 的 Windows 内核级 Type-2 研究 Hypervisor,支持 Intel VT-x 和 EPT,用于安全研究与虚拟化技术学习。
Stars: 328 | Forks: 36
# Windows 内核 Rust 编写的蓝药丸 Type-2 Hypervisor(代号:Matrix)




一个轻量级、内存安全且极速的基于 Rust 的 type-2 研究 hypervisor,支持 Intel VT-x Hook,专注于研究虚拟化的核心概念。
**注意:** [**Illusion** hypervisor](https://github.com/memN0ps/illusion-rs)(Windows UEFI 蓝药丸 Type-1 Hypervisor,Rust 编写)更稳定,支持更多功能,整体设计也更好。[**Matrix** hypervisor](https://github.com/memN0ps/matrix-rs)(Windows 内核蓝药丸 Type-2 Hypervisor,Rust 编写)是一个较旧的实验版本,不适用于生产环境。这两个项目都旨在作为模板,帮助人们入门 Rust 中的 Hypervisor 开发。
## 功能特性
- :white_check_mark: **Extended Page Tables (EPT)**:支持 Memory Type Range Registers (MTRR)。
- :white_check_mark: **VM Exit 处理**:处理 `ExceptionOrNmi (#GP, #PF, #BP, #UD)`、`Cpuid`、`Getsec`、`Vmcall`、`Vmclear`、`Vmlaunch`、`Vmptrld`、`Vmptrst`、`Vmresume`、`Vmxon`、`Vmxoff`、`Rdmsr`、`Wrmsr`、`Invd`、`Rdtsc`、`EptViolation`、`EptMisconfiguration`、`Invept`、`Invvpid`、`Xsetbv`。
- :white_check_mark: **隐藏式内核内联 Hook**:兼容 PatchGuard 的断点 (`int3`) Hook。
- :white_check_mark: **隐藏式系统调用 Hook**:兼容 PatchGuard 的 System Service Descriptor Table (SSDT) 函数入口 Hook。
- :white_check_mark: **隔离与安全**:Global Descriptor Table (GDT)、Interrupt Descriptor Table (IDT) 和页表的自定义实现,以提高 hypervisor 的安全性和隔离性。[感谢 @namazso](https://www.unknowncheats.me/forum/2779560-post4.html) 的提醒。
## 支持的硬件
- :white_check_mark: 支持 VT-x 和 Extended Page Tables (EPT) 的 Intel 处理器。
- :x: 支持 AMD-V (SVM) 和 Nested Page Tables (NPT) 的 AMD 处理器。
## 支持的平台
- :white_check_mark: Windows 10 - Windows 11,仅限 x64。
## 安装说明
1. 从[这里](https://www.rust-lang.org/tools/install)安装 Rust。
2. 切换到 Rust Nightly 版本:`rustup toolchain install nightly` 和 `rustup default nightly`。
3. 安装 LLVM:`winget install LLVM.LLVM`。
4. 安装工具:`cargo install cargo-make cargo-expand cargo-edit cargo-workspaces`。
5. 安装 WDK/SDK/EWDK:步骤见[这里](https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk)。
6. 设置 `WDKContentRoot` 环境变量指向你的 WDK 安装路径(如果 WDK 安装过程中未自动设置):
```
[System.Environment]::SetEnvironmentVariable("WDKContentRoot", "C:\Program Files (x86)\Windows Kits\10", [System.EnvironmentVariableTarget]::User)
```
## 构建项目
- 开发版:`cargo make --profile development`。
- 正式版:`cargo make --profile release`。
## 调试
#### 开启调试模式
- 测试模式:使用 `bcdedit.exe /set testsigning on` 激活测试签名。
- Windows 调试:按照此 [Microsoft 指南](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/bcdedit--bootdebug)中的步骤操作。
```
bcdedit.exe /bootdebug {bootmgr} on
bcdedit.exe /bootdebug on
bcdedit.exe /debug on
```
#### 使用 Windbg 进行网络调试
设置:`bcdedit.exe /dbgsettings net hostip:w.x.y.z port:n`。
#### 调试打印过滤器
1. 打开 `regedit.exe`。
2. 转到 `HKLM\SYSTEM\CurrentControlSet\Control\Session Manager`。
3. 创建 `Debug Print Filter`,设置 `DEFAULT` DWORD = `8`。
#### VMware 串口调试
1. 在 VMware 中添加串口:'使用输出文件'。
2. 在 Windows 虚拟机中配置:`$serialPort = New-Object System.IO.Ports.SerialPort COM2,9600,None,8,One; $serialPort.Open()`。
#### 服务管理
使用服务控制器 (`sc.exe`) 创建和管理 hypervisor 服务:
```
sc.exe create matrix type= kernel binPath= C:\Windows\System32\drivers\matrix.sys
sc.exe query matrix
sc.exe start matrix
```
## 使用说明
1. **为 VMware Workstation 做准备**
构建项目:按照前面章节提供的构建说明编译项目。
2. **设置 VMware Workstation**
配置 VMware 串口以进行调试。

**图 1:VMware 串口设置**
确保启用了 `"Virtualize Intel VT-x/EPT or AMD-V/RVI"` 并且启用了 `"Virtualize IOMMU (IO memory management unit)"`。

**图 2:VMware 处理器设置**
3. **复制驱动程序**
将 `matrix.sys` 驱动程序复制到 `C:\Windows\System32\drivers` 目录。
4. **VMware 串口调试**
执行串口调试部分中的 PowerShell 脚本以打开串口。
5. **加载 Hypervisor**
使用服务管理部分显示的 `load.ps1` PowerShell 脚本创建并启动 hypervisor 服务。

**图 3:加载 Hypervisor**
### PoC

**图 4:日志和 Windbg PoC 设置**

**图 5:日志和 Windbg PoC 执行**
## 致谢、参考与动机
非常感谢那些塑造了这个项目的杰出人士和资源。特别鸣谢下面列出的每一个人。虽然我没有在工作中使用所有这些资源,但它们一直是信息的金矿,对于任何深入研究 Hypervisor 开发(包括我)的人来说都非常有帮助。
- **[Daax (@daaximus)](https://github.com/daaximus)**:感谢他出色的免费 Hypervisor 开发系列,这是现有的最佳资源之一,以其详尽的研究和清晰的解释极大地影响了我的工作。他对我的问题的支持和回答对于我入门 Hypervisor 开发非常宝贵:
- [7 Days to Virtualization](https://revers.engineering/7-days-to-virtualization-a-series-on-hypervisor-development/)。
- [MMU Virtualization via Intel EPT](https://revers.engineering/mmu-virtualization-via-intel-ept-index/)。
- **[Satoshi Tanda (@tandasat)](https://github.com/tandasat)**:Satoshi Tanda 的指导、项目和结构化培训计划非常有帮助。他在 GitHub 上的详细解释和贡献显著提高了我的理解力,使他在我的整个旅程中成为了一位伟大的导师:
- [Hypervisor Development for Security Researchers](https://tandasat.github.io/Hypervisor_Development_for_Security_Researchers.html)。
- [Hypervisor 101 in Rust](https://github.com/tandasat/Hypervisor-101-in-Rust)。
- 其他项目:[Hello-VT-rp](https://github.com/tandasat/Hello-VT-rp)、[DdiMon](https://github.com/tandasat/DdiMon)、[HyperPlatform](https://github.com/tandasat/HyperPlatform)、[MiniVisorPkg](https://github.com/tandasat/MiniVisorPkg)。
- **[Jess (@jessiep_)](https://github.com/Intege-rs)**:感谢他在本项目的多个领域提供的宝贵支持与协作,提供了关键的见解和专业知识,并迅速回答了我的问题。
- **[Drew (@drew)](https://github.com/drew-gpf)**:感谢他在 Hypervisor 开发各个方面提供的帮助、指导和对我的问题的快速响应。
- **[Sina Karvandi (@Intel80x86)](https://github.com/SinaKarvandi)**:感谢他详尽的免费 Hypervisor From Scratch 系列:
- [教程系列](https://rayanfam.com/tutorials/)。
- [GitHub 仓库](https://github.com/SinaKarvandi/Hypervisor-From-Scratch/)。
- **[Matthias (@not-matthias)](https://github.com/not-matthias)**:感谢他在 [amd_hypervisor](https://github.com/not-matthias/amd_hypervisor) 项目上的影响力工作,该工作极大地启发并影响了这项研究。
- **[Nick Peterson (@everdox)](https://github.com/everdox)** 和 **[Aidan Khoury (@ajkhoury)](https://github.com/ajkhoury)**:感谢他们对 Hypervisor 自省和系统调用 Hook 的深刻探索:
- [Patchguard: Hypervisor Based Introspection [P1]](https://revers.engineering/patchguard-detection-of-hypervisor-based-instrospection-p1/)。
- [Patchguard: Hypervisor Based Introspection [P2]](https://revers.engineering/patchguard-detection-of-hypervisor-based-instrospection-p2/)。
- [Syscall Hooking Via Extended Feature Enable Register (EFER)](https://revers.engineering/syscall-hooking-via-extended-feature-enable-register-efer/)。
#### 社区和技术资源
- **[Secret Club](https://github.com/thesecretclub)**:对抗作弊系统和 Hypervisor 检测的见解,这也启发了本项目:
- 由 [@Daax](https://github.com/daaximus)、[@iPower](https://github.com/iPower)、[@ajkhoury](https://github.com/ajkhoury)、[@drew](https://github.com/drew-gpf) 撰写的 [System emulation detection](https://secret.club/2020/04/13/how-anti-cheats-detect-system-emulation.html)。
- 由 [@vmcall](https://github.com/vmcall)、[@Daax](https://github.com/daaximus) 撰写的 [BattlEye hypervisor detection](https://secret.club/2020/01/12/battleye-hypervisor-detection.html)。
- **其他重要资源**:
- [Intel's Software Developer's Manual](https://www.intel.com/)。
- [Maurice Heumann's (@momo5502)](https://github.com/momo5502/) [Detecting Hypervisor-Assisted Hooking](https://momo5502.com/posts/2022-05-02-detecting-hypervisor-assisted-hooking/)。
- [Guided Hacking's](https://guidedhacking.com/) [x64 Virtual Address Translation](https://www.youtube.com/watch?v=W3o5jYHMh8s) (YouTube)。
- [UnKnoWnCheaTs](https://unknowncheats.me/) 上 [@namazso](https://github.com/namazso) 的 [论坛帖子](https://www.unknowncheats.me/forum/2779560-post4.html)。
- [RVM1.5](https://github.com/rcore-os/RVM1.5)、[Barbervisor](https://github.com/Cisco-Talos/Barbervisor)、[rustyvisor](https://github.com/iankronquist/rustyvisor)、[orange_slice](https://github.com/gamozolabs/orange_slice)、[mythril](https://github.com/mythril-hypervisor/mythril)、[uhyve](https://github.com/hermit-os/uhyve)、[maystorm](https://github.com/neri/maystorm)。
- [AMD-V Hypervisor Development by Back Engineering](https://blog.back.engineering/04/08/2022)、[@_xeroxz](https://git.back.engineering/_xeroxz/bluepill) 的 [bluepill](https://git.back.engineering/_xeroxz/bluepill)。
- [@wbenny](https://github.com/wbenny/hvpp) 的 [hvpp](https://github.com/wbenny/hvpp)。
- [@Air14](https://github.com/Air14/HyperHide) 的 [HyperHide](https://github.com/Air14/HyperHide)。
- M3ll0wN1ght 的 [How AetherVisor works under the hood](https://mellownight.github.io/AetherVisor)。
- [Rust library to use x86 (amd64) specific functionality and registers (x86 crate for Rust)](https://github.com/gz/rust-x86)。
- [UnknownCheats](https://www.unknowncheats.me/forum/c-and-c-/173560-hyperbone-windows-hypervisor.html) 上 [DarthTon](https://github.com/DarthTon/HyperBone) 的 [HyperBone](https://github.com/DarthTon/HyperBone)(基于传奇的 [Alex Ionescu's](https://github.com/ionescu007/SimpleVisor) 版本)。
- [Joanna Rutkowska: Pioneering the Blue Pill Hypervisor Concept, one of the earliest proofs of concept](https://blog.invisiblethings.org/2006/06/22/introducing-blue-pill.html)。
#### 帮助者和合作者
特别感谢:
- [Daax](https://revers.engineering/)。
- [Satoshi Tanda (@tandasat)](https://github.com/tandasat)。
- [Drew (@drew)](https://github.com/drew-gpf)。
- [iPower (@iPower)](https://github.com/iPower)。
- [Namazso (@namazso)](https://github.com/namazso)。
- [Jess (@jessiep_)](https://github.com/Intege-rs)。
- [Matthias @not-matthias](https://github.com/not-matthias/)。
- [@felix-rs / @joshuа](https://github.com/felix-rs)。
- [Ryan McCrystal / @rmccrystal](https://github.com/rmccrystal)。
- [Jim Colerick (@vmprotect)](https://github.com/thug-shaker)。
## 许可证
本项目采用 MIT 许可证授权。欲了解更多信息,请参阅 [MIT 许可证详情](./LICENSE)。
标签:Blue Pill, EPT, Hypervisor, Intel VT-x, PatchGuard, Rootkit, Rust, SSDT Hook, Type-2虚拟机, VM Exit, Windows内核, Zeek, 二进制安全, 云资产清单, 内存安全, 内核开发, 可视化界面, 子域名枚举, 白帽子, 系统安全, 网络安全监控, 网络流量审计, 虚拟化, 计算机底层, 逆向工程, 通知系统, 钩子技术, 驱动开发