The-Z-Labs/bof-launcher

GitHub: The-Z-Labs/bof-launcher

一个开源的跨平台 BOF 加载执行库,无需 Cobalt Strike 即可在 Windows 和 Linux(含 ARM 架构)上加载运行 Beacon Object Files。

Stars: 301 | Forks: 28

# Beacon Object File (BOF) 启动器

收录于: * [Cobalt Strike Community Kit](https://cobalt-strike.github.io/community_kit/) * [cli4bofs](https://github.com/The-Z-Labs/cli4bofs) * [z-beac0n - Open Adversary Simulation Toolkit](examples/implant/) * [Linux Attack, Detection and Live Forensics Course](https://edu.defensive-security.com/view/courses/linux-attack-live-forensics-at-scale/1427336-12-linux-rootkits-for-red-and-blue-teams/9443073-us-beacon-object-file-bof-stager) 博客文章: * [So you want to rapidly run a BOF? Let's look at this 'cli4bofs' thing then](https://blog.z-labs.eu/2025/06/04/all-about-cli4bofs-tool.html) * [Write, debug and execute BOFs with bof-launcher (part 2)](https://blog.z-labs.eu/2025/01/15/write-debug-and-execute-bofs-part2.html) * [Write, debug and execute BOFs with bof-launcher (part 1)](https://blog.z-labs.eu/2024/12/02/write-debug-and-execute-bofs-part1.html) * [Running BOFs with 'bof-launcher' library](https://blog.z-labs.eu/2024/02/08/bof-launcher.html) * [Executing Cobalt Strike's BOFs on ARM-based Linux devices](https://blog.z-labs.eu/2024/05/10/bofs-on-arm-based-devices.html) ## 简介 2020 年 6 月 25 日发布的 [Cobalt Strike 4.1](https://www.cobaltstrike.com/blog/cobalt-strike-4-1-the-mark-of-injection/) 引入了一项(在当时)名为 [Beacon Object Files](https://hstechdocs.helpsystems.com/manuals/cobaltstrike/current/userguide/content/topics/beacon-object-files_main.htm) 的新功能——*在 [Beacon](https://www.cobaltstrike.com/) 中执行的小型后渗透能力,解析参数,调用少量 Win32 API,报告输出并退出*。从那时起,BOF 变得非常流行,并且在 [Cobalt Strike's Beacon](https://www.cobaltstrike.com/) 之外的其他环境中启动/执行 BOF 的需求也随之出现。 ## 目的 我们 [Z-Labs](https://z-labs.eu) 看到了 BOF 的巨大潜力,决定进一步扩展其功能、通用性和实用性。这个项目就是这样诞生的。 [bof-launcher](bof-launcher/src/bof_launcher_api.h) 是一个从零开始编写的开源库,用于在 Windows 和 UNIX/Linux 系统上加载和启动 BOF。它是 Trustedsec 的 [COFFLoader](https://github.com/trustedsec/COFFLoader) 和 [ELFLoader](https://github.com/trustedsec/ELFLoader) 的替代方案,具有许多额外功能和简洁的 [C API](bof-launcher/src/bof_launcher_api.h): - 与 [Cobalt Strike's Beacon](https://www.cobaltstrike.com/) 完全兼容。可以编译并运行 [Cobalt Strike Community Kit](https://cobalt-strike.github.io/community_kit/) 中提供的每一个 BOF,以及所有遵循[通用 BOF 模板](https://github.com/Cobalt-Strike/bof_template)的其他开源 BOF。 - 作为完全独立的库分发,零依赖(甚至不使用 `libc`)。 - 可完全集成到用 C/C++ 和/或 [Zig](https://ziglang.org/) 编程语言编写的程序中。 - 增加了使用 [Zig 编程语言](https://ziglang.org/) 编写 BOF 的能力——这是一种低级语言,目标是成为“更好的 C”。该语言的所有特性和丰富的标准库都可以在 BOF 中使用(哈希表 map 和其他数据结构、跨平台 OS 层、http、网络、线程、加密等)。 - 能够在单独的线程中运行异步或长时间运行的 BOF。 - 提供在牺牲进程(sacrificial process)内部启动更高风险 BOF(即权限提升漏洞利用)的模式。 - 无缝支持 Windows COFF 和 UNIX/Linux ELF 格式。 - 支持 Linux 上的 ARM 和 AARCH64 架构。 - 用于我们的 [cli4bofs 工具](https://github.com/The-Z-Labs/cli4bofs),允许直接从文件系统运行 BOF 文件。 - 非常灵活高效的 [API](bof-launcher/src/bof_launcher_api.h),支持所谓的 BOF 链式调用(chaining)。 ## BOF launcher 库 我们提供开源、独立的库,可用于执行任何 BOF。在 Windows 上我们支持 x86 和 x86_64 架构,在 Linux 上我们支持 x86、x86_64、ARMv6+ 和 AArch64 架构。我们的库同时提供 [C API](bof-launcher/src/bof_launcher_api.h) 和 [Zig API](bof-launcher/src/bof_launcher_api.zig)。它解析 COFF/ELF 目标数据,进行重定位,加载所有需要的符号,并为您处理 BOF 输出。有关详细信息,请参阅 API 和测试。 基本 C API 用法: ``` // Load object file (COFF or ELF) and get a handle to it BofObjectHandle bof_handle; if (bofObjectInitFromMemory(obj_file_data, obj_file_data_size, &bof_handle) < 0) { // handle the error } // Execute BofContext* context = NULL; if (bofObjectRun(bof_handle, NULL, 0, &context) < 0) { // handle the error } // Get output const char* output = bofContextGetOutput(context, NULL); if (output) { // handle BOF output } bofContextRelease(context); ``` ## 使用 Zig 0.15.2 构建项目 [Zig](https://ziglang.org/) 作为一个支持开箱即用跨平台编译的零依赖、可替换的 C/C++ 编译器,可用于构建此项目。为此,需要下载 [Zig's tarball (0.15.2)](https://ziglang.org/download/#release-0.15.2) 并将其解压到所选目录中。将该目录添加到 `PATH` 环境变量后,构建整个项目只需运行: ``` zig build ``` 上述命令将为所有支持的平台构建所有包含的 BOF、示例程序和 bof-launcher 库。 要将 BOF 构建为可调试的独立可执行文件,请运行: ``` zig build -Doptimize=Debug ``` 构建产物将出现在 `zig-out/bin` 和 `zig-out/lib` 文件夹中。 要构建并运行测试 BOF,请执行: ``` zig build test ``` 要在非本地 CPU 架构上运行测试,可以使用与 Zig 良好集成的 [QEMU](https://www.qemu.org/): ``` zig build test -fqemu --glibc-runtimes /usr ``` ## Z-Labs BOFs 集合 除了 bof-launcher 库本身,我们还提供了[我们编写的 BOF 集合](bofs/)。我们计划逐步扩展此集合。我们专注于使用 Zig 语言开发 BOF,但用 C 实现并将其添加到集合中也完全可以。为此,只需将您的 BOF 放入 `bofs/src` 目录,并在 [bofs/build.zig](https://github.com/The-Z-Labs/bof-launcher/blob/main/bofs/build.zig) 文件中为其添加一个条目,如下所示: ``` .{ .name = "YOUR_BOF_NAME", .formats = &.{.elf, .coff}, .archs = &.{ .x64, .x86 } }, ``` 构建系统将识别文件扩展名,并使用适当的编译器(为所有指定的架构)构建它。通过这种方式,您也可以构建任何第三方 BOF。 下面您可以看到我们的一个 BOF 的两个版本:一个用 Zig 编写,另一个用 C 编写。编译后,Zig 版本大小为 502 字节,C 版本大小为 562 字节。 有关更大型的跨平台 BOF 示例,请参阅我们的 [UDP 端口扫描器](bofs/src/udpScanner.zig)或 [TCP 端口扫描器](bofs/src/tcpScanner.zig)。 ``` const w32 = @import("bof_api").win32; const beacon = @import("bof_api").beacon; pub export fn go(adata: ?[*]u8, alen: i32) callconv(.c) u8 { @import("bof_api").init(adata, alen, .{}); var version_info: w32.OSVERSIONINFOW = undefined; version_info.dwOSVersionInfoSize = @sizeOf(@TypeOf(version_info)); if (w32.RtlGetVersion(&version_info) != .SUCCESS) return 1; _ = beacon.printf( .output, "Windows version: %d.%d, OS build number: %d\n", version_info.dwMajorVersion, version_info.dwMinorVersion, version_info.dwBuildNumber, ); return 0; } ``` ``` #include #include "beacon.h" NTSYSAPI NTSTATUS NTAPI NTDLL$RtlGetVersion(OSVERSIONINFOW* lpVersionInformation); unsigned char go(unsigned char* arg_data, int arg_len) { OSVERSIONINFOW version_info; version_info.dwOSVersionInfoSize = sizeof(version_info); if (NTDLL$RtlGetVersion(&version_info) != 0) return 1; BeaconPrintf( CALLBACK_OUTPUT, "Windows version: %d.%d, OS build number: %d\n", version_info.dwMajorVersion, version_info.dwMinorVersion, version_info.dwBuildNumber ); return 0; } ``` ## 为您的 BOF 编写文档 我们还引入了一种通过 BOF 源代码文件中的简单 yaml 语法来编写 BOF 文档的方法。要为 BOF 编写文档,可以在 BOF 源代码文件顶部的每一行文档前加上 `///` 前缀。默认情况下,在使用 `zig build` 构建库时,每个 BOF 文件中的文档将被连接到一个公共文件 `BOF-collection.yaml` 中。此文件可直接用于我们的 [cli4bofs 工具](https://github.com/The-Z-Labs/cli4bofs)。用于 BOF 文档编写的 yaml 语法如下所示。带有完整文档的 BOF 示例可以在[这里](https://github.com/The-Z-Labs/bof-launcher/blob/main/bofs/src/udpScanner.zig)和[这里](https://github.com/The-Z-Labs/bof-launcher/blob/main/bofs/src/kmodLoader.zig)(针对导出 API 的 BOF 示例)看到。 ``` name: BOFname description: string:"short description of a BOF" author: BOFauthor tags: list of tags OS: string:linux|windows|cross sources: list of URLs entrypoint: optional:"go" api: optional:list of signatures of exported functions examples: string:"usage examples of a BOF" - arguments: - name: string:argName desc: string:"short description of an argument" type: string:"short|integer|string|stringW" required: bool api: optional:string:"api function name" - errors: - name: errorName code: int message: string:"short description of the error" ``` ## 示例使用场景 ### 使用 bof-launcher 的最小基于 Windows 的 C 应用程序 *bof-launcher 的“Hello world”程序* [bof-minimal_win_x64](https://github.com/The-Z-Labs/bof-minimal_win_x64) - 如果您是 `bof-launcher` 的新手,请务必查看此仓库并阅读这篇[博客文章](https://blog.z-labs.eu/2024/02/08/bof-launcher.html)。 ### z-beac0n - 开源对手模拟工具包 *实现自定义、跨平台植入物(implants)* [z-beac0n implant](examples/implant) - 用于构建自定义植入物的开放且灵活的架构解决方案,遵循:*(几乎)一切都作为 BOF 实现*的概念。 ### 从磁盘运行 BOF *快速启动、原型设计和测试 BOF* [cli4bofs](https://github.com/The-Z-Labs/cli4bofs) - 用于直接从文件系统运行 BOF 的独立命令行程序。在测试/验证第三方 BOF 或开发 BOF 期间也非常方便。不需要 [Cobalt Strike's Beacon](https://www.cobaltstrike.com/) 及其 aggressor 脚本即可运行。 ### 在其他编程语言中使用 bof-launcher 的示例 *将 bof-launcher 集成到用 C、Rust 和 Go 编写的程序中* [integration-with-c](examples/integration-with-c) - 简单示例,展示如何将 bof-launcher 集成到用 `C` 编写的应用程序中。 [integration-with-rust](examples/integration-with-rust) - 简单示例,展示如何将 bof-launcher 集成到用 `Rust` 编写的应用程序中。 [integration-with-go](examples/integration-with-go) - 简单示例,展示如何将 bof-launcher 集成到用 `Go` 编写的应用程序中。
标签:adversary simulation, ARM架构, BOF, C++, Cobalt Strike, DAST, DNS 反向解析, Go, Implant开发, Ruby工具, Rust, XML 请求, Zig, 内存加载, 内存掩码, 动态执行, 可视化界面, 安全开发, 恶意软件分析, 攻击模拟, 攻击诱捕, 数据擦除, 日志审计, 欺骗防御, 系统底层, 网络流量审计, 驱动签名利用