使用 binwalk 對家庭路由器進行逆向工程。 您信任您的路由器軟件嗎?

使用 binwalk 對家庭路由器進行逆向工程。 您信任您的路由器軟件嗎?

幾天前,我決定使用 binwalk 對路由器的韌體進行逆向工程。

我自己買的 TP-Link Archer C7 家用路由器。 不是最好的路由器,但足以滿足我的需求。

每次我買新路由器時,我都會安裝 OpenWRT的。 為了什麼? 一般來說,製造商不太關心支援他們的路由器,隨著時間的推移,軟體會變得過時,出現漏洞,等等,一般來說,你就明白了。 因此,我更喜歡OpenWRT固件,它得到了開源社群的良好支援。

下載了OpenWRT後,我也 下載了最新的韌體鏡像 從官方網站下我的新Archer C7並決定對其進行分析。 純粹為了好玩,來談談binwalk。

什麼是賓步行道?

賓沃克 是一個用於分析、逆向工程和韌體映像提取的開源工具。

binwalk 由 Craig Heffner 於 2010 年創建,可掃描韌體映像並尋找檔案、識別和提取檔案系統映像、可執行程式碼、壓縮檔案、引導程式和內核、JPEG 和 PDF 等檔案格式等等。

您可以使用 binwalk 對韌體進行逆向工程以了解其工作原理。 搜尋二進位檔案中的漏洞、提取檔案並尋找後門或數位憑證。 您還可以找到 opcodes 對於一堆不同的CPU。

您可以提取檔案系統映像來尋找特定的密碼檔案(passwd、shadow 等)並嘗試破解密碼雜湊。 您可以在兩個或多個檔案之間執行二進位解析。 您可以對資料執行熵分析以查找壓縮資料或編碼的加密金鑰。 所有這一切都無需訪問原始程式碼。

一般來說,您需要的一切都在那裡:)

Binwalk 是如何運作的?

binwalk的主要特點是它的簽名掃描。 Binwalk可以掃描韌體鏡像來搜尋各種內建檔案類型和檔案系統。

你知道命令列實用程式嗎 file?

file /bin/bash
/bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=12f73d7a8e226c663034529c8dd20efec22dde54, stripped

團隊 file查看文件頭並尋找簽名(幻數)以確定文件類型。 例如,如果檔案以位元組序列開頭 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A,它知道這是一個 PNG 檔案。 在 維基百科 有一個常見文件簽名的清單。

Binwalk 的工作原理相同。 但 binwalk 不會只在檔案開頭尋找簽名,而是會掃描整個檔案。 此外,binwalk 可以提取圖像中找到的檔案。

工具 file и binwalk 使用圖書館 libmagic 識別文件簽名。 但 binwalk 另外也支援自訂魔術簽章清單來搜尋壓縮檔案、韌體頭、Linux 核心、引導程式、檔案系統等。

我們來玩點樂子吧?

Binwalk安裝

Binwalk 支援多種平台,包括 Linux、OSX、FreeBSD 和 Windows。

若要安裝最新版本的 binwalk,可以 下載原始碼 並關注 安裝說明快速指南,可在項目網站上找到。

Binwalk 有許多不同的參數:

$ binwalk

Binwalk v2.2.0
Craig Heffner, ReFirmLabs
https://github.com/ReFirmLabs/binwalk

Usage: binwalk [OPTIONS] [FILE1] [FILE2] [FILE3] ...

Signature Scan Options:
    -B, --signature              Scan target file(s) for common file signatures
    -R, --raw=<str>              Scan target file(s) for the specified sequence of bytes
    -A, --opcodes                Scan target file(s) for common executable opcode signatures
    -m, --magic=<file>           Specify a custom magic file to use
    -b, --dumb                   Disable smart signature keywords
    -I, --invalid                Show results marked as invalid
    -x, --exclude=<str>          Exclude results that match <str>
    -y, --include=<str>          Only show results that match <str>

Extraction Options:
    -e, --extract                Automatically extract known file types
    -D, --dd=<type:ext:cmd>      Extract <type> signatures, give the files an extension of <ext>, and execute <cmd>
    -M, --matryoshka             Recursively scan extracted files
    -d, --depth=<int>            Limit matryoshka recursion depth (default: 8 levels deep)
    -C, --directory=<str>        Extract files/folders to a custom directory (default: current working directory)
    -j, --size=<int>             Limit the size of each extracted file
    -n, --count=<int>            Limit the number of extracted files
    -r, --rm                     Delete carved files after extraction
    -z, --carve                  Carve data from files, but don't execute extraction utilities
    -V, --subdirs                Extract into sub-directories named by the offset

