Files
cnmcdee eac6be4802 scar: file carver built on sfiles format table
Scan a raw disk image for sfiles known magics (831 formats) and carve
out the files: PNG chunk walk, JPEG marker walk, GIF block walk, ZIP
local-header walk + EOCD, PDF %%EOF scan, gzip inflate sizing; next-
header/EOF fallback with a size cap for the rest. Report lists offset,
format, size, confidence and how the end was found.

- src/scar.c, src/footers.h: carver + footer/trailer table
- Makefile: scar target (links -lz), test-scar target, install both
- tests/test_scar.py: synthetic image, 7 files recovered byte-identical
- docs/scar.md: usage, real example output, decisions, limits
2026-12-02 09:12:01 +00:00

6.9 KiB
Raw Permalink Blame History

scar — file carver

scar recovers deleted files from a raw disk image (or any file: a memory dump, a firmware blob, a corrupted partition image). It scans for the magic bytes sfile knows about — all 831 formats in src/formats.h — and carves out every file it finds, writing each one to the output directory as <offset>.<ext> and printing a report of what it recovered, where it was, and how sure it is.

make scar            # builds ./scar (needs zlib: gzip is sized by inflate)
scar image.dd                    # carves into ./carved/
scar -o recovered image.dd       # carve into recovered/
scar -m 64m -o out image.dd      # cap each carved file at 64 MiB
scar -q image.dd                 # quiet: report.txt only, no stdout table

Example run

The image below is synthetic — built by tests/test_scar.py, 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 /tmp/scar-test/test.img
scar 1.0: /tmp/scar-test/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

The same table is written to carved/report.txt. Every recovered file is byte-identical to the planted original — the test verifies that (python3 tests/test_scar.py, exit 0 on success).

How carving decisions are made

A header alone is not a file: scar has to know where the file ends, and it works through the methods below in order of preference. The method column says which one was used; confidence says how much to trust it.

method confidence how the end is found
png-chunk-walk high Walk the PNG chunks (length + type + data + CRC) until the IEND chunk. First chunk must be IHDR.
jpeg-marker-walk high Walk JPEG markers; SOS scan data is skipped byte-by-byte (FF00 is an escaped literal, FFD0–FFD7 are restart markers) until the EOI marker FFD9.
gif-block-walk high Walk GIF blocks (image descriptors, extensions, sub-blocks) until the 0x3B trailer.
zip-local-walk high Walk the local file headers, then the central directory; the end is the EOCD record + comment. Entries with the data-descriptor flag carry no sizes, so their data is skipped by scanning for the next PK signature.
pdf-eof-scan high Scan for %%EOF, consume the trailing newline.
gzip-inflate high Inflate the member (zlib, gzip wrapper); total_in at stream end is the member length. Concatenated members are followed.
png-iend-scan medium Chunk walk failed on a damaged PNG; the IEND chunk (type + fixed CRC AE 42 60 82) was found by scanning.
zip-eocd-scan medium Local-header walk failed; the EOCD was found by scanning and validated (central-directory offset + size must land exactly on the EOCD).
next-header low No structure or footer: the file runs until the next recognised header.
eof low No structure, footer, or following header: the file runs to the end of the image (usually a file truncated by the end of the image, like the last JPEG above).
capped low The file ran past -m (default 512 MiB) and was cut there.

Three rules keep the results honest:

  1. 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 (PNG's first chunk is IHDR, JPEG's markers walk, the gzip stream inflates, …). A failed candidate costs one byte of scanning, not a bogus file.

  2. The next-header scan never looks inside a file's own structure. When a file is truncated, the walk reports how many leading bytes it validated, and the fallback scan starts after them. This matters because a file's own data is full of other formats' magics: 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 would have cut the truncated JPEG in the example above to 13 bytes instead of the 200 that were really there. A candidate next-header only counts if it sizes plausibly itself, or has a magic of at least 4 bytes.

  3. Scanning resumes after the carved file, never inside it. Bytes inside a recovered file are never re-scanned, so an embedded header (a JPEG thumbnail in a PNG's metadata, a ZIP stored inside another file) does not produce a second, bogus carve. The outer file wins.

Limits

  • Only headers at offset 0 of a format count. Formats whose magic sits at a nonzero offset in the file cannot anchor a carve.
  • 2-byte magics are only carved for gzip. Any other format with a 2-byte magic is skipped: in random data it would false-positive every ~64 KiB. (3-byte magics are carved, but only bound a fallback scan if they size plausibly — see rule 2.)
  • ZIP64 is not walked. A ZIP whose sizes are 0xFFFFFFFF falls back to the EOCD scan (medium confidence) instead of the local-header walk.
  • No embedded-file recovery. A file stored inside another carved file is not carved separately (rule 3). Run scar on the carved file itself to go one level deeper.
  • Fragmented files are not reassembled. Like every header carver, scar recovers contiguous runs only; a file whose clusters are scattered across the image comes back as its first run (low confidence) or not at all.
  • Low-confidence carves need checking. A next-header / eof / capped result means "bytes that start with a known header and run this far" — open the file (or run sfile on it) before trusting it.
  • The image is memory-mapped read-only, so very large images need enough virtual address space; the per-file cap (-m) bounds output, not input.

Exit codes

0 = at least one file carved, 1 = usage error, 2 = image unreadable, 3 = nothing found.

See also

  • src/scar.c — the carver (generic engine + six structure walkers).
  • src/footers.h — the footer/trailer signature table it scans for.
  • tests/test_scar.py — the synthetic-image test; run with make test-scar.