Skip to content

pdf2john: extract hashes from damaged PDFs (stale xref / trailing junk) - #6061

Open
OrenMatza wants to merge 7 commits into
openwall:bleeding-jumbofrom
OrenMatza:fix/extract-hashes-from-damaged-PDFs
Open

OrenMatza wants to merge 7 commits into
openwall:bleeding-jumbofrom
OrenMatza:fix/extract-hashes-from-damaged-PDFs

Conversation

@OrenMatza

Copy link
Copy Markdown

pdf2john.pl: extract hashes from damaged PDFs

Problem

pdf2john.pl extracts no hash at all from PDFs whose cross-reference
information is damaged, even when the encryption dictionary is fully readable.
Two independent defects trigger this (often together):

  1. Trailing junk after %%EOF — the parser only searches for
    startxref … %%EOF inside the last 1024 bytes of the file (PDF spec,
    appendix H). Garbage appended after %%EOF pushes that pair out of the
    window and the parse aborts with Invalid xref table.
  2. Stale xref offsets — when bytes were inserted or removed in the body,
    the startxref value and every xref entry point at the wrong offset. The
    parser seeks there, does not find xref, and aborts.

In both cases DecryptInit() never runs and no $pdf$ line is printed, so
tools that rely on pdf2john (e.g. Mantis's PDFDecryptor, which shells out to
perl pdf2john.pl <file>) give up on files that are otherwise perfectly
recoverable — qpdf already handles them via "reconstruct cross-reference table".

Fix (run/lib/PDF.pm only)

  • Robust startxref search — scan backwards in overlapping 64 KB chunks
    for the last startxref <offset> %%EOF pair instead of trusting the
    1024-byte window. Healthy files match on the first chunk, same result.
  • Xref reconstruction fallback — when the main xref walk fails, the new
    ReconstructXref() scans the file for N G obj markers and the trailer keys
    /Encrypt and /ID, validates that the Encrypt object looks like a security
    handler dictionary, and feeds the normal FetchObject → DecryptInit path.
    Same strategy as qpdf's xref reconstruction; works no matter how the body
    offsets shifted (constant or scattered).
  • The fallback runs only after the normal parse fails — healthy files take
    the original code path unchanged, and error codes/warnings for secondary xref
    tables are preserved exactly.

Notes

  • Only run/lib/PDF.pm is modified (pdf2john.pl unchanged): +130 / −20.
  • Not covered (behaviour unchanged): damage inside the Encrypt dictionary
    itself, /Filter /PubSec public-key encryption, and trailing junk that
    contains fake startxref/obj markers.lk fails, reconstruct xref

@OrenMatza OrenMatza changed the title 1) scan for latest startxref, instead of relay on EOF. 2) when xref w… pdf2john: extract hashes from damaged PDFs (stale xref / trailing junk) Nov 8, 2026
Oren Matza added 6 commits November 8, 2026 21:53
… full object map)

So nested objects (Info/Root/XMP streams) resolve through the rebuilt
map via the normal FetchObject/ProcessDict path - same strategy as the
patch proposed for ExifTool's PDF.pm (exiftool/exiftool#491).

Assisted-by: y-deepseek-v4.1-flash via opencode
…r dict over scanned keys, warn on recovery

- keep only the newest 8 candidate offsets per object number to bound
  memory when a crafted file is stuffed with fake 'N G obj' markers
- take /Encrypt and /ID from a parseable trailer dictionary in
  preference to the scanned keys, which a crafted file could plant
  inside a stream
- print a 'Reconstructed cross-reference table' warning to stderr when
  recovery runs, so it is never silent (stderr only - the hash line on
  stdout is unchanged)

Assisted-by: y-deepseek-v4.1-flash via opencode
…al xref

A fake startxref/%%EOF pair planted in trailing junk used to win as the
'last pair' and hijack the entry point. Now every pair in the scanned
window is tried newest-first and validated with a small peek (xref
keyword or N G obj header, tolerating blank lines as the walk does);
an invalid pair falls back to the pair before it.

Assisted-by: y-deepseek-v4.1-flash via opencode
…d past 100k reconstructed object markers

- the backward startxref search now stops after 256 KB of reads and falls
  through to reconstruction, so junk appended after %%EOF can no longer
  make it read through the whole file (healthy and small-junk files are
  unaffected: the 1024-byte tail window still finds their pair)
- ReconstructXref refuses to build its object table past 100k distinct
  object numbers and fails closed with the original error, so a crafted
  file stuffed with fake 'N G obj' markers cannot blow up memory or the
  later registration lookups

Assisted-by: y-deepseek-v4.1-flash via opencode
- $MAX_BACK_SCAN_BYTES (262144) bounds the backwards startxref scan
- $MAX_OBJECT_NUMBERS (100000) is the fail-closed limit of reconstructed
  object bookkeeping

No behavior change - same values, just named at the top of ReadPDF and
ReconstructXref.

Assisted-by: y-deepseek-v4.1-flash via opencode
@OrenMatza

OrenMatza commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

pdf2john emits an empty hash for two real-world damaged-PDF classes, both of
which open fine in normal readers (which rebuild the xref):

  1. junk after %%EOF — the startxref … %%EOF marker falls outside the
    spec's 1024-byte tail window
  2. stale xref offsets — bytes inserted/removed in the body, so every xref
    entry points at the wrong offset

Reproducer samples (AES-128, password test123, attached below):
malformed-junk-after-eof-AES-128-open-test123.pdf,
malformed-xref-stale-offsets-AES-128-open-test123.pdf.
On current bleeding-jumbo both yield an empty hash; with this PR both yield the
same hash as the uncorrupted file (verified: john cracks test123).

malformed-junk-after-eof-AES-128-open-test123.pdf
malformed-xref-stale-offsets-AES-128-open-test123.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant