Skip to main content
CTF Support
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

PDF Files

Introduction

PDF files are complex document formats that can contain embedded files, JavaScript, forms, and multiple data streams. In CTF forensics challenges, flags are often hidden in PDF metadata, obfuscated JavaScript, or attached files. Understanding PDF structure is essential for thorough analysis.

Quick Reference

Command Description
pdfinfo file.pdf Display PDF metadata
pdfdetach -list file.pdf List embedded files
pdfdetach -save 1 -o out file.pdf Extract embedded file
pdf-parser file.pdf Parse PDF structure
pdfimages file.pdf output Extract all images
strings file.pdf | grep "CTF{" Search for plain text flags

Tools & Resources

Tool / Resource Description
pdfinfo Extract PDF metadata (part of poppler)
pdfdetach List and extract embedded files
pdfimages Extract images from PDFs
pdf-parser Analyze PDF structure and streams
peepdf Interactive PDF analysis with JavaScript support
PDFStreamDumper GUI tool for PDF analysis (Windows)
qpdf PDF transformation and inspection
exiftool Extract comprehensive metadata

PDF Structure Basics

PDF files are structured into several components:

  • Header - PDF version (e.g., %PDF-1.7)
  • Body - Objects containing data (text, images, streams)
  • Cross-reference table - Index of objects
  • Trailer - Points to cross-reference table and root object

Objects can contain:

  • Text content
  • Images
  • Embedded files (attachments)
  • JavaScript code
  • Forms and annotations
  • Fonts

Analyzing PDF Metadata

Extract basic metadata with pdfinfo:

pdfinfo document.pdf

Example output:

Title:          Secret Document
Author:         CTF Organizer
Creator:        Adobe Acrobat
Producer:       PDFlib 9.0.0
CreationDate:   Mon Jan 15 10:23:45 2024
ModDate:        Mon Jan 15 10:23:45 2024
Tagged:         no
Pages:          5
Encrypted:      no

Extract comprehensive metadata with exiftool:

exiftool document.pdf

Look for:

  • Custom metadata fields
  • Hidden comments
  • Unusual author/creator information
  • Modified dates that don’t match

Extracting Embedded Files

List embedded files with pdfdetach:

pdfdetach -list document.pdf

Extract a specific embedded file (by index):

pdfdetach -save 1 -o extracted_file document.pdf

Extract all embedded files:

pdfdetach -saveall -o extracted_files document.pdf

Analyzing PDF Structure

Use pdf-parser to analyze the PDF structure:

pdf-parser document.pdf

Search for specific object types:

# Find JavaScript objects
pdf-parser --search javascript document.pdf

# Find embedded files
pdf-parser --search "/EmbeddedFile" document.pdf

# Dump a specific object
pdf-parser --object 42 document.pdf

# Dump stream content
pdf-parser --object 42 --filter --raw document.pdf

Extracting Images

Extract all images with pdfimages:

pdfimages document.pdf output_prefix

This will save images as output_prefix-000.jpg, output_prefix-001.png, etc.

Extract in original format:

pdfimages -all document.pdf output_prefix

Analyzing JavaScript

PDFs can contain JavaScript, which may be used for malicious purposes. Use pdf-parser to find and analyze JavaScript:

pdf-parser --search javascript document.pdf

Handling Encrypted PDFs

Check if a PDF is encrypted:

pdfinfo document.pdf | grep Encrypted

Common CTF scenarios:

  • Empty password - Try opening without password
  • Weak password - Use dictionary attacks with tools like pdfcrack
  • Metadata accessible - Some metadata visible even when encrypted

Attempt to crack password:

pdfcrack -f document.pdf -w wordlist.txt

Remove password if known:

qpdf --password=PASSWORD --decrypt input.pdf output.pdf

Examining Hidden Data

Using binwalk

PDFs can contain embedded ZIP archives or other files:

binwalk document.pdf
binwalk -e document.pdf  # Extract found files

Using strings

Search for flags or interesting text:

strings document.pdf | grep "CTF{"
strings document.pdf | less

Hexdump analysis

Examine raw bytes for hidden data:

hexdump -C document.pdf | less

Look for:

  • Multiple file signatures (polyglot files)
  • Data after %%EOF marker
  • Unusual byte patterns

Practical Example

Extracting Hidden Flag from Embedded File

# 1. Check metadata
pdfinfo challenge.pdf

# 2. List embedded files
pdfdetach -list challenge.pdf
# Output: 1 embedded files found
#         1: secret.txt

# 3. Extract the file
pdfdetach -save 1 -o secret.txt challenge.pdf

# 4. Read the flag
cat secret.txt
# CTF{h1dd3n_1n_4tt4chm3nt}

Finding JavaScript with Flag

# 1. Analyze PDF structure
pdf-parser --search javascript challenge.pdf
# Output: obj 15 0 contains /JavaScript

# 2. Extract the JavaScript object
pdf-parser --object 15 --raw challenge.pdf
# Output: var flag = "CTF{j4v4scr1pt_fl4g}";

# 3. Decode if obfuscated
pdf-parser --object 15 --filter --raw challenge.pdf

Tips & Common Pitfalls

Best Practices

  • Always check both metadata AND embedded files
  • Use multiple tools - each may reveal different information
  • Look for data after the %%EOF marker (PDF files can have trailing data)
  • Check for polyglot files (PDF + ZIP, PDF + image)
  • Examine object streams with pdf-parser

Common CTF Techniques

  • Steganography in images - Extract images then analyze with steganography tools
  • Base64 in JavaScript - JavaScript objects may contain encoded flags
  • Custom metadata fields - Unusual XMP or custom tags
  • White text on white background - Copy all text from PDF reader
  • Embedded files with wrong extensions - Check actual file type after extraction

Gotchas

  • Some PDFs use compression (FlateDecode) - use --filter flag with pdf-parser
  • Encrypted PDFs may still reveal object count and structure
  • Layer analysis - Some PDFs have hidden layers (check with PDF reader)
  • Form fields can contain hidden data in JavaScript handlers

Further Reading