Reverse engineering ny router an-trano mampiasa binwalk. Matoky ny rindrambaiko router-nao ve ianao?

Reverse engineering ny router an-trano mampiasa binwalk. Matoky ny rindrambaiko router-nao ve ianao?

Andro vitsy lasa izay, nanapa-kevitra ny hanova ny rindrankajy an'ny router aho amin'ny alàlan'ny binwalk.

Nividy ny tenako aho TP-Link Archer C7 router an-trano. Tsy ny router tsara indrindra, fa ampy ho an'ny filàko.

Isaky ny mividy router vaovao aho dia mametraka OpenWRT. Hatao inona? Amin'ny maha-fitsipika, ny mpanamboatra dia tsy miraharaha loatra ny fanohanana ny routers ary rehefa mandeha ny fotoana dia lasa lany andro ny rindrambaiko, vulnerabilities miseho, sy ny sisa, amin'ny ankapobeny, mahazo ny hevitra. Noho izany, aleoko ny firmware OpenWRT, izay tohanan'ny vondrom-piarahamonina open-source.

Rehefa nisintona ny OpenWRT aho dia naka ny sary firmware farany eo ambanin'ny Archer C7 vaovao avy amin'ny tranokala ofisialy ary nanapa-kevitra ny hamakafaka izany. Mahafinaritra sy miresaka momba ny binwalk.

Inona no atao hoe binwalk?

Binwalk dia fitaovana loharano misokatra ho an'ny famakafakana, reverse engineering ary fakana sary firmware.

Noforonin'i Craig Heffner tamin'ny taona 2010, ny binwalk dia afaka mijery sary fikandrana sy mahita rakitra, hamantatra sy hanesorana ny sarin'ny rafi-drakitra, kaody azo tanterahana, arisiva voatsindry, bootloader sy kernels, endrika rakitra toy ny JPEG sy PDF, sy ny maro hafa.

Azonao atao ny mampiasa binwalk mba hamadika ny firmware mba hahatakarana ny fomba fiasa. Mitadiava rakitra binary ho an'ny vulnerabilities, esory ny rakitra, ary tadiavo ny backdoors na fanamarinana nomerika. Azonao atao koa ny mahita opcodes ho an'ny andiana CPU samihafa.

Azonao atao ny maka sary fichier system mba hitadiavana fisie tenimiafina manokana (passwd, aloka, sns.) ary manandrana manapaka ny tenimiafina hashes. Azonao atao ny manao parsing binary eo anelanelan'ny rakitra roa na maromaro. Azonao atao ny manao famakafakana entropy amin'ny angon-drakitra mba hitadiavana angon-drakitra voaporitra na fanalahidin'ny encryption. Izany rehetra izany tsy mila miditra amin'ny kaody loharano.

Amin'ny ankapobeny dia ao daholo izay ilainao :)

Ahoana no fiasan'ny binwalk?

Ny endri-javatra lehibe indrindra amin'ny binwalk dia ny fitarafana sonia. Ny Binwalk dia afaka mijery ny sarin'ny firmware mba hikaroka karazana rakitra sy rafitra fichier isan-karazany.

Fantatrao ve ny utility line command 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

ekipa filemijery ny lohatenin'ny rakitra ary mitady sonia (nomerao majika) hamaritana ny karazana rakitra. Ohatra, raha manomboka amin'ny filaharan'ny bytes ny rakitra 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A, fantany fa rakitra PNG izy io. On Wikipedia Misy lisitry ny sonia rakitra mahazatra.

Ny Binwalk dia miasa toy izany koa. Fa raha tokony hitady sonia fotsiny eo am-piandohan'ny rakitra, ny binwalk dia hijery ny rakitra manontolo. Fanampin'izany, ny binwalk dia afaka manala ireo rakitra hita ao amin'ny sary.

fitaovana file и binwalk mampiasa ny tranomboky libmagic hamantarana sonia rakitra. SAINGY binwalk Ankoatra izany, dia manohana ny lisitry ny sonia majika manokana hitadiavana rakitra voaporitra/zip, lohatenin'ny firmware, kernel Linux, bootloader, fichier sy ny sisa.

Andao hifalifaly?

Fametrahana Binwalk

Ny Binwalk dia tohanana amin'ny sehatra maro ao anatin'izany ny Linux, OSX, FreeBSD ary Windows.

Azonao atao ny mametraka ny kinova farany amin'ny binwalk alaivo ny kaody loharano ary manaraka torolàlana fametrahana na mpitari-dalana haingana, hita ao amin'ny tranokalan'ny tetikasa.

Ny Binwalk dia manana paramètre maro samihafa:

$ 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

Fikarohana sary

Andao hanomboka amin'ny fitadiavana sonia rakitra ao anaty sary (sary avy amin'ny tranokala TP-Link).

Mandeha binwalk miaraka amin'ny --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

Ankehitriny dia manana fampahalalana be dia be momba ity sary ity isika.

Fampiasana sary Sambo mpisitrika ho bootloader (lohahevitra sary at 0x5AC0 ary sary bootloader voaporitra ao 0x5B00). Mifototra amin'ny lohatenin'ny uImage ao amin'ny 0x13270, fantatray fa MIPS ny rafitra processeur ary ny kernel Linux dia version 3.3.8. Ary mifototra amin'ny sary hita ao amin'ny adiresy 0x11CEA5, hitantsika izany rootfs dia rafitra fichier squashfs.

