Skip to content

Latest commit

 

History

History
221 lines (150 loc) · 10.7 KB

README.md

File metadata and controls

221 lines (150 loc) · 10.7 KB

StateAFL:一种用于状态网络协议的覆盖驱动(Greybox)模糊器

StateAFL 是为网络服务器设计的模糊器。它扩展了 AFL 模糊器的原始思想,该模糊器自动演化模糊输入以最大限度地扩大代码覆盖范围。除了代码覆盖范围外,StateAFL 还寻求最大化协议状态覆盖范围。

该工具的目的是为有状态协议模糊化提供一个完全自动化的解决方案(类似于 AFL 能够为无状态程序实现的解决方案),并促进模糊化在现实世界系统中的更广泛应用。模糊器不需要开发人员为测试中的协议实现自定义消息解析器。

StateAFL 自动推断服务器的当前协议状态。在编译时,它通过探测内存分配和网络 I/O 操作来检测目标服务器。在运行时,它为每个协议迭代获取进程内存中长期数据的快照(见图),并应用模糊哈希将内存中的状态映射到唯一的协议状态。

The fundamental loop of network servers

StateAFL blocks

StateAFL 是在 AFLAFLnet 的代码库之上实现的。要模糊服务器,应该使用 afl-clang-fast (使用此项目中包含的修改版本,而不是来自 afl 的版本)对其进行编译,以执行编译器检查目标。

引用

有关 StateAFL 的更多信息,请参阅以下开放获取的研究论文。该论文也可在 arXiv 上获得。如果您在科学工作中使用此代码,请引用以下论文:

