关于 binary-hardening
面向配置和检查已编译二进制文件安全缓解措施的 Agent 技能,涵盖 checksec、编译与链接标志、控制流保护和 seccomp-bpf 过滤。
主要功能
- 01用 checksec 分析缓解措施
- 02配置编译器和链接器加固标志
- 03应用 Clang CFI 检查
- 04检查栈保护和 FORTIFY_SOURCE
- 05使用 seccomp-bpf 系统调用过滤
- 06检查硬件控制流保护
适用场景
- 01审计二进制文件启用的保护
- 02为构建添加加固标志
- 03限制程序可用的系统调用
- 04验证 CET 或 ARM 保护
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill binary-hardening安装后在 Claude Code 中输入/binary-hardening确认已加载。
附带文件references/hardening-flags.md
详细介绍
二进制文件加固
目的
指导 Agent 启用并验证二进制安全缓解措施:checksec 分析、编译器和链接器加固标志(RELRO、PIE、栈保护金丝雀、FORTIFY_SOURCE、CFI)、硬件影子栈,以及用于深度防御的 seccomp-bpf 系统调用过滤。
触发条件
- “如何加固二进制文件以防范漏洞利用?”
- “如何检查二进制文件启用了哪些安全缓解措施?”
- “checksec 的输出是什么意思?”
- “如何启用 RELRO、PIE 和栈保护金丝雀?”
- “如何使用 seccomp 限制系统调用?”
- “如何启用 CFI(控制流完整性)?”
工作流
1. 使用 checksec 分析现有二进制文件
# Install checksec
pip install checksec.py # or: apt install checksec
# Check a binary
checksec --file=./mybinary
checksec --file=/usr/bin/ssh
# Output example
# RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
# Full RELRO Canary found NX PIE No RPATH No RUNPATH No Symbols Yes 6 10 ./mybinary
# Check all binaries in a directory
checksec --dir=/usr/bin
| 保护措施 | 良好值 | 风险 |
|---|---|---|
| RELRO | Full RELRO | Partial / No RELRO |
| 栈保护金丝雀 | 检测到金丝雀 | 无金丝雀 |
| NX | 已启用 NX | 已禁用 NX |
| PIE | 已启用 PIE | 无 PIE |
| FORTIFY | 是 | 否 |
2. 编译器和链接器加固标志
# Full hardened build (GCC or Clang)
CFLAGS="-O2 -pipe \
-fstack-protector-strong \
-fstack-clash-protection \
-fcf-protection \
-D_FORTIFY_SOURCE=3 \
-D_GLIBCXX_ASSERTIONS \
-fPIE \
-Wformat -Wformat-security -Werror=format-security"
LDFLAGS="-pie \
-Wl,-z,relro \
-Wl,-z,now \
-Wl,-z,noexecstack \
-Wl,-z,separate-code"
gcc ${CFLAGS} -o prog main.c ${LDFLAGS}
标志参考:
| 标志 | 保护措施 | 说明 |
|---|---|---|
-fstack-protector-strong | 栈保护金丝雀 | 比 -fstack-protector 更强 |
-fstack-clash-protection | 栈冲突保护 | 防止分配巨大的栈空间 |
-fcf-protection | Intel CET(IBT+SHSTK) | x86 硬件 CFI(需要内核和 CPU 支持) |
-D_FORTIFY_SOURCE=2 | 缓冲区溢出检查 | 为字符串和内存函数添加边界检查 |
-D_FORTIFY_SOURCE=3 | 增强的 FORTIFY | GCC ≥12、Clang ≥12 |
-fPIE + -pie | PIE/ASLR | 位置无关可执行文件 |
-Wl,-z,relro | Partial RELRO | 在 main 之前将 GOT 设为只读 |
-Wl,-z,now | Full RELRO | 启动时解析所有 PLT → GOT 完全只读 |
-Wl,-z,noexecstack | NX 栈 | 将栈标记为不可执行 |
3. 控制流完整性(CFI)
Clang 的 CFI 可防止通过错误类型调用虚函数(vtable CFI),并防止对类型不匹配的函数进行间接调用:
# Clang CFI — requires LTO and visibility
clang -fsanitize=cfi -fvisibility=hidden -flto \
-O2 -fPIE -pie main.cpp -o prog
# Specific CFI checks
clang -fsanitize=cfi-vcall # virtual call type check
clang -fsanitize=cfi-icall # indirect call type check
clang -fsanitize=cfi-derived-cast # derived-to-base cast
clang -fsanitize=cfi-unrelated-cast # unrelated type cast
# Cross-DSO CFI (across shared libraries — more complex)
clang -fsanitize=cfi -fsanitize-cfi-cross-dso -flto -fPIC -shared
# Microsoft CFG (Windows equivalent)
cl /guard:cf prog.c
link /guard:cf prog.obj
4. 深入了解栈保护金丝雀
# GCC canary options
-fno-stack-protector # disabled
-fstack-protector # protect functions with alloca or buffers > 8 bytes
-fstack-protector-strong # protect functions with local arrays/addresses taken
-fstack-protector-all # protect all functions (slowest, most complete)
# Verify canary presence
objdump -d prog | grep -A5 "__stack_chk"
readelf -s prog | grep "stack_chk"
5. FORTIFY_SOURCE
当缓冲区大小可在编译时确定时,FORTIFY_SOURCE 会用经过边界检查的版本包装不安全的 libc 函数(memcpy、strcpy、sprintf):
# Level 2 (GCC/Clang default for hardened builds)
-D_FORTIFY_SOURCE=2
# Runtime check: abort() on overflow
# Level 3 (GCC ≥12, catches more cases)
-D_FORTIFY_SOURCE=3
# Adds dynamic buffer size tracking for more coverage
# Check FORTIFY coverage
objdump -d prog | grep "__.*_chk" # fortified variants
checksec --file=prog | grep FORTIFY
6. seccomp-bpf 系统调用过滤
#include <seccomp.h>
void apply_seccomp_filter(void) {
scmp_filter_ctx ctx;
// Default: kill process on any non-allowlisted syscall
ctx = seccomp_init(SCMP_ACT_KILL_PROCESS);
// Allowlist needed syscalls
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0);
// Apply filter (irreversible after this point)
seccomp_load(ctx);
seccomp_release(ctx);
}
// Call early in main(), after all setup
int main(void) {
// ... initialization ...
apply_seccomp_filter();
// ... restricted operation ...
}
# Test seccomp filter with strace
strace -e trace=all ./prog 2>&1 | grep "killed by SIGSYS"
# Profile syscalls to build allowlist
strace -c ./prog # count all syscalls used
7. Intel CET(影子栈 + IBT)
# Full CET: SHSTK (shadow stack) + IBT (indirect branch tracking)
gcc -fcf-protection=full -O2 -o prog main.c
# Verify in binary
readelf -n prog | grep -E 'SHSTK|IBT'
readelf --notes prog | grep GNU_PROPERTY
# IBT landing pads in disassembly
objdump -d prog | grep endbr64
# CPU and kernel support
grep -m1 shstk /proc/cpuinfo
需要硬件支持 CET(Intel Tiger Lake 及更新型号支持 SHSTK/IBT;AMD Zen 3 及更新型号的部分 SKU 支持影子栈),且内核需在构建时启用 CET。
8. ARM BTI 和 PAC(AArch64)
# Branch Target Identification + Pointer Authentication
gcc -mbranch-protection=standard -O2 -o prog main.c
# Or: -mbranch-protection=bti+pauth
readelf -n prog | grep -E 'BTI|PAC'
llvm-objdump -d prog | grep bti
| 功能 | 保护对象 |
|---|---|
| BTI | 跳转到未标记目标的间接分支 |
| PAC | 已签名的返回地址和指针(ARMv8.3+) |
9. ARM 内存标记(MTE)
# Userspace MTE tagging (experimental, arm64 hardware)
clang -fsanitize=memtag -g -o prog main.c
# Check MTE CPU support
grep -m1 mte /proc/cpuinfo
MTE 会为每个 16 字节粒度区域分配 4 位标记;硬件会在访问时检测标记是否不匹配。
10. glibc 影子栈(2.39+)
# Recent glibc may enable shadow stack for CET when hardware supports SHSTK
ldd --version # feature availability varies by distro glibc build
# Explicit link with shadow stack support (toolchain dependent)
gcc -fcf-protection=full -Wl,-z,shstk -o prog main.c
# Verify GNU_PROPERTY_SHSTK in output
readelf -n prog | grep SHSTK
影子栈会维护一份由硬件保护的返回地址副本,并使其与数据栈分离。
完整的加固标志参考请参阅 references/hardening-flags.md。
相关技能
- 在开发期间使用
skills/runtimes/sanitizers进行 ASan/UBSan 检查 - 使用
skills/observability/ebpf通过 libbpf 编写 seccomp-bpf 程序 - 使用
skills/rust/rust-security了解 Rust 的内存安全加固方法 - 使用
skills/binaries/elf-inspection验证 ELF 二进制文件中的缓解措施
评论
支持 Markdown