对lua进行模糊测试及问题记录

Source

网上对lua进行测试的帖子太少了,然后最近正好要跑对lua进行模糊测试的过程,下面以afl为例,讲解如何对lua进行测试。

安装lua

安装lua的详细过程可以参考此链接
https://github.com/HexHive/magma/tree/v1.2/targets/lua
首先按照preinstall.sh中的文件执行需要提前安装的依赖,然后执行fetch.sh下载lua,再执行build.sh对lua进行编译即可运行,详细执行过程如下:

安装前准备

在安装lua之前需要先安装编译lua需要的依赖:

apt-get update && \
    apt-get install -y git make libreadline-dev

下载lua

讲lua下载并安装到repo目录下,repo可以替换为任何你想安装的目录

git clone https://github.com/lua/lua.git "./repo"

编译lua

在编译lua的时候可以采用gcc来编译,也可以使用afl-clang,lua默认的编译方式是gcc,在进行模糊测试时我采用的是afl-clang,也因此,在执行时报了一个错,在下文中进行讲解。

  1. 配置环境变量
    如果你需要使用gcc来进行编译则跳过这一步,your path是你的afl-calng-fast路径,一般在你的fuzzer目录下
export CC=your path /afl-clang-fast CXX=/your path/afl-clang-fast++

修改makefile配置

cd repo
vim makefile

在makefile中将CC=gcc这一句设置为无效:
在这里插入图片描述
2. 编译lua

#进入前面lua拉去的目录下
cd repo  
make clean
make liblua.a
cp liblua.a ../out/
make lua
cp lua ../out/

模糊测试

接下来就可以对lua进行模糊测试了,如果想要添加字典可以添加此链接的字典:lua.dict
将字典也放到out目录下.

接下来将lua中的测试用例复制到out目录:

#注意测试用例在repo目录下的testes中,将其复制到out目录下得得corpus中
cp ./repo/testes/*.lua …/out/corpus/

此时各个文件夹情况如下:
在这里插入图片描述
执行模糊测试:

afl-fuzz -i ./corpus/  -o out -x ./lua.dict -- ./lua

遇见问题

1.编译lua时

前面已经提到,本文选择的是afl-clang来配置的环境变量,然而在make编译完后进行模糊测试时报错如下:

Looks like the target binary is not instrumented! The fuzzer depends on
compile-time instrumentation to isolate interesting test cases while
mutating the input data. For more information, and for tips on how to
instrument binaries, please see /usr/local/share/doc/afl/README.

When source code is not available, you may be able to leverage QEMU
mode support. Consult the README for tips on how to enable this.
(It is also possible to use afl-fuzz as a traditional, “dumb” fuzzer.
For that, you can use the -n option - but expect much worse results.)

[-] PROGRAM ABORT : No instrumentation detected
Location : check_binary(), afl-fuzz.c:6989

在这里插入图片描述
这个实际仔细看就会发现在执行make编译liblua.a时尽管我们设置了环境变量为afl-clang,但编译时仍然是按照gcc来编译的。
在这里插入图片描述
这是因为makefile文件中直接定义了CC为gcc,只需要将makefile修改了就行。
进入repo文件夹下
vim makefile
将CC=gcc引掉即可
在这里插入图片描述

2.模糊测试时报错

在进行模糊测试时还报了这个错,显示超时,直接把对应的corpus里面的测试用例删除就可以了
在这里插入图片描述