Fuzzing101笔记

记录一下做这个遇到的问题和理解

Exercise 1

What you will learn

After completing this exercise, you will know the basis of fuzzing with AFL, such as:

Compiling a target application with instrumentation
Running a fuzzer (afl-fuzz)
Triaging crashes with a debugger (GDB)

是pdftotext软件的一个无限递归导致dos的漏洞 ,pdftotext的用法:pdftotext -layout pdf-entrada.pdf pdf-salida.txt

  • 首先对源代码进行插桩编译,这里只要指定编译器为AFL++里对应的编译器即可
export LLVM_CONFIG="llvm-config-11"
CC=$HOME/Fuzz/AFL++/afl-clang-fast CXX=$HOME/Fuzz/AFL++/afl-clang-fast++ ./configure --prefix="$HOME/fuzzing_xpdf/install/"
make
make install
  • 启动命令,–后都是target_file对应的参数
@@:

这是 AFL++ 中的一个特殊占位符。在模糊测试过程中,AFL++ 会将 @@ 替换为当前生成或变异的测试用例文件的路径。
每次 AFL++ 运行一次测试迭代时,它会用一个生成的 PDF 文件路径来替换 @@,以模拟实际输入提供给 pdftotext。

在每次模糊测试迭代中,pdftotext 将:

接收 @@ 替代的 PDF 文件作为输入,
将内容转换为文本,
并将结果保存到 $HOME/fuzzing_xpdf/output。
afl-fuzz -i $HOME/fuzzing_xpdf/pdf_examples/ -o $HOME/fuzzing_xpdf/out/ -s 123 -- $HOME/fuzzing_xpdf/install/bin/pdftotext @@ $HOME/fuzzing_xpdf/output
  • First of all, you need to rebuild Xpdf with debug info to get a symbolic stack trace.再次重新编译获得对应的符号表,便于调试。fuzz只是为了获得让其崩溃的输入
rm -r $HOME/fuzzing_xpdf/install
cd $HOME/fuzzing_xpdf/xpdf-3.02/
make clean
CFLAGS="-g -O0" CXXFLAGS="-g -O0" ./configure --prefix="$HOME/fuzzing_xpdf/install/"
make
make install
  • 最后就可以看到程序为什么崩溃了
gdb --args $HOME/fuzzing_xpdf/install/bin/pdftotext $HOME/fuzzing_xpdf/out/default/crashes/<your_filename> $HOME/fuzzing_xpdf/output
run
bt

Exercise 2

What you will learn

Once you complete this exercise you will know how:

To fuzz a library using an external application
To use afl-clang-lto, a collision free instrumentation that is faster and provides better results than afl-clang-fast
To use Eclipse IDE as an easy alternative to GDB console for triaging

这个实验自己没有去做,与第一个实验的区别在于,这个实验是发现链接库的漏洞

所以一开始就要编译libexif链接库和用于运行这个链接库的程序exif,然后运行exif这个文件,通过二进制文件exif来触发libexif的漏洞

Exercise 3

What you will learn

Once you complete this exercise you will know:

What is ASan (Address Sanitizer), a runtime memory error detection tool
How to use ASAN to fuzz a target
How much easy is to triage the crashes using ASan

AddressSanitizer

AddressSanitizer (ASan) is a fast memory error detector for C and C++. It consists of a compiler instrumentation module and a run-time library. 
The tool is capable of finding out-of-bounds accesses to heap, stack, and global objects, as well as use-after-free, double-free and memory leaks bugs.

这个实验主要是教会如何用ASan选项快速发现溢出错误,具体用法如下

cd $HOME/fuzzing_tcpdump/tcpdump-4.9.2/
AFL_USE_ASAN=1 CC=afl-clang-lto ./configure --prefix="$HOME/fuzzing_tcpdump/install/"
AFL_USE_ASAN=1 make
AFL_USE_ASAN=1 make install

Exercise 4

What you will learn

Once you complete this exercise you will know:

How to measure code coverage using LCOV
How to use code coverage data to improve the effectiveness of fuzzing

主要是教会使用LCOV这个代码覆盖率的工具

Exercise 5

What you will learn

Once you complete this exercise you will know how:

To use custom dictionaries for helping the fuzzer to find new execution paths
To parallelize the fuzzing job accross multiple cores

这次实验主要是教如何提高fuzz效率,这里使用的是并行的方法

The use of shared instances is a better approach to parallel fuzzing. In this case, each fuzzer instance gathers any test cases found by other fuzzers.

#You will usually have only one master instance at a time:

./afl-fuzz -i afl_in -o afl_out -M Master -- ./program @@
#and N-1 number of slaves:

./afl-fuzz -i afl_in -o afl_out -S Slave1 -- ./program @@
./afl-fuzz -i afl_in -o afl_out -S Slave2 -- ./program @@
#...
./afl-fuzz -i afl_in -o afl_out -S SlaveN -- ./program @@

还有一个就是学会用字典

-x ./dictionaries/xml.dict 

-x <dictionary_file> 参数的作用

指定字典文件:<dictionary_file> 是一个包含关键字或标记的文件,每行定义一个输入字符串。这些字符串可以是关键字、符号或格式标记,用于指导 AFL++ 在生成测试用例时插入这些标记,增加输入的有效性。

优化测试效率:字典文件可以使 AFL++ 更容易生成符合目标程序输入格式的有效测试用例,从而更高效地探索新的执行路径和触发潜在的漏洞。例如,测试 JSON 格式时,字典中可以包含 {}, [], : 等符号,帮助 AFL++ 生成符合 JSON 语法的输入。

字典文件格式

字典文件的每一行是一个输入标记,标记可以被双引号包裹。示例文件如下:

"GET"
"POST"
"{"
"}"
"username"
"password"

使用示例

假设我们在模糊测试一个支持 JSON 输入的程序,并希望 AFL++ 使用 JSON 格式的标记,那么可以编写一个字典文件 json.dict,内容如下:

"{"
"}"
"["
"]"
":"
","
"true"
"false"
"null"

然后,在运行 AFL++ 时使用 -x json.dict 参数:

afl-fuzz -i input_dir -o output_dir -x json.dict -- target_program @@

总结:-x 参数可以提高 AFL++ 模糊测试的效率和测试质量,尤其适用于格式复杂或输入严格的程序。通过使用字典文件,AFL++ 能够更快速地生成有效输入,从而更快地发现潜在漏洞和边界情况。