Skip to content
SlateGitOrgPublic

About

A disk-image file carver that rejoins fragmented PNG and ZIP files by searching gap positions checked against their own CRCs, and never labels an output recovered unless every check passes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

gapcarve

CI

gapcarve

gapcarve is a file carver for raw disk images that rejoins fragmented PNG and ZIP files. When a header-to-footer pass produces a corrupt file, it localises the split using the first chunk that fails its CRC-32, then searches gap lengths until the candidate passes every integrity check the format offers. A file is labelled recovered only if those checks pass. The runtime uses the Python standard library only.

The problem

When a file table or filesystem metadata is gone, examiners carve files by signature. Header-to-footer carvers cut from a header to the next footer, which returns corrupt output whenever the file was fragmented, and the corrupt file is listed next to the good ones. An examiner who presents a carved screenshot or archive has to be able to say that it is byte-exact and which image offsets it came from. gapcarve treats validity as a checked property: PNG chunk CRCs plus the exact decompressed size, and ZIP entry CRCs and sizes.

How it works

flowchart TD
    A[Raw image<br/>memory-mapped] --> B[Signature scan<br/>PNG and ZIP headers]
    B --> C{Contiguous<br/>validation passes?}
    C -- yes --> L1[contiguous]
    C -- no --> D[Localise the split<br/>first failing chunk or<br/>central directory length]
    D --> E[Gap search over cluster<br/>boundaries, CRC-validated]
    E -- found --> L2[bifragment<br/>gap start and length]
    E -- not found --> F{Valid prefix?}
    F -- yes --> L3[partial<br/>written as .partial]
    F -- no --> L4[rejected]
    L1 --> O[Recovered files<br/>plus DFXML and JSON report]
    L2 --> O
Loading
  • PNG: the chunk walker checks each chunk length and CRC-32, IHDR sanity (8-bit RGB or RGBA, non-interlaced) and that the IDAT stream inflates to exactly height x (1 + width x bytes_per_pixel) bytes. The first failing chunk limits the split to a few cluster boundaries (at most three for IDAT chunks of 8 KiB or less; a failing chunk declared longer than 64 KiB gets none inside it), and gap lengths are tried upward from one cluster. Each PNG has a work budget of 4096 candidates and 256 MiB of CRC-checked bytes; spending either ends it as partial with the reason recorded.
  • ZIP: the central directory fixes the gap length as (EOCD end - header) - archive length, so only the split cluster is searched. Each entry is inflated and checked against its CRC-32 and size.
  • Only contiguous and bifragment outputs are written as recovered files. partial keeps the longest validated prefix in a .partial file, and rejected writes nothing. dfxml.xml lists recovered files only, so a standard DFXML consumer never sees a hash on a truncated file; partials appear in summary.json with their label and reason.

Quick start

python3 -m venv .venv
.venv/bin/pip install -e ".[dev]"
.venv/bin/gapcarve demo                       # build the synthetic image and carve it
.venv/bin/gapcarve carve .tmp/demo/disk.img -o .tmp/out
.venv/bin/gapcarve report .tmp/out            # table; --format json or xml
.venv/bin/gapcarve compare .tmp/demo/disk.img --truth .tmp/demo/truth.json
.venv/bin/pytest

Without --truth, compare has no independent reference: the gapcarve column reads validated (the output passed its own integrity checks) and the naive column is compared against that output.

carve and compare take --sector-size (default 512), --cluster-size (default 4096) --max-gap (default 256 clusters) and --max-candidates (default 4096 per PNG).

Example output

The image is synthetic (seed 133, 8 MiB). gapcarve demo produces the table below. The offsets depend on the exact byte length of the zlib.compress output used to build the image, so a Python built against another zlib (zlib-ng, for instance) can lay the files out differently and print different offsets; the tests never hard-code offsets.

image: disk.img, 8388608 bytes, seed 133
sector 512 bytes, cluster 4096 bytes

