[Firm-AFL] FirmAFL 환경 셋팅 및 오류 해결
IoT 허브 펌웨어를 분석하던 중 Firm-AFL에 대해 알게되어 이와 관련한 환경 셋팅을 하는 과정을 기록해보려 한다
1. 환경
설치 환경은 Ubuntu 18.04이다
2. FirmAFL & firmadyne git clone 받기
github에 FirmAFL을 검색하게 되면 크게 3명의 repository가 나오는데 별이 가장 많은 사람을 기준으로 다운로드했다
GitHub - zyw-200/FirmAFL: FIRM-AFL is the first high-throughput greybox fuzzer for IoT firmware.
FIRM-AFL is the first high-throughput greybox fuzzer for IoT firmware. - GitHub - zyw-200/FirmAFL: FIRM-AFL is the first high-throughput greybox fuzzer for IoT firmware.
github.com
또한 해당 github를 참고하여 fuzzer를 실행하기 위해서는 하단의 firmadyne 레포지토리를 FirmAFL 디렉토리의 가장 상단에 추가해주어야 한다
GitHub - firmadyne/firmadyne: Platform for emulation and dynamic analysis of Linux-based firmware
Platform for emulation and dynamic analysis of Linux-based firmware - GitHub - firmadyne/firmadyne: Platform for emulation and dynamic analysis of Linux-based firmware
github.com
경로 확인을 통해 알 수 있듯이 FirmAFL 아래 firmadyne를 clone 받아준다
3. firmadyne Setup & Error
1) dependencies 설정을 위한 설치
$ sudo apt-get install busybox-static fakeroot git dmsetup kpartx netcat-openbsd nmap python-psycopg2 python3-psycopg2 snmp uml-utilities util-linux vlan
2) binwalk 설치를 위한 python default 변경
defalut 버전 변경을 위해서는 설치된 파이썬들을 alternative로 설정해주어야 하기 때문에 다음과 같이 버전을 확인한다
다음 명령어를 통해 설치된 파이썬을 alternative로 설정 한 후 --config python 옵션을 통해 python의 default 버전을 변경해준다
이때 python 3.6에 해당하는 Status 번호를 선택해주면 된다
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.6 2 auto mode
1 /usr/bin/python2.7 1 manual mode
2 /usr/bin/python3.6 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
3) 펌웨어 추출을 위한 binwalk 및 firmadyne 설정 시 필요한 명령어 설치
$ git clone https://github.com/ReFirmLabs/binwalk.git
$ cd binwalk
$ sudo ./deps.sh
$ sudo python ./setup.py install
$ sudo -H pip install git+https://github.com/ahupp/python-magic
$ sudo -H pip install git+https://github.com/sviehb/jefferson
dep.sh 실행 시 /usr/bin/python3: No module named pip 와 같은 오류가 발생한다면 다음 명령어를 통해 pip를 설치해 준 후 다시 dep.sh를 실행하면 된다
$ sudo apt-get install python3-pip
이후 dep.sh 실행 중 다음과 같은 오류가 발생한다면 추가적으로 pillow를 설치해주면 된다
The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.
Please see the install instructions at:
https://pillow.readthedocs.io/en/latest/installation.html
언급되는 사이트를 들어가보면 다음과 같은 명령어를 볼 수 있으며 언급되는 대로 설치해준다
해당 명령어로 upgrade를 모두 진행하면 이후에는 dep.sh에서 에러가 발생하지 않으며 잘 설치되는 것을 확인할 수 있다
$ python3 -m pip install --upgrade pip
$ python3 -m pip install --upgrade Pillow
4) Database 설정
두 번째 명령어를 실행할 때, password를 firmadyne으로 설정한다
$ sudo apt-get install postgresql
$ sudo -u postgres createuser -P firmadyne (password: firmadyne)
$ sudo -u postgres createdb -O firmadyne firmware
$ sudo -u postgres psql -d firmware < ./firmadyne/database/schema
5) binary
모든 구성요소에 대해 사전 빌드된 바이너리를 다운로드하기 위해 다음 스크립트를 실행한다
$ cd ./firmadyne; ./download.sh
6) QEMU
배포판에서 제공하는 QEMU를 사용하기 위해서는 아래 명령어를 통해 설치할 수 있다
$ sudo apt-get install qemu-system-arm qemu-system-mips qemu-system-x86 qemu-utils
4. FirmAFL Setup & Error
FirmAFL 구동을 위해서는 두가지 부분(user mode & system mode)에 대한 설정이 필요하다
1) user mode
./configure 설정을 위해서는 아까 변경했던 python 버전을 다시 2.7로 변경해주어야한다
# pwd = ~/Desktop/FirmAFL/
$ cd user_mode/
$ ./configure --target-list=mipsel-linux-user,mips-linux-user,arm-linux-user --static --disable-werror
$ make
usermode - ./configure ~ 실행 시
① "ERROR: Cannot use 'python', Python 2.6 or later is required. Note that Python 3 or later is not yet supported. Use --python=/path/to/python to specify a supported Python."
해당 에러 메시지가 나타난다면 처음에 3.7로 버전을 수정해주었던 alternatives를 다시 python2.7로 변경해준다
② "ERROR: pkg-config binary 'pkg-config' not found"
해당 에러 메시지가 나타난다면 다음 명령어를 통해 dependency를 추가한다
$ sudo apt-get install pkg-config
③ "ERROR: glib-2.22 gthread-2.0 is required to compile QEMU"
해당 에러 메시지가 나타난다면 다음 명령어를 통해 또 한번 dependency를 추가한다
$ sudo apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev libpython-dev python-pip python-capstone virtualenv
user mode - make 시
① "static declaration of 'memfd_create' follow non-static declartion"
에러 메시지는 구체적으로 다음과 같다
util/memfd.c:40:12: error: static declaration of ‘memfd_create’ follows non-static declaration
static int memfd_create(const char *name, unsigned int flags)
^~~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/bits/mman-linux.h:115:0,
from /usr/include/x86_64-linux-gnu/bits/mman.h:45,
from /usr/include/x86_64-linux-gnu/sys/mman.h:41,
from /home/username/FirmAFL-master/user_mode/include/sysemu/os-posix.h:29,
from /home/username/FirmAFL-master/user_mode/include/qemu/osdep.h:104,
from util/memfd.c:28:
/usr/include/x86_64-linux-gnu/bits/mman-shared.h:46:5: note: previous declaration of ‘memfd_create’ was here
int memfd_create (const char *__name, unsigned int __flags) __THROW;
해당 문제를 해결하기 위해서는 총 2가지의 파일을 수정해주어야 한다
▶ 첫번째 파일 경로: 사용자가 지정한 경로/FirmAFL/user_mode/util/memfd.c
해당 파일 내용에서 아래 코드와 같이 앞에 '-'가 붙은 3줄을 지우고 '+'가 붙은 주석 한 줄을 추가한다
여기서 가장 중요한 것은 #include <sys/memfd.h>를 지우는 것이다
#include "qemu/memfd.h"
- #ifdef CONFIG_MEMFD
- #include <sys/memfd.h>
- #elif defined CONFIG_LINUX
+ #if defined CONFIG_LINUX && !defined CONFIG_MEMFD
#include <sys/syscall.h>
#include <ams/unistd.h>
▶ 두번째 파일 경로: 사용자가 지정한 경로/FirmAFL/user_mode/configure
해당 파일의 분량은 많으므로 vim에서 문자열 검색을 통해 "memfd"을 찾은 후 sys/memfd.h을 sys/mman.h로 변경해준다
# check if memfd is supported
memfd=no
cat > $TMPC << EOF
- #include <sys/memfd.h>
+ #include <sys/mman.h>
2) system mode
$ cd qemu_mode/DECAF_qemu_2.10/
$ ./configure --target-list=mipsel-softmmu,mips-softmmu,arm-softmmu --disable-werror
$ make
위의 과정이 에러 없이 모두 잘 컴파일 되었다면 상관없겠지만 나는 에러로 해당 코드를 수정해주어야 했다
system mode - make 시
① "static declaration of 'memfd_create' follow non-static declartion"
해당 에러는 1)에서 발생한 에러와 동일하기 때문에 설정 파일의 경로만 언급하고 넘어가도록 하겠다
▶ 첫번째 파일 경로: 사용자가 지정한 경로/FirmAFL/FirmAFL/qemu_mode/DECAF_qemu_2.10/util/memfd.c
▶ 두번째 파일 경로: 사용자가 지정한 경로/FirmAFL/FirmAFL/qemu_mode/DECAF_qemu_2.10/configure
두 가지 파일의 내용을 1)에서 수정한 내용과 동일하게 update 하면 오류가 해결된다
다음과 같은 설치 과정를 통해 Firm-AFL 환경 setting을 완료해 보았으므로
다음에는 실제 펌웨어를 대상으로 fuzzer를 구동시켜보도록 하겠다 :)
'Hacking Tech > Fuzzer' 카테고리의 다른 글
[AFL fuzzer] AFL fuzzer 원리 (0) | 2022.11.08 |
---|---|
[Fuzzer] What the Fuzz (0) | 2022.11.08 |
[firmadyne/Firm-AFL] firmadyne & FirmAFL 사용을 위한 지식 (0) | 2022.11.07 |