1 个不稳定发布
0.5.55 | 2023年9月28日 |
---|
#49 在 #fuzzer
45KB
556 代码行
honggfuzz-rs
![文档](https://img.gs/czjpqfbdkz/full/https://docs.rs/honggfuzz/badge.svg)
使用Google开发的Honggfuzz对Rust代码进行模糊测试!
文档
关于Honggfuzz
Honggfuzz是一个以安全为导向的模糊测试工具,具有强大的分析选项。支持基于代码覆盖率的进化式、反馈式模糊测试(软件和硬件基础)。
- 项目主页 honggfuzz.com
- 项目仓库 github.com/google/honggfuzz
- 这个上游项目由Google维护,但...
- 这不是一个官方的Google产品
兼容性
- Rust:稳定版、beta版、nightly版
- 操作系统:GNU/Linux、macOS、FreeBSD、NetBSD、Android、WSL(Windows Subsystem for Linux)
- 架构:x86_64、x86、arm64-v8a、armeabi-v7a、armeabi
- Sanitizer:无、地址、线程、泄露
依赖项
Linux
- C编译器:
cc
- GNU Make:
make
- GNU Binutils开发文件,用于BFD库:
libbfd.h
- libunwind开发文件:
libunwind.h
- Blocks运行时库(当使用clang编译时)
- liblzma开发文件
例如在Debian及其衍生版本上
sudo apt install build-essential binutils-dev libunwind-dev libblocksruntime-dev liblzma-dev
如何使用此crate
安装Honggfuzz命令以使用仪器和模糊测试进行构建
# installs hfuzz and honggfuzz subcommands in cargo
cargo install honggfuzz
将其添加到依赖项中
[dependencies]
honggfuzz = "0.5"
创建一个用于模糊测试的目标
use honggfuzz::fuzz;
fn main() {
// Here you can parse `std::env::args and
// setup / initialize your project
// You have full control over the loop but
// you're supposed to call `fuzz` ad vitam aeternam
loop {
// The fuzz macro gives an arbitrary object (see `arbitrary crate`)
// to a closure-like block of code.
// For performance reasons, it is recommended that you use the native type
// `&[u8]` when possible.
// Here, this slice will contain a "random" quantity of "random" data.
fuzz!(|data: &[u8]| {
if data.len() != 3 {return}
if data[0] != b'h' {return}
if data[1] != b'e' {return}
if data[2] != b'y' {return}
panic!("BOOM")
});
}
}
为了乐趣和利益而模糊测试!
# builds with fuzzing instrumentation and then fuzz the "example" target
cargo hfuzz run example
一旦发生崩溃,就可以轻松地在调试环境中重放它
# builds the target in debug mode and replays automatically the crash in rust-lldb
cargo hfuzz run-debug example hfuzz_workspace/*/*.fuzz
您还可以在不使用编译时软件检测(LLVM的SanCov passes)的情况下构建和运行您的项目。
这使您能够尝试仅使用硬件反馈驱动的模糊测试。
# builds without fuzzing instrumentation and then fuzz the "example" target using hardware-based feedback
HFUZZ_RUN_ARGS="--linux_perf_ipt_block --linux_perf_instr --linux_perf_branch" cargo hfuzz run-no-instr example
清洁
# a wrapper on "cargo clean" which cleans the fuzzing_target directory
cargo hfuzz clean
版本
cargo hfuzz version
环境变量
RUSTFLAGS
您可以使用 RUSTFLAGS
将额外的参数发送到 rustc
。
例如,您可以启用LLVM的sanitizers。如果您想测试您的 unsafe
rust代码,这是一个推荐选项,但它会影响性能。
RUSTFLAGS="-Z sanitizer=address" cargo hfuzz run example
HFUZZ_BUILD_ARGS
您可以使用 HFUZZ_BUILD_ARGS
将额外的参数发送到 cargo build
。
HFUZZ_RUN_ARGS
您可以使用 HFUZZ_RUN_ARGS
将额外的参数发送到 honggfuzz
。有关这些参数的列表,请参阅USAGE。
例如
# 1 second of timeout
# use 12 fuzzing thread
# be verbose
# stop after 1000000 fuzzing iteration
# exit upon crash
HFUZZ_RUN_ARGS="-t 1 -n 12 -v -N 1000000 --exit_upon_crash" cargo hfuzz run example
HFUZZ_DEBUGGER
默认情况下,我们使用 rust-lldb
,但您可以将它更改为 rust-gdb
、gdb
、/usr/bin/lldb-7
等。
CARGO_TARGET_DIR
目标编译目录,默认为 hfuzz_target
,以避免与 cargo build
的默认 target
目录冲突。
HFUZZ_WORKSPACE
Honggfuzz工作目录,默认为 hfuzz_workspace
。
HFUZZ_INPUT
Honggfuzz输入文件(也称为“语料库”),默认为 $HFUZZ_WORKSPACE/{TARGET}/input
。
条件编译
有时,您可能需要对您的代码进行一些特定的适配,以提高模糊测试效率。
例如
- 尽量使软件在模糊测试输入上的行为尽可能确定。
- PRNG必须使用常量或模糊测试输入进行初始化。
- 行为不应基于计算机的时钟而改变。
- 避免来自竞态线程的潜在不确定性行为。
- ...
- 永远不要在运行时调用
std::process::exit()
。 - 禁用日志和其他不必要的功能。
- 尽可能避免在可能的情况下修改全局状态。
- 不要在运行
cfg(fuzzing)
时设置自己的panic hook。
当使用 cargo hfuzz
进行构建时,将 --cfg fuzzing
参数传递给 rustc
,以便您可以通过 cfg
宏进行条件编译,如下所示:
#[cfg(fuzzing)]
let mut rng = rand_chacha::ChaCha8Rng::from_seed(&[0]);
#[cfg(not(fuzzing))]
let mut rng = rand::thread_rng();
此外,在调试模式下构建时,除了 fuzzing
外,还会添加 fuzzing_debug
参数。
有关条件编译的更多信息,请参阅参考。
有关honggfuzz的相关文档
关于Rust模糊测试
在github.com/rust-fuzz上还有其他项目提供Rust模糊测试支持。
您将发现对 AFL 和 LLVM 的 LibFuzzer 的支持,还有一个 trophy case ;-)。
这个crate受到了那些项目的启发!
依赖项
约220KB