JavaScript in PDFs is a double-edged sword. It enables useful features like forms and dynamic content, but it's also one of the most common delivery methods for malware. In this tutorial, we'll walk you through detecting JavaScript in PDF files.
Why JavaScript Matters in PDFs
PDF JavaScript isn't the same as web JavaScript. It runs through Adobe's acrobat.js API and has access to powerful functions:
- File system access (in older versions)
- Network requests
- Execution of external programs
- Reading/writing to disk
This power makes it attractive to attackers. A single malicious PDF can compromise your entire system.
How JavaScript Gets Into PDFs
1. JavaScript Action
The most common method. The PDF contains a JavaScript action that executes when the document opens.
/JS (app.alert('Hello');)
// or
/AA /O << /S /JavaScript /JS (this.getPageNum();)>>
2. OpenAction
Similar to above but specifically triggers on document open.
/OpenAction << /S /JavaScript /JS (malicious code) >>
3. Named Actions
Actions that trigger on events like printing, saving, or closing.
4. Form Fields
PDF forms can have JavaScript attached to calculate fields or validate input.
How to Detect JavaScript
Method 1: Use HackThePDF
The easiest way is to use our JavaScript Payload Detector:
- Go to HackThePDF.com
- Select "JS Payload Detector"
- Upload your PDF
- Review the results
The tool performs a heuristic byte-scan and flags any suspicious scripts.
Method 2: Manual Analysis
For deeper analysis, you can examine the PDF manually:
- Open the PDF in a text editor
- Search for "/JS" or "/JavaScript"
- Look for "app." or "this." function calls
- Check for encoded/obfuscated scripts
Method 3: pdfid Tool
This Python tool analyzes PDF structures:
python pdfid.py suspicious.pdf
Look for elevated counts of /JS, /JavaScript, or /AA (automatic actions).
What to Look For
- /JS — Indicates JavaScript presence
- /JavaScript — Same as above
- /AA — Automatic actions (could be malicious)
- /OpenAction — Code that runs on open
- /AcroForm — Interactive forms (potential attack surface)
Real Example
Here's what a malicious PDF's JavaScript often looks like:
/JS (this.exportDataObject({cName: "update.exe", nLaunch: 2}))
This attempts to extract an embedded file and execute it. If you see anything like this, don't open the PDF.
Conclusion
Detecting JavaScript in PDFs is essential for security professionals and anyone handling suspicious files. Use automated tools for quick analysis, and manual methods for deeper investigation. Stay curious, stay safe.