sebafvs/bof-shell

GitHub: sebafvs/bof-shell

一个兼容 Cobalt Strike 的 Beacon Object File,通过匿名管道在进程内执行 shell 命令并流式回传输出,实现无磁盘写入、无可见窗口的轻量命令执行。

Stars: 0 | Forks: 0

# bof-shell 一个通过 `cmd.exe /c` 执行 shell 命令并将完整输出返回给操作员的 Beacon Object File (BOF)。 ## 它的功能 生成 `cmd.exe /c `,将 `stdout` 和 `stderr` 重定向到匿名管道,读取所有输出,并通过 `BeaconOutput` 发送回去。无磁盘写入,无可见控制台窗口。 ## 文件 | 文件 | 用途 | | ---------- | -------------------------------------------------- | | `shell.c` | BOF;编译为 `.o` 并加载到你的 agent 中 | | `beacon.h` | Cobalt Strike 兼容的 API,包含在每个 BOF 中 | | `loader.c` | 独立的测试加载器,无需 C2 即可运行 BOF | | `Makefile` | 同时构建 `shell.o` 和 `loader.exe` | ## 构建 需要 [mingw-w64](https://www.mingw-w64.org/)。 ``` make ``` 或者手动进行: ``` # BOF x86_64-w64-mingw32-gcc -o shell.o -c shell.c -masm=intel -O0 -mno-stack-arg-probe -fno-stack-check # Loader(用于本地测试) x86_64-w64-mingw32-gcc -o loader.exe loader.c -Wall -O2 -mconsole ``` ## 用法 ### 使用独立加载器 ``` loader.exe shell.o str:"whoami /all" loader.exe shell.o str:"net localgroup administrators" loader.exe shell.o str:"ipconfig /all" loader.exe shell.o str:"net user" loader.exe shell.o str:"tasklist /v" ``` ### 使用 C2 agent(兼容 Cobalt Strike) ``` beacon> inline-execute shell.o str:"whoami /all" ``` 通过 aggressor 脚本执行: ``` $bof = openf(script_resource("shell.o")); $data = readb($bof, -1); closef($bof); $args = bof_pack("z", "whoami /all"); beacon_inline_execute($bid, $data, "go", $args); ``` ## 工作原理 ``` go(args) └─ BeaconDataExtract() parse command from args blob └─ CreatePipe() create anonymous pipe (read/write ends) └─ CreateProcessA() spawn cmd.exe /c └─ hStdOutput = hWrite redirect stdout into pipe └─ hStdError = hWrite redirect stderr into pipe └─ CloseHandle(hWrite) close parent's write end └─ critical: without this, ReadFile blocks after child exits └─ ReadFile() loop read output and forward via BeaconOutput └─ WaitForSingleObject() wait for process to finish (10s timeout) └─ CloseHandle() cleanup ``` 在 `CreateProcess` 之后,父进程会立即关闭管道的写入端。如果它保持打开状态,即使子进程退出后,`ReadFile` 也会无限期阻塞,因为管道仍然有一个处于打开状态的写入者。 ## 注意事项 - **无 CRT** — BOF 不能链接标准库。字符串的构建是在没有 `snprintf` 的情况下手动完成的。 - **无 `printf`** — 输出通过 `BeaconOutput` 进行,从不使用 `printf`。 - **超时** — 挂起的命令会在 10 秒后通过 `WaitForSingleObject` 被终止。 - **退出代码** — 非零退出代码通过 `BeaconPrintf(CALLBACK_ERROR, ...)` 进行报告。 ## 系统要求 - Windows x64 - `mingw-w64` 交叉编译器 - 兼容 Cobalt Strike 的 C2 agent(或使用包含的 loader 进行测试) ## 许可证 MIT
标签:BOF, C2框架, cmd.exe, Cobalt Strike, CreateProcess, MinGW, QEMU, SCP, Shell执行, stdout重定向, Windows API, XML 请求, 内联执行, 匿名管道, 命令执行, 安全学习资源, 安全开发, 客户端加密, 攻击诱捕, 数据展示, 无文件落盘, 横向移动前置, 欺骗防御, 端点可见性, 管道重定向, 红队, 网络安全, 进程内执行, 隐私保护