Reverse engineering router ea lapeng u sebelisa binwalk. Na u tšepa software ea router ea hau?

Reverse engineering router ea lapeng u sebelisa binwalk. Na u tšepa software ea router ea hau?

Matsatsi a 'maloa a fetileng, ke ile ka etsa qeto ea ho khutlisa firmware ea router ea ka ke sebelisa binwalk.

Ke ithekile TP-Link Archer C7 router ea lapeng. Ha se router e ntle ka ho fetisisa, empa e lekane litlhoko tsa ka.

Nako le nako ha ke reka router e ncha, kea e kenya OpenWRT. Molelmong oa eng? E le molao, bahlahisi ha ba tsotelle haholo ka ho tšehetsa li-routers tsa bona 'me ha nako e ntse e ea software e fetoha ea khale, bofokoli bo hlaha, joalo-joalo, ka kakaretso, u fumana maikutlo. Ka hona, ke khetha firmware ea OpenWRT, e tšehetsoeng hantle ke sechaba se bulehileng.

Ka mor'a ho khoasolla OpenWRT, le 'na e jarollotse setšoantšo sa morao-rao sa firmware tlasa Archer C7 ea ka e ncha ho tsoa webosaeteng ea semmuso mme ke nkile qeto ea ho e sekaseka. Feela bakeng sa monate le ho bua ka binwalk.

Binwalk ke eng?

Binwalk ke sesebelisoa sa mohloli o bulehileng oa ho hlahloba, ho khutlisa boenjiniere le ho ntša setšoantšo sa firmware.

E thehiloe ka 2010 ke Craig Heffner, binwalk e khona ho hlahloba litšoantšo tsa firmware le ho fumana lifaele, ho khetholla le ho ntša litšoantšo tsa tsamaiso ea lifaele, khoutu e sebetsang, li-archive tse hatelitsoeng, li-bootloader le li-kernels, liforomo tsa lifaele tse kang JPEG le PDF, le tse ling tse ngata.

U ka sebelisa binwalk ho fetola moenjiniere oa firmware ho utloisisa hore na e sebetsa joang. Batla lifaele tsa binary bakeng sa bofokoli, ntša lifaele, 'me u batle litifikeiti tsa morao-rao kapa tsa dijithale. U ka boela ua fumana opcodes bakeng sa sehlopha sa li-CPU tse fapaneng.

U ka ntša litšoantšo tsa tsamaiso ea lifaele ho batla lifaele tse itseng tsa password (passwd, moriti, joalo-joalo) ebe u leka ho senya li-hashes tsa password. U ka etsa khetho ea binary lipakeng tsa lifaele tse peli kapa ho feta. U ka etsa tlhahlobo ea entropy ho data ho batla lintlha tse hatisitsoeng kapa linotlolo tsa encryption. Tsena tsohle ntle le tlhoko ea ho fihlella khoutu ea mohloli.

Ka kakaretso, tsohle tseo u li hlokang li teng :)

Binwalk e sebetsa joang?

Ntho e ka sehloohong ea binwalk ke ho tekena ha eona. Binwalk e ka hlahloba setšoantšo sa firmware ho batla mefuta e fapaneng ea lifaele tse hahelletsoeng kahare le litsamaiso tsa lifaele.

Na u tseba tšebeliso ea line line 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

sehlopha filee sheba hlooho ea faele mme e sheba tekeno (nomoro ea boselamose) ho fumana mofuta oa faele. Ka mohlala, haeba faele e qala ka tatellano ea li-byte 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A, ea tseba hore ke faele ea PNG. E butsoe Wikipedia Ho na le lethathamo la li-signature tse tloaelehileng tsa lifaele.

Binwalk e sebetsa ka tsela e ts'oanang. Empa ho e-na le ho batla li-signature feela qalong ea faele, binwalk e tla hlahloba faele eohle. Ho feta moo, binwalk e ka ntša lifaele tse fumanehang setšoantšong.

Lisebelisoa file и binwalk sebelisa laebrari libmagic ho khetholla li-signature tsa lifaele. Empa binwalk e boetse e ts'ehetsa lethathamo la li-signature tsa boselamose ba tloaelo ho batla lifaele tse hatelitsoeng / tse ikopantseng, lihlooho tsa firmware, li-Linux kernels, bootloaders, li-filesystem joalo-joalo.

Ha re ithabiseng?

Ho kenya Binwalk

Binwalk e tšehetsoa ka li-platform tse ngata tse kenyeletsang Linux, OSX, FreeBSD le Windows.

Ho kenya phetolelo ea morao-rao ea binwalk u ka khoasolla mohloli khoutu le ho latela litaelo tsa ho kenya kapa tataiso e potlakileng, e fumanehang webosaeteng ea morero.

Binwalk e na le liparamente tse ngata tse fapaneng:

$ 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

Ho hlahloba litšoantšo

Ha re qale ka ho batla li-signature tsa faele ka har'a setšoantšo (setšoantšo se tsoang sebakeng sa marang-rang Tl-Link).

Ho matha binwalk ka --signature parameter:

$ 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

Hona joale re na le boitsebiso bo bongata ka setšoantšo sena.

Tšebeliso ea litšoantšo Sekepe sa metsing joalo ka bootloader (sehlooho sa setšoantšo ho 0x5AC0 le setšoantšo se hatelitsoeng sa bootloader ho 0x5B00). Ho ipapisitsoe le sehlooho sa uImage ho 0x13270, rea tseba hore meralo ea processor ke MIPS mme Linux kernel ke mofuta oa 3.3.8. 'Me ho latela setšoantšo se fumanoang atereseng 0x11CEA5, re ka bona seo rootfs ke sistimi ea faele squashfs.

Joale a re ntšeng bootloader (U-Boot) re sebelisa taelo 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

Kaha setšoantšo se hatelloa ho sebelisa LZMA, re hloka ho se fokotsa:

$ unlzma u-boot.bin.lzma

Joale re na le setšoantšo sa U-Boot:

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

Ho thoe'ng ka ho fumana boleng ba kamehla ba 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 Tikoloho e Fetohang bootargs e neng e sebelisoa ho fetisetsa liparamente ho kernel ea Linux. 'Me ho tloha ka holimo, re na le kutloisiso e molemo ea mohopolo oa flash oa sesebelisoa.

Ho thoe'ng ka ho ntša setšoantšo sa Linux kernel?

$ 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

Re ka hlahloba hore na setšoantšo se ntšitsoe ka katleho ho sebelisa taelo 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

Sebopeho sa faele sa uImage ha e le hantle ke setšoantšo sa Linux kernel se nang le hlooho e eketsehileng. Ha re tloseng sehlooho sena ho fumana setšoantšo sa ho qetela sa Linux kernel:

$ 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

Setšoantšo se petelitsoe, kahoo ha re se phutholle:

$ unlzma Image.lzma

Hona joale re na le setšoantšo sa kernel ea Linux:

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

Re ka etsa'ng ka setšoantšo sa kernel? Ka mohlala, re ka etsa lipatlisiso tsa likhoele setšoantšong 'me ra fumana mofuta oa Linux kernel' me ra ithuta ka tikoloho e sebelisoang ho aha kernel:

$ 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

Leha firmware e ile ea lokolloa selemong se fetileng (2019), ha ke ngola sengoloa sena e sebelisa mofuta oa khale oa Linux kernel (3.3.8) e lokollotsoeng ka 2012, e hlophisitsoeng ka mofuta oa khale haholo oa GCC (4.6) hape ho tloha 2012. !
(approx. transl. na u ntse u tšepa li-routers tsa hau ofising le lapeng?)

Ka khetho --opcodes re ka boela ra sebelisa binwalk ho sheba litaelo tsa mochini le ho tseba sebopeho sa processor ea setšoantšo:

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

Ho thoe'ng ka tsamaiso ea metso ea lifaele? Sebakeng sa ho ntša setšoantšo ka letsoho, ha re sebeliseng khetho binwalk --extract:

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

Sistimi e felletseng ea metso e tla ntšoa ho subdirectory:

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

Hona joale re ka etsa lintho tse ngata tse fapaneng.

Re ka batla lifaele tsa tlhophiso, li-hashes tsa password, linotlolo tsa cryptographic le litifikeiti tsa dijithale. Re ka sekaseka lifaele tsa binary bakeng sa ho batle phoso le bofokoli.

Ka thuso ea ke mang и chroot re ka ba ra matha (emulate) e ka phethisoang ho tsoa setšoantšong:

$ 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

E kholo! Empa ka kopo hlokomela hore mofuta oa BusyBox ke 1.19.4. Ena ke mofuta oa khale haholo oa BusyBox, e lokollotsoeng ka April 2012.

Kahoo TP-Link e lokolla setšoantšo sa firmware ka 2019 e sebelisa software (GCC toolchain, kernel, BusyBox, joalo-joalo) ho tloha 2012!

Joale na ua utloisisa hore na hobaneng ke lula ke kenya OpenWRT ho li-routers tsa ka?

Ha se phetho

Binwalk e ka boela ea etsa tlhahlobo ea entropy, ea hatisa data e tala ea entropy, le ho hlahisa li-graph tsa entropy. Ka tloaelo, entropy e kholo e bonoa ha li-byte tse setšoantšong li sa fetohe. Sena se ka bolela hore setšoantšo se na le faele e patiloeng, e hatelitsoeng, kapa e hlakileng. Senotlolo sa encryption se thata? Hobaneng ho se joalo.

Reverse engineering router ea lapeng u sebelisa binwalk. Na u tšepa software ea router ea hau?

Hape re ka sebelisa parameter --raw ho fumana tatelano e tloahelehileng ea li-byte setšoantšong kapa paramethareng --hexdump ho etsa hex dump ho bapisa lifaele tse peli kapa ho feta tsa ho kenya.

Mesaeno ea tloaelo e ka eketsoa ho binwalk kapa ka faele e saenneng ea tloaelo e boletsoeng molaong oa taelo ho sebelisa parameter --magic, kapa ka ho li kenya bukeng $ HOME / .config / binwalk / magic.

U ka fumana boitsebiso bo eketsehileng mabapi le binwalk ho litokomane tsa molao.

binwalk extension

Ho na le API binwalk, e kentsoeng e le mojule oa Python o ka sebelisoang ke mongolo ofe kapa ofe oa Python ho etsa tlhahlobo ea binwalk scan, mme ts'ebeliso ea mohala oa taelo ea binwalk e ka kopitsoa ka ho felletseng ka mela e 'meli feela ea khoutu ea Python!

import binwalk
binwalk.scan()

U sebelisa Python API u ka boela ua theha Li-plugins tsa Python ho lokisa le ho holisa binwalk.

Hape e teng Sesebelisoa sa IDA le mofuta oa leru Binwalk Pro.

Joale ke hobane'ng ha u sa khoasolle setšoantšo sa firmware ho Internet ebe u leka binwalk? Ke ts'episa hore u tla ba le thabo e ngata :)

Source: www.habr.com

Eketsa ka tlhaloso