How can I extract a JavaScript object from a PDF file using a command line tool?
I am trying to make a GUI using Python with this function.
I found these two modules but couldn't run them: pyPdf2 and pyPdf.
How can I extract a JavaScript object from a PDF file using a command line tool?
I am trying to make a GUI using Python with this function.
I found these two modules but couldn't run them: pyPdf2 and pyPdf.
When you deal with JavaScript in PDFs, you have to be aware of two cases (which you cannot necessarily distinguish in advance, before closely investigating the file in question).
Case 1: Harmless, "useful", "open" JavaScript
The OP gave a link to a sample JavaScript-loaded PDF from PlanetPDF:
That one is easy to handle. Just use
pdfinfo -js
(but be sure that you use one of the most recent, Poppler-based releases -- the XPDF-basedpdfinfo
does not know about-js
!)Here is the result:
As you can see,
-js
attempts to automatically extract all JavaScript from the PDF and prints it to<stdout>
.This one was a harmless JavaScript, not trying to hide itself, not obfuscated, inserting the current date into a form field, after popping up an info message about what it is going to do.
Case 2: Malicious, damaging, hidden and obfuscated JavaScript
There are numerous examples of PDFs out in the wilderness containing JavaScripts which are not as harmless as the above, written by Malware authors who are after your money, or just after the "fun" it gives them if they succeed.
The JavaScripts in these cases are very frequently hidden and obfuscated.
For example, in order to hide the fact that there is even JavaScript contained, they do not use the 'clear'
/JavaScript
and/JS
names in the respective PDF object dictionaries. These names must be present for the PDF readers to know what they should do with the object.Instead, they use another method to express the same names:
This method, unfortunately, was even made "legal" by the official PDF specification documents. It allows to replace a selection of some or even of all characters in a PDF name token by their respective ASCII hex number (combined with a leading hash sign for each replaced char).
This can fool some of the more naive attempts to find the
/JavaScript
string inside a PDF (such as using a simplegrep -a
).There are a few Free Software tools available, which can be used to dissect and analyze such cases:
Didier Stevens' Python scripts
pdfid.py
andpdf-parser.py
are very useful for a first look (and even for a complete analysis) of these cases.Jose Miguel Esparza's Python framework peepdf is even more powerful. It can even de-obfuscate, beautify and make readable again any obfuscated JavaScript contents inside the PDF.
Origami is Ruby-based, and also quite powerful. And there are a few more...
But all these tools are only useful if you already have (at least some basic) knowledge about PDF syntax (and about JavaScript, of course).
Here are three short examples using
pdfid.py
against three different PDFs:the first does not cantain any JavaScript that is discovered by
pdfid.py
:the second contains JavaScript, and the name
/JavaScript
appears in clear text inside the PDF:the last contains JavaScript, and the name tokens
/JavaScript
and/JS
both are obfuscated:The fact that
pdfid.py
lists a second number in parentheses shows, that it discovered the obfuscation. 30 out of 30/JavaScript
name tokens are obscured -- this makes the PDF file highly suspicious, which warrants further investigation. Because no "normal" PDF generating tool (that is known to me) uses this obfuscation...Update
A list of different methods (including command line tools) is available in another answer of mine here:
The best tool currently is peepdf.py, because it can handle even heavily obfuscated JavaScript. This is a Python framework to explore (and change) the source code of PDF files, specialized in analysing malicious PDFs.
Its author(s) recently added the
extract
sub-command, which extracts and prints the source code of JavaScripts contained in the PDF:Short usage info:
git clone https://github.com/jesparza/peepdf.git git.peepdf
$PATH
) to the script:cd git.peepdf ;
ln -s $(pwd)/peepdf.py ${HOME}/bin/peepdf.py
echo 'extract js > all-javascripts-from-my.pdf' > xtract.txt
-l
, and force mode to ignore errors,-f
) to execute non-interactively the sub-command line(s) contained in the newly created script file,-s
:peepdf.py -l -f -s xtract.txt my.pdf
cat all-javascripts-from-my.pdf