"""Install the reviewed client payload through the standard macOS administrator prompt.""" import argparse import hashlib import json import os import platform import plistlib import pwd import shutil import subprocess import time import socket from pathlib import Path ROOT = Path("/Library/Application Support/NODE.DC/MissionWireGuard") PLIST = Path("/Library/LaunchDaemons/com.nodedc.mission-wireguard.plist") LABEL = "com.nodedc.mission-wireguard" def main(): parser = argparse.ArgumentParser() parser.add_argument("--uid", type=int, required=True) args = parser.parse_args() if os.geteuid() != 0 or platform.system() != "Darwin" or platform.machine() != "arm64" or args.uid < 500: raise ValueError("macOS Apple Silicon administrator installation required") user = pwd.getpwuid(args.uid) source = Path(__file__).resolve().parent binaries = source / "build/macos-arm64" manifest = json.loads((binaries / "manifest.json").read_text()) for name, digest in manifest["files"].items(): path = binaries / name if not path.resolve().is_relative_to(binaries.resolve()) or hashlib.sha256(path.read_bytes()).hexdigest() != digest: raise ValueError("Client payload hash mismatch") if ROOT.exists() and not (ROOT / "installed.json").is_file(): raise ValueError("Foreign installation directory") if PLIST.exists(): old = plistlib.loads(PLIST.read_bytes()) if old.get("Label") != LABEL or not str(old.get("ProgramArguments", ["", "", ""])[-1]).startswith(str(ROOT)): raise ValueError("Foreign launch daemon") identity = hashlib.sha256((source / "helper.py").read_bytes() + json.dumps(manifest, sort_keys=True).encode()).hexdigest() ROOT.mkdir(mode=0o700, parents=True, exist_ok=True) ROOT.chmod(0o700) release = ROOT / "releases" / identity release.mkdir(mode=0o700, parents=True, exist_ok=True) for name in ("wg", "wireguard-go"): shutil.copy2(binaries / name, release / name) (release / name).chmod(0o700) shutil.copy2(source / "helper.py", release / "helper.py") (release / "helper.py").chmod(0o600) (release / "owner.json").write_text(json.dumps({"uid": args.uid, "gid": user.pw_gid})) (release / "owner.json").chmod(0o600) value = {"Label": LABEL, "ProgramArguments": ["/usr/bin/python3", "-I", str(release / "helper.py")], "RunAtLoad": True, "KeepAlive": True, "ThrottleInterval": 10, "ProcessType": "Background", "Umask": 63} previous = PLIST.read_bytes() if PLIST.exists() else None try: subprocess.run(["/bin/launchctl", "bootout", "system/" + LABEL], capture_output=True) PLIST.write_bytes(plistlib.dumps(value)) PLIST.chmod(0o644) subprocess.run(["/bin/launchctl", "bootstrap", "system", str(PLIST)], check=True, capture_output=True) for attempt in range(40): try: with socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) as connection: connection.settimeout(1) connection.connect("/var/run/ndc-mission-wireguard.sock") connection.sendall(b'{"action":"health"}\n') result=json.loads(connection.recv(4096)) if result.get("result",{}).get("sha256") != hashlib.sha256((release / "helper.py").read_bytes()).hexdigest(): raise ValueError("Helper identity mismatch") break except Exception: if attempt == 39: raise ValueError("Installed helper did not become ready") time.sleep(.25) (ROOT / "installed.json").write_text(json.dumps({"artifact": identity, "uid": args.uid, "label": LABEL})) (ROOT / "installed.json").chmod(0o600) except Exception: subprocess.run(["/bin/launchctl", "bootout", "system/" + LABEL], capture_output=True) if previous: PLIST.write_bytes(previous) subprocess.run(["/bin/launchctl", "bootstrap", "system", str(PLIST)], capture_output=True) else: PLIST.unlink(missing_ok=True) raise if __name__ == "__main__": main()