Cài OpenWrt trên Xiaomi Mi Router 4 (R4)

Bài viết này hướng dẫn cách flash firmware OpenWrt lên Xiaomi Mi Router 4 (R4) hoàn toàn qua mạng, sử dụng công cụ OpenWRTInvasion — không cần UART, không cần mở vỏ router.

Yêu cầu
- Router đang chạy firmware gốc Xiaomi phiên bản 2.18.62 hoặc 2.26.175
- Máy tính Windows có Ubuntu WSL (hoặc Linux/macOS)
- Router cắm dây WAN ra internet, PC cắm vào cổng LAN của router
Bước 1 — Chuẩn bị Ubuntu WSL
Mở Ubuntu WSL (tìm trong Start Menu → gõ Ubuntu). Dấu nhắc lệnh phải là username@máy:~$ chứ không phải PowerShell.
cd ~
git clone https://github.com/acecilia/OpenWRTInvasion.git
cd OpenWRTInvasion
pip3 install -r requirements.txt --break-system-packages
⚠️ Nếu gặp lỗi Permission denied khi chạy script sau này, chạy lệnh sau để fix quyền:
sudo chown -R $USER:$USER ~/OpenWRTInvasion
Bước 2 — Lấy STOK từ router
Đăng nhập vào http://192.168.31.1 bằng trình duyệt trên máy tính đang kết nối LAN với router. Nhìn lên thanh địa chỉ, URL sẽ có dạng:
http://192.168.31.1/cgi-bin/luci/;stok=XXXXXXXXXXXXXXXX/web/home
Copy phần XXXXXXXXXXXXXXXX — đó là STOK code.
⚠️ STOK được tạo riêng cho từng máy tính. Nếu reboot router hoặc đổi máy, STOK sẽ thay đổi và cần lấy lại.
Bước 3 — Chạy exploit OpenWRTInvasion
Trong Ubuntu WSL, trong thư mục OpenWRTInvasion:
python3 remote_command_execution_vulnerability.py
Script sẽ hỏi:
- Router IP: nhấn Enter để dùng mặc định
192.168.31.1 - stok: dán STOK code vừa lấy ở bước 2
Nếu thành công, script sẽ in ra:
done! Now you can connect to the router using several options: (user: root, password: root)
* telnet 192.168.31.1
* ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -c 3des-cbc -o UserKnownHostsFile=/dev/null [email protected]
Bước 4 — Kết nối vào shell router
Cài telnet và kết nối:
sudo apt install inetutils-telnet -y
telnet 192.168.31.1
Đăng nhập: Login: root — Password: root
Dấu nhắc lệnh hiện root@XiaoQiang:~# là thành công ✅
Bước 5 — Tải và flash firmware OpenWrt
Mi Router 4 dùng NAND flash nên cần flash 2 partition riêng biệt: kernel và rootfs.
cd /tmp
# Tải kernel (dùng HTTP vì router cũ không hỗ trợ TLS mới, thêm -4 để tránh lỗi IPv6)
curl -4 -o kernel1.bin http://downloads.openwrt.org/releases/23.05.5/targets/ramips/mt7621/openwrt-23.05.5-ramips-mt7621-xiaomi_mi-router-4-squashfs-kernel1.bin
# Tải rootfs
curl -4 -o rootfs0.bin http://downloads.openwrt.org/releases/23.05.5/targets/ramips/mt7621/openwrt-23.05.5-ramips-mt7621-xiaomi_mi-router-4-squashfs-rootfs0.bin
# Flash kernel
mtd write kernel1.bin kernel1
# Flash rootfs
mtd write rootfs0.bin rootfs0
# Reboot
reboot
⚠️ Không được mất điện router trong lúc flash! Chờ từng lệnh
mtd writechạy xong mới chạy lệnh tiếp theo.
Bước 6 — Đăng nhập OpenWrt
Chờ router khởi động lại khoảng 1-2 phút. Truy cập http://192.168.1.1 trên trình duyệt.
- Username:
root - Password: để trống, nhấn Log in
Giao diện LuCI OpenWrt hiện ra là hoàn tất! 🎉
Các lỗi thường gặp và cách fix
| Lỗi | Nguyên nhân | Cách fix |
|---|---|---|
Permission denied: 'payload.tar.gz' | Thư mục build bị lỗi quyền từ lần chạy trước | sudo rm -rf build rồi chạy lại script |
Permission denied: 'build' | Toàn bộ thư mục bị sai quyền | sudo chown -R $USER:$USER ~/OpenWRTInvasion |
ssl_handshake returned | Router không hỗ trợ TLS mới | Dùng http:// thay vì https:// |
Failed to connect: Invalid argument | Lỗi IPv6 | Thêm -4 vào lệnh curl |
Connection refused khi SSH | SSH server chưa khởi động | Dùng telnet thay thế |
Lưu ý sau khi cài xong
- Đặt mật khẩu ngay tại System → Administration
- Nếu muốn đổi dải IP LAN: vào Network → Interfaces → LAN → Edit, đổi IPv4 address rồi nhấn Save & Apply
- Sau khi đổi IP LAN, cần đổi IP tĩnh máy tính hoặc xin lại DHCP để vào lại giao diện quản trị




