Hacking/system

Fuzzing

__PS 2024. 7. 5. 12:16
728x90

Find Exploitable Bug

1. 1-day 

- 패치된 버전과 이전 버전을 모두 가지고 비교 분석

 

2. PoC 코드 분석

- 실제 취약점이 무엇인지, 잠재적 취약점까지 분석

 

3. 정적 분석

- black box testing: 리버싱

- white box testing: 소스코드 전문을 분석

 

정적 분석의 시작점: Attack surface

-> 직접 입력값, 설정값, 다운로드

 

4. Fuzzing

- Crash나 memory corruption으로 이어질 수 있는 입력값을 찾는 방법

- 코드리뷰는 개발 언어와 구조에 대한 이해 필요, Fuzzing은 쉽고 간단한 접근이 가능

- Mutation based Fuzzing / Generation based Fuzzing

 

Mutation base Fuzzing (Dumb Fuzzing)

- 입력값을 랜덤하게 변형 -> 구조를 알기 어려운 경우 유용

 

Generation base Fuzzing

- 필요한 입력값이나 파일의 구조에 맞추어 생성

- 문법 기반 Fuzzing (DOM Fuzzing) 

 

Asan (Address Sanitizer)

UAF, Heap / Stack BOF, Memory leak 등의 탐지 가능

gcc -fsanitize=adddress 옵션 추

-> heap buffer overflow가 발생했음을 알 수 있음

 

AFL

컴파일 시에 AFL 코드를 삽입하는 방식

genetic algorithm

입력값을 받은 이후 새로운 프로그램 실행 흐름이 발견되면 이를 기록하고 다음 mutation에 사용

- 각 crash를 유니크하게 발견하는데 용이

- 실행 흐름을 다양하고 꼼꼼하게 파악 가능

 

설치

https://lcamtuf.coredump.cx/afl/releases 에 들어가 af-latest.tgz 다운로드

tar -zxvf afl-latest.tgz 압축 해제

cd afl-2.52b

make; sudo make install 

 

분석

http://prdownloads.sourceforge.net/optipng/optipng-0.7.5.tar.gz 다운로드

tar -zxvf optipng-0.7.5.tar.gz 압축 해제

cd opripng-0.7.5

- export CC=afl-gcc

- export CXX=afl-gcc

optipng 컴파일 시에 afl-gcc를 사용하도록 컴파일 옵션 변경

make

cd src/optipng

 

실행

sudo sysctl -w kernel.core_pattern=core

- crash 발생 시 core dump 파일 생성

mkdir input; cp ~ 경로/poc.png input

afl-fuzz -i input -o output -- ./optipng -o4 -zm3 -zc1 -zw256 -snip @@

 

재실행

make clean

export CC="gcc -fsanitize=address -g"

export CXX="g++ -fsanitize=address -g"

./configure

make

AFL QEMU 모드 (포기)

제시된 파일 다운로드

분석

 

해당 바이너리 파일이 존재하지 않으므로 QEMU mode 를 통해 분석이 가능할 것 같다.

 

우선 qemu 모드 경로로 들어가보니, python이 필요하다고 뜬다.

그런데 python3가 존재하므로 

sudo apt-get install python-is-python3 이 명령어나,

nano build_qemu_support.sh 명령어를 통해 python을 python3로 바꾸어주었다.

 

다음으로는 bison이 없다는 문구

sudo apt install bison 이 명령어로 bison 설치

 

 

모든 체크가 통과되었으나, 404 Error 발생

 

그래서 다시 처음부터 설치

 

git clone https://gitlab.com/qemu-project/qemu.git

cd qemu

wget https://download.qemu.org/qemu-2.10.0.tar.xz --no-check-certificate

tar -xf qemu-2.10.0.tar.xz

cd qemu-2.10.0

sudo apt-get update

sudo apt-get install -y git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev

sudo apt-get install -y libaio-dev libbluetooth-dev libbrlapi-dev libbz2-dev

sudo apt-get install -y libcap-dev libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev

sudo apt-get install -y libibverbs-dev libjpeg8-dev libncurses5-dev libnuma-dev

sudo apt-get install -y librdmacm-dev libseccomp-dev libsnappy-dev libssh-dev

sudo apt-get install -y libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev

sudo apt-get install -y liblzo2-dev valgrind xfslibs-dev libnfs-dev libiscsi-dev sudo apt-get install -y ninja-build

 

이후

./configure

make

sudo make install

python3는 지원하지 않으므로 python2를 설치해야함.

 

지금까지는 그냥 기록용으로 하고 AFL++ 설치 후 최신버전으로 실행


AFL++

sudo apt update

sudo apt install -y build-essential python3-dev automake git flex bison libglib2.0-dev libpixman-1-dev python3-setuptools

sudo apt-get install -y lld-11 llvm-11 llvm-11-dev clang-11 || sudo apt-get install -y lld llvm llvm-dev clang

sudo apt-get install -y gcc-$(gcc --version|head -n1|sed 's/.* //'|sed 's/\..*//')-plugin-dev libstdc++-$(gcc --version|head -n1|sed 's/.* //'|sed 's/\..*//')-dev

 

cd ~

git clone https://github.com/AFLplusplus/AFLplusplus && cd AFLplusplus

export LLVM_CONFIG="llvm-config-11"
make distrib
sudo make install

이 중에 make distrib을 시도할 때 계속 뜬 에러

