Extraction
Scanning produces a map. Extraction produces
the territory: -e carves each member out of
the image into a file of its own and, where it
has a rule for the format, runs the matching
unpacking tool on it.
The first attempt on the build machine failed before touching the image, and the failure is part of the lesson:
Extractor Exception: Binwalk extraction uses many third party utilities,
which may not be secure. If you wish to have extraction utilities
executed as the current user, use '--run-as=root' (binwalk itself
must be run as root).
The build shell was running as root, and
binwalk refuses to execute its shelf of
external unpacking tools with root's privileges
unless root says --run-as=root out loud.
That is not bureaucratic fuss. Extraction is
the moment untrusted bytes meet a pile of
parsers — archive tools, filesystem tools,
decompressors — running with your
permissions. On an ordinary user account the
same command runs without the extra flag,
because the exposure is already limited to
that account. Either way, the working rule for
real images is stronger than any flag:
extract firmware on a machine, or in a
virtual machine, that you are willing to throw
away, and never run a program you extracted
just to see what it does.
With the flag given, the run is quiet — the scan table prints, and the results are on disk:
$ binwalk -e --run-as=root specimen.bin
(scan table, same three rows as the first scan)
The extracted tree
_specimen.bin.extracted
_specimen.bin.extracted/10000.squashfs
_specimen.bin.extracted/11000
_specimen.bin.extracted/11090
_specimen.bin.extracted/squashfs-root
_specimen.bin.extracted/squashfs-root/etc
_specimen.bin.extracted/squashfs-root/etc/lab-banner.txt
_specimen.bin.extracted/squashfs-root/notes
_specimen.bin.extracted/squashfs-root/notes/field-notes.txt
_specimen.bin.extracted/squashfs-root/www
_specimen.bin.extracted/squashfs-root/www/index.html
Naming is mechanical, and worth learning to read:
- The output folder is the image's name with
extractedappended (and a leading underscore):_specimen.bin.extracted. - Each carve is named by its offset in hex:
the SquashFS found at 0x10000 becomes
10000.squashfs. No extension is invented for formats binwalk leaves alone; the extension appears when binwalk knows the type and keeps the carve as that type. - A member binwalk unpacks is replaced by
its contents.
11000and11090have no extension because they are no longer gzip files — they are what was inside the gzip files (a text file and a tar archive, respectively). The SquashFS carve stays, and its unpacked filesystem appears beside it under the tool's own name for it,squashfs-root— the extractor for SquashFS isunsquashfs, and that is its default output folder.
So one member can leave one file, or a file plus a tree, depending on whether an extractor claimed it. Nothing announces which; the tree is the record.
Carving without unpacking
-z carves and stops there:
$ binwalk -e -z --run-as=root specimen.bin
_specimen.bin.extracted/10000.squashfs
_specimen.bin.extracted/11000.gz
_specimen.bin.extracted/11090.gz
Same offsets, but now the gzip members keep
their .gz and their compressed bytes, and
there is no squashfs-root. Carve-only is the
mode for handing members to tools of your own
choosing — and the safe mode when you want the
pieces without running anybody's parser on
them yet.
Proving an extraction
An extracted file is a claim. Prove it the way this guide proves everything, with a checksum taken before and after. The planted notes file inside the SquashFS was hashed when the specimen was built:
7ccaa2b81c3f9aa9d677756ff16be21f13ada62afcc82fe19abdfbe6bc944a1e (source, at build time)
7ccaa2b81c3f9aa9d677756ff16be21f13ada62afcc82fe19abdfbe6bc944a1e (_specimen.bin.extracted/squashfs-root/notes/field-notes.txt)
Identical: the file that went into the filesystem came back out byte for byte, through an image, a scan, a carve, and an unpack.
Useful variations, stated once here and used
through the rest of the guide: -C <dir>
extracts somewhere other than the current
folder; -V sorts carves into subfolders
named by offset; -r deletes each carve after
its extractor runs, for when only the unpacked
trees matter and the disk is small. One
caution with a flag of its own: extracted
filesystems can contain symbolic links
pointing outside the extraction folder, and
following them while unpacking has
historically been a way for a hostile image to
write elsewhere on your disk. Binwalk
sanitises such links by default; --preserve-symlinks
(also written -1) turns that protection off.
Leave it off's opposite — leave the default
alone.
Check: after any extraction, the evidence is a tree whose names you can read (offsets, in hex) and at least one recovered file whose checksum, or content, you can verify against something outside binwalk's say-so.