Entropy Options:
    -E, --entropy                Calculate file entropy
    -F, --fast                   Use faster, but less detailed, entropy analysis
    -J, --save                   Save plot as a PNG
    -Q, --nlegend                Omit the legend from the entropy plot graph
    -N, --nplot                  Do not generate an entropy plot graph
    -H, --high=<float>           Set the rising edge entropy trigger threshold (default: 0.95)
    -L, --low=<float>            Set the falling edge entropy trigger threshold (default: 0.85)

Binary Diffing Options:
    -W, --hexdump                Perform a hexdump / diff of a file or files
    -G, --green                  Only show lines containing bytes that are the same among all files
    -i, --red                    Only show lines containing bytes that are different among all files
    -U, --blue                   Only show lines containing bytes that are different among some files
    -u, --similar                Only display lines that are the same between all files
    -w, --terse                  Diff all files, but only display a hex dump of the first file

Raw Compression Options:
    -X, --deflate                Scan for raw deflate compression streams
    -Z, --lzma                   Scan for raw LZMA compression streams
    -P, --partial                Perform a superficial, but faster, scan
    -S, --stop                   Stop after the first result

General Options:
    -l, --length=<int>           Number of bytes to scan
    -o, --offset=<int>           Start scan at this file offset
    -O, --base=<int>             Add a base address to all printed offsets
    -K, --block=<int>            Set file block size
    -g, --swap=<int>             Reverse every n bytes before scanning
    -f, --log=<file>             Log results to file
    -c, --csv                    Log results to file in CSV format
    -t, --term                   Format output to fit the terminal window
    -q, --quiet                  Suppress output to stdout
    -v, --verbose                Enable verbose output
    -h, --help                   Show help output
    -a, --finclude=<str>         Only scan files whose names match this regex
    -p, --fexclude=<str>         Do not scan files whose names match this regex
    -s, --status=<int>           Enable the status server on the specified port

影像掃描

讓我們先在圖像中搜尋文件簽名(圖片來自網站 TP-LINK).

使用 --signature 參數執行 binwalk:

$ binwalk --signature --term archer-c7.bin

DECIMAL       HEXADECIMAL     DESCRIPTION
------------------------------------------------------------------------------------------
21876         0x5574          U-Boot version string, "U-Boot 1.1.4-g4480d5f9-dirty (May
                              20 2019 - 18:45:16)"
21940         0x55B4          CRC32 polynomial table, big endian
23232         0x5AC0          uImage header, header size: 64 bytes, header CRC:
                              0x386C2BD5, created: 2019-05-20 10:45:17, image size:
                              41162 bytes, Data Address: 0x80010000, Entry Point:
                              0x80010000, data CRC: 0xC9CD1E38, OS: Linux, CPU: MIPS,
                              image type: Firmware Image, compression type: lzma, image
                              name: "u-boot image"
23296         0x5B00          LZMA compressed data, properties: 0x5D, dictionary size:
                              8388608 bytes, uncompressed size: 97476 bytes
64968         0xFDC8          XML document, version: "1.0"
78448         0x13270         uImage header, header size: 64 bytes, header CRC:
                              0x78A267FF, created: 2019-07-26 07:46:14, image size:
                              1088500 bytes, Data Address: 0x80060000, Entry Point:
                              0x80060000, data CRC: 0xBB9D4F94, OS: Linux, CPU: MIPS,
                              image type: Multi-File Image, compression type: lzma,
                              image name: "MIPS OpenWrt Linux-3.3.8"
78520         0x132B8         LZMA compressed data, properties: 0x6D, dictionary size:
                              8388608 bytes, uncompressed size: 3164228 bytes
1167013       0x11CEA5        Squashfs filesystem, little endian, version 4.0,
                              compression:xz, size: 14388306 bytes, 2541 inodes,
                              blocksize: 65536 bytes, created: 2019-07-26 07:51:38
15555328      0xED5B00        gzip compressed data, from Unix, last modified: 2019-07-26
                              07:51:41

現在我們有很多關於這張圖片的資訊。

圖片用途 潛艇 作為引導程式(圖像標題位於 0x5AC0 和壓縮的引導程式映像 0x5B00)。 根據0x13270處的uImage頭,我們知道處理器架構是MIPS,Linux核心版本是3.3.8。 並根據在該地址找到的圖像 0x11CEA5,我們可以看到 rootfs 是一個檔案系統 squashfs.

現在讓我們使用命令來提取引導程式(U-Boot) dd:

$ dd if=archer-c7.bin of=u-boot.bin.lzma bs=1 skip=23296 count=41162
41162+0 records in
41162+0 records out
41162 bytes (41 kB, 40 KiB) copied, 0,0939608 s, 438 kB/s

