Analyze PDFs with colors (and YARA)
A PDF analysis tool for visualizing the inner tree-like data structure[^1] of a PDF in spectacularly large and colorful diagrams as well as scanning the binary streams embedded in the PDF for hidden potentially malicious content. The Pdfalyzer makes heavy use of YARA (via The Yaralyzer) for matching/extracting byte patterns.
You can use pip but pipx is a cleaner way to install for normal users. Developers should probably use poetry.
# If you don't care about extracting text, encrypted PDFs, images, etc. just do this:
pipx install pdfalyzer
# Or if you want to open encrypted PDFs or use the tools that ship with Pdfalyzer (e.g. extract_pdf_text):
pipx install pdfalyzer[extract]
# Then pdfalyze to your hearts content:
pdfalyze martin_heidegger-being_illmatic.pdf
# Consult the help screen if needed:
pdfalyze -h
# (Optional) Forcibly extract all text (including from embedded images) from a file:
extract_pdf_text martin_heidegger-being_illmatic.pdfIf you're looking for one of these things this may be the tool for you.
This tool is mostly for examining/working with a PDF's data and logical structure. As such it doesn't have much to offer as far as extracting text, rendering[^3], writing, etc. etc.
If you suspect you are dealing with a malcious PDF you can safely run pdfalyze on it. Embedded javascript and /OpenAction nodes etc. will not be executed. If you want to actually look at the contents of a suspect PDF you can use dangerzone to sanitize the contents with extreme prejudice before opening it.
Installation with pipx[^4] is preferred though pip3 / pip should also work.
pipx install pdfalyzerIf you are on macOS and use homebrew someone out there was kind enough to make Pdfalyzer available via homebrew so this should work:
brew install pdfalyzerpip3 instead of pipx and have an issue you should try to install with pipx.cryptography package check your pip version (pip --version). If it's less than 22.0, upgrade pip with pip install --upgrade pip.pdfalyze command to work try python -m pdfalyzer. It's an equivalent but more portable version of the same command that does not rely on your python script paths being set up in a sane way..zip, .gz, whatever) PDF that is causing the problem attached (if possible) and I'll take a look when I can. I will not take a look at any uncompressed PDFs due to the security risks so make sure you zip it before you ship it.wheel or cffi you may need to install some packages:sudo apt-get install build-essential libssl-dev libffi-dev rustcRun pdfalyze --help to see usage instructions. As of right now these are the options:
If you provide none of the flags in the ANALYSIS SELECTION section of the --help then all of the analyses will be done except the --streams. In other words, these two commands are equivalent:
pdfalyze lacan_buys_the_dip.pdfpdfalyze lacan_buys_the_dip.pdf -d -t -r -f -y -cThe --streams output is the one used to hunt for patterns in the embedded bytes and can be extremely verbose depending on the --quote-char options chosen (or not chosen) and contents of the PDF. The Yaralyzer handles this task; if you want to hunt for patterns in the bytes other than bytes surrounded by backticks/frontslashes/brackets/quotes/etc. you may want to use The Yaralyzer directly. Yaralyzer is a prequisite for Pdfalyzer so you may already have the yaralyze command installed and available.
.pdfalyzer FileWhen you run pdfalyze on some PDF the tool will check for a file called .pdfalyzer in these places in this order:
If it finds a .pdfalyzer file in either such place it will load configuration options from it. Documentation on the options that can be configured with these files lives in .pdfalyzer.example which doubles as an example file you can copy into place and edit to your needs. Handy if you find yourself typing the same command line options over and over again.
If any PDF object ID between 1 and the /Size reported by the PDF itself could not be successfully placed in the tree warnings will be printed and the pdfalyze command will return an error code (1, specifically) to the shell. If you do not get any warnings and/or pdfalyze returns success then all[^2] of the inner PDF objects should be seen in the output.
BUFFERZONE Team posted an excellent example of how one might use The Pdfalyzer in tandem with Didier Stevens' PDF tools to investigate a potentially malicious PDF (archived in the doc/ dir in this repo if the link rots).
The Pdfalyzer comes with a few command line tools for doing stuff with PDFs:
combine_pdfs - Combines multiple PDFs into a single PDF. Run combine_pdfs --help for more info.extract_pdf_pages - Extracts page ranges (e.g. "10-25") from a PDF and writes them to a new PDF. Run extract_pdf_pages --help for more info.extract_pdf_text - Extracts text from a PDF, including applying OCR to all embedded images. Run extract_pdf_text --help for more info.Running extract_pdf_text requires that you install The Pdfalyzer's optional dependencies:
pipx install pdfalyzer[extract]For info about setting up a dev environment see Contributing below.
At its core The Pdfalyzer is taking PDF internal objects gathered by PyPDF and wrapping them in AnyTree's NodeMixin class. Given that things like searching the tree or accessing internal PDF properties will be done through those packages' code it may be helpful to review their documentation.
As far as The Pdfalyzer's unique functionality goes, Pdfalyzer is the class at the heart of the operation. It holds the PDF's logical tree as well as a few other data structures. Chief among these are the FontInfo class which pulls together various properties of a font strewn across 3 or 4 different PDF objects and the BinaryScanner class which lets you dig through the embedded streams' bytes looking for suspicious patterns.
Here's a short intro to how to access these objects:
…The Pdfalyzer can export visualizations to HTML, ANSI colored text, and both PNG and SVG vector images using the file export functionality that comes with Rich.
If you want to export .png images like the ones below directly from Pdfalyzer you'll need to do one of these things:
brew install --cask inkscape)img extra when installing Pdfalyzer which will install cairosvg: pipx install pdfalyzer[img]. You'll also have to install the CairoSVG executable manually in different ways depending on your operating system, see the CairoSVG docs for the specifics.In our experience Inkscape and CairoSVG both work though we've seen some glitchiness in the rendered output (along with straight up crashes) using CairoSVG on the SVGs rendered by Pdfalyzer so Inkscape is recommended.
As you can see the suspicious /OpenAction relationship is highlighted bright red, as would be a couple of other sus PDF instructions like /JavaScript or /AcroForm if they exist in the PDF being pdfalyzed.
The dimmer (as in "harder to see") nodes[^5] marked with Non Child Reference give you a way to visualize the relationships between PDF objects that exist outside of the tree structure's parent/child relationships.
That's a pretty basic document. Here's the basic tree for a more complicated PDF containing an NMAP cheat sheet.
This image shows a more in-depth view of of the PDF tree for the same document shown above. This tree (AKA the "rich" tree) has almost everything. Shows all PDF object properties, all relationships between objects, and sizable previews of any binary data streams embedded or encrypted in the document. Note that in addition to /OpenAction, the Adobe Type1 font binary is also red (Google's project zero regards any Adobe Type1 font as "mad sus").
PyPDF doing the lifting here but we're happy to take the credit.This repo contains all the PDF specific YARA rules I could dig up that identify byte patterns indicating a PDF may have been modified for evil. While the real dangers exist in PDF modifications no one has seen yet (and thus we have no YARA rules for), there are still a bunch of 'sus' indicators (e.g. has a Type1 font, is incorrectly terminated, etc.) that will be tripped when you scan a PDF with The Pdfalyzer.
Things like, say, a hidden binary /F (PDF instruction meaning "URL") followed b
No open issues yet, or sync has not completed.