AFL工具使用实践:以libtiff为例的使用指南
字数 742 2025-08-22 12:23:24
AFL工具使用实践:以libtiff为例的模糊测试指南
1. 环境准备
1.1 目标程序准备
首先下载并编译libtiff库作为测试目标:
mkdir libtiff
cd libtiff
wget http://download.osgeo.org/libtiff/tiff-4.0.9.tar.gz
tar zxvf tiff-4.0.9.tar.gz
cd tiff-4.0.9
1.2 AFL编译器设置
设置环境变量使用AFL的编译器:
export CC=afl-gcc
export CXX=afl-g++
1.3 编译目标程序
./configure --disable-shared
make
cd ..
2. 测试用例准备
2.1 创建输入输出目录
mkdir input output
2.2 获取测试用例
从AFL测试用例库中获取TIFF相关测试用例:
cp ../AFL/testcases/tiff/edges-only/images/* input
3. 核心模糊测试
3.1 设置核心转储
sudo su
echo core >/proc/sys/kernel/core_pattern
3.2 启动模糊测试
afl-fuzz -i input -o output ./tiff-4.0.9/tools/tiff2rgba -a @@
4. 语料库优化
4.1 语料库蒸馏(cmin)
移除执行相同代码路径的输入文件:
afl-cmin -i input -o output -- ./tiff-4.0.9/tools/tiff2rgba -a @@
4.2 测试用例最小化(tmin)
减小单个输入文件的大小:
afl-tmin -i ./input/id:000001,src:000000,op:flip1,pos:0,+cov.tif -o output_file -- ./tiff-4.0.9/tools/tiff2rgba -a @@
5. 执行路径分析
使用showmap跟踪单个输入的执行路径:
afl-showmap -m none -o /dev/null -- ./tiff-4.0.9/tools/tiff2rgba ./input/id:000001,src:000000,op:flip1,pos:0,+cov.tif out.png
6. 持久化测试会话
使用screen保持模糊测试会话:
sudo apt install screen
screen afl-fuzz -i input -o output ./tiff-4.0.9/tools/tiff2rgba -a @@
命名screen会话:
screen -S fuzzer1
afl-fuzz -i testcase_dir -o findings_dir /path/to/program [params] @@
[detached from 6999.fuzzer1]
screen -r fuzzer1
7. 并行模糊测试
7.1 查看CPU核心数
cat /proc/cpuinfo | grep "cpu cores" | uniq
7.2 启动并行fuzzer
主fuzzer:
screen afl-fuzz -i input -o output -M fuzzer1 -- ./tiff-4.0.9/tools/tiff2rgba -a @@
从属fuzzer:
screen afl-fuzz -i input -o output -S fuzzer2 -- ./tiff-4.0.9/tools/tiff2rgba -a @@
8. 结果分析工具
8.1 状态查看(whatsup)
afl-whatsup output/fuzzer1/crashes
8.2 图形化展示(plot)
安装gnuplot:
apt-get install gnuplot
生成图表:
afl-plot ./output ./dir
8.3 崩溃分析(collect)
安装必要工具:
git clone https://github.com/rc0r/exploitable
sudo python setup.py install
git clone https://github.com/rc0r/afl-utils
sudo python setup.py install
执行崩溃分析:
afl-collect -d crashes.db -e gdb_script -r -rr ./output ./input -j 8 -- ././tiff-4.0.9/tools/tiff2rgba
注意:如果遇到Python2的which问题,可以自定义which函数:
def which(program):
# 查找程序在系统中的路径
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return exe_file
return None
并修改源代码中的which调用:
gdb_binary = which("gdb")
9. 关键注意事项
- 编译目标程序时务必使用AFL的编译器(afl-gcc/afl-g++)
- 模糊测试前需要正确设置核心转储模式
- 并行测试时,主fuzzer(-M)和从属fuzzer(-S)需要同步目录
- 长时间运行的模糊测试建议使用screen保持会话
- 定期使用cmin和tmin优化测试用例集
- 分析结果时结合多种工具(whatsup, plot, collect)获取全面信息