由於影像是使用LZMA壓縮的,因此我們需要對其進行解壓縮:

$ unlzma u-boot.bin.lzma

現在我們有了一個 U-Boot 映像:

$ ls -l u-boot.bin
-rw-rw-r-- 1 sprado sprado 97476 Fev  5 08:48 u-boot.bin

如何找到預設值 bootargs?

$ strings u-boot.bin | grep bootargs
bootargs
bootargs=console=ttyS0,115200 board=AP152 rootfstype=squashfs init=/etc/preinit mtdparts=spi0.0:128k(factory-uboot),192k(u-boot),64k(ART),1536k(uImage),14464k@0x1e0000(rootfs) mem=128M

U-Boot環境變數 bootargs 用於向 Linux 核心傳遞參數。 而從上面,我們對裝置的快閃記憶體有了更多的了解。

如何提取 Linux 核心映像?

$ dd if=archer-c7.bin of=uImage bs=1 skip=78448 count=1088572
1088572+0 records in
1088572+0 records out
1088572 bytes (1,1 MB, 1,0 MiB) copied, 1,68628 s, 646 kB/s

我們可以使用命令檢查圖像是否已成功提取 file:

$ file uImage
uImage: u-boot legacy uImage, MIPS OpenWrt Linux-3.3.8, Linux/MIPS, Multi-File Image (lzma), 1088500 bytes, Fri Jul 26 07:46:14 2019, Load Address: 0x80060000, Entry Point: 0x80060000, Header CRC: 0x78A267FF, Data CRC: 0xBB9D4F94

uImage 檔案格式基本上是帶有附加標頭的 Linux 核心映像。 讓我們刪除這個標頭以獲得最終的 Linux 核心映像:

$ dd if=uImage of=Image.lzma bs=1 skip=72
1088500+0 records in
1088500+0 records out
1088500 bytes (1,1 MB, 1,0 MiB) copied, 1,65603 s, 657 kB/s

圖片已經被壓縮了,我們來解壓縮一下:

$ unlzma Image.lzma

現在我們有了一個 Linux 核心鏡像:

$ ls -la Image
-rw-rw-r-- 1 sprado sprado 3164228 Fev  5 10:51 Image

我們可以用內核鏡像做什麼? 例如,我們可以在映像中進行字串搜索,找到 Linux 核心的版本,並了解用於建立核心的環境:

$ strings Image | grep "Linux version"
Linux version 3.3.8 (leo@leo-MS-7529) (gcc version 4.6.3 20120201 (prerelease) (Linaro GCC 4.6-2012.02) ) #1 Mon May 20 18:53:02 CST 2019

儘管該韌體是去年(2019 年)發布的,但在我撰寫本文時,它使用的是3.3.8 年發布的舊版本Linux 核心(2012),也是自4.6 年以來使用非常舊版本的GCC (2012 ) 進行編譯的!
(大約翻譯。您仍然信任辦公室和家裡的路由器嗎?)

有選項 --opcodes 我們也可以使用 binwalk 尋找機器指令並確定影像的處理器架構:

$ binwalk --opcodes Image
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
2400          0x960           MIPS instructions, function epilogue
2572          0xA0C           MIPS instructions, function epilogue
2828          0xB0C           MIPS instructions, function epilogue

那麼根檔案系統呢? 讓我們使用該選項,而不是手動提取圖像 binwalk --extract:

$ binwalk --extract --quiet archer-c7.bin

完整的根檔案系統將被提取到一個子目錄中:

$ cd _archer-c7.bin.extracted/squashfs-root/

$ ls
bin  dev  etc  lib  mnt  overlay  proc  rom  root  sbin  sys  tmp  usr  var  www

$ cat etc/banner
     MM           NM                    MMMMMMM          M       M
   $MMMMM        MMMMM                MMMMMMMMMMM      MMM     MMM
  MMMMMMMM     MM MMMMM.              MMMMM:MMMMMM:   MMMM   MMMMM
MMMM= MMMMMM  MMM   MMMM       MMMMM   MMMM  MMMMMM   MMMM  MMMMM'
MMMM=  MMMMM MMMM    MM       MMMMM    MMMM    MMMM   MMMMNMMMMM
MMMM=   MMMM  MMMMM          MMMMM     MMMM    MMMM   MMMMMMMM
MMMM=   MMMM   MMMMMM       MMMMM      MMMM    MMMM   MMMMMMMMM
MMMM=   MMMM     MMMMM,    NMMMMMMMM   MMMM    MMMM   MMMMMMMMMMM
MMMM=   MMMM      MMMMMM   MMMMMMMM    MMMM    MMMM   MMMM  MMMMMM
MMMM=   MMMM   MM    MMMM    MMMM      MMMM    MMMM   MMMM    MMMM
MMMM$ ,MMMMM  MMMMM  MMMM    MMM       MMMM   MMMMM   MMMM    MMMM
  MMMMMMM:      MMMMMMM     M         MMMMMMMMMMMM  MMMMMMM MMMMMMM
    MMMMMM       MMMMN     M           MMMMMMMMM      MMMM    MMMM
     MMMM          M                    MMMMMMM        M       M
       M
 ---------------------------------------------------------------
   For those about to rock... (%C, %R)
 ---------------------------------------------------------------

