MayerDaniel/the-one-wsl-bof
GitHub: MayerDaniel/the-one-wsl-bof
一个通过 COM 接口直接操作 WSL 发行版的 Cobalt Strike BOF 工具,无需生成 wsl.exe 进程即可在红队行动中隐蔽执行 Linux 命令。
Stars: 182 | Forks: 13
# WSL BOF - 适用于 Linux 的 Windows 子系统 Beacon Object File
这是一个 Cobalt Strike Beacon Object File (BOF),它允许直接从 Beacon 会话中执行 WSL 发行版内的命令,而无需生成 `wsl.exe` 进程。
## 概述
该项目提供了一个 BOF,它通过内部 COM 接口 (`ILxssUserSession`) 和注册表与 WSL 进行交互,允许操作者:
- **列出**所有已安装的 WSL 发行版
- **执行**任何 WSL 发行版内的任意命令
### 为什么使用它?
传统的运行 WSL 命令的方法需要生成 `wsl.exe`,这会:
- 创建新进程(对 EDR 可见)
- 出现在命令行日志记录中
- 可能被应用程序白名单拦截
该 BOF 直接调用 WSL COM 服务,完全避免了 `wsl.exe` 的进程创建。
## 项目结构
```
the-one-wsl-bof/
├── bof/ # Main BOF project
│ ├── WSL/ # BOF source code
│ │ ├── bof.cpp # Main BOF implementation
│ │ ├── beacon.h # Cobalt Strike Beacon API
│ │ ├── Makefile # nmake build file
│ │ ├── headers/ # Generated COM interface headers
│ │ │ └── wslserviceproxystub_*.h # Version-specific interfaces
│ │ └── base/ # Helper utilities
│ ├── Release/ # Compiled BOF output
│ │ └── wsl.x64.o # 64-bit BOF
│ ├── wsl-com.cna # Aggressor script
│ └── WSL.sln # Visual Studio solution
│
└── discovery/ # Research/discovery tools
├── ComProxyDump/ # Tool to extract IDL from proxy DLLs
└── MsiScripts/ # Scripts for extracting WSL interface definitions
```
## 支持的 WSL 版本
该 BOF 会自动检测已安装的 WSL 版本,并使用相应的 COM 接口:
| WSL 版本范围 | 接口版本 |
|-------------------|-------------------|
| 2.0.0.0 - 2.2.4.0 | `ILxssUserSession_2_0_0_0` |
| 2.3.11.0 - 2.3.17.0 | `ILxssUserSession_2_3_11_0` |
| 2.3.21.0 - 2.3.26.0 | `ILxssUserSession_2_3_21_0` |
| 2.4.4.0 - 2.4.13.0 | `ILxssUserSession_2_4_4_0` |
| 2.5.1.0 | `ILxssUserSession_2_5_1_0` |
| 2.5.4.0 | `ILxssUserSession_2_5_4_0` |
| 2.5.6.0 - 2.5.10.0 | `ILxssUserSession_2_5_6_0` |
| 2.6.0.0 - 2.7.0.0+ | `ILxssUserSession_2_6_0_0` |
## 构建
### 前置条件
- **Visual Studio 2019+** 及 C++ 桌面开发工作负载
- **Windows SDK** (10.0.19041.0 或更高版本)
- **Python 3**(可选,用于 boflint)
### 使用 Visual Studio 构建
1. 在 Visual Studio 中打开 `bof/WSL.sln`
2. 选择 **Release** 配置和 **x64** 平台
3. 构建解决方案 (Ctrl+Shift+B)
编译后的 BOF 将位于 `bof/Release/bof.x64.o`
### 使用 nmake 构建(命令行)
```
:: Open a Developer Command Prompt for VS, or run vcvars64.bat
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
:: Navigate to the project and build
cd bof\WSL
nmake
:: Output: ..\Release\bof.x64.o
```
## 在 Cobalt Strike 中使用
### 加载 Aggressor Script
1. 在 Cobalt Strike 中,转到 **Cobalt Strike → Script Manager**
2. 点击 **Load** 并选择 `bof/wsl-com.cna`
### 命令
#### 列出 WSL 发行版
```
beacon> wsl list
```
输出:
```
Windows Subsystem for Linux Distributions:
Ubuntu (Default) (WSL2)
Debian (WSL2)
kali-linux (WSL2)
```
#### 执行命令
```
beacon> wsl exec
```
示例:
```
beacon> wsl exec Ubuntu whoami
beacon> wsl exec Ubuntu "cat /etc/passwd"
beacon> wsl exec Ubuntu "uname -a"
beacon> wsl exec kali-linux "id && hostname"
```
### 帮助
```
beacon> wsl
```
## 工作原理
1. **版本检测**:从注册表中读取 WSL 版本 (`HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\MSI\Version`)
2. **COM 初始化**:使用 `COINIT_MULTITHREADED` 和模拟级别安全 (impersonation-level security) 初始化 COM
3. **会话创建**:通过带有 CLSID `{A9B7A1B9-0671-405C-95F1-E0612CB4CE7E}` 的 `CoCreateInstance` 创建 `ILxssUserSession` 实例
4. **发行版查找**:调用 `GetDistributionId()` 将发行版名称解析为 GUID
5. **进程创建**:调用 `CreateLxProcess()` 以在发行版内生成 `/bin/bash -c ""`
6. **输出捕获**:从返回的 socket 句柄读取 stdout/stderr 并发送到 Beacon
## 发现工具
`discovery/` 文件夹包含了用于逆向工程 WSL COM 接口的工具:
### ComProxyDump
从 COM 代理/存根 (proxy/stub) DLL 中提取 IDL 接口定义。用于生成 `bof/WSL/headers/` 中的头文件。
```
cd discovery\ComProxyDump
dotnet build -c Release
:: Dump WSL proxy interfaces
bin\Release\ComProxyDump.exe "C:\Program Files\WSL\wslserviceproxystub.dll" output.idl
```
### MsiScripts
PowerShell 脚本,用于:
- 从 Microsoft 下载多个版本的 WSL
- 从每个版本中提取 `wslserviceproxystub.dll`
- 生成并对比不同版本的 IDL 定义
## 致谢
- 感谢 [Adam Chester](https://github.com/xpn)、[Antero Guy](https://github.com/antroguy) 和 [Lee Chagolla-Christensen](https://github.com/leechristensen) 的贡献
- COM 接口研究基于 [wslbridge2 team](https://github.com/Biswa96/wslbridge2) 的工作
- BOF 模板基于 Cobalt Strike 的 [VS Template](https://github.com/Cobalt-Strike/bof-vs)
- 感谢 James Forshaw 开发的 [OleViewDotNet](https://github.com/tyranid/oleviewdotnet) 用于 COM 代理分析
标签:AI合规, BOF, C++, Cobalt Strike, SCP, Windows子系统Linux, 多人体追踪, 攻击诱捕, 数据擦除, 欺骗防御, 知识库安全, 逆向工具