@article{StateAFL,
  author={Roberto Natella},
  title={StateAFL: Greybox Fuzzing for Stateful Network Servers},
  journal={Empirical Software Engineering},
  volume={27},
  number={191},
  publisher={Springer},
  url={https://doi.org/10.1007/s10664-022-10233-3},
  year={2022},
}

ProFuzzBench

如果你想快速运行一些实验,请查看 ProFuzzBench。ProFuzzBench 包括一套用于流行协议(如 TLS、SSH、SMTP、FTP、SIP)的代表性开源网络服务器,以及自动化实验的工具。StateAFL 已被纳入该基准。

安装(在 Ubuntu 18.04 和 16.04 64 位上测试)

准备

# 安装 clang(afl-clang-fast 需要)
sudo apt-get install clang
# 安装 graphviz-dev
sudo apt-get install graphviz-dev

StateAFL

下载 StateAFL 并编译它。我们已经在 Ubuntu 18.04 和 Ubuntu 16.04 64位上测试了 StateAFL ,它也可以在所有支持 AFL 和 graphviz 的环境中工作。

# 首先,将此 StateAFL 克隆到名为 StateAFL 的文件夹中
git clone <links to the repository> stateafl
# 然后移动到源代码文件夹
cd stateafl
make clean all
cd llvm_mode
# 如果找不到 llvm-config,则以下 make 命令可能不起作用
# 要解决此问题,只需设置 LLVM_CONFIG 环境。变量到您机器上的特定 llvm 配置版本
# 在 Ubuntu 18.04 上,如果你使用 apt-get 安装了 clang,它可能是 llvm-config-6.0
make
# 移动到 StateAFL 的父文件夹
cd ../..
export STATEAFL=$(pwd)/stateafl

设置 PATH 环境变量

export PATH=$STATEAFL:$PATH
export AFL_PATH=$STATEAFL

Usage

StateAFL 可以使用 AFL 和 AFLNet 的相同命令行选项运行(协议规范除外)。运行 afl-fuzz --help 查看所有选项。有关更多信息,请参阅 README-AFLnet.md

  • -i folder: 包含输入文件的文件夹,可回放格式(见下文)

  • -N netinfo: 服务器信息(例如。tcp://127.0.0.1/8554)

  • -P protocol: (可选,用于对照 AFLNet 交叉检查协议状态机)要测试的应用程序协议(例如,RTSP、FTP、DTLS12、DNS、DICOM、SMTP、SSH、TLS、DAP-HTTP、SIP)

  • -D usec: (可选)服务器完成初始化的等待时间(以微秒为单位)

  • -K : (可选)在消耗完所有请求消息后,发送 SIGTERM 信号以正常终止服务器

  • -E : (可选)启用状态感知模式

  • -R : (可选)启用区域级突变运算符

  • -F : (可选)启用假阴性还原模式(false negative reduction mode)

  • -c script : (可选)用于服务器清理的脚本的名称或完整路径

  • -q algo: (可选)状态选择算法(例如:1. RANDOM_SELECTION,2. OUND_ROBIN, 3. FAVOR)

  • -s algo: (可选)种子选择算法(例如:1. RANDOM_SELECTION,2. ROUND_ROBIN, 3. FAVOR)

  • -u path to "vanilla" executable: (可选)如果提供了这一点,StateAFL 使用两个分叉服务器,以获得更好的性能:一个用于分析代码覆盖范围(通过在"-u"中指示的目标可执行文件上运行输入,该可执行文件可以与 AFLNet/ALNwe 中使用的可执行文件相同);另一个用于分析协议状态(通过运行用 StateAFL 的 afl-clang-fast编译的可执行文件)

  • -U path to folder: (可选)运行"vanilla"可执行文件的工作目录(参见选项 -u)

命令示例:

afl-fuzz -d -i in -o out -N <server info> -x <dictionary file> -D 10000 -q 3 -s 3 -E -K -R <executable binary and its arguments (e.g., port number)>

准备种子输入

StateAFL 接收"可重放"格式的输入种子文件。它是一种简单的格式,也用于 AFLNet 中,用于使用命令 aflnet-replay 来重放输入(即,保存在replayable-queuereplayable-crashes文件夹中)。该格式交替显示消息的大小( 4字节,无符号整数)和该消息的内容。

Network replay format

您可以根据 PCAP 自动生成种子文件,如下所示:

$ python3 convert-pcap-replay-format.py --input ftp_requests_full_normal.pcap --server-port 2200 --output ftp_requests_full_normal.replay

Writing 13 bytes...
Writing 13 bytes...
Writing 6 bytes...
Writing 5 bytes...
Writing 24 bytes...
Writing 6 bytes...
Writing 10 bytes...
Writing 6 bytes...
Converted PCAP saved to ftp_requests_full_normal.replay

$ hexdump -C ftp_requests_full_normal.replay

00000000  0d 00 00 00 55 53 45 52  20 75 62 75 6e 74 75 0d  |....USER ubuntu.|
00000010  0a 0d 00 00 00 50 41 53  53 20 75 62 75 6e 74 75  |.....PASS ubuntu|
00000020  0d 0a 06 00 00 00 53 59  53 54 0d 0a 05 00 00 00  |......SYST......|
00000030  50 57 44 0d 0a 18 00 00  00 50 4f 52 54 20 31 32  |PWD......PORT 12|
00000040  37 2c 30 2c 30 2c 31 2c  31 33 32 2c 32 30 39 0d  |7,0,0,1,132,209.|
00000050  0a 06 00 00 00 4c 49 53  54 0d 0a 0a 00 00 00 4d  |.....LIST......M|
00000060  4b 44 20 74 65 73 74 0d  0a 06 00 00 00 51 55 49  |KD test......QUI|
00000070  54 0d 0a                                          |T..|
00000073

PCAP 文件应该只包含服务器和客户端之间的一个消息流。同时支持 TCP 和 UDP。如果 PCAP 包含多个消息流,则可以使用选项 --client-port--ignore-multiple-clients来选择特定流并忽略其他流。要运行转换脚本,需要安装 pyshark Python 包和 tshark 工具。测试中的协议不要求tshark 可以解析。该脚本仅从 TCP 或 UDP 流量中提取原始负载。

Demo

下面的演示展示了如何在 FTP 服务器上使用 StateAFL。它从一个简单的 client-server 会话中收集 PCAP 开始。然后,PCAP 被转换为"可回放"格式,并充当 StateAFL 的种子。StateAFL 从 FTP 服务器的内存状态中逐步构建协议的有限状态机。

有关自动模糊实验的更多信息,请参阅 ProFuzzBench

Demo transcript

环境变量:

WORKDIR=/home/ubuntu/experiments
STATEAFL=/home/ubuntu/stateafl

要使用 tcpdump 收集 PCAP,请执行以下操作:

sudo tcpdump -i lo -w pcaps/ftp.pcap port 2200

要运行 LightFTP 服务器,请执行以下操作:

./fftp fftp.conf 2200

要运行 FTP 客户端,请执行以下操作:

ftp localhost 2200
Logged-in as "ubuntu", pass "ubuntu"
pwd
mkdir TEST
cd TEST
pwd
quit

转换 PCAP :

python3 $STATEAFL/convert-pcap-replay-format.py --input $WORKDIR/pcaps/ftp.pcap --server-port 2200 --output $WORKDIR/in-ftp-replay/ftp.replay

模糊 FTP 服务器:

make clean
CC=${STATEAFL}/afl-clang-fast make clean all -j3

${STATEAFL}/afl-fuzz -d -i ${WORKDIR}/in-ftp-replay -x ${WORKDIR}/ftp.dict -o ${WORKDIR}/output -N tcp://127.0.0.1/2200 -D 10000 -q 3 -s 3 -E -K -m none -t 5000 -c ${WORKDIR}/ftpclean -u ${WORKDIR}/LightFTP/Source/Release/fftp -- ./fftp fftp.conf 2200

要绘制(在终端上)协议状态机,请执行以下操作:

$ watch -n 1 'cat $WORKDIR/output/ipsm.dot | graph-easy --from=dot --as_ascii 2>/dev/null'

许可证

StateAFL is licensed under Apache License, Version 2.0.

StateAFL extends AFLnet, written and maintained by Van-Thuan Pham <[email protected]>, and American Fuzzy Lop written and maintained by Michał Zalewski <[email protected]>. For details about these fuzzers, we refer to README-AFLnet.md and README-AFL.md.

StateAFL uses the Trend Micro Locality Sensitive Hash (TLSH) library and the MVPTree C library for fuzzy hashing and for nearest neighbor search. StateAFL uses the Containers library for map, queue, and set data structures. StateAFL uses an open-source implementation of memcpy from the XNU project to override the ASAN's memcpy interceptor.

  • AFL: Copyright 2013, 2014, 2015, 2016 Google Inc. All rights reserved. Released under terms and conditions of Apache License, Version 2.0.

  • TLSH: Copyright 2013 Trend Micro Incorporated. Released under terms and conditions of Apache License, Version 2.0.

  • MVPTree C library: Copyright 2008-2009 by D. Grant Starkweather. Released under terms and conditions of GNU Public License, Version 3.0.

  • Containers library: Copyright (c) 2017-2020 Bailey Thompson. Released under terms and conditions of MIT License.

  • memcpy: Copyright (c) 1990, 1993 The Regents of the University of California. This code is derived from software contributed to Berkeley by Chris Torek. Released under terms and conditions of BSD License.