現在我們可以做很多不同的事情。

我們可以搜尋設定檔、密碼雜湊、加密金鑰和數位憑證。 我們可以分析二進位文件 故障排除 和漏洞。

e木 и chroot環境 我們甚至可以從映像運行(模擬)可執行檔:

$ ls
bin  dev  etc  lib  mnt  overlay  proc  rom  root  sbin  sys  tmp  usr  var  www

$ cp /usr/bin/qemu-mips-static .

$ sudo chroot . ./qemu-mips-static bin/busybox
BusyBox v1.19.4 (2019-05-20 18:13:49 CST) multi-call binary.
Copyright (C) 1998-2011 Erik Andersen, Rob Landley, Denys Vlasenko
and others. Licensed under GPLv2.
See source distribution for full notice.

Usage: busybox [function] [arguments]...
   or: busybox --list[-full]
   or: function [arguments]...

    BusyBox is a multi-call binary that combines many common Unix
    utilities into a single executable.  Most people will create a
    link to busybox for each function they wish to use and BusyBox
    will act like whatever it was invoked as.

Currently defined functions:
    [, [[, addgroup, adduser, arping, ash, awk, basename, cat, chgrp, chmod, chown, chroot, clear, cmp, cp, crond, crontab, cut, date, dd, delgroup, deluser, dirname, dmesg, echo, egrep, env, expr, false,
    fgrep, find, free, fsync, grep, gunzip, gzip, halt, head, hexdump, hostid, id, ifconfig, init, insmod, kill, killall, klogd, ln, lock, logger, ls, lsmod, mac_addr, md5sum, mkdir, mkfifo, mknod, mktemp,
    mount, mv, nice, passwd, pgrep, pidof, ping, ping6, pivot_root, poweroff, printf, ps, pwd, readlink, reboot, reset, rm, rmdir, rmmod, route, sed, seq, sh, sleep, sort, start-stop-daemon, strings,
    switch_root, sync, sysctl, tail, tar, tee, telnet, test, tftp, time, top, touch, tr, traceroute, true, udhcpc, umount, uname, uniq, uptime, vconfig, vi, watchdog, wc, wget, which, xargs, yes, zcat

偉大的! 但請注意,BusyBox 版本是 1.19.4。 這是 BusyBox 的一個非常舊的版本,於 2012 年 XNUMX 月發布。

因此,TP-Link 在 2019 年發布了使用 2012 年的軟體(GCC 工具鏈、核心、BusyBox 等)的韌體映像檔!

現在你明白為什麼我總是在路由器上安裝OpenWRT了嗎?

那不是全部

Binwalk還可以執行熵分析、列印原始熵數據並產生熵圖。 通常,當圖像中的位元組是隨機的時,會觀察到更大的熵。 這可能意味著圖像包含加密、壓縮或混淆的檔案。 硬核加密金鑰? 為什麼不。

使用 binwalk 對家庭路由器進行逆向工程。 您信任您的路由器軟件嗎?

我們也可以使用參數 --raw 在影像或參數中尋找自訂原始位元組序列 --hexdump 執行十六進位轉儲比較兩個或多個輸入檔。

自訂簽名 可以透過使用參數在命令列上指定的自訂簽名檔新增至 binwalk --magic,或將它們添加到目錄中 $ HOME / .config / binwalk / magic.

您可以在以下位置找到有關 binwalk 的更多資訊: 官方文檔.

binwalk 擴展

那裡 應用程式介面 binwalk,作為一個 Python 模組實現,任何 Python 腳本都可以使用它以程式設計方式執行 binwalk 掃描,而 binwalk 命令列實用程式幾乎可以透過兩行 Python 程式碼完全複製!

import binwalk
binwalk.scan()

使用 Python API 您也可以創建 Python插件 配置和擴展 binwalk。

還有一個 IDA插件 和雲端版本 Binwalk Pro.

那麼為什麼不從互聯網上下載韌體映像並嘗試 binwalk 呢? 我保證你會玩得很開心:)

來源: www.habr.com

添加評論