Repository navigation
Conversation
…alk fails, reconstruct xref
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
Author
|
Reproducer samples (AES-128, password malformed-junk-after-eof-AES-128-open-test123.pdf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pdf2john.pl: extract hashes from damaged PDFs
Problem
pdf2john.plextracts no hash at all from PDFs whose cross-referenceinformation is damaged, even when the encryption dictionary is fully readable.
Two independent defects trigger this (often together):
%%EOF— the parser only searches forstartxref … %%EOFinside the last 1024 bytes of the file (PDF spec,appendix H). Garbage appended after
%%EOFpushes that pair out of thewindow and the parse aborts with
Invalid xref table.the
startxrefvalue and every xref entry point at the wrong offset. Theparser seeks there, does not find
xref, and aborts.In both cases
DecryptInit()never runs and no$pdf$line is printed, sotools that rely on pdf2john (e.g. Mantis's PDFDecryptor, which shells out to
perl pdf2john.pl <file>) give up on files that are otherwise perfectlyrecoverable — qpdf already handles them via "reconstruct cross-reference table".
Fix (run/lib/PDF.pm only)
startxrefsearch — scan backwards in overlapping 64 KB chunksfor the last
startxref <offset> %%EOFpair instead of trusting the1024-byte window. Healthy files match on the first chunk, same result.
ReconstructXref()scans the file forN G objmarkers and the trailer keys/Encryptand/ID, validates that the Encrypt object looks like a securityhandler dictionary, and feeds the normal
FetchObject→DecryptInitpath.Same strategy as qpdf's xref reconstruction; works no matter how the body
offsets shifted (constant or scattered).
the original code path unchanged, and error codes/warnings for secondary xref
tables are preserved exactly.
Notes
run/lib/PDF.pmis modified (pdf2john.plunchanged): +130 / −20.itself,
/Filter /PubSecpublic-key encryption, and trailing junk thatcontains fake
startxref/objmarkers.lk fails, reconstruct xref