Communitygithub.com

AFL++ 模糊測試工具組

AFL++ 是 AFL 的分支,提供更佳的模糊測試效能與進階功能。適用於 C/C++ 專案的多核心模糊測試。

AFL++ 模糊測試工具組 是什麼?

AFL++ 是經典 AFL 模糊測試器的進化版,專為現代 C/C++ 專案的大型模糊測試任務設計。它的核心優勢在於穩健的多核心支援,能讓 fuzzing 效能隨實體核心數線性擴展,並支援非對稱式 fuzzing(例如一個核心執行 AddressSanitizer,其餘不用)。此技能詳細說明如何透過 Docker(支援 Apple Silicon)或 Ubuntu/Debian 套件安裝 AFL++,並提供完整的實作指南,包括撰寫 LLVMFuzzerTestOneInput 風格的 harness、選擇最佳編譯模式(LTO、LLVM 或 GCC plugin)、管理語料庫(corpus)、以及使用 CMPLOG/RedQueen 路徑約束求解機制。對於需要自動化持續整合流程或進行深度二進位安全研究的開發者與 AI 代理,此技能涵蓋了從單一執行序測試到大規模多核心 fuzzing campaign 的完整生命週期。它也整合了 AddressSanitizer 與 UndefinedBehaviorSanitizer,協助找出記憶體損壞與未定義行為等難以捕捉的錯誤。整體而言,AFL++ 是在生產環境程式碼中尋找安全性弱點與穩定性問題的必備工具。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/SamuelChien/mega-skills-collection/tree/HEAD/aflpp

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

AFL++

AFL++ 是原始 AFL 模糊測試器的分支,在維持穩定性的同時,提供更佳的模糊測試效能與更多進階功能。與 libFuzzer 相比,其主要優勢在於能穩定支援多核心的模糊測試活動,非常適合大規模的模糊測試任務。

使用時機

模糊測試器最適合複雜度
AFL++多核心模糊測試、多樣化突變、成熟專案中
libFuzzer快速設定、單執行緒、簡單的 harness低
LibAFL自訂模糊測試器、研究、進階用例高

在以下情況選擇 AFL++:

  • 你需要多核心模糊測試以最大化吞吐量
  • 你的專案可以使用 Clang 或 GCC 編譯
  • 你想要多樣化的突變策略和成熟的工具
  • libFuzzer 已達到瓶頸,你需要更多覆蓋率
  • 你正在對受益於並行執行的生產級程式碼庫進行模糊測試

快速開始

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    // 使用模糊測試提供的資料呼叫你的程式碼
    check_buf((char*)data, size);
    return 0;
}

編譯並執行:

# 先設定 AFL++ 包裝腳本(請見安裝章節)
./afl++ docker afl-clang-fast++ -DNO_MAIN=1 -O2 -fsanitize=fuzzer harness.cc main.cc -o fuzz
mkdir seeds && echo "aaaa" > seeds/minimal_seed
./afl++ docker afl-fuzz -i seeds -o out -- ./fuzz

安裝

AFL++ 有許多依賴項,包括 LLVM、Python 和 Rust。我們建議使用最新的 Debian 或 Ubuntu 發行版來進行 AFL++ 的模糊測試。

方法使用時機支援的編譯器
Ubuntu/Debian 套件庫最新的 Ubuntu,僅基本功能Ubuntu 23.10:Clang 14 與 GCC 13Debian 12:Clang 14 與 GCC 12
Docker(來自 Docker Hub)特定 AFL++ 版本,Apple Silicon 支援截至 4.35c:Clang 19 與 GCC 11
Docker(從原始碼建置)測試未發佈的功能,套用修補程式可在 Dockerfile 中設定
從原始碼建置避免使用 Docker,需要特定修補程式可透過 LLVM_CONFIG 環境變數調整

Ubuntu/Debian

在安裝 afl++ 之前,請先使用 apt-cache show afl++ 檢查套件的 clang 版本依賴,並安裝對應的 lld 版本(例如 lld-17)。

apt install afl++ lld-17

Docker(來自 Docker Hub)

docker pull aflplusplus/aflplusplus:stable

Docker(從原始碼建置)

git clone --depth 1 --branch stable https://github.com/AFLplusplus/AFLplusplus
cd AFLplusplus
docker build -t aflplusplus .

從原始碼建置

請參閱 Dockerfile 以了解 Ubuntu 版本需求和依賴項。設定 LLVM_CONFIG 來指定 Clang 版本(例如 llvm-config-18)。