type  offset   label       gap_start  gap_clusters  gapcarve  naive
----  -------  ----------  ---------  ------------  --------  --------
png   20480    bifragment  110592     60            match     MISMATCH
png   425984   contiguous  -          -             match     match
zip   618496   bifragment  626688     5             match     MISMATCH
png   741376   bifragment  745472     1             match     MISMATCH
png   974848   rejected    -          -             no file   MISMATCH
png   1024000  partial     -          -             no file   MISMATCH
png   1228800  partial     -          -             no file   MISMATCH
png   1511424  bifragment  1556480    3             match     MISMATCH
png   1847296  contiguous  -          -             match     match
png   2158592  contiguous  -          -             match     match
zip   2404352  contiguous  -          -             match     match
png   2551808  contiguous  -          -             match     match
zip   2748416  contiguous  -          -             match     match
zip   2850816  bifragment  2883584    9             match     MISMATCH
png   3002368  bifragment  3219456    17            match     MISMATCH
png   3219456  contiguous  -          -             match     match
png   3354624  contiguous  -          -             match     match
zip   3661824  contiguous  -          -             match     match

byte-exact against ground truth: gapcarve 15/15, naive 9/15

gapcarve carve writes the recovered files, summary.json and dfxml.xml. In dfxml.xml each fileobject (recovered files only; the .partial prefixes are in summary.json) lists its byte_runs as image offsets and lengths and a SHA-256 hashdigest.

Design decisions

  • First failing chunk instead of a blind split search. A candidate (split, gap) costs a chunk walk and an inflate. Localising the split to at most three cluster boundaries (for IDAT chunks of 8 KiB or less; larger chunks have proportionally more, up to a 64 KiB cap) makes the search linear in the gap length, and a candidate and byte budget bounds the worst case, such as a junk length field that looks sane. A test counts validator calls and asserts at most 3 x (g + 1) candidates for a gap of g clusters. The cost is the assumption that the split falls on a cluster boundary.
  • Bifragmentation only. Files in more than two pieces are reported as partial or rejected, never guessed. This keeps the search bounded and every output verifiable.
  • Validity by the file's own checks. CRC-32 per chunk and the exact inflated size reject a wrong join with overwhelming probability, but they are integrity checks, not authenticity checks. A file that passes is intact, not proven to be the original.
  • Gap length from the ZIP central directory. The directory states how far the end of the archive is from its header, so no length search is needed. Archives with a damaged directory end as partial.
  • No overlap between outputs. Headers inside an already carved file are skipped, so validated byte runs never overlap.
  • Known limits. Only 8-bit RGB and RGBA non-interlaced PNGs are validated: a palette, grayscale or 16-bit PNG is labelled rejected (or partial) even when intact and contiguous. ZIP64 archives and ZIP entries that use a data descriptor are not supported and end as rejected or partial.
  • Synthetic ground truth. The generator records SHA-256 and byte runs for every planted file, so the tests compare against known answers instead of the carver's own opinion.

Testing

GITHUB_ACTIONS=true CI=true COLUMNS=80 .venv/bin/pytest

92 tests, including a hypothesis test that the PNG validator rejects any single flipped bit inside an IDAT chunk, an instrumented bound on candidates tried, and ground-truth checks against the synthetic image.

Project layout

src/gapcarve/
  image.py          memory-mapped image reader, cluster-aligned iteration
  signatures.py     header and footer scanning
  validate_png.py   chunk walker, IHDR checks, IDAT size check
  validate_zip.py   central directory, local headers, entry CRCs
  carve.py          contiguous attempt, bifragment gap search, labels
  naive.py          header-to-footer carver for comparison
  compare.py        comparison rows and ground-truth checks
  report.py         DFXML and JSON summary
  synth.py          synthetic image builder and ground truth
  cli.py            gapcarve command
tests/              pytest suite

License

MIT. See LICENSE.

About

A disk-image file carver that rejoins fragmented PNG and ZIP files by searching gap positions checked against their own CRCs, and never labels an output recovered unless every check passes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages