본문 바로가기

카테고리 없음

QEMU User emulation을 사용해서 ipTIME 공유기 테스트 환경 구성

Ubuntu 16.04 버전을 기준으로 작성되었습니다.


필요한 패키지 설치

$ sudo apt-get install bridge-utils qemu-user-static binwalk 


ipTIME 공식 사이트에서 펌웨어 다운로드

http://iptime.com/iptime/?page_id=126


binwalk를 사용해서 펌웨어에서 파일시스템 확인

$ binwalk a6004nm_kr_10_068.bin


DECIMAL       HEXADECIMAL     DESCRIPTION

--------------------------------------------------------------------------------

0             0x0             uImage header, header size: 64 bytes, header CRC: 0x6017745B, created: 2018-07-24 03:12:15, image size: 16066999 bytes, Data Address: 0x81001000, Entry Point: 0x81608270, data CRC: 0x37DE26E3, OS: Linux, CPU: MIPS, image type: OS Kernel Image, compression type: lzma, image name: "a6004nm"

64            0x40            LZMA compressed data, properties: 0x5D, dictionary size: 33554432 bytes, uncompressed size: 9288256 bytes

3574263       0x3689F7        Squashfs filesystem, little endian, version 4.0, compression:xz, size: 12491892 bytes, 1717 inodes, blocksize: 131072 bytes, created: 2018-07-24 03:12:10 


dd를 이용해서 펌웨어에서 파일시스템 추출

$ dd if=a6004nm_kr_10_068.bin of=rootfs.squash bs=1 skip=3574263

12492800+0 records in

12492800+0 records out

12492800 bytes (12 MB) copied, 14.1107 s, 885 kB/s


unsquashfs를 이용해서 파일시스템에서 파일 추출

$ unsquashfs rootfs.squash

$ cd squashfs-root


필요한 파일과 파일시스템 준비

cp /usr/bin/qemu-mipsel-static ./usr/bin

sudo mount -t devtmpfs dev dev

sudo mount -t proc none proc 

sudo mount -t devpts none "./dev/pts" -o ptmxmode=0666,newinstance

sudo ln -fs "pts/ptmx" "./dev/ptmx"


sudo mount -t ramfs none tmp


sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /tmp/var
sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /tmp/etc
sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /var/lock
sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /var/log
sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /var/run
sudo chroot . /usr/bin/qemu-mipsel-static /bin/mkdir /var/tmp

sudo chroot . /usr/bin/qemu-mipsel-static /bin/cp /default/var/boa_vh.conf /var
sudo chroot . /usr/bin/qemu-mipsel-static /bin/cp /default/var/firewall_rule /var
sudo chroot . /usr/bin/qemu-mipsel-static /bin/cp /default/var/virtsvr_rule /var
sudo chroot . /usr/bin/qemu-mipsel-static /bin/cp /default/var/checkup /var

sudo cp -r default/var/run/* ./tmp/var/run/
sudo cp -r ./default/etc/* ./tmp/etc

sudo cp /etc/resolv.conf ./tmp/etc/ 

브릿지 인터페이스 생성

sudo brctl addbr br0
sudo ifconfig br0 up

sudo ifconfig br0 192.168.0.1 netmask 255.255.255.0 


/tmp/var/boa_vh.conf 파일 변경, init_cgi[/lib/libcgi.so] 함수에서 하는 것

Port 80
User root
Group root
ServerAdmin root@localhost
VirtualHost
DocumentRoot /home/httpd
UserDir public_html
DirectoryIndex index.html
KeepAliveMax 100
KeepAliveTimeout 10
MimeTypes /etc/mime.types
DefaultType text/plain
AddType application/x-httpd-cgi cgi
AddType text/html html
ScriptAlias /sess-bin/ /cgibin/
ScriptAlias /cgi-bin/ /cgibin/
ScriptAlias /nd-bin/ /ndbin/
ScriptAlias /login/ /cgibin/login-cgi/
ScriptAlias /ddns/ /cgibin/ddns/
ServerName ""
SinglePostLimit 4194304
Auth /cgi-bin /etc/httpd.passwd

Auth /main /etc/httpd.passwd 


환경을 구성했기 때문에 웹 데몬이나 CGI 기능을 테스트할 수 있습니다.


httpd 데몬 실행

sudo chroot . /usr/bin/qemu-mipsel-static /sbin/httpd 


CGI 실행 및 Remote GDB로 디버깅하기

// QEMU User Emulator에는 gdbserver와 strace 기능이 내장되어 있습니다.

// -g 옵션을 사용하면 gdbserver처럼 동작해서 GDB가 붙을 수 있도록 지정된 포트를 오픈합니다.

// 해당 포트로 IDA의 "Remote GDB"나 gdb의 "target remote"로 접속해서 원격 디버깅을 수행할 수 있습니다.


// GET 요청
sudo chroot . /usr/bin/qemu-mipsel-static -E QUERY_STRING="[CGI_PARAMETERS]" -strace -g 1234 [CGI_PATH]


// POST 요청

echo "[CGI_PARAMETERS]" | chroot . /usr/bin/qemu-mipsel-static -E CONTENT_LENGTH=[LENGTH_OF_CGI_PARAMETERS] -strace -g 1234 [CGI_PATH]