You've got a suspicious PDF. The usual checksâscanning for JavaScript, looking for launch actionsâcome up clean. But your gut says something's wrong. That's where entropy analysis becomes your secret weapon. This technique measures the randomness of data within a PDF, helping you spot encrypted payloads, compressed exploit code, and other hidden nasties that conventional tools miss.
What Is Entropy and Why Should You Care?
Entropy, in the context of information theory, measures how unpredictable data is. Plain English text has low entropyâlots of repeated letters and common words. Compressed or encrypted data has high entropyânear-random byte sequences that look like gibberish to the human eye.
Malware authors love high entropy because it masks their payloads. When they embed shellcode, exploit binaries, or additional malware inside a PDF, they often compress or encrypt it to avoid detection. Entropy analysis exposes exactly that.
The Shannon entropy scale runs from 0 to 8 bits per byte. Here's the quick reference:
- 0-4 bits: Uncompressed, plain text, or simple structure
- 4-6 bits: Mixed content, moderate compression
- 6-8 bits: Compressed data, encrypted payloads, or packed executables
Calculating PDF Entropy: Practical Tools
You don't need a PhD to calculate entropy. Several tools make this straightforward for PDF analysis:
Using peepdf
If you've read our peepdf guide, you're already familiar with this tool. Peepdf calculates entropy automatically when analyzing a PDF:
peepdf -f suspicious.pdf
Look for the entropy graph in the output. Peaks above 7.0 are worth investigatingâsomething is heavily compressed or encrypted in those regions.
Using pngcheck and binwalk
For deeper analysis, extract specific objects and measure their entropy:
binwalk -E extracted_object.bin
Binwalk shows entropy visually, making it easy to spot embedded executables or packed data within PDF streams.
Python Script for Block-Based Entropy
Sometimes you want entropy broken down by section. Here's a simple approach:
import math
from collections import Counter
def shannon_entropy(data):
if not data:
return 0
entropy = 0
for x in range(256):
p = data.count(bytes([x])) / len(data)
if p > 0:
entropy -= p * math.log2(p)
return entropy
# Read PDF in chunks and analyze
chunk_size = 1024
with open('suspicious.pdf', 'rb') as f:
while chunk := f.read(chunk_size):
ent = shannon_entropy(chunk)
print(f"Offset {f.tell()}: {ent:.2f} bits")
This gives you a granular view of which parts of the PDF contain suspicious data.
Real-World Application: Finding Embedded Payloads
Let's walk through a practical scenario. You receive a PDF that passes initial checksâno JavaScript, no launch actions, no obvious exploits. But the file size seems disproportionately large for its content.
Run entropy analysis. You might discover:
- A stream object with entropy of 7.8âextremely high, indicating compressed or encrypted data
- An object that claims to be an image but has executable-level entropy
- Embedded files packed with near-random content
Once you identify high-entropy regions, extract those specific objects for closer inspection. Tools like pdf-parser from the Didier Stevens suite let you isolate and analyze individual objects:
python pdf-parser.py -o 15 suspicious.pdf
Object 15 might be your encrypted payload. Try decompressing itâif it's standard Flate compression, you'll see the actual exploit code or embedded malware.
Limitations and Countermeasures
Entropy analysis isn't foolproof. Skilled attackers know about this technique and try to fool it:
- Padding attacks: Attackers add random garbage data to lower overall entropy
- Layered encoding: Multiple levels of weak encryption or compression can mask payloads
- Legitimate high entropy: Some PDF featuresâdigital signatures, encrypted attachments, certain imagesânaturally have high entropy
Always correlate entropy findings with other indicators. A high-entropy object combined with suspicious metadata or unusual object relationships strengthens your case.
Building Entropy Analysis Into Your Workflow
For regular PDF security work, make entropy analysis a standard step in your malware analysis pipeline:
- Quick scan: Run pdfid to flag suspicious elements
- Entropy check: Use peepdf or binwalk to identify high-entropy regions
- Object extraction: Isolate and decompress suspicious streams
- Behavioral analysis: If possible, execute in a sandbox and monitor system behavior
Combined with the techniques in our other guidesâchecking for launch actions, analyzing stream objects, and detecting embedded JavaScriptâentropy analysis gives you a more complete picture of what's actually inside that PDF.
Wrapping Up
Entropy analysis is one of those techniques that separates casual analysts from serious threat hunters. It catches what signature-based tools miss and gives you a window into the hidden layers of PDF malware. The next time a "clean" PDF gives you a bad feeling, run the entropy check. You might be surprised what you find lurking inside.