Formats

Why flipping random bytes does not corrupt a DOCX

A .docx is a ZIP archive, and that single fact is why most online file corrupters produce a document Word opens without complaint.

Files Corrupter ·

Why flipping random bytes does not corrupt a DOCX

Almost every online file corrupter works the same way: read the file into a byte array, overwrite a few dozen bytes at random offsets, hand it back. On a plain text file that is enough. On a Word document it usually is not, and the reason is worth understanding if you rely on these tools for testing.

A .docx is a ZIP archive

Since Office 2007, a .docx has been a ZIP container holding XML parts: word/document.xml for the text, word/styles.xml for formatting, word/_rels/ for the relationships between parts, and [Content_Types].xml as the manifest.

Those parts are deflate-compressed inside the archive. When a naive corrupter picks fifty random offsets in a 2 MB document, the overwhelming majority land inside compressed streams rather than in structural records.

Why that is survivable

A damaged compressed stream is a local problem. Word notices the checksum mismatch on one part, reports “unreadable content”, and offers to recover. The central directory is still intact, so it can still enumerate every entry, read the parts that are fine, and rebuild a document from them. You get a file that opens, with some formatting lost.

That is not corruption in any useful sense. If you are testing an upload pipeline against malformed input, a file that still parses tells you nothing.

What actually breaks the container

The ZIP format is read from the back:

  1. The end of central directory record sits in the last 64 KB and points at the central directory.
  2. The central directory lists every entry with its name, size and offset.
  3. Each entry begins with a local file header.

Destroy the end-of-central-directory record and the file stops being a ZIP at all. Destroy the central directory and nothing can enumerate it. Destroy the local file headers as well and you defeat the fallback that repair tools use, which is to scan forward looking for local header signatures when the index is missing.

Take out all three and there is nothing left to recover from. Word does not offer to repair, because it cannot even identify the file as a package.

The same pattern everywhere

This is not specific to Office. Nearly every format people care about has an index that matters more than its payload:

  • PDF keeps a cross-reference table, and Acrobat rebuilds it automatically when it is damaged, which is why byte-flipping a PDF so often produces a file that still opens.
  • MP4 keeps the sample table in the moov atom. Damage the payload and a player skips ahead; damage moov and nothing can locate a single frame.
  • TAR has no index at all, which makes it the most recoverable archive format in existence, because tar simply scans forward for the next header.

The same pattern in a document format that is not a ZIP archive is covered in how a PDF finds its own pages, and the recovery side of it, what repair tools can and cannot rebuild, is in how archive repair tools actually work.

The general rule is that structure matters more than payload. A corrupter that only touches payload is testing your decompressor’s error handling. A corrupter that destroys structure is testing whether your code fails safely when handed something that is not the format it claims to be, which is the case that actually shows up in production.

What to check

If you are evaluating a file corrupter for testing work, take the output and try this:

  • Open it in the native application and click through any repair prompt.
  • Run unzip -l or 7z l on Office files. If it lists entries, the container survived.
  • For PDFs, check whether the %%EOF and startxref markers still appear in the tail.

If any of those succeed, the file is damaged rather than corrupted, and your error-handling path is not being exercised the way you think it is. To see the difference immediately, break a DOCX on purpose at Standard level and try Word’s Open and Repair on the result.