包裝腳本設定

建立一個包裝腳本,以便在主機或 Docker 上執行 AFL++:

cat <<'EOF' > ./afl++
#!/bin/sh
AFL_VERSION="${AFL_VERSION:-"stable"}"
case "$1" in
   host)
        shift
        bash -c "$*"
        ;;
    docker)
        shift
        /usr/bin/env docker run -ti \
            --privileged \
            -v ./:/src \
            --rm \
            --name afl_fuzzing \
            "aflplusplus/aflplusplus:$AFL_VERSION" \
            bash -c "cd /src && bash -c \"$*\""
        ;;
    *)
        echo "Usage: $0 {host|docker}"
        exit 1
        ;;
esac
EOF
chmod +x ./afl++

安全警告: afl-system-config 和 afl-persistent-config 腳本需要 root 權限,並會停用作業系統的安全功能。請勿在生產系統或開發環境中進行模糊測試。請使用專用虛擬機。

系統設定

每次重新啟動後執行,可獲得最多 15% 的每秒執行次數提升:

./afl++ <host/docker> afl-system-config

為了獲得最大效能,請停用核心安全緩解措施(需要 grub 引導程式,Docker 不支援):

./afl++ host afl-persistent-config
update-grub
reboot
./afl++ <host/docker> afl-system-config

使用 cat /proc/cmdline 驗證 - 輸出應包含 mitigations=off。

撰寫 Harness

Harness 結構

AFL++ 支援 libFuzzer 風格的 harness:

#include <stdint.h>
#include <stddef.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    // 1. 如有需要,驗證輸入大小
    if (size < MIN_SIZE || size > MAX_SIZE) return 0;

    // 2. 使用模糊測試資料呼叫目標函式
    target_function(data, size);

    // 3. 回傳 0(非零值保留給未來使用)
    return 0;
}

Harness 規則

應該做不應該做
在每次執行之間重置全域狀態依賴前次執行的狀態
優雅地處理邊界情況在無效輸入時退出
保持 harness 的確定性使用隨機數產生器
釋放已分配的記憶體造成記憶體洩漏
驗證輸入大小處理無界輸入

另請參閱: 有關詳細的 harness 撰寫技巧、處理複雜輸入的樣板,以及進階策略,請參閱 fuzz-harness-writing 技術技能。

編譯

AFL++ 提供多種編譯模式,各有不同的取捨。

編譯模式決策樹

選擇你的編譯模式:

  • LTO 模式 (afl-clang-lto):最佳效能和插樁。請先嘗試此模式。
  • LLVM 模式 (afl-clang-fast):如果 LTO 編譯失敗,則回退到此模式。
  • GCC 插件 (afl-gcc-fast):用於需要 GCC 的專案。

基本編譯(LLVM 模式)

./afl++ <host/docker> afl-clang-fast++ -DNO_MAIN=1 -O2 -fsanitize=fuzzer harness.cc main.cc -o fuzz

GCC 編譯

./afl++ <host/docker> afl-g++-fast -DNO_MAIN=1 -O2 -fsanitize=fuzzer harness.cc main.cc -o fuzz

重要: GCC 版本必須與編譯 AFL++ GCC 插件時使用的版本相符。

搭配 Sanitizer

./afl++ <host/docker> AFL_USE_ASAN=1 afl-clang-fast++ -DNO_MAIN=1 -O2 -fsanitize=fuzzer harness.cc main.cc -o fuzz

另請參閱: 有關詳細的 sanitizer 設定、常見問題和進階旗標,請參閱 address-sanitizer 和 undefined-behavior-sanitizer 技術技能。

建置旗標

請注意,-g 並非必要,AFL++ 編譯器預設會加入它。

旗標用途
-DNO_MAIN=1使用 libFuzzer harness 時跳過 main 函式
-O2生產最佳化等級(建議用於模糊測試)
-fsanitize=fuzzer啟用 libFuzzer 相容模式,並在連結可執行檔時加入模糊測試執行時期
-fsanitize=fuzzer-no-link進行插樁但不連結模糊測試執行時期(用於靜態函式庫和目標檔)

語料庫管理

建立初始語料庫

AFL++ 需要至少一個非空的種子檔案:

mkdir seeds
echo "aaaa" > seeds/minimal_seed

