Open a PDF in a normal reader and you get pages. Open the same file in a security scanner and you might get a parse error. Both tools read the same bytes, and the reason they disagree tells you something about how the file was built.

That disagreement is not always an accident. Deliberately malformed PDFs are a known way of slipping content past automated inspection, because most scanners walk the document structure strictly while most readers are built to be forgiving. The thing being scanned and the thing being read are no longer the same thing.

Core idea: a PDF that fails to parse is not automatically safe, and a PDF that renders is not automatically clean. Structure anomalies are a signal to investigate, not a verdict.

How PDF Structure Works, and Where It Breaks

Every PDF is a set of numbered objects, each one an indirect reference such as 12 0 obj. The cross-reference table, or xref, is the index that says where object 12 starts in the file. At the end of the document sits %%EOF and the whole thing works like a table of contents for a byte stream.

Modern files replace the plain xref table with an xref stream, which is itself a compressed object whose entries point at other objects. Either way, the reader's first move is to find the trailer, read the xref, and jump to each object by byte offset.

The three classic malformations

1. Wrong byte offsets

An xref entry says object 7 lives at offset 41230. Change that number to 412300 and the reader is sent to the wrong place in the file. A strict parser reports a hard failure. A forgiving reader sees garbage, discards the bad entry, and rebuilds the index by scanning the file for obj markers. The document renders. The scanner never got to the content.

2. A missing or empty xref

Some files have no usable xref, or an xref that points to a startxref value nowhere near the real end of the file. Reconstruction from scratch is the only way to read them, which is exactly where different implementations produce different object graphs.

3. Length mismatches on streams

A stream dictionary declares /Length 5000 and the file actually contains 5000 bytes plus a hidden chunk. A parser that trusts the dictionary stops early and never sees the extra data. A parser that respects the declared length behaves exactly as the attacker intends, because the hidden bytes sit behind content the scanner believes it has already finished reading.

# Where a strict parser gives up and a resilient one shrugs
$ qpdf --check suspicious.pdf
qpdf: suspicious.pdf: file is damaged
qpdf: attempting to reconstruct cross-reference table
$ mutool info suspicious.pdf 2>& | head -5
mutool: cannot recognize format as PDF, but trying anyway
info: 12 pages, 5 objects, 1 streams, 2 fonts, 0 images

One tool refuses. Another recovers the objects and reports the page count. That difference is the fingerprint.

Why Malformed Structure Helps Malware

The evasion logic is straightforward. Scanners and viewers disagree about what a valid PDF is, and malware authors only need the scanner to be on the losing side of that disagreement.

Parser disagreement as cover

Malicious content sitting in an object that only reconstruction-based readers surface means a scanner requiring a valid xref never inspects it. The file reaches a user, opens in Acrobat or Chrome, and the payload that was invisible to the scanner executes.

Offset shifting for payload placement

When xref offsets are wrong, some readers fall back to sequential parsing, interpreting the file as one continuous byte sequence. An attacker can position a shellcode blob or redirect chain after padding and know which family of readers will find it.

Confusing pre-processing and extraction

Sandbox and detonation setups often rely on a text or object extraction step. If that step errors out, the automated verdict becomes "no content of interest" rather than "could not parse". The sample gets waved through with a clean bill of health it never earned.

Sandbox note: if your detonation pipeline swallows parser errors and reports "clean", you are not measuring the file's safety. You are measuring the file's compatibility with your parser.

Triage: Finding Structural Anomalies

The first rule of PDF forensics is to describe the file before interpreting it. A structural triage pass is fast, mostly free, and it tells you whether you are looking at a normal document.

Step one: run a structural checker

# qpdf's checker reports every structural problem it finds
qpdf --check suspicious.pdf

# Mutool lists the recovered object inventory
mutool info suspicious.pdf

# pdftk dumps the object map and metadata
pdftk suspicious.pdf dump_data

Note the count of revisions and objects. A mismatch between what qpdf reports, what mutool recovers, and what the reader shows is the finding.

Step two: inspect the trailer and xref directly

# Show the trailer dictionary
tail -c 2048 suspicious.pdf | strings | tail -20

# Find the startxref pointer and see where it actually lands
grep -a "startxref" suspicious.pdf

# Count how many revisions the file claims
grep -a -c "%%EOF" suspicious.pdf

If startxref points to an offset that does not land on an xref keyword, the file was rewritten by something other than a conforming writer.

Step three: check whether the two parsers agree

# Objects seen by a reconstruction-based reader
mutool show suspicious.pdf trailer
mutool show suspicious.pdf xref

# Raw object headers with their real positions
grep -a -b -o "[0-9]* 0 obj" suspicious.pdf | head -20

Comparing the offsets qpdf believes with the offsets where objects actually sit tells you what was tampered with and how far.

Repairing a Suspect File for Deeper Analysis

You cannot analyse what you cannot read. Repair the structure first, then run the analysis on the normalised file. Repaired copies are for analysis only, never for forwarding to anyone else.

# Rewrite the file with a rebuilt xref table
qpdf --qdf --object-streams=disable suspicious.pdf repaired.pdf

# Force a linearisation pass, another good normalisation
mutool clean -d suspicious.pdf cleaned.pdf

# Expand all streams so content becomes greppable
qpdf --qdf --stream-data=uncompress suspicious.pdf expanded.pdf
strings expanded.pdf | grep -iE "http|\.exe|powershell|cmd\.exe"

Uncompressed streams matter. A lot of scanners and a lot of quick manual reviews look at the compressed bytes and find nothing. Expanding them is often the moment the payload becomes visible.

Compare the repaired file to the original

# Did repair change anything structurally?
qpdf --check repaired.pdf
md5sum suspicious.pdf repaired.pdf

A large size delta between the original and the repaired copy is itself informative. It usually means the original was carrying content the parser was skipping.

Building This Into Your Pipeline

The checks are cheap enough to run on every inbound PDF.

  • Run qpdf --check and treat any error as a review trigger rather than a pass
  • Parse with two independent tools and flag when their object counts disagree
  • Expand streams and run content scanning against the normalised copy, not just the original
  • Record the structural warnings alongside the verdict so an analyst sees the shape of the file
  • Never forward a repaired file as if it were the original. The repair itself changes hashes and can mask what was really there

Key Takeaways

A PDF does not have to be valid to be dangerous. Broken cross-reference tables, wrong byte offsets, and mismatched stream lengths let an attacker hide content from strict parsers while leaving it fully visible to forgiving readers, which is the whole basis of PDF malware evasion.

The practical habit: when a file behaves differently across tools, treat the disagreement as the finding. Run qpdf --check, compare object inventories between two parsers, repair a working copy, and scan the expanded content. An unscannable file is not a clean file.

Frequently Asked Questions

Does qpdf --check failing mean the file is malicious?

No. Legitimate files get damaged by truncating uploads, bad scanners, and partial downloads all the time. A structural error is a reason to look closer, not a reason to declare a verdict.

Why can a broken PDF still open normally?

Because many readers can rebuild the cross-reference index by scanning the file for object markers when the table fails. The document works, but a parser that trusts the table will not reach the same objects.

Should I analyse the repaired file or the original?

Analyse both. The original tells you what the sender intended the reader to see. The repaired copy tells you what a tolerant parser will actually reconstruct. Differences between the two are worth your attention.

Is a repaired PDF safe to forward to someone else?

No. Repairing rewrites the structure and changes the hash, so it destroys the evidence trail and may hide how the original was constructed. Keep repaired copies inside the analysis environment.