make distrib
[*] Compiling AFL++ for OS Linux on ARCH x86_64
/bin/sh: 1: gcc-fsanitize=address: not found
[!] Warning: no ZLIB detected
[*] Checking for the default compiler cc...

Oops, looks like there is no compiler 'gcc-fsanitize=address -g' in your path.

Don't panic! You can restart with '/usr/bin/make CC=<yourCcompiler>'.

make: *** [GNUmakefile:423: test_x86] Error 1

/usr/bin/make CC="gcc -fsanitize=address -g" 명령어를 입력한 이후 터미널 재실행으로 해결

 

make distrib 까지 실행한 결과 / 여기서 제일 오래 걸림

 

이후 설치가 완료된 듯 하다

여기서 알 수 있듯이 -Q 옵션을 통해 QEMU 모드를 사용한다.

명령어는 afl-fuzz [options] -- /path/to/fuzz_app [...] 이다.

 

+ 중간 중간에 발생했던 에러

 

실습 - xpdf

참고: https://jiravvit.tistory.com/entry/Fuzzing101-AFL%EB%A1%9C-Xpdf-%ED%8D%BC%EC%A7%95%ED%95%98%EA%B8%B0

 

https://www.cvedetails.com/cve/CVE-2019-13288/

cd $HOME
mkdir fuzzing_xpdf && cd fuzzing_xpdf/

sudo apt install build-essential

 

wget https://dl.xpdfreader.com/old/xpdf-3.02.tar.gz
tar -xvzf xpdf-3.02.tar.gz

 

cd xpdf-3.02
sudo apt update && sudo apt install -y build-essential gcc
./configure --prefix="$HOME/fuzzing_xpdf/install/"
make
make install

 

예제 pdf 다운로드

cd $HOME/fuzzing_xpdf
mkdir pdf_examples && cd pdf_examples
wget https://github.com/mozilla/pdf.js-sample-files/raw/master/helloworld.pdf
wget https://www.melbpc.org.au/wp-content/uploads/2017/10/small-example-pdf-file.pdf

 

$HOME/fuzzing_xpdf/install/bin/pdfinfo -box -meta $HOME/fuzzing_xpdf/pdf_examples/helloworld.pdf

 

잘 실행되는 것을 확인 했으므로 AFL 컴파일러로 xpdf를 재컴파일 해준다.

 

rm -r $HOME/fuzzing_xpdf/install
cd $HOME/fuzzing_xpdf/xpdf-3.02/
make clean

 

export LLVM_CONFIG="llvm-config-11"
CC=$HOME/AFLplusplus/afl-clang-fast CXX=$HOME/AFLplusplus/afl-clang-fast++ 
./configure --prefix="$HOME/fuzzing_xpdf/install/"
make
make install

 

No instrumentation detected 오류

왜 오류가 뜨지..?

나중에 다시 해봐야겠다..

-> 바이너리 파일이 없어서 blackbox test로 진행해야한다.

그러므로 -Q 옵션을 추가

 

afl-qemu-trace 오류

그러므로 아래처럼 qemu-mode를 수동으로 설치해주었다.


그런데 이번에는 Ninja가 없다고 뜨기에, 아래처럼 빌드해주었다.

sudo apt-get update
sudo apt-get install -y ninja-build

 

 

이후 qemu-mode를 다시 빌드했다.

 

# 이동: AFL++ 소스 코드 디렉토리

cd /path/to/AFLplusplus/qemu_mode 

# QEMU 모드 설치
./build_qemu_support.sh
 
 
그러므로 아래처럼 qemu를 수동으로 설치해주었다.
 

# QEMU 소스 다운로드
cd qemu_mode
wget https://download.qemu.org/qemu-2.10.0.tar.xz
tar -xvf qemu-2.10.0.tar.xz
cd qemu-2.10.0

# QEMU 빌드
./configure --target-list=aarch64-linux-user --python=/usr/bin/python2
make -j$(nproc)

 

 

설치 이후 제대로 존재하는 것을 확인했다.

 

Crash 탐색

afl-fuzz -Q -i $HOME/fuzzing_xpdf/pdf_examples/ -o $HOME/fuzzing_xpdf/out/ -s 123 -t 1000 -- $HOME/fuzzing_xpdf/install/bin/pdftotext @@ $HOME/fuzzing_xpdf/output

위의 명령어를 통해 퍼징을 돌렸다.

해석하자면, 

밥먹고 돌아오니까 충돌을 찾은 듯 하다.

여기서 끊고 저장된 crash를 확인했다.

 

$HOME/fuzzing_xpdf/install/bin/pdftotext $HOME/fuzzing_xpdf/out/default
/crashes/id:000000,sig:11,src:001306,time:1764620,execs:306896,op:havoc,rep:1 $HOME/fuzzing_xpdf/output

위의 명령어로 실행해보니

이렇게 segmentation fault가 발생한 것을 알 수 있다.

 

추가로 여러 크래시 중 하나를 잡고 gdb를 통해 분석을 진행했다.

~/fuzzing_xpdf$ gdb --args ./install/bin/pdftotext ./out/default/crashes/id:000003,sig:11,src:000917,time:2176481,exe
cs:210295,op:havoc,rep:4

위의 파일은 787번 라인에서 크래시가 발생한 것을 확인할 수 있고, 이를 통해 Stream.cc 파일을 분석하면 되겠다.