對於真實專案,請收集代表性的輸入:

  • 下載你要模糊測試的格式的範例檔案
  • 從專案的測試套件中擷取測試案例
  • 為你的檔案格式使用最小的有效輸入

語料庫最小化

在戰役之後,最小化語料庫以僅保留唯一的覆蓋率:

./afl++ <host/docker> afl-cmin -i out/default/queue -o minimized_corpus -- ./fuzz

另請參閱: 有關語料庫建立策略、字典和種子選擇,請參閱 fuzzing-corpus 技術技能。

執行戰役

基本執行

./afl++ <host/docker> afl-fuzz -i seeds -o out -- ./fuzz

設定環境變數

./afl++ <host/docker> AFL_FAST_CAL=1 afl-fuzz -i seeds -o out -- ./fuzz

解讀輸出

AFL++ 的介面會顯示即時的模糊測試統計資料:

輸出含義
execs/sec執行速度 - 越高越好
cycles done完成的佇列遍歷次數
corpus count佇列中獨特的測試案例數量
saved crashes找到的獨特崩潰數量
stability穩定邊緣的百分比(應接近 100%)

輸出目錄結構

out/default/
├── cmdline          # SUT 是如何被呼叫的?
├── crashes/         # 導致 SUT 崩潰的輸入
│   └── id:000000,sig:06,src:000002,time:286,execs:13105,op:havoc,rep:4
├── hangs/           # 導致 SUT 掛起的輸入
├── queue/           # 重現最終模糊測試器狀態的測試案例
│   ├── id:000000,time:0,execs:0,orig:minimal_seed
│   └── id:000001,src:000000,time:0,execs:8,op:havoc,rep:6,+cov
├── fuzzer_stats     # 戰役統計資料
└── plot_data        # 用於繪圖的資料

分析結果

檢視即時戰役統計資料:

./afl++ <host/docker> afl-whatsup out

建立覆蓋率圖表:

apt install gnuplot
./afl++ <host/docker> afl-plot out/default out_graph/

重新執行測試案例

./afl++ <host/docker> ./fuzz out/default/crashes/<test_case>

模糊測試器選項

選項用途
-G 4000最大測試輸入長度(預設:1048576 位元組)
-t 1000每個測試案例的超時時間(毫秒)(預設:1000ms)
-m 1000記憶體限制(MB)(預設:0 = 無限制)
-x ./dict.dict使用字典檔案來引導突變

多核心模糊測試

AFL++ 擅長多核心模糊測試,具有兩大優勢:

  1. 更高的每秒執行次數(與實體核心數成線性比例)
  2. 非對稱模糊測試(例如,一個 ASan 任務,其餘不帶 sanitizer)

啟動戰役

啟動主要模糊測試器(背景執行):

./afl++ <host/docker> afl-fuzz -M primary -i seeds -o state -- ./fuzz 1>primary.log 2>primary.error &

啟動次要模糊測試器(數量可與核心數相同):

./afl++ <host/docker> afl-fuzz -S secondary01 -i seeds -o state -- ./fuzz 1>secondary01.log 2>secondary01.error &
./afl++ <host/docker> afl-fuzz -S secondary02 -i seeds -o state -- ./fuzz 1>secondary02.log 2>secondary02.error &

監控多核心戰役

列出所有正在執行的任務:

jobs

檢視即時統計資料(每秒更新):

./afl++ <host/docker> watch -n1 --color afl-whatsup state/

停止所有模糊測試器

kill $(jobs -p)

覆蓋率分析

AFL++ 透過邊緣插樁自動追蹤覆蓋率。覆蓋率資訊儲存在 fuzzer_stats 和 plot_data 中。

測量覆蓋率

使用 afl-plot 來視覺化隨時間變化的覆蓋率:

./afl++ <host/docker> afl-plot out/default out_graph/

提升覆蓋率

  • 對特定格式的模糊測試使用字典
  • 執行更長時間的戰役(cycles_wo_finds 表示已達高原期)
  • 透過多核心模糊測試嘗試不同的突變策略
  • 分析覆蓋率缺口並加入有針對性的種子輸入

另請參閱: 有關詳細的覆蓋率分析技術、識別覆蓋率缺口,以及系統性地提升覆蓋率,請參閱 coverage-analysis 技術技能。

CMPLOG

CMPLOG/RedQueen 是任何模糊測試器中最優秀的路徑約束求解機制。 要啟用它,需要對模糊測試目標進行插樁。 在建置模糊測試目標之前,設定環境變數:

