Lately I have been analyzing some embedded devices such as routers, and want to share how to emulate, extract, and modify firmware.
We may want to do this for several reasons, such as enabling SSH/telnet to debug a service remotely, adding new features and looking for security vulnerabilities.
If you want to dump the firmware from a physical device, you will want to use something such as the CH341A, but it won’t be covered here.
Prerequisites
We will need some tools to analyze and emulate firmware, but thankfully the nice folks at Attify have made a specialized VM for analyzing embedded devices.
It comes with Firmware Analysis Toolkit which we will use to emulate the Netgear WNAP320 firmware, since it works well, and this way buying the hardware is not required to follow along.
Now we want to run the firmware, and for that we will use Firmware Analysis Toolkit. From their GitHub:
Firmware Analysis Toolkit (FAT henceforth) is based on Firmadyne with some changes. Firmadyne uses a PostgreSQL database to store information about the emulated images. However just for the core functionality i.e. emulating firmware, PostgreSQL is not really needed. Hence FAT doesn’t use it.
[email protected]:~/WNAP320/firmware$ cd ~/tools/firmware-analysis-toolkit/
[email protected]:~/tools/firmware-analysis-toolkit$ ./fat.py ~/WNAP320/WNAP320_V3.7.11.4.zip
__ _
/ _|||||_ __ _ ||_
| _| / _`|| __||||(_||||_
|_|\__,_|\__| Welcome to the Firmware Analysis Toolkit - v0.3
Offensive IoT Exploitation Training http://bit.do/offensiveiotexploitation
By Attify - https://attify.com | @attifyme
[+] Firmware: WNAP320_V3.7.11.4.zip
[+] Extracting the firmware...
[+] Image ID: 1[+] Identifying architecture...
[+] Architecture: mipseb
[+] Building QEMU disk image...
[+] Setting up the network connection, please standby...
[+] Network interfaces: [('brtrunk', '192.168.0.100')][+] All set! Press ENTER to run the firmware...
[+] When running, press Ctrl + A X to terminate qemu
Press ENTER and wait a bit, then visit 192.168.0.100 on your browser to make sure everything worked.
Analyzing the firmware
For this firmware, it seems the interesting stuff are inside a tar archive. It’s usually not that simple.
Great, it is simply a squashfs filesystem. This is a compressed read-only filesystem often used in embedded systems. We will extract it later.
1
2
3
4
5
6
[email protected]:~/WNAP320/firmware$ binwalk vmlinux.gz.uImage
DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
0 0x0 uImage header, header size: 64 bytes, header CRC: 0x8DD2855, created: 2018-10-25 08:11:29, image size: 983040 bytes, Data Address: 0x80020000, Entry Point: 0x8020E000, data CRC: 0x3663CBB2, OS: Linux, CPU: MIPS, image type: OS Kernel Image, compression type: gzip, image name: "Linux Kernel"64 0x40 gzip compressed data, has original file name: "vmlinux.bin", from Unix, last modified: 2018-10-25 08:11:29
It seems this firmware uses U-Boot as a boot loader, which is very common in embedded devices. We also have the Linux kernel there. We also can see it’s meant to run on MIPS devices. Most embedded devices run either on ARM or MIPS architectures, as they are more energy efficient.
Extracting squashfs
This is a very easy task, we can either use unsquashfs or binwalk to extract it:
[email protected]:/home/iot/WNAP320/firmware# ssh [email protected]The authenticity of host '192.168.0.100 (192.168.0.100)' can't be established.
RSA key fingerprint is SHA256:tK8/SfB/lQb+8MCX32XWyrTTYjiEHZZ6cAGs+FcO0Ug.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.100' (RSA) to the list of known hosts.
[email protected]'s password:
[[email protected] /root]#
Using chroot
I want to show another way of doing the same thing, by making use of QEMU to chroot onto the firmware filesystem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[email protected]:/home/iot/WNAP320/firmware/squashfs-root# cp /usr/bin/qemu-mips-static usr/bin
[email protected]:/home/iot/WNAP320/firmware/squashfs-root# chroot . /bin/sh
/ # uname -aLinux attifyos 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020 mips unknown/ # adduser -G root -s /bin/sh -h /root user adduser: /root: File exists
Changing password for user
New password:
Retype password:
Password for user changed by root
/ # cat /etc/passwd | grep useruser:x:1001:1001:Linux User,,,:/root:/bin/sh
/ # sed -i 's/1001/0/g' /etc/passwd/ # cat /etc/passwd | grep useruser:x:0:0:Linux User,,,:/root:/bin/sh
/ # su user/ # whoamiroot
/ #
Wrapping up
Not every manufacturer will publish their firmware online, and in those cases you may have to dump it from the device. Other manufacturers will sometimes encrypt their firmware.
Even if you can download the firmware and it’s not encrypted, it’s not always a simple tar archive: in those cases, binwalk is your friend.
And of course, always backup the firmware of devices you are going to modify.