'n Paar dae gelede het ek besluit om my router se firmware te reverse engineer met binwalk.
Ek het myself gekoop
Elke keer as ek 'n nuwe router koop, installeer ek
Nadat ek myself OpenWRT afgelaai het, het ek ook
Wat is binwalk?
Geskep in 2010 deur Craig Heffner, kan binwalk firmwarebeelde skandeer en lêers vind, lêerstelselbeelde identifiseer en onttrek, uitvoerbare kode, saamgeperste argiewe, selflaaiers en pitte, lêerformate soos JPEG en PDF, en nog baie meer.
Jy kan binwalk gebruik om die firmware te reverse engineer om te verstaan hoe dit werk. Soek kwesbaarhede in binêre lêers, onttrek lêers en soek agterdeure of digitale sertifikate. Kan ook gevind word opcodes
vir 'n klomp verskillende SVE's.
Jy kan lêerstelselbeelde uitpak om spesifieke wagwoordlêers (passwd, shadow, ens.) te soek en probeer om wagwoord-hashes te breek. Jy kan binêre analise tussen twee of meer lêers uitvoer. Jy kan data-entropie-analise uitvoer om saamgeperste data of geënkodeerde enkripsiesleutels te vind. Dit alles sonder om toegang tot die bronkode te hê.
Oor die algemeen is alles wat jy nodig het daar 🙂
Hoe werk binwalk?
Die belangrikste kenmerk van binwalk is sy handtekeningskandering. Binwalk kan die firmware-beeld skandeer vir verskeie ingeboude lêertipes en lêerstelsels.
Ken jy die command line nut 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
Span file
kyk na die lêeropskrif en soek die handtekening (magiese nommer) om die lêertipe te bepaal. Byvoorbeeld, as die lêer met die greepvolgorde begin 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
, sy weet dit is 'n PNG-lêer. Aan
Binwalk werk op dieselfde manier. Maar in plaas daarvan om net aan die begin van die lêer na handtekeninge te soek, sal binwalk die hele lêer skandeer. Daarbenewens kan binwalk lêers onttrek wat in die prent gevind word.
Tools file
и binwalk
gebruik die biblioteek libmagic
om lêerhandtekeninge te identifiseer. Maar binwalk
hou ook 'n lys van pasgemaakte magiese handtekeninge in stand om te soek na saamgeperste/gezipte lêers, firmware-opskrifte, Linux-kerne, selflaailaaiers, lêerstelsels, ensovoorts.
Laat ons pret hê?
Die installering van binwalk
Binwalk word ondersteun op verskeie platforms, insluitend Linux, OSX, FreeBSD en Windows.
Om die nuutste weergawe van binwalk te installeer, kan jy
Binwalk het baie verskillende opsies:
$ 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
Beeldskandering
Kom ons begin deur te soek na lêerhandtekeninge in die prent (prent van die webwerf
Hardloop binwalk met die --signature opsie:
$ 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
Nou het ons baie inligting oor hierdie beeld.
Beeld gebruike 0x5AC0
en 'n saamgeperste selflaaiprogram-beeld by 0x5B00
). Gebaseer op die uImage-kopskrif by 0x13270, weet ons dat die verwerker-argitektuur MIPS is en die Linux-kern weergawe 3.3.8 is. En gebaseer op die beeld gevind by 0x11CEA5
, ons kan dit sien rootfs
is 'n lêerstelsel squashfs
.
Kom ons onttrek nou die selflaaiprogram (U-Boot) met behulp van die opdrag 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
Aangesien die beeld saamgepers is met LZMA, moet ons dit dekomprimeer:
$ unlzma u-boot.bin.lzma
Ons het nou 'n U-Boot-beeld:
$ ls -l u-boot.bin
-rw-rw-r-- 1 sprado sprado 97476 Fev 5 08:48 u-boot.bin
Hoe gaan dit met die vind van die verstekwaarde vir 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 omgewingsveranderlike bootargs
gebruik om parameters na die Linux-kern deur te gee. En uit bogenoemde het ons 'n beter begrip van die flitsgeheue van die toestel.
Hoe gaan dit met die onttrekking van 'n Linux-kernbeeld?
$ 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
Ons kan verifieer dat die prent suksesvol onttrek is met behulp van die opdrag 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
Die uImage-lêerformaat is basies 'n Linux-kernbeeld met 'n bykomende kop. Kom ons verwyder hierdie kopskrif om die finale Linux-kernbeeld te kry:
$ 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
Die prent is saamgepers, so kom ons pak dit uit:
$ unlzma Image.lzma
Ons het nou 'n Linux-kernbeeld:
$ ls -la Image
-rw-rw-r-- 1 sprado sprado 3164228 Fev 5 10:51 Image
Wat kan ons met die kernbeeld doen? Ons kan byvoorbeeld deur die stringe in die prent soek en die weergawe van die Linux-kern vind en uitvind oor die omgewing wat gebruik word om die kern te bou:
$ 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
Selfs al is die firmware verlede jaar (2019) vrygestel, wanneer ek hierdie artikel skryf, gebruik dit 'n ou weergawe van die Linux-kern (3.3.8) wat in 2012 vrygestel is, saamgestel met 'n baie ou weergawe van GCC (4.6) ook sedert 2012!
Vertrou jy steeds jou routers by die kantoor en by die huis?
Met opsie --opcodes
ons kan ook binwalk gebruik om masjieninstruksies op te soek en die verwerker-argitektuur van die beeld te bepaal:
$ binwalk --opcodes Image
DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
2400 0x960 MIPS instructions, function epilogue
2572 0xA0C MIPS instructions, function epilogue
2828 0xB0C MIPS instructions, function epilogue
Wat van die wortellêerstelsel? In plaas daarvan om die prent met die hand te onttrek, kom ons gebruik die opsie binwalk --extract
:
$ binwalk --extract --quiet archer-c7.bin
Die volledige wortellêerstelsel sal na 'n subgids onttrek word:
$ 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)
---------------------------------------------------------------
Nou kan ons baie dinge doen.
Ons kan soek na konfigurasielêers, wagwoord-hashes, kriptografiese sleutels en digitale sertifikate. Ons kan binaries ontleed vir
Met
$ 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
Puik! Let egter daarop dat die BusyBox-weergawe 1.19.4 is. Dit is 'n baie ou weergawe van BusyBoxin April 2012 vrygestel.
TP-Link stel dus 'n firmware-beeld in 2019 vry met behulp van sagteware (GCC toolchain, kern, BusyBox, ens.) vanaf 2012!
Verstaan jy nou hoekom ek altyd OpenWRT op my routers installeer?
Dit is nie al nie
Binwalk kan ook entropie-analise uitvoer, rou entropiedata druk en entropie-plotte genereer. Tipies word meer entropie waargeneem wanneer die grepe in die beeld ewekansig is. Dit kan beteken dat die prent 'n geïnkripteer, saamgeperste of verduisterde lêer bevat. Hardcore enkripsiesleutel? Hoekom nie.
Ons kan ook die parameter gebruik --raw
om te soek vir 'n pasgemaakte volgorde van rou grepe in 'n prent of parameter --hexdump
om 'n hex dump uit te voer wat twee of meer invoerlêers vergelyk.
--magic
, of deur hulle by die gids te voeg $ HOME / .config / binwalk / magic
.
Jy kan meer inligting oor binwalk vind by
binwalk uitbreiding
Daar
import binwalk
binwalk.scan()
Met die hulp van die Python API kan jy ook skep
Bestaan ook
So hoekom laai jy nie die firmware-beeld van die internet af en probeer binwalk nie? Ek belowe jy sal baie pret hê 🙂
Bron: will.com