grisuno/blacksandbeacon
GitHub: grisuno/blacksandbeacon
一款面向 Linux 平台的轻量级内存驻留 C2 Beacon,为 LazyOwn 红队框架提供了原生 BOF 加载与无文件后渗透能力。
Stars: 12 | Forks: 4
# Black Sand Beacon
一个带有原生内存中 ELF BOF 加载器的 Linux C2 beacon,使用 C 语言编写,无 Go 或 Rust 运行时。专为接入 LazyOwn RedTeam 框架而设计,灵感来自 Cobalt Strike 的 Beacon Object Files。
此仓库包含:
* **Beacons** 位于 `beacons/v1/`、`beacons/v2/`、`beacons/v3/`
同一 HTTPS/CFB beacon 的三个变体。v1 是标准的拉取模式 beacon。v2 添加了 mesh/p2p 发现功能。v3 是一个实验性分支。
* **BOFs** 位于 `bof//bof.c`
beacon 可以获取并在内存中执行的位置无关 ELF 对象。包含的五个对象是:`whoami`、`is_sudo`、`cat`、`userenum`、`suid_enum`。
* **C2 server** 位于 `c2/server.py`
一个小型类似于 Gopher 的 Python 服务器,用于提供命令、接收结果并托管 BOFs。
* **共享库** 位于 `include/`
独立的 AES-256(`aes.c`/`aes.h`)、cJSON、运行时配置加载器(`config.c`/`config.h` 以及 Python 镜像 `config_py.py`),以及 beacon 和测试共享的 AES-256-CFB 包装器。
* **构建** 位于 `Makefile`,**CI** 位于 `.github/workflows/ci.yml`,**测试** 位于 `tests/`。
有关 5 分钟端到端运行,请参阅 [QUICKSTART.md](QUICKSTART.md)。
有关 BOF 编写指南,请参阅 [docs/BOF_AUTHORING.md](docs/BOF_AUTHORING.md)。
有关 C2 通信协议,请参阅 [docs/PROTOCOL.md](docs/PROTOCOL.md)。
## 环境要求
构建 (Debian/Ubuntu):
```
sudo apt-get install gcc libcurl4-openssl-dev libssl-dev make
```
C2 server 的运行时:
```
pip install -r requirements.txt
```
## 构建
```
make config # one-time: copies config.example.json -> config.json
make beacon # builds build/beacon (runnable, with build/config.json staged next to it)
make bofs # builds all BOFs into build/bof/*.x64.o
make all-beacons # builds v1, v2, v3
make test # runs the full test suite
make clean # removes build/
```
执行 `make beacon` 后,二进制文件即可运行:
```
./build/beacon
```
它会通过二进制相对搜索路径读取 `build/config.json`(位于其同级目录下),因此不需要设置 `BSB_CONFIG` 或执行 `cd config/`。只要旁边放着一个 `config.json`,复制到另一台主机的相同二进制文件也能以相同方式工作。
如果你只想重新构建一个 BOF:
```
make bof-whoami
make bof-suid_enum
```
## 部署到另一台主机(可选)
为了发送到目标机器,Makefile 提供了一个 `install` 目标,用于暂存一个独立的目录:
```
make install-all DESTDIR=/tmp/deploy
scp -r /tmp/deploy/* user@target:/opt/bsb/
ssh user@target /opt/bsb/beacon
```
这**不是**常规流程。仅在你需要将二进制文件 + 配置 + BOFs 打包到一个可以复制到其他地方的单个目录中时才使用它。
## 运行 C2
```
python3 c2/server.py
```
服务器默认绑定 `0.0.0.0:7070`,并从 `config/config.json` 读取其 AES 密钥和 C2 URI。使用 `BSB_CONFIG=/path/to/config.json` 覆盖配置路径。
一旦服务器启动,你就可以从 REPL 中注入命令:
```
Client ID: linux
Command: id
```
## 运行 beacon
```
./build/beacon
```
beacon 会读取 `config/config.json`(相对于二进制文件,通过二进制相对搜索路径找到),轮询 C2,并打印它发送和接收的内容。如果队列中没有命令,它只会在轮询之间休眠。
为了运行 BOF,C2 会返回一个带有 `bof` 字段的 JSON 对象,该字段指向 `/bof/.x64.o`。beacon 会获取该文件,并通过内存中 ELF 加载器(`beacons/v1/beacon.c` 中的 `RunELF`)将其加载到自己的进程中。
## 配置
所有操作参数都位于 `config/config.json` 中,并在启动时读取。没有任何内容被硬编码到二进制文件中。
| 部分 | 键 | 默认值 | 用途 |
|---|---|---|---|
| `c2` | `url` | `https://127.0.0.1:4444` | C2 基础 URL |
| `c2` | `uri` | `/api/poll/` | client id 之前的 URI 路径 |
| `c2` | `client_id` | `linux` | Beacon 标识符 |
| `crypto` | `aes_key_hex` | (占位符) | 64 个十六进制字符 = 32 字节 |
| `crypto` | `mode` | `cfb` | 加密模式 |
| `timing` | `sleep_seconds` | `6` | 基础轮询间隔 |
| `timing` | `jitter_percent` | `20` | 休眠的 +/- 百分比抖动 |
| `timing` | `curl_timeout_seconds` | `10` | libcurl 总超时时间 |
| `timing` | `curl_connect_timeout_seconds` | `5` | libcurl 连接超时时间 |
| `network` | `user_agents` | (4 个字符串) | 轮换列表 |
| `network` | `verify_tls` | `false` | C2 TLS 验证 |
| `bof` | `download_chunk_size` | `4096` | BOF 下载缓冲区 |
CI 会运行一项护栏检查,如果源码中出现 64 字符的十六进制密钥,则构建失败。真正的密钥应放在被 git 忽略的 `config/config.json` 中。
## 编写新的 BOF
BOF 只是一个导出 `void go(char *args, int alen)` 的 C 源文件。
包含 `bof/include/beacon_api.h` 以获取回调 API,包含 `bof/include/syscalls.h` 以获取直接系统调用助手。将其编译为位置无关且无 libc:
```
gcc -c -fPIC -nostdlib -m64 -O2 -I bof/include bof//bof.c -o build/bof/.x64.o
```
`make bof-` 目标封装了此操作。将生成的 `.x64.o` 放入 C2 主机的 `sessions/uploads/` 目录中,并通过从你的命令注入中返回类似 `bof:` 的命令来触发它。
有关完整契约,请参阅 [docs/BOF_AUTHORING.md](docs/BOF_AUTHORING.md)。
## 测试
```
make test
```
测试套件分为 `tests/` 中的几个文件:
| 文件 | 涵盖内容 |
|---|---|
| `test_config.py` | C JSON 配置加载器:默认值、覆盖、错误 |
| `test_crypto.py` | AES-256-CFB 往返测试及 Python/C 互操作性 |
| `test_bof_compile.py` | 每个 BOF 的编译、导出 `go`、无 libc 泄漏 |
| `test_beacon_build.py` | Beacon 二进制构建、暴露 BOF API + RunELF |
| `test_c2_server.py` | C2 调度器:GET/POST/BOF、路径遍历强化 |
| `test_c2_http_e2e.py` | 带有加密的真实 HTTP/1.1 socket 往返测试 |
| `test_install_deploy.py` | `make` 工作流生成可运行树,`make clean` 将其清除 |
## 仓库布局
```
beacons/
v1/beacon.c canonical beacon
v2/beacon.c mesh/p2p variant
v3/beacon.c experimental variant
bof/
include/ shared BOF headers (beacon_api.h, syscalls.h)
whoami/bof.c sample: print effective UID
is_sudo/bof.c sample: check sudo/wheel membership
cat/bof.c sample: read a file
userenum/bof.c sample: list /etc/passwd users and privileges
suid_enum/bof.c privesc recon: walk FS, list SUID/SGID binaries
c2/
server.py Gopher-style C2 server
config/
config.example.json template (copy to config.json)
include/
config.h, config.c runtime config loader
config_py.py Python mirror for the C2
aes.h, aes.c standalone AES (ECB/CTR/CBC)
aes_cfb.h, aes_cfb.c AES-256-CFB the beacon actually uses
cJSON.h, cJSON.c vendored JSON parser
beacon.h BOF API the beacon implements
tests/
test_*.py Python test suite
*_harness.c C test harnesses
docs/
PROTOCOL.md wire protocol
BOF_AUTHORING.md BOF contract and gotchas
.github/workflows/
ci.yml build + test + key-leak guard
Makefile
requirements.txt C2 server runtime deps
```
## 许可证
GPL v3。请参阅 `LICENSE`。
标签:BOF, C2框架, IP 地址批量处理, Raspberry Pi, 内存执行, 安全学习资源, 客户端加密, 欺骗防御, 逆向工具