corbel/scripts/gen_md_epub_fixtures.py

159 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generate Markdown and EPUB test fixtures for CorbelPurge.
Creates:
- benign.md — clean educational text
- cve_writeup.md — security literature with code samples (should whitelist)
- malicious.md — markdown with javascript: phishing links + shellcode
- benign.epub — clean EPUB
- malicious.epub — EPUB with <script> tag and external tracker
"""
from pathlib import Path
FIXTURES_DIR = Path(__file__).parent.parent / "tests" / "fixtures"
FIXTURES_DIR.mkdir(parents=True, exist_ok=True)
def make_benign_md():
content = """# Benign Document
This is a paragraph of perfectly normal text. It discusses the weather,
the state of the economy, and other uncontroversial topics.
## Subsection
More text here. Nothing suspicious whatsoever.
"""
path = FIXTURES_DIR / "benign.md"
path.write_text(content)
return path
def make_cve_writeup_md():
"""Educational markdown that *mentions* exploits but in a literature context."""
content = """# CVE-2024-1234: PDF JavaScript Injection
## Abstract
In this paper we describe a vulnerability in which a malicious PDF
uses a /JavaScript action to execute arbitrary code. The eval() function
is called with attacker-controlled input.
## Proof of Concept
```python
# This is a PoC for the vulnerability described above.
import subprocess
# Note: this code is for educational purposes only.
payload = "eval('alert(1)')"
print(f"Payload: {payload}")
```
## Remediation
Patch the reader to ignore /JavaScript actions in /OpenAction.
"""
path = FIXTURES_DIR / "cve_writeup.md"
path.write_text(content)
return path
def make_malicious_md():
"""Markdown with phishing links and obfuscated shellcode in a paragraph."""
# 16+ hex-encoded bytes in a non-code context
shellcode = "\\x90" * 20
content = f"""# Click Here
Free money! [Click now](javascript:alert('xss'))
Run this: {shellcode}
"""
path = FIXTURES_DIR / "malicious.md"
path.write_text(content)
return path
def make_benign_epub():
"""Minimal clean EPUB with just text content."""
import zipfile
path = FIXTURES_DIR / "benign.epub"
with zipfile.ZipFile(path, "w", zipfile.ZIP_STORED) as z:
z.writestr("mimetype", "application/epub+zip")
z.writestr(
"OEBPS/content.opf",
"""<?xml version="1.0"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Benign EPUB</dc:title>
<dc:author>CorbelPurge Tests</dc:author>
</metadata>
<manifest>
<item id="ch1" href="ch1.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="ch1"/>
</spine>
</package>""",
)
z.writestr(
"OEBPS/ch1.xhtml",
"""<?xml version="1.0"?>
<html><head><title>Ch1</title></head>
<body><p>Hello world.</p><p>Second paragraph.</p></body></html>""",
)
return path
def make_malicious_epub():
"""EPUB with a <script> tag and an external tracker image."""
import zipfile
path = FIXTURES_DIR / "malicious.epub"
with zipfile.ZipFile(path, "w", zipfile.ZIP_STORED) as z:
z.writestr("mimetype", "application/epub+zip")
z.writestr(
"OEBPS/content.opf",
"""<?xml version="1.0"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Malicious EPUB</dc:title>
<dc:author>Attacker</dc:author>
</metadata>
<manifest>
<item id="ch1" href="ch1.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="ch1"/>
</spine>
</package>""",
)
z.writestr(
"OEBPS/ch1.xhtml",
"""<?xml version="1.0"?>
<html><head><title>Ch1</title></head>
<body>
<p>Hello.</p>
<script>alert('xss from epub');</script>
<img src="https://192.168.1.1/track.png" />
</body></html>""",
)
return path
def main():
paths = [
make_benign_md(),
make_cve_writeup_md(),
make_malicious_md(),
make_benign_epub(),
make_malicious_epub(),
]
for p in paths:
print(f" wrote {p} ({p.stat().st_size} bytes)")
if __name__ == "__main__":
main()