安装环境
kali
afl-2.52b
下载源代码压缩包
官网:american fuzzy lop (coredump.cx)
安装
- 将压缩包解压,编译
sudo su
tar -xvf afl-latest.tgz
cd afl-2.52b
make
make install
有源码
在桌面创建一个fuzztest项目
mkdir fuzztest
cd fuzztest
vi afl_test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
int vuln(char *str)
{
int len = strlen(str);
if(str[0] == 'A' && len == 66)
{
raise(SIGSEGV);
//如果输入的字符串的首字符为A并且长度为66,则异常退出
}
else if(str[0] == 'F' && len == 6)
{
raise(SIGSEGV);
//如果输入的字符串的首字符为F并且长度为6,则异常退出
}
else
{
printf("it is good!\n");
}
return 0;
}
int main(int argc, char *argv[])
{
char buf[100]={0};
gets(buf);//存在栈溢出漏洞
printf(buf);//存在格式化字符串漏洞
vuln(buf);
return 0;
}
afl-gcc -g -o afl_test afl_test.c //编译插桩指令
建立两个文件夹,fuzz_in和fuzz_out
fuzz_in中需要创建一个测试样本testcase,并在里边插入字符“aaa”
mkdir fuzz_in fuzz_out
vi fuzz_in/testcase
afl-fuzz -i fuzz_in -o fuzz_out ./afl_test
一段时间后需要手动ctrl + c
结束
cd fuzz_out/crashes
ls
更新apt下载源,下载xxd
# apt-get updat
# apt-get upgrade
# apt-get install xxd
查看测试结果(二进制文件)
xxd 文件名
无源码
afl-qemu mode的安装
cd qemu_mode
./build_qemu_support.sh
- 多次执行
./build_qemu_support.sh
安装命令反馈缺少的库
安装libtoolo库
apt-get install litbool-bin
libtool 是一个通用库支持脚本(/usr/bin/libtool),将使用动态库的复杂性隐藏在统一、可移植的接口中。可以在不同平台上创建并调用动态库,可以认为libtool是gcc的一个抽象,即它包装了gcc或者其他的任何编译器,用户无需知道细节, 只要告诉libtool需要编译哪些库即可。
安装bison库
./build_qemu_support.sh
apt-get install bison
安装glib2
apt-get install libglib2.0-dev
- 修改配置文件
vi build_qemu_support.sh
修改官网路径为Index of / (qemu.org)
删除黄框部分
加入此命令
patch -p1 <../patches/syscall2.diff || exit 1
patch -p1 <../patches/memfd_create.diff || exit 1
添加补丁文件syscall2.diff,文件内容如下:
--- qemu-2.10.0-clean/linux-user/syscall.c 2020-03-12 18:47:47.898592169 +0100
+++ qemu-2.10.0/linux-user/syscall.c 2020-03-12 19:16:41.563074307 +0100
@@ -34,6 +34,7 @@
#include <sys/resource.h>
#include <sys/swap.h>
#include <linux/capability.h>
+#include <linux/sockios.h> // https://lkml.org/lkml/2019/6/3/988
#include <sched.h>
#include <sys/timex.h>
#ifdef __ia64__
@@ -116,6 +117,8 @@ int __clone2(int (*fn)(void *), void *ch
#include "qemu.h"
+extern unsigned int afl_forksrv_pid;
+
#ifndef CLONE_IO
#define CLONE_IO 0x80000000 /* Clone io context */
#endif
@@ -256,7 +259,9 @@ static type name (type1 arg1,type2 arg2,
#endif
#ifdef __NR_gettid
-_syscall0(int, gettid)
+// taken from https://patchwork.kernel.org/patch/10862231/
+#define __NR_sys_gettid __NR_gettid
+_syscall0(int, sys_gettid)
#else
/* This is a replacement for the host gettid() and must return a host
errno. */
@@ -6219,7 +6224,8 @@ static void *clone_func(void *arg)
cpu = ENV_GET_CPU(env);
thread_cpu = cpu;
ts = (TaskState *)cpu->opaque;
- info->tid = gettid();
+ // taken from https://patchwork.kernel.org/patch/10862231/
+ info->tid = sys_gettid();
task_settid(ts);
if (info->child_tidptr)
put_user_u32(info->tid, info->child_tidptr);
@@ -6363,9 +6369,11 @@ static int do_fork(CPUArchState *env, un
mapping. We can't repeat the spinlock hack used above because
the child process gets its own copy of the lock. */
if (flags & CLONE_CHILD_SETTID)
- put_user_u32(gettid(), child_tidptr);
+ // taken from https://patchwork.kernel.org/patch/10862231/
+ put_user_u32(sys_gettid(), child_tidptr);
if (flags & CLONE_PARENT_SETTID)
- put_user_u32(gettid(), parent_tidptr);
+ // taken from https://patchwork.kernel.org/patch/10862231/
+ put_user_u32(sys_gettid(), parent_tidptr);
ts = (TaskState *)cpu->opaque;
if (flags & CLONE_SETTLS)
cpu_set_tls (env, newtls);
@@ -11402,7 +11410,8 @@ abi_long do_syscall(void *cpu_env, int n
break;
#endif
case TARGET_NR_gettid:
- ret = get_errno(gettid());
+ // taken from https://patchwork.kernel.org/patch/10862231/
+ ret = get_errno(sys_gettid());
break;
#ifdef TARGET_NR_readahead
case TARGET_NR_readahead:
vi patches/syscall2.diff
./build_qemu_support.sh
cd ..
make install
看到afl-qemu-trace
安装成功
无源码二进制文件进行fuzz测试
以系统中指令为例,使用如下指令执行fuzz,参数表示使用qemu模式;参数设置使用内存大小,不设置则默认200MB:wget -Q -m
afl-fuzz -i fuzz_in -o fuzz_out -m 200 -Q wget @@
fuzz测试语料库fuzz_in
语料库文档下载https://lcamtuf.coredump.cx/afl/demo/afl_testcases.tgz
将其移动解压到fuzz_in文件中
安装afl-plot绘图:gnuplot
apt-get install gnuplot
在fuzztest目录下新建image
mkdir image
afl-plot fuzz_out image
打开index.html
参考文章:
AFL(afl-qemu mode)的安装和使用(一)_afl qemu-CSDN博客
深入分析 afl / qemu-mode(qemu模式) / afl-unicorn 编译及安装存在的问题以及相应的解决方案_模糊测试 afl qemu模式-CSDN博客