./afl++ <host/docker> AFL_LLVM_CMPLOG=1 make

編譯和連結 harness 不需要特殊操作。

若要執行一個使用 CMPLOG 插樁的模糊測試目標的模糊測試器實例,請在命令列參數中加入 -c0:

./afl++ <host/docker> afl-fuzz -c0 -S cmplog -i seeds -o state -- ./fuzz 1>secondary02.log 2>secondary02.error &

Sanitizer 整合

Sanitizer 對於尋找不會立即導致崩潰的記憶體損壞錯誤至關重要。

AddressSanitizer (ASan)

./afl++ <host/docker> AFL_USE_ASAN=1 afl-clang-fast++ -DNO_MAIN=1 -O2 -fsanitize=fuzzer harness.cc main.cc -o fuzz

注意: 由於 ASan 會保留 20TB 的虛擬記憶體,因此不支援記憶體限制(-m)。

UndefinedBehaviorSanitizer (UBSan)

./afl++ <host/docker> AFL_USE_UBSAN=1 afl-clang-fast++ -DNO_MAIN=1 -O2 -fsanitize=fuzzer,undefined harness.cc main.cc -o fuzz

常見的 Sanitizer 問題

問題解決方案
ASan 拖慢模糊測試在多核心設定中僅使用 1 個 ASan 任務
堆疊耗盡使用 ASAN_OPTIONS=stack_size=... 增加堆疊
GCC 版本不符確保系統 GCC 與 AFL++ 插件的版本相符

另請參閱: 有關全面的 sanitizer 設定和故障排除,請參閱 address-sanitizer 技術技能。

進階用法

提示與技巧

提示為何有幫助
盡可能使用 LLVMFuzzerTestOneInput harness如果模糊測試戰役的穩定性至少達到 85%,則這是最有效率的模糊測試風格。如果沒有,請嘗試標準輸入或檔案輸入模糊測試
使用字典幫助模糊測試器發現特定格式的關鍵字和 magic bytes
設定合理的超時時間防止因系統負載而導致誤報
限制輸入大小較大的輸入不一定能探索更多空間
監控穩定性低穩定性表示非確定性行為

標準輸入模糊測試

AFL++ 可以對從 stdin 讀取資料的程式進行模糊測試,而無需 libFuzzer harness:

./afl++ <host/docker> afl-clang-fast++ -O2 main_stdin.c -o fuzz_stdin
./afl++ <host/docker> afl-fuzz -i seeds -o out -- ./fuzz_stdin

這比持久模式慢,但不需要任何 harness 程式碼。

檔案輸入模糊測試

對於讀取檔案的程式,請使用 @@ 佔位符:

./afl++ <host/docker> afl-clang-fast++ -O2 main_file.c -o fuzz_file
./afl++ <host/docker> afl-fuzz -i seeds -o out -- ./fuzz_file @@

為了獲得更好的效能,請使用 fmemopen 從記憶體建立檔案描述符。

引數模糊測試

使用 argv-fuzz-inl.h 對命令列引數進行模糊測試:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __AFL_COMPILER
#include "argv-fuzz-inl.h"
#endif

void check_buf(char *buf, size_t buf_len) {
    if(buf_len > 0 && buf[0] == 'a') {
        if(buf_len > 1 && buf[1] == 'b') {
            if(buf_len > 2 && buf[2] == 'c') {
                abort();
            }
        }
    }
}

int main(int argc, char *argv[]) {
    #ifdef __AFL_COMPILER
    AFL_INIT_ARGV();
    #endif
    check_buf(argv[0], strlen(argv[0]));
    return 0;
}

編譯:

./afl++ <host/docker> afl-clang-fast++ -O2 fuzz_argv.c -o fuzz_argv

執行:

./afl++ <host/docker> afl-fuzz -i seeds -o out -- ./fuzz_argv

另請參閱: 有關詳細的引數模糊測試技術和 AFL_INIT_ARGV 巨集的進階用法,請參閱 argv-fuzzing 技術技能。

故障排除

問題解決方案
"Hmm, your system is configured to send core dump notifications..."執行 echo core >/proc/sys/kernel/core_pattern
"Fatal error: ... afl-as ..."重新安裝 AFL++
低穩定性(< 90%)確保程式碼是確定性的;檢查執行緒、全域變數和未初始化的記憶體。即使使用 -fsanitize=fuzzer 模式,低穩定性也很常見。
LLVM 版本不匹配確保 LLVM 工具鏈版本一致
使用 Docker 時權限被拒驗證 Docker 容器的 --privileged 旗標

