顧名思義, American Fuzzy Lop 是一款用于測試程序安全性的模糊測試工具, 官網(wǎng)簡介如下:
American fuzzy lop is a security-oriented fuzzer that employs a novel type of compile-time instrumentation and genetic algorithms to automatically discover clean, interesting test cases that trigger new internal states in the targeted binary. This substantially improve the functional coverage for the fuzzed code. The compact synthesized corpora produced by a tool are also useful for seeding other, more labor-or resource-intensive testing regimes down the road.
簡單來說, 這款工具能夠在程序運行的時候注入自己的code, 然后自動產(chǎn)生testcase進行模糊測試.

AFL 好在哪?
無需配置, 速度快, 可以應對復雜的程序.
void test(char *buf)
{
int n = 0;
if(buf[0] == 'b') n++;
if(buf[1] == 'a') n++;
if(buf[2] == 'd') n++;
if(buf[3] == '!') n++;
if(n == 4) {
crash();
}
}
上面的例子中, 需要2^32 或者4百萬個嘗試才能出發(fā)一次崩潰, 這顯然效率是很低的. 如果我們一秒鐘嘗試1000次, 那么出發(fā)崩潰所需要的時間就是 2^32/1000/3600/24 = 49 天.
下面我們來嘗試一下用AFL來進行模糊測試.
首先編寫一個目標程序.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void test (char *buf) {
int n = 0;
if(buf[0] == 'b') n++;
if(buf[1] == 'a') n++;
if(buf[2] == 'd') n++;
if(buf[3] == '!') n++;
if(n == 4) {
raise(SIGSEGV);
}
}
int main(int argc, char *argv[]) {
char buf[5];
FILE* input = NULL;
input = fopen(argv[1], "r");
if (input != 0) {
fscanf(input, "%4c", &buf);
test(buf);
fclose(my_file):
}
return 0;
}
然后編譯一下
./afl-gcc crasher.c -o crash
因為這個程序是讀文件的, 所以我們得給他一個測試用例.
mkdir testcase
echo 'jianshu' > testcase/file
然后開跑!
./afl-fuzz -i testcase -o output/ ./crash @@

通過 run time - last uniq crash的時間可以看出, afl只用了20秒就將程序crash了. 當然, 這是在實驗室機器跑的, 如果是一般的機器的話, 時間可能久一點, 我在自己的Mac上跑的時間是15分鐘. 對比起暴力測試方法要用49天, afl對效率的提高不止一點半點.