Skip to content

Files

Latest commit

 

History

History
116 lines (74 loc) · 2.89 KB

fuzz-testing.rst

File metadata and controls

116 lines (74 loc) · 2.89 KB

Fuzz Testing

Cargo Fuzz 是 LLVM libFuzzer 的包裝。

安裝:

$ cargo install cargo-fuzz

使用:

# 初始化會建立 fuzz/ 資料夾,
# 裡面是一個 Fuzz Testing 用的小專案,
# 其中的 fuzz_target! 會不斷地被呼叫,
# 裡面會傳入各種資料給 "data" 變數
$ cargo fuzz init

# 觀察產生出來的檔案
$ tree fuzz/
fuzz/
├── Cargo.toml
└── fuzzers
    └── fuzzer_script_1.rs

1 directory, 2 files

# 新增測試 Script
$ cargo fuzz add another_script

$ tree fuzz/
fuzz/
├── artifacts
│   └── another_script
├── Cargo.toml
├── corpus
│   └── another_script
└── fuzzers
    ├── another_script.rs
    └── fuzzer_script_1.rs

5 directories, 3 files

# 執行測試
$ cargo fuzz run fuzzer_script_1

# 重新執行某個 Input
$ cargo fuzz run fuzzer_script_1 fuzz/artifacts/fuzzer_script_1/<file mentioned in crash output>

AFL 是一套 coverage-guided fuzzing 的測試工具, Rust 這邊有一套包裝叫 afl.rsafl.rscargo-fuzz 相比之下環境建立麻煩許多。

從原始碼編譯:

$ git clone https://github.com/rust-fuzz/afl.rs
$ cd afl.rs/afl/
$ cargo build --release
$ cargo install --path .

# 嘗試呼叫
$ cargo afl-fuzz

使用:

  • aflafl-plugin 加入 Dependencies
  • lib.rs 內加入 #![feature(plugin)]#![plugin(afl_plugin)]
  • main.rs 內使用 afl 內的函式(例如 handle_stringhandle_bytes )並傳入要執行的 Lambda 函式