A HEIF with no header
ASIS CTF Finals 2017: If He Finds Out, a Broken HEIF Image
The file command calls it data, yet the first bytes spell out a HEIF container. Comparing it with a known-good HEIC sample shows exactly four missing bytes and one overwritten brand list.
- 01
Inspect
file reports plain data, but xxd shows ftyp, mif1, a meta box and two infe entries labelled hvc1 HEVC Image.
- 02
Hypothesize
The challenge name is an anagram of HEIF, so the file is a damaged HEIF container with HEVC-coded images.
- 03
Test
A valid .heic sample is compared byte by byte with the first 0x100 bytes of the challenge file.
- 04
Solve
Prepend the 4-byte box size, restore the 12-byte compatible brand list, then render the file with a reference JavaScript HEIF reader.
What the challenge provided
The forensics task shipped a single file named ifhe_Find_Out. The name is the first hint: shuffle the letters of “ifhe” and the result is HEIF, the High Efficiency Image File Format. The task was worth 343 points at ASIS CTF Finals 2017, a jeopardy-style online event organized by the ASIS team, and the whole solve fits into a few shell commands once the format is recognized.
Running file on it returns just data. That answer is useful in itself. Tools such as file and libmagic match signatures at known offsets, so a plain “data” means that none of the usual signatures are where they should be. The file is either encrypted, compressed in an unknown way, or a known format with a damaged start.
First look at the hex
The first 0x100 bytes, dumped with xxd -l 0x100, are mostly readable ASCII. The bytes at offset 0 read ftypmif1, followed by a run of zero bytes and then asisctffinal. A little further on come the strings meta, hdlr, pict, pitm, iloc, iinf, infe and, twice, hvc1HEVC Image.
These are ISO base media file format boxes. Each box starts with a 4-byte big-endian size and a 4-byte type, so a healthy file begins with a size field and only then the letters ftyp. Here the letters ftyp are at offset 0, which means the leading size is gone. The hvc1 entries describe images coded with HEVC, and a search for that string leads straight to HEIF, which matches the anagram in the file name.
Comparing against a good file
Guessing at box layouts from memory is error-prone, so the efficient move is to download a valid sample from the same family, here an example .heic photo of 1440 by 960 pixels from a public HEIF sample set, and dump the same range. The two dumps line up almost perfectly once the shift is accounted for. Everything from meta onward has the same structure in both files: the same hdlr box with handler type pict, the same pitm box naming the primary item, the same iloc and iinf tables.
The differences are small and concrete:
| Offset in reference | Reference bytes | Challenge file |
|---|---|---|
| 0x00 | 00 00 00 1c |
absent |
| 0x10 to 0x1b | mif1heichevc |
asisctffinal |
| meta size | 00 00 02 00 |
00 00 02 1c |
The ftyp box is 28 bytes long: 4 bytes of size, 4 of type, 4 of major brand (mif1), 4 of minor version and a 12-byte list of compatible brands. That arithmetic explains the 00 00 00 1c at the start of the reference, and it explains why the replacement text asisctffinal is exactly 12 characters. The author of the challenge swapped the brand list for the event name and cut the size field, two edits that are trivial to make and just enough to defeat automatic detection. The differing meta size, 0x21c in the challenge file, is not damage: the challenge image simply has different content than the sample, and the size field was left alone.
Repairing it
Two dd calls rebuild the header. The first writes the four size bytes from the sample in front of the challenge file:
dd if=autumn_1440x960.heic bs=1 count=4 | cat - ifhe_Find_Out > ifhe_fixed
The second copies the twelve brand bytes from offset 16 of the sample over the same range in the repaired file, with conv=notrunc so the file is not truncated:
dd conv=notrunc if=autumn_1440x960.heic of=ifhe_fixed bs=1 count=12 skip=16 seek=16
Dumping the first 0x80 bytes of ifhe_fixed should now show 0000 001c 6674 7970 6d69 6631 0000 0000 6d69 6631 6865 6963 6865 7663 and a normal meta box right after it. A common wrong turn is to repair only the brand text and forget the missing size, which shifts every later box by four bytes and leaves the parser lost.
Getting the picture on screen
In 2017 almost no image viewer opened HEIF. Nokia published a reference implementation in JavaScript, with bundled sample files, in a repository on GitHub, and it renders HEVC-coded images inside the page. Cloning the gh-pages branch, replacing content/images/autumn_1440x960.heic with the repaired file and opening examples.html shows the picture when the matching example is clicked.
Firefox is the browser to use for this. Chrome treats every file:// path as a separate origin and blocks the reader from loading the image data, so the page appears to do nothing. Serving the folder over a local HTTP server avoids the problem in any browser. The flag was written inside the image in the format of the event, so no further decoding was needed.
What transfers to other challenges
Three habits carry over to most forensics files that refuse to identify themselves. First, read the first sixteen bytes as text before reaching for tools, because a signature, a brand string or a tag often gives the format away. Second, count the bytes of the structure you suspect: a box size, a chunk length or a header length that does not add up points directly at the edit. Third, compare against a clean sample of the same format instead of reading the specification top to bottom.
The same approach applies to PNG chunks with a bad CRC, ZIP archives with a mangled local header and ELF files with a zeroed ident. When a structure is damaged on purpose, the sample file is the specification in executable form. For a feel of how header regions differ from payload regions in a byte picture, see binary visualization explained, and for a related damaged-file challenge from another event, the BITSCTF 2017 Fanfie writeup takes a similar route.
Questions readers ask
Why does the file command report only data?
file identifies formats by signatures at fixed offsets. HEIF and other ISO base media files carry their signature at offset 4 (the ftyp type), after a 4-byte box size. With the size field missing, the signature sits at offset 0 and no rule matches, so file falls back to the generic answer.
Could a hex editor alone have solved it?
The repair itself needs only a hex editor or dd. The harder part is knowing what a correct file looks like, which means obtaining a valid sample of the same format and comparing the two headers line by line.
Why was the image opened in a browser?
In 2017 HEIF had almost no desktop viewer support. A reference reader written in JavaScript decodes HEVC images in the page, so replacing one of its bundled sample files with the repaired file was the quickest way to see the picture.
New to capture the flag? Formats, teams and a first-month plan in one place.
Read the CTF guide