Yiyipada ẹrọ olulana ile nipa lilo binwalk. Ṣe o gbẹkẹle sọfitiwia olulana rẹ?

Yiyipada ẹrọ olulana ile nipa lilo binwalk. Ṣe o gbẹkẹle sọfitiwia olulana rẹ?

Ni ọjọ diẹ sẹhin, Mo pinnu lati yi ẹnjinia ẹrọ famuwia olulana mi nipa lilo binwalk.

Mo ra ara mi TP-Link Archer C7 olulana ile. Kii ṣe olulana ti o dara julọ, ṣugbọn o to fun awọn aini mi.

Ni gbogbo igba ti Mo ra olulana tuntun, Mo fi sori ẹrọ OpenWRT. Fun kini? Gẹgẹbi ofin, awọn aṣelọpọ ko bikita pupọ nipa atilẹyin awọn olulana wọn ati ni akoko pupọ sọfitiwia di igba atijọ, awọn ailagbara han, ati bẹbẹ lọ, ni gbogbogbo, o gba imọran naa. Nitorinaa, Mo fẹran famuwia OpenWRT, eyiti o ni atilẹyin daradara nipasẹ agbegbe orisun-ìmọ.

Lẹhin ti ṣe igbasilẹ OpenWRT, Mo tun gbaa lati ayelujara titun famuwia image labẹ Archer C7 tuntun mi lati oju opo wẹẹbu osise ati pinnu lati ṣe itupalẹ rẹ. Nikan fun igbadun ati sọrọ nipa binwalk.

Kini binwalk?

Binwalk jẹ ohun elo orisun ṣiṣi fun itupalẹ, imọ-ẹrọ yiyipada ati isediwon aworan famuwia.

Ti a ṣẹda ni ọdun 2010 nipasẹ Craig Heffner, binwalk le ṣe ọlọjẹ awọn aworan famuwia ati wa awọn faili, ṣe idanimọ ati jade awọn aworan eto faili, koodu ti o ṣiṣẹ, awọn ile-ipamọ fisinuirindigbindigbin, awọn bata bata ati awọn kernels, awọn ọna kika faili bii JPEG ati PDF, ati pupọ diẹ sii.

O le lo binwalk lati yi ẹlẹrọ pada famuwia lati ni oye bi o ṣe n ṣiṣẹ. Ṣewadii awọn faili alakomeji fun awọn ailagbara, jade awọn faili, ki o wa awọn ẹhin tabi awọn iwe-ẹri oni-nọmba. O tun le wa opcodes fun opo ti o yatọ si CPUs.

O le jade awọn aworan eto faili lati wa awọn faili ọrọ igbaniwọle kan pato (passwd, ojiji, ati bẹbẹ lọ) ati gbiyanju lati fọ awọn hashes ọrọ igbaniwọle. O le ṣe itupalẹ alakomeji laarin awọn faili meji tabi diẹ sii. O le ṣe itupalẹ entropy lori data lati wa data fisinuirindigbindigbin tabi awọn bọtini fifi ẹnọ kọ nkan. Gbogbo eyi laisi iwulo lati wọle si koodu orisun.

Ni gbogbogbo, ohun gbogbo ti o nilo wa nibẹ :)

Bawo ni binwalk ṣiṣẹ?

Ẹya akọkọ ti binwalk ni wíwo Ibuwọlu rẹ. Binwalk le ṣe ayẹwo aworan famuwia lati wa awọn oriṣi faili ti a ṣe sinu ati awọn eto faili.

Ṣe o mọ IwUlO laini aṣẹ 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

Egbe filewo akọsori faili ati pe o wa ibuwọlu (nọmba idan) lati pinnu iru faili naa. Fun apẹẹrẹ, ti faili ba bẹrẹ pẹlu ọna ti awọn baiti 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A, o mọ pe faili PNG ni. Lori Wikipedia Atokọ ti awọn ibuwọlu faili ti o wọpọ wa.

Binwalk ṣiṣẹ ni ọna kanna. Ṣugbọn dipo wiwa awọn ibuwọlu nikan ni ibẹrẹ faili, binwalk yoo ṣayẹwo gbogbo faili naa. Ni afikun, binwalk le jade awọn faili ti o rii ni aworan naa.

Awọn irin-iṣẹ file и binwalk lo ìkàwé libmagic lati ṣe idanimọ awọn ibuwọlu faili. Sugbon binwalk afikun ohun ti o ṣe atilẹyin atokọ ti awọn ibuwọlu idan aṣa lati wa awọn faili fisinuirindigbindigbin / zipped, awọn akọle famuwia, awọn ekuro Linux, awọn bata bata, awọn eto faili ati bẹbẹ lọ.

Jẹ ki a ni diẹ ninu fun?

fifi sori Binwalk

Binwalk ni atilẹyin lori ọpọ awọn iru ẹrọ pẹlu Lainos, OSX, FreeBSD ati Windows.

Lati fi ẹya tuntun ti binwalk sori ẹrọ o le download orisun koodu ki o si tẹle fifi sori ilana tabi awọn ọna guide, wa lori oju opo wẹẹbu ise agbese.

Binwalk ni ọpọlọpọ awọn paramita oriṣiriṣi:

$ 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

Ṣiṣayẹwo aworan

Jẹ ki a bẹrẹ nipa wiwa awọn ibuwọlu faili inu aworan (aworan lati aaye naa TP-asopọ).

Nṣiṣẹ binwalk pẹlu paramita Ibuwọlu:

$ 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

Bayi a ni alaye pupọ nipa aworan yii.

Awọn lilo aworan Submarine bi bootloader (akọsori aworan ni 0x5AC0 ati aworan bootloader fisinuirindigbindigbin ni 0x5B00). Da lori akọsori uImage ni 0x13270, a mọ pe faaji ero isise jẹ MIPS ati ekuro Linux jẹ ẹya 3.3.8. Ati da lori aworan ti o rii ni adirẹsi naa 0x11CEA5, a le rii iyẹn rootfs jẹ eto faili squashfs.

Jẹ ki a jade ni bayi bootloader (U-Boot) nipa lilo aṣẹ naa 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

Niwọn bi aworan ti wa ni fisinuirindigbindigbin nipa lilo LZMA, a nilo lati decompress o:

$ unlzma u-boot.bin.lzma

Bayi a ni aworan U-Boot:

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

Bawo ni nipa wiwa iye aiyipada fun 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 Ayika oniyipada bootargs ti a lo lati kọja awọn paramita si ekuro Linux. Ati lati oke, a ni oye ti o dara julọ ti iranti filasi ti ẹrọ naa.

Bawo ni nipa yiyo aworan ekuro Linux bi?

$ 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

A le ṣayẹwo pe aworan ti yọ jade ni aṣeyọri nipa lilo aṣẹ naa 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

Ọna kika faili uImage jẹ ipilẹ aworan ekuro Linux pẹlu akọsori afikun. Jẹ ki a yọ akọsori yii kuro lati gba aworan ekuro Linux ti o kẹhin:

$ 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

Aworan naa ti ni fisinuirindigbindigbin, nitorinaa jẹ ki a tu silẹ:

$ unlzma Image.lzma

Bayi a ni aworan ekuro Linux kan:

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

Kini a le ṣe pẹlu aworan kernel? A le, fun apẹẹrẹ, ṣe wiwa okun ni aworan naa ki o wa ẹya ti ekuro Linux ati kọ ẹkọ nipa agbegbe ti a lo lati kọ ekuro naa:

$ 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

Paapaa botilẹjẹpe famuwia ti tu silẹ ni ọdun to kọja (2019), bi MO ṣe kọ nkan yii o nlo ẹya atijọ ti ekuro Linux (3.3.8) ti a tu silẹ ni ọdun 2012, ti a ṣajọpọ pẹlu ẹya atijọ ti GCC (4.6) tun lati ọdun 2012 !
(approx. transl. Ṣe o tun gbẹkẹle awọn olulana rẹ ni ọfiisi ati ni ile?)

Pẹlu aṣayan --opcodes a tun le lo binwalk lati wo awọn ilana ẹrọ ati pinnu faaji ero isise ti aworan naa:

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

Kini nipa eto faili root? Dipo yiyọ aworan jade pẹlu ọwọ, jẹ ki a lo aṣayan naa binwalk --extract:

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

Eto faili gbongbo ti o pe ni yoo fa jade si iwe-ipamọ-ipin kan:

$ 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)
 ---------------------------------------------------------------

Bayi a le ṣe ọpọlọpọ awọn ohun ti o yatọ.

A le wa awọn faili iṣeto ni, awọn hashes ọrọ igbaniwọle, awọn bọtini cryptographic ati awọn iwe-ẹri oni-nọmba. A le ṣe itupalẹ awọn faili alakomeji fun laasigbotitusita ati awọn ailagbara.

Nipasẹ Tani и kroot a le paapaa ṣiṣẹ (farawe) iṣẹ ṣiṣe lati aworan naa:

$ 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

Nla! Ṣugbọn jọwọ ṣe akiyesi pe ẹya BusyBox jẹ 1.19.4. Eyi jẹ ẹya atijọ ti BusyBox, ti a tu silẹ ni Oṣu Kẹrin ọdun 2012.

Nitorinaa TP-Link ṣe idasilẹ aworan famuwia ni ọdun 2019 ni lilo sọfitiwia (GCC toolchain, ekuro, BusyBox, ati bẹbẹ lọ) lati ọdun 2012!

Bayi ṣe o loye idi ti Mo fi sori ẹrọ OpenWRT nigbagbogbo lori awọn olulana mi?

Iyẹn kii ṣe gbogbo rẹ

Binwalk tun le ṣe itupalẹ entropy, tẹjade data entropy aise, ati ṣe ina awọn aworan entropy. Ni deede, entropy ti o tobi julọ ni a ṣe akiyesi nigbati awọn baiti ninu aworan jẹ laileto. Eyi le tunmọ si pe aworan naa ni fifi ẹnọ kọ nkan, fisinuirindigbindigbin, tabi faili ti o pa. Bọtini ìsekóòdù Hardcore? Ki lo de.

Yiyipada ẹrọ olulana ile nipa lilo binwalk. Ṣe o gbẹkẹle sọfitiwia olulana rẹ?

A tun le lo paramita naa --raw lati wa aṣa baiti aise ọkọọkan ninu aworan tabi paramita --hexdump lati ṣe idalenu hex ti o ṣe afiwe awọn faili igbewọle meji tabi diẹ sii.

Awọn ibuwọlu aṣa le ṣe afikun si binwalk boya nipasẹ faili ibuwọlu aṣa kan pato lori laini aṣẹ nipa lilo paramita naa --magic, tabi nipa fifi wọn kun si liana $ HOME / .config / binwalk / magic.

O le wa alaye diẹ sii nipa binwalk ni osise iwe aṣẹ.

binwalk itẹsiwaju

nibẹ API binwalk, ti ​​a ṣe bi module Python ti o le ṣee lo nipasẹ eyikeyi iwe afọwọkọ Python lati ṣe ọlọjẹ binwalk ni eto eto, ati pe ohun elo laini aṣẹ binwalk le fẹrẹ ṣe pidánpidán patapata pẹlu awọn laini meji ti koodu Python!

import binwalk
binwalk.scan()

Lilo Python API o tun le ṣẹda Python afikun lati tunto ati faagun binwalk.

O tun wa IDA itanna ati awọsanma version Binwalk Pro.

Nitorinaa kilode ti o ko ṣe igbasilẹ aworan famuwia lati Intanẹẹti ki o gbiyanju binwalk? Mo ṣe ileri pe iwọ yoo ni igbadun pupọ :)

orisun: www.habr.com

Fi ọrọìwòye kun