Andeha isika haka ny bootloader (U-Boot) mampiasa ny baiko 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

Koa satria ny sary dia voatsindry amin'ny fampiasana LZMA, dia ilaintsika ny manafoana azy:

$ unlzma u-boot.bin.lzma

Manana sary U-Boot izahay izao:

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

Ahoana ny amin'ny fitadiavana ny sandan'ny default for 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 Environment Variable bootargs ampiasaina handefasana paramètre amin'ny kernel Linux. Ary avy amin'ny etsy ambony, dia manana fahatakarana tsara kokoa ny fahatsiarovana tselatra ny fitaovana.

Ahoana ny momba ny fanesorana ny sary kernel 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

Azontsika atao ny manamarina fa nalaina soa aman-tsara ilay sary tamin'ny alalan'ny baiko 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

Ny endrika rakitra uImage dia sary kernel Linux miaraka amin'ny lohateny fanampiny. Andao esorina ity lohateny ity mba hahazoana ny sary kernel Linux farany:

$ 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

Ny sary dia voatsindry, ka andeha hovahantsika:

$ unlzma Image.lzma

Manana sary kernel Linux izahay izao:

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

Inona no azontsika atao amin'ny sary kernel? Azonao atao, ohatra, ny manao fikarohana tady amin'ny sary ary mahita ny dikan-tenin'ny kernel Linux ary mianatra momba ny tontolo ampiasaina hanamboarana ny 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

Na dia navoaka tamin'ny taon-dasa (2019) aza ny firmware, rehefa manoratra ity lahatsoratra ity aho dia mampiasa dikan-teny taloha an'ny kernel Linux (3.3.8) navoaka tamin'ny taona 2012, natambatra niaraka tamin'ny dikan-teny tranainy taloha an'ny GCC (4.6) hatramin'ny 2012. !
(eo ho eo. transl. mbola matoky ny router-nao any amin'ny birao sy ao an-trano ve ianao?)

Miaraka amin'ny safidy --opcodes Afaka mampiasa binwalk koa isika hijerena ny toromarika momba ny masinina sy hamaritana ny rafitry ny processeur amin'ny sary:

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

Ahoana ny momba ny rafitra fichier root? Raha tokony hanesorana ny sary amin'ny tanana, dia aleo ny safidy binwalk --extract:

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

Ny rafitra fichier root feno dia ho alaina amin'ny 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)
 ---------------------------------------------------------------

Afaka manao zavatra maro samihafa isika izao.

Azontsika atao ny mikaroka ireo rakitra fikirakirana, tenimiafina tenimiafina, fanalahidy kriptografika ary mari-pankasitrahana nomerika. Afaka mamakafaka ny rakitra binary ho an'ny Fanamboarana ary ny vulnerability.

Amin'ny fanampian'ny whoa и chroot afaka mihazakazaka (maka tahaka) executable avy amin'ny sary mihitsy aza isika:

$ 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

Mahafinaritra! Nefa azafady mariho fa ny dikan-teny BusyBox dia 1.19.4. Ity dia dikan-teny tranainy indrindra amin'ny BusyBox, navoaka tamin'ny Aprily 2012.

Ka ny TP-Link dia namoaka sary firmware tamin'ny 2019 tamin'ny fampiasana rindrambaiko (GCC toolchain, kernel, BusyBox, sns.) nanomboka tamin'ny 2012!

Azonareo ve izao ny antony ametrahako foana ny OpenWRT amin'ny router-ko?

Tsy izay ihany

Ny Binwalk dia afaka manao famakafakana entropy ihany koa, manonta angona entropy manta, ary mamorona sarin'ny entropy. Amin'ny ankapobeny, ny entropy lehibe kokoa dia hita rehefa kisendrasendra ny bytes amin'ny sary. Mety hidika izany fa misy rakitra voafefy, voatsindry, na obfuscated ilay sary. Key encryption hardcore? Fa nahoana no tsy.

Reverse engineering ny router an-trano mampiasa binwalk. Matoky ny rindrambaiko router-nao ve ianao?

Afaka mampiasa ny parameter koa isika --raw mba hitadiavana filaharana byte manta mahazatra amin'ny sary na parameter --hexdump hanaovana fanariam-pako hex mampitaha rakitra fampidirana roa na maromaro.

sonia manokana azo ampidirina amin'ny binwalk na amin'ny alàlan'ny rakitra sonia manokana voatondro ao amin'ny tsipika baiko amin'ny fampiasana ny parameter --magic, na amin'ny fampidirana azy ireo amin'ny lahatahiry $ HOME / .config / binwalk / magic.

Afaka mahita fampahalalana bebe kokoa momba ny binwalk ianao ao antontan-taratasy ofisialy.

fanitarana binwalk

Misy izany API binwalk, napetraka ho modely Python izay azo ampiasaina amin'ny script Python rehetra hanaovana scan binwalk amin'ny programa, ary ny utility command line binwalk dia azo adika tanteraka amin'ny andalana roa amin'ny code Python!

import binwalk
binwalk.scan()

Mampiasa ny Python API ianao dia afaka mamorona ihany koa Python plugins hanamboatra sy hanitarana ny binwalk.

Misy koa IDA plugin ary version cloud Binwalk Pro.

Koa maninona raha alainao amin'ny Internet ny sary firmware ary andramo binwalk? Mampanantena aho fa hanana fahafinaretana be ianao :)

Source: www.habr.com

Add a comment