scar: carve deleted files out of a raw disk image

scar: carve deleted files out of a raw disk image

Deleted a file? On most filesystems the data is still there — the directory entry is gone, but the bytes sit on the disk until something overwrites them. A file carver finds those bytes again by scanning the raw image for known file headers and cutting out everything up to the end of each file. scar, new in the sfile repo, does exactly that — and it does it with sfile's whole format table: all 831 known formats, not just the usual handful.

What it does

scar takes a raw image — a disk dump, a memory dump, a firmware blob, a corrupted partition — and returns the files inside it:

$ scar image.dd                  # carves into ./carved/
$ scar -o recovered image.dd    # pick the output directory
$ scar -m 64m -o out image.dd   # cap each carved file at 64 MiB

Each recovered file is written as <offset>.<ext>, where the offset is the byte position it was found at, and a report lists every carve: offset, format, size, confidence, and how the end of the file was found. The same report goes to stdout and to carved/report.txt.

Example run

The image below is synthetic — built by the repo's test (make test-scar), which plants a PNG, a JPEG, a GIF, a ZIP, a PDF and a gzip member at known offsets in random filler, plus a JPEG truncated by the end of the image — but the output is the real output of that run:

$ scar -o carved test.img
scar 1.0: test.img (10531 bytes) — 7 files carved, 2985 bytes total
offset       size       format                       ext    confidence method            file
0            135        PNG image                    png    high       png-chunk-walk    0000000000.png
248          1116       JPEG image                   jpeg   high       jpeg-marker-walk  0000000248.jpeg
5460         600        GIF image                    gif    high       gif-block-walk    0000005460.gif
6060         521        ZIP archive                  zip    high       zip-local-walk    0000006060.zip
7358         329        PDF document                 pdf    high       pdf-eof-scan      0000007358.pdf
9735         84         gzip archive                 gz     high       gzip-inflate      0000009735.gz
10331        200        JPEG image                   jpeg   low        eof               0000010331.jpeg

All seven files came back byte-identical to the planted originals, at the exact offsets and sizes. The last one is the interesting row: a JPEG cut off by the end of the image, recovered as far as it goes, flagged low confidence so you know it is partial.

How it finds the end of a file

A header alone is not a file — the hard part of carving is knowing where the file stops. scar works through the methods below in order of preference, and the report says which one was used:

methodconfidencehow the end is found
png-chunk-walkhighWalk the PNG chunks until IEND (first chunk must be IHDR)
jpeg-marker-walkhighWalk JPEG markers; scan data is skipped byte-by-byte until the EOI marker
gif-block-walkhighWalk GIF blocks until the 0x3B trailer
zip-local-walkhighWalk local file headers, then the central directory; end is the EOCD record + comment
pdf-eof-scanhighScan for %%EOF
gzip-inflatehighInflate the member; the byte count at stream end is the member length
png-iend-scan / zip-eocd-scanmediumStructure walk failed on a damaged file; the trailer was found by scanning (and validated for ZIP: central-directory offset + size must land exactly on the EOCD)
next-header / eof / cappedlowNo structure or footer: the file runs to the next recognised header, the end of the image, or the -m cap

Three rules keep the results honest. First, a header that fails structural validation is not a file — random data contains magic bytes (a 2-byte gzip magic occurs about once per 64 KiB), so a candidate is only carved if its structure checks out. Second, scanning resumes after a carved file, never inside it, so an embedded header does not produce a second, bogus carve. And third — the one that bit during testing — the fallback scan for the next header never looks inside a file's own structure: a JFIF header contains the byte sequence 00 00 01 00, which is a real ICO magic, 13 bytes into every JPEG. Scanning from the file start cut the truncated JPEG above to 13 bytes instead of the 200 that were really there. The fix: when a file is truncated, the walk reports how many leading bytes it validated, and the fallback scan starts after them.

Limits

  • Only headers at offset 0 of a format count; formats whose magic sits at a nonzero offset cannot anchor a carve.
  • 2-byte magics are only carved for gzip — anything else would false-positive every ~64 KiB of random data.
  • ZIP64 is not walked; those archives fall back to the EOCD scan.
  • No embedded-file recovery: a file stored inside another carved file is not carved separately (run scar on the carved file to go one level deeper).
  • Fragmented files are not reassembled — like every header carver, scar recovers contiguous runs only.
  • Low-confidence carves need checking: open the file, or run sfile on it, before trusting it.

Get it

scar lives in the sfile repo — github.com/cnmcdee/sfile (make scar, needs zlib) — with full documentation at docs/scar.md and the synthetic-image test at tests/test_scar.py (make test-scar).

Linux Rocks Every Day
Decode any of these formats with sfile — a unified header decoder for the 831 formats documented on this blog.
Geotagged photos? stag lists every GPS point as CSV and sgeo plots them on a map — both in sfile.