PDF Strings and Decoding: Extracting Hidden Payloads
When analyzing suspicious PDF files, what you see on the surface is rarely the full picture. Attackers hide malicious code, encoded commands, and exploit payloads deep within PDF structures — often obscured through hexadecimal encoding, base64 transformation, or custom obfuscation routines. This guide shows you how to dig into PDF strings to extract payloads that traditional antivirus tools miss.
Why Strings Matter in PDF Analysis
The strings command is one of the fastest ways to peek inside any binary file, and PDFs are no exception. Even when JavaScript is stripped or compressed, residual strings often remain — function names, URL patterns, file paths, and encoded payloads leave fingerprints that can be analyzed.
In our experience at HackThePDF, strings analysis catches about 30% of malicious PDFs that pass initial static scans. It's a lightweight technique that works on any operating system and requires no specialized tools beyond the terminal.
Extracting Strings from PDF Files
Start with the basics. Pull all readable ASCII and Unicode strings from a PDF:
strings suspicious.pdf | head -100
strings -e l suspicious.pdf # Unicode (little-endian)
strings -e S suspicious.pdf # Big-endian
The first 100 lines often reveal metadata, author information, and embedded URLs. Look further down for the interesting stuff — that's where encoded payloads hide.
To save all strings for grep-based analysis:
strings suspicious.pdf > pdf_strings.txt
grep -i "javascript\|eval\|document\|app\|shellcode" pdf_strings.txt
Decoding Hex-Encoded Payloads
One of the most common obfuscation techniques in malicious PDFs is hex encoding. Attackers convert JavaScript or shellcode into hex strings that look innocuous until decoded.
Here's how to spot and decode hex patterns:
# Extract hex strings (continuous sequences of 0-9a-f)
strings suspicious.pdf | grep -E "^[0-9a-f]{8,}$" > hex_strings.txt
# Decode using xxd or printf
xxd -r -p hex_strings.txt decoded_output.bin
Watch for patterns like \x escape sequences or long strings of hex characters that don't match typical PDF structure. If you find something around 1000+ characters of seemingly random hex, decode it — there's a good chance it's a payload.
Base64 Decoding in PDFs
Base64 encoding appears frequently in PDF malware, particularly in JavaScript that uses app.doc.getProp("Content") or custom decryption routines. Extract potential base64 strings and decode them:
# Find base64-like strings (longer = more suspicious)
strings suspicious.pdf | grep -E "^[A-Za-z0-9+/]{20,}={0,2}$" > b64_strings.txt
# Decode the first candidate
cat b64_strings.txt | head -1 | base64 -d > decoded.bin
After decoding, run strings on the output. If you see JVBERi (the base64 header for PDF), you've found an embedded PDF within a PDF — a classic evasion technique.
Working with Object Streams
PDF objects often contain streams compressed with FlateDecode. While tools like peepdf automatically decompress these, you can manually extract and analyze raw object data:
# Extract all stream data
pdfid -s suspicious.pdf # List streams
peepdf -s suspicious.pdf # Detailed analysis
Within decompressed streams, look for /JS (JavaScript), /AA (additional actions), and /OpenAction — these are the primary triggers for automatic code execution.
Practical Analysis Workflow
Here's a practical workflow combining strings extraction with decoding:
- Pull all strings:
strings suspicious.pdf > all_strings.txt - Search for keywords:
grep -iE "javascript|eval|unescape|shellcode|cmd|powershell|http" all_strings.txt - Identify encoding: Look for patterns — long hex strings suggest hex encoding, base64 patterns suggest base64
- Decode candidates: Pipe through xxd, base64, or custom decoders
- Analyze decoded output: Run strings again on decoded files to find embedded scripts
Real-World Example
Consider a PDF that triggers no antivirus alerts but contains a suspicious 4,096-character hex string in object 5. Extracting that string and running it through an xxd reverse yields a JavaScript payload that downloads a second-stage payload from a compromised domain. This is exactly the kind of multi-stage attack that strings analysis catches.
The key is patience — not every odd-looking string is malicious, but unusual patterns deserve investigation. In our testing, PDFs with strings longer than 500 characters of apparent gibberish correlate with malicious intent about 70% of the time.
Automated Tools for Strings Analysis
If manual analysis feels slow, several tools automate strings extraction and flag suspicious patterns:
- pdfid: Scans PDF for suspicious keywords and element counts
- peepdf: Interactive PDF analysis with automatic decoding
- peepdf: Also extracts and displays encoded JavaScript
- CyberChef: Web-based tool for hex/base64 decoding with recipe support
Defensive Recommendations
For security teams and SOC analysts, strings analysis should be part of the standard PDF inspection workflow before sandbox detonation. It catches encoded payloads that sandbox detonation might miss if the PDF's triggering conditions aren't met.
Consider implementing automated strings extraction as a first-pass filter in your email security pipeline. Any PDF with hex or base64 strings exceeding a certain length threshold gets flagged for deeper inspection.
Conclusion
Strings analysis won't catch every PDF threat, but it's a fast, effective technique that belongs in every security analyst's toolkit. Combined with entropy analysis, object inspection, and behavioral sandboxing, it forms a comprehensive approach to PDF malware detection. Start pulling strings from your suspicious files — you might be surprised what surfaces.