另請參閱: 如需更多故障排除提示和深入的效能最佳化建議,請參閱 afl++-performance 技術技能。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

SamuelChien/mega-skills-collection

Security audit, hardening, threat modeling (STRIDE/PASTA), Red/Blue Team, OWASP checks, code review, incident response, and infrastructure security for any project.

SamuelChien/mega-skills-collection

Arquitecto de Soluciones Principal y Consultor Tecnológico de Andru.ia. Diagnostica y traza la hoja de ruta óptima para proyectos de IA en español.

SamuelChien/mega-skills-collection

Ingeniero de Sistemas de Andru.ia. Diseña, redacta y despliega nuevas habilidades (skills) dentro del repositorio siguiendo el Estándar de Diamante.

SamuelChien/mega-skills-collection

Estratega de Inteligencia de Dominio de Andru.ia. Analiza el nicho específico de un proyecto para inyectar conocimientos, regulaciones y estándares únicos del sector. Actívalo tras definir el nicho.

SamuelChien/mega-skills-collection

2D game development principles. Sprites, tilemaps, physics, camera.

SamuelChien/mega-skills-collection

3D game development principles. Rendering, shaders, physics, cameras.

SamuelChien/mega-skills-collection

Expert in building 3D experiences for the web - Three.js, React Three Fiber, Spline, WebGL, and interactive 3D scenes. Covers product configurators, 3D portfolios, immersive websites, and bringing depth to web experiences.

SamuelChien/mega-skills-collection

Accessibility audit skill for scanning, fixing, and verifying WCAG 2.2 Level A and AA compliance across React, Next.js, Vue, Angular, Svelte, and plain HTML codebases. Use when auditing accessibility, fixing a11y violations, checking color contrast, generating compliance reports, or integrating accessibility checks into CI/CD pipelines.

SamuelChien/mega-skills-collection

Ab Test Analyzer - Auto-activating skill for Data Analytics. Triggers on: ab test analyzer, ab test analyzer Part of the Data Analytics skill category.

SamuelChien/mega-skills-collection

A B Test Config Creator - Auto-activating skill for ML Deployment. Triggers on: a b test config creator, a b test config creator Part of the ML Deployment skill category.

SamuelChien/mega-skills-collection

When the user wants to plan, design, or implement an A/B test or experiment. Also use when the user mentions "A/B test," "split test," "experiment," "test this change," "variant copy," "multivariate test," "hypothesis," "conversion experiment," "statistical significance," or "test this." For tracking implementation, see analytics-tracking.

SamuelChien/mega-skills-collection

Acceptance Criteria Creator - Auto-activating skill for Enterprise Workflows. Triggers on: acceptance criteria creator, acceptance criteria creator Part of the Enterprise Workflows skill category.

SamuelChien/mega-skills-collection

Use when a coding task should be driven end-to-end from issue intake through implementation, review, deployment, and acceptance verification with minimal human re-intervention.

SamuelChien/mega-skills-collection

Accessibility Audit Runner - Auto-activating skill for Frontend Development. Triggers on: accessibility audit runner, accessibility audit runner Part of the Frontend Development skill category.

SamuelChien/mega-skills-collection

You are an accessibility expert specializing in WCAG compliance, inclusive design, and assistive technology compatibility. Conduct audits, identify barriers, and provide remediation guidance.

SamuelChien/mega-skills-collection

Web accessibility patterns for WCAG 2.2 compliance including ARIA, keyboard navigation, screen readers, and testing

SamuelChien/mega-skills-collection

Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed, or change policy for the Discord channel.

SamuelChien/mega-skills-collection

Create unified specification packages across Business, Development, and Design teams. Staged elaboration (L0 Vision → L1 Requirements → L2 Team Detail → L3 Acceptance Criteria) to build shared understanding. Does not write code.

SamuelChien/mega-skills-collection

Research a company or person and get actionable sales intel. Works standalone with web search, supercharged when you connect enrichment tools or your CRM. Trigger with "research [company]", "look up [person]", "intel on [prospect]", "who is [name] at [company]", or "tell me about [company]".

SamuelChien/mega-skills-collection

Validate messaging consistency across website, GitHub repos, and local documentation generating read-only discrepancy reports. Use when checking content alignment or finding mixed messaging. Trigger with phrases like "check consistency", "validate documentation", or "audit messaging".

相關技能