[Tutorial] MiWIFI Firmware Modification
The official firmware of Xiaomi routers is in a custom format, and the RSA signature must be verified when flashing, so it is almost impossible to flash the modified Firmware
Through the analysis of the firmware tool mkxqimage, the process of packaging and unpacking the firmware is basically clarified. If you use your own RSA private key to package the firmware, and then replace /usr/share/xiaoqiang/public.pem with your own RSA public key, then It is possible to flash self-made firmware through the web interface.
I. Unpack Firmware
The firmware tool mkxqimage completes the unpacking of the firmware. Before unpacking, check whether the checksum is correct, and then use the RSA public key /usr/share/xiaoqiang/public.pem to check the RSA signature. After these two steps pass, according to [0x0C], the firmware type and the 4 offsets of [0x10], [0x14], [0x18] and [0x1C] split the firmware. Items that may be included in the firmware include:
brcm4709_fac_update_nor.binbrcm4709_nor.binfac_mode.binnvram_fac.binramfszroot.ext4.lzmaroot.squashfsupsetting.shupsetting_fac1.shupsetting_fac2.shupsetting_fac3.shvmlinuz.trx
II. Firmware Packaging
First, create a pair of RSA private key and public key.
The following takes the stable version 0.4.85 as an example to introduce the firmware modification and packaging method.
- Unpack the firmware
cd /userdisk/datamkdir xqimagecd xqimagemkxqimage -x brcm4709_hdr_039ef_0.4.85.bin
Got 2 files:
ls-rw-r--r-- 1 root root 16646144 May 30 08:14 brcm4709_nor.bin-rw-r--r-- 1 root root 34853955 May 30 08:14 root.ext4.lzma
- Firmware modification
- for EXT4 img:
mount -o loop -t ext4 root.ext4 /mnt - for Squashfs:
unsquashfs rootfs.squashfs
- for EXT4 img:
Next, replace the RSA public key first, and then open SSH
cd /mnt/usr/share/xiaoqiangmv public.pem public.pem.origcp /userdisk/data/xqimage/public.pem public.pem.newln -s public.pem.new public.pemsed -i 's/ssh_en=0/ssh_en=1/g' xiaoqiang-defaults.txt
Restore some shut down web interfaces
cd /mnt/usr/lib/lua/luci/controller/websed -i 's/--entry/entry/g' index.lua
Modify by your own…
Save the above modification back to the hard disk firmware image file:
cd /userdisk/data/xqimageumount /mntsleep 30# Be sure to wait a while before compressing the image
- Firmware packaging
Compress the modified hard disk image:
EXT4 img: ./lzma e -a0 root.ext4 root.ext4.lzma
SquashFS: mksquashfs path/to/squashfs_rootfs_folder ... newrootfs.squashfs
./mkxqimage -o brcm4709_hdr_00000_0.4.85.bin -p ./private.pem -t 5 -f brcm4709_nor.bin -f root.ext4.lzma
III. Upgrade Preparation
- Public key replacement
Before upgrading the self-made firmware generated in the previous step, you need to replace the RSA public key of the current router. The specific method is as follows:
mount -o rw,remount /cd /usr/share/xiaoqiangmv public.pem public.pem.origcp /userdisk/data/xqimage/public.pem public.pem.newln -s public.pem.new public.pemmount -o ro,remount /
- Homemade firmware test
Test with the following command:
cd /userdisk/data/xqimagemkdir testcd testmkxqimage -x ../brcm4709_hdr_00000_0.4.85.binls -l
If there is no error message in the process of decompressing the firmware, and 2 files are listed at the end, it means that the self-made firmware is packaged without any problem.
-rw-r--r-- 1 root root 16646144 Jun 1 12:00 brcm4709_nor.bin-rw-r--r-- 1 root root 33188560 Jun 1 12:00 root.ext4.lzma
Note: The size of the brcm4709_nor.bin file must be 16646144.
Verify that root.ext4.lzma is correct:
cd /userdisk/data/xqimage/testunlzma root.ext4.lzmamount -o loop -t ext4 root.ext4 /mntls -l /mnt/usr/share/xiaoqiang/public.pem*grep ssh_en /mnt/usr/share/xiaoqiang/xiaoqiang-defaults.txtumount /mnt
After the above command is executed, you should be able to see some information (the time information will be different):
lrwxrwxrwx 1 root root 14 Jun 1 11:30 /mnt/usr/share/xiaoqiang/public.pem -> public.pem.new -rw-r--r-- 1 root root 451 Jun 1 11:30 /mnt/ usr/share/xiaoqiang/public.pem.new -rwxr-xr-x 1 root root 451 May 27 11:33 /mnt/usr/share/xiaoqiang/public.pem.orig ssh_en=1
Backup homebrew firmware
Enter in the computer resource manager: \192.168.31.1\XiaoMi\xqimage , you can see brcm4709_hdr_00000_0.4.85 in the directory list, and copy the self-made firmware to the computer.
IV. Upgrade Homemade Firmware
Log in to the Xiaomi router management page, select brcm4709_hdr_00000_0.4.85 in the Routing Settings - Advanced Features - Manual Router Upgrade interface, click the Upload and Install Firmware button, and wait for a few minutes.
After installing the firmware, SSH may not start. This is because the nvram item ssh_en is cleared to 0 when the firmware is upgraded, and the action of setting ssh_en to 1 through the defaults setting at the first startup is after the dropbear is started. Just restart the router and you can log in with SSH.
- Special reminder
Due to the replacement of the RSA public key in the router, the official firmware of the router cannot be upgraded. If you want to restore the official firmware, you only need to replace the original public key. The method is as follows:
mount -o rw,remount / cd /usr/share/xiaoqiang cp public.pem.orig public.pem mount -o ro,remount /
Source: https://www.openwrt.pro/post-415.html
Bình Luận & Thảo Luận
Góp ý, hỏi đáp và chia sẻ suy nghĩ của bạn