چند روز پیش تصمیم گرفتم سیستم عامل روتر خود را با استفاده از binwalk مهندسی معکوس کنم.
من خودم خریدم
هر بار که یک روتر جدید می خرم، نصب می کنم
پس از دانلود OpenWRT، من نیز
بین واک چیست؟
binwalk که در سال 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
علاوه بر این از لیستی از امضاهای جادویی سفارشی برای جستجوی فایل های فشرده/زیپ شده، هدرهای سیستم عامل، هسته های لینوکس، بوت لودرها، سیستم های فایل و غیره پشتیبانی می کند.
بیا کمی تفریح کنیم؟
نصب Binwalk
Binwalk در چندین پلتفرم از جمله لینوکس، 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
اسکن تصویر
بیایید با جستجوی امضاهای فایل در داخل تصویر شروع کنیم (تصویر از سایت
اجرای binwalk با پارامتر --signature:
$ 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
). بر اساس هدر uImage در 0x13270، می دانیم که معماری پردازنده MIPS و هسته لینوکس نسخه 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
برای ارسال پارامترها به هسته لینوکس استفاده می شود. و با توجه به مطالب بالا درک بهتری از فلش مموری دستگاه داریم.
در مورد استخراج تصویر هسته لینوکس چطور؟
$ 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 اساساً یک تصویر هسته لینوکس با یک هدر اضافی است. برای دریافت تصویر نهایی هسته لینوکس، این هدر را حذف می کنیم:
$ 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
اکنون یک تصویر هسته لینوکس داریم:
$ ls -la Image
-rw-rw-r-- 1 sprado sprado 3164228 Fev 5 10:51 Image
با تصویر هسته چه کنیم؟ برای مثال میتوانیم یک جستجوی رشتهای در تصویر انجام دهیم و نسخه هسته لینوکس را پیدا کنیم و در مورد محیطی که برای ساختن کرنل استفاده میشود آشنا شویم:
$ 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) استفاده می کند که در سال 2012 منتشر شد، که با نسخه بسیار قدیمی GCC (4.6) نیز از سال 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)
---------------------------------------------------------------
حالا ما می توانیم کارهای مختلف زیادی انجام دهیم.
میتوانیم فایلهای پیکربندی، هش رمز عبور، کلیدهای رمزنگاری و گواهیهای دیجیتال را جستجو کنیم. ما می توانیم فایل های باینری را برای
با
$ 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 منتشر شد.
بنابراین TP-Link یک تصویر سیستم عامل را در سال 2019 با استفاده از نرم افزار (GCC toolchain، هسته، BusyBox و غیره) از سال 2012 منتشر می کند!
حالا متوجه شدید که چرا من همیشه OpenWRT را روی روترهایم نصب می کنم؟
این همش نیست
Binwalk همچنین می تواند تجزیه و تحلیل آنتروپی را انجام دهد، داده های آنتروپی خام را چاپ کند و نمودارهای آنتروپی تولید کند. به طور معمول، زمانی که بایت های تصویر تصادفی باشند، آنتروپی بیشتر مشاهده می شود. این می تواند به این معنی باشد که تصویر حاوی یک فایل رمزگذاری شده، فشرده یا مبهم است. کلید رمزگذاری هاردکور؟ چرا که نه.
ما همچنین می توانیم از پارامتر استفاده کنیم --raw
برای پیدا کردن یک دنباله بایت خام سفارشی در یک تصویر یا پارامتر --hexdump
برای انجام یک dump hex با مقایسه دو یا چند فایل ورودی.
--magic
، یا با افزودن آنها به دایرکتوری $ HOME / .config / binwalk / magic
.
شما می توانید اطلاعات بیشتر در مورد binwalk در
پسوند binwalk
آنجا
import binwalk
binwalk.scan()
با استفاده از API پایتون نیز می توانید ایجاد کنید
همچنین وجود دارد
پس چرا تصویر سیستم عامل را از اینترنت دانلود نمی کنید و binwalk را امتحان نمی کنید؟ قول میدم خیلی بهت خوش بگذره :)
منبع: www.habr.com