搭建AFL++环境并fuzz mcrypt

我的计算机安全课件设计:AFL++环境配置以及上手一个简单的fuzz任务。

这部分材料是我为计算机安全课程中fuzz部分准备的,因为我很菜又需要找一个(网上没有答案的)真实的crash,所以用的CVE是很老的QAQ。
我为计安课准备的其他材料还有:

Fuzzing task instruction

mcrypt is a small tool but supports many encryption algorithms. It is widely use in many web applications like php. In this lab, we will use AFL++ to find a real crash in mcrypt-2.6.5 (CVE-2012-4409).
CVE-2012-4409 happens when mcrypt decrypts a file.

If you find any new crash not on record, please report it to the maintainer.

Setup AFL++ environment

Note below steps will install shared libs in your Virtual Machine, be careful if you are using your own machine and don’t want to install shared libs.

picture 1

1 Compile AFL++:

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt update 
sudo apt install -y automake cmake ninja-build clang-12 lld-12 llvm-12 llvm-12-dev
sudo apt install -y gcc-$(gcc --version|head -n1|sed 's/.* //'|sed 's/\..*//')-plugin-dev libstdc++-$(gcc --version|head -n1|sed 's/.* //'|sed 's/\..*//')-dev
git clone https://github.com/AFLplusplus/AFLplusplus
export LLVM_CONFIG="llvm-config-12"
cd AFLplusplus
make distrib
sudo make install
cd qemu_mode
CPU_TARGET=i386 ./build_qemu_support.sh
cd ../
sudo make install

2 Fuzz binary in previous lab

First create a directory for input and output files:

1
2
3
4
5
6
7
8
9
10
sudo su
echo core >/proc/sys/kernel/core_pattern
exit

cd ~/Desktop/lab5
chmod +x ./ret2libc_static
mkdir inputs
mkdir outputs
echo "random input" > inputs/1
afl-fuzz -i inputs/ -o outputs/ -Q ./ret2libc_static

AFL may find crash quickly.

The crash can be found in outputs/default/crashes/

picture 0

3 compile mcrypt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
tar -zxvf mhash-0.9.9.9.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
tar -zxvf mcrypt-2.6.5.tar.gz
# z: uncompress with gzip
# x: extract files from archive
# v: verbose
# f: use archive file

# compile mhash first
cd mhash-0.9.9.9/
./configure
make -j
sudo make install # remeber type your password
cd -
sudo ln -s /usr/local/lib/libmhash.so /lib/libmhash.so
sudo ln -s /usr/local/lib/libmhash.so.2 /lib/libmhash.so.2

# then compile libmcrypt
cd libmcrypt-2.5.8/
./configure
make -j
sudo make install
sudo ln -s /usr/local/lib/libmcrypt.so /lib/libmcrypt.so
sudo ln -s /usr/local/lib/libmcrypt.so.4 /lib/libmcrypt.so.4

# at last compile mcrypt
cd mcrypt-2.6.5/
CC=~/Desktop/AFLplusplus/afl-clang-fast ./configure --disable-shared
export AFL_USE_CFISAN=1
export LLVM_CONFIG="llvm-config-12"
# change directory to absolute path of your AFLplusplus
make CC=~/Desktop/AFLplusplus/afl-clang-fast CXX=~/Desktop/AFLplusplus/afl-clang-fast++ LD=~/Desktop/AFLplusplus/afl-clang-fast

./src/mcrypt --version
cp ./src/mcrypt ..

Now you should have mcrypt in src/ directory (in mcrypt-2.6.5/src/).

Firstly, open a new terminal and run:
while true; do rm outputs/default/.cur_input.dc;done;

Then use afl-fuzz -i inputs/ -o outputs/ -- ./mcrypt arg1 arg2 (change agr1
and arg2 to real argument!!) to fuzz mcrypt program, if some arguments represent file, you can use @@ to represent the file.

If you need to stop and re-start the fuzzing, use the same command line options and switch the input directory with a dash (-):
afl-fuzz -i - -o outputs/ -- ./mcrypt arg1 arg2

This time, AFL may not find crash quickly, we can create a more vaild input for mcrypt to help AFL reach more code and find crash.

1
2
3
echo "random input" > raw_text
./mcrypt raw_text # type some password
cp raw_text.nc inputs/2
作者

Frank Wu

发布于

2023-09-01

更新于

2024-10-21

许可协议