Python
Windows sandbox: two accounts, loopback filters, and remove on the CLI
Rohit C Prasad666363179aef
diff --git a/coworker/remote/joiner.py b/coworker/remote/joiner.pyindex cb45f63a..e68cfe4f 100644--- a/coworker/remote/joiner.py+++ b/coworker/remote/joiner.py@@ -648,6 +648,8 @@ def cli(sandbox_sub.add_parser("status", help="what OpenShell sandboxes need here, and what is missing")p_sandbox_setup = sandbox_sub.add_parser("setup", help="set this machine up (shows each change and asks first)")p_sandbox_setup.add_argument("--yes", action="store_true", help="make the changes without asking")+ p_sandbox_remove = sandbox_sub.add_parser("remove", help="undo what `setup` did on this machine (Windows)")+ p_sandbox_remove.add_argument("--yes", action="store_true", help="remove without asking")args = parser.parse_args(argv)state = state_dir()@@ -672,7 +674,11 @@ def cli(if args.command == "sandbox":from ..sandbox import setup_cmd- return setup_cmd.setup(yes=args.yes) if args.sandbox_command == "setup" else setup_cmd.status()+ if args.sandbox_command == "setup":+ return setup_cmd.setup(yes=args.yes)+ if args.sandbox_command == "remove":+ return setup_cmd.remove(yes=args.yes)+ return setup_cmd.status()return 2diff --git a/coworker/sandbox/launch.py b/coworker/sandbox/launch.pyindex 766815f4..ddeea34e 100644--- a/coworker/sandbox/launch.py+++ b/coworker/sandbox/launch.py@@ -100,6 +100,10 @@ def maybe_run_runner(argv: list[str]) -> bool:"""`openworker-server sandbox-runner <serve|attach> ...`: run the runner and exit.Returns False when argv is anything else. Imports only the runner package, so thedaemon inside a sandbox stays small and starts fast."""+ if argv[:1] == ["sandbox-wfp"]: # the elevated setup step on Windows (windows_setup.py)+ from . import windows_wfp++ raise SystemExit(windows_wfp.main(argv[1:]))if argv[:1] != ["sandbox-runner"]:return Falsefrom .runner.__main__ import main as runner_maindiff --git a/coworker/sandbox/netproxy.py b/coworker/sandbox/netproxy.pyindex 7e4ac0fd..9c78ae98 100644--- a/coworker/sandbox/netproxy.py+++ b/coworker/sandbox/netproxy.py@@ -13,12 +13,18 @@ from __future__ import annotationsimport selectimport socket+import sysimport threadingfrom collections import dequefrom typing import Optional, Sequencefrom . import network_profiles+# On Windows the proxy listens inside a FIXED range, because the closed sandbox account's+# loopback filters (windows_wfp.py) are written once at setup and name this range: every+# other local port is closed to that account. Elsewhere the kernel gives any free port.+WINDOWS_PORTS = range(47800, 47900)+_IDLE_SECONDS = 300.0_CONNECT_SECONDS = 15.0_MAX_HEAD = 16 * 1024@@ -36,9 +42,7 @@ class AllowListProxy:if host and port.isdigit():self._extra.add((host.lower().rstrip("."), int(port)))self.denied: deque[str] = deque(maxlen=50) # recent refusals, newest last- self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)- self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)- self._server.bind(("127.0.0.1", 0)) # this machine only+ self._server = _listen()self._server.listen(64)self.port: int = self._server.getsockname()[1]self._closed = False@@ -148,6 +152,24 @@ class AllowListProxy:(b if src is a else a).sendall(data)+def _listen() -> socket.socket:+ """A socket bound on loopback: any free port, or on Windows the first free port of+ WINDOWS_PORTS (no SO_REUSEADDR there: on Windows it would let two proxies share one)."""+ if sys.platform != "win32":+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ server.bind(("127.0.0.1", 0)) # this machine only+ return server+ for port in WINDOWS_PORTS:+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ try:+ server.bind(("127.0.0.1", port))+ return server+ except OSError:+ server.close()+ raise OSError(f"no free port for the sandbox proxy in {WINDOWS_PORTS.start}-{WINDOWS_PORTS.stop - 1}")++_lock = threading.Lock()_proxies: dict[str, AllowListProxy] = {}diff --git a/coworker/sandbox/providers/windows.py b/coworker/sandbox/providers/windows.pyindex 5ec145a3..bbd7a291 100644--- a/coworker/sandbox/providers/windows.py+++ b/coworker/sandbox/providers/windows.py@@ -3,17 +3,19 @@Same runner, same protocol. Two modes, chosen by whether `openworker machine sandboxsetup` has run (windows_setup.py):-FULL (after setup): the daemon is logged on as the hidden local account-`OpenWorkerSandbox` (CreateProcessWithLogonW, the secondary logon service; no privilege-needed). Windows keeps that account out of the person's profile: `.ssh`, `.aws`, documents,-OpenWorker's own state and keys cannot be read. The session's folders get one inheritable-entry each for the account (Modify for writable, Read for read-only); the sandbox's private-folder lives under `C:\\ProgramData\\OpenWorker\\sandbox\\sandboxes`. A firewall rule from-setup blocks every outbound connection for the account, so the internet is reachable only-through our allow-list proxy on loopback. Windows Firewall does not filter loopback, so-other local ports stay reachable until the WFP filters are built; `describe()` says so and-reports `partial` until then. Folders outside the profile (`C:\\work`) are readable by any-local account by default (spike finding A); that too is stated, not hidden.+FULL (after setup): the daemon is logged on as a hidden local account+(CreateProcessWithLogonW, the secondary logon service; no privilege needed), chosen by the+network profile: `OWSandboxClosedNet` for the allow-list profiles, `OWSandboxOpenNet` for+`open` (ruling 3d.2). Windows keeps both out of the person's profile: `.ssh`, `.aws`,+documents, OpenWorker's own state and keys cannot be read. The session's folders get one+inheritable entry each for the account (Modify for writable, Read for read-only); the+sandbox's private folder lives under `C:\\ProgramData\\OpenWorker\\sandbox\\sandboxes`. For the+closed account a firewall rule from setup blocks every outbound connection and WFP filters+close loopback except the proxy's port range (netproxy.WINDOWS_PORTS), so the internet is+reachable only through our allow-list proxy; `verify()` proves both from inside before the+session starts. The open account has no rules: any host, any local port, files still+confined. Folders outside the profile (`C:\\work`) are readable by any local account by+default (spike finding A); that is stated, not hidden (ruling 3d.5).SAME-USER (no setup): the daemon runs as the current user under a write-restricted tokenwhose restricting SID is a made-up session SID; writes are allowed only where that SID has@@ -56,13 +58,12 @@ from .seatbelt import _CACHE_VARIABLES, clean_environmentFULL, PARTIAL = "full", "partial"_EVERYONE_WRITABLE = ("the drive root (new folders only)", r"C:\Users\Public", r"C:\Windows\Temp")_PARTIAL_REASON = (- "Windows write-restricted token: writes limited to the session's folders; reads and the network are NOT limited"- " (run `openworker machine sandbox setup` for the full sandbox)"+ "Windows write-restricted token (setup not run): writes limited to the session's folders; reads and the network are"+ " NOT limited (run `openworker machine sandbox setup` for the full sandbox)")_FULL_REASON = ("Windows sandbox account: the user's profile (keys, documents, OpenWorker's state) is out of reach; files limited to"- " the session's folders plus what any local account may read outside profiles; outbound network blocked except the"- " allow-list proxy; other local ports still reachable (loopback filters not built yet)"+ " the session's folders plus what any local account may read outside profiles")@@ -83,8 +84,8 @@ def preflight() -> None:def mode() -> str:- """`full` when setup has run and this user can read the account's credential."""- return FULL if windows_setup.account() is not None else PARTIAL+ """`full` when setup has run and this user can read the accounts' credentials."""+ return FULL if windows_setup.account(windows_setup.OPEN) and windows_setup.account(windows_setup.CLOSED) else PARTIALclass WindowsProvider:@@ -116,9 +117,12 @@ class WindowsProvider:self._relay_silence = relay_silence_secondsself.sandbox = f"sb-{uuid.uuid4().hex[:12]}"self.mode = force_mode or mode()- self._account = windows_setup.account() if self.mode == FULL else None+ self.open_network = network and network_profiles.is_open(self.profile)+ # The account is the network mode: closed (allow list through the proxy) or open.+ self.kind = windows_setup.OPEN if self.open_network else windows_setup.CLOSED+ self._account = windows_setup.account(self.kind) if self.mode == FULL else Noneif self.mode == FULL and self._account is None:- raise WindowsUnavailable("the full Windows sandbox needs `openworker machine sandbox setup` first")+ raise WindowsUnavailable("the full Windows sandbox needs `openworker machine sandbox setup` first (or again: the accounts changed)")# Who the entries and the pipe name: the hidden account, or the made-up session SID.self.session_sid = self._account[1] if self._account else winsec.session_sid()if self.mode == FULL:@@ -138,15 +142,25 @@ class WindowsProvider:# -- what it is ---------------------------------------------------------------------def describe(self) -> dict[str, Any]:if self.mode == FULL:- network = f"the '{self.profile}' profile through the allow-list proxy; the rest is blocked by the firewall" if self.network else "blocked"+ if not self.network:+ network = "blocked"+ elif self.open_network:+ network = "open (any host, any local port; the 'open' profile)"+ else:+ network = f"the '{self.profile}' profile through the allow-list proxy; the rest is blocked by the firewall and the loopback filters"reason = f"{_FULL_REASON}. Network: {network}"else:- network = f"advisory: the '{self.profile}' profile through the proxy variables only" if self.network else "the user's own"+ if not self.network:+ network = "the user's own"+ elif self.open_network:+ network = "the user's own (the 'open' profile)"+ else:+ network = f"advisory: the '{self.profile}' profile through the proxy variables only"reason = f"{_PARTIAL_REASON}. Network: {network}. Still writable by anyone: {', '.join(_EVERYONE_WRITABLE)}"return {"provider": self.name,"sandbox": self.sandbox,- "enforcement": PARTIAL, # `full` once the loopback filters exist; the mode says how far it goes+ "enforcement": FULL if self.mode == FULL else PARTIAL,"mode": self.mode,"reason": reason,"credentials": self.copied.describe() if self.copied is not None else [],@@ -166,7 +180,7 @@ class WindowsProvider:from .. import winsechosts = sorted({h for g in self.grants for h in g.hosts})- if self.network:+ if self.network and not self.open_network:self._proxy = netproxy.AllowListProxy(self.profile, extra_hosts=hosts) if hosts else netproxy.shared(self.profile)for root in self.roots:self._grant(root["path"], "write" if root["writable"] else "read")@@ -286,6 +300,7 @@ class WindowsProvider:try:client.call("fs.list", {"path": home, "limit": 1}, timeout=15)except RunnerError:+ self._verify_loopback(client)returnraise WindowsUnavailable("the sandbox did not take effect: the home folder can be listed from inside")probe = os.path.join(home, f"owr-verify-{uuid.uuid4().hex[:8]}.txt")@@ -299,6 +314,33 @@ class WindowsProvider:passraise WindowsUnavailable("the sandbox did not take effect: a file could be written into the home folder from inside")+ def _verify_loopback(self, client: Any) -> None:+ """The closed account may reach the proxy on loopback and nothing else there+ (ruling 3d.2). A listener of ours outside the proxy's range must be unreachable;+ the proxy must be reachable. Anything else means setup is stale: refuse."""+ if self.open_network or not self.network or self._proxy is None:+ return+ import socket++ probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ try:+ probe.bind(("127.0.0.1", 0))+ probe.listen(1)+ port = probe.getsockname()[1]+ if port in netproxy.WINDOWS_PORTS:+ return # the kernel picked a port inside the proxy's range; nothing to prove+ reached = client.call("net.probe", {"host": "127.0.0.1", "port": port}, timeout=15)+ finally:+ probe.close()+ if reached.get("ok"):+ raise WindowsUnavailable(+ "the sandbox did not take effect: a local port outside the proxy's range can be reached from inside"+ " (the loopback filters are missing; run `openworker machine sandbox setup` again)"+ )+ via = client.call("net.probe", {"host": "127.0.0.1", "port": self._proxy.port}, timeout=15)+ if not via.get("ok"):+ raise WindowsUnavailable(f"the sandbox cannot reach the allow-list proxy on port {self._proxy.port}: {via.get('error')}")+# -- the entries ----------------------------------------------------------------------def _grant(self, folder: str, kind: str) -> None:from .. import winsecdiff --git a/coworker/sandbox/providers/windows_setup.py b/coworker/sandbox/providers/windows_setup.pyindex 307f7954..48f62d98 100644--- a/coworker/sandbox/providers/windows_setup.py+++ b/coworker/sandbox/providers/windows_setup.py@@ -1,23 +1,26 @@-"""The one-time setup for the full Windows sandbox (design doc, section 7, step 3).+"""The one-time setup for the full Windows sandbox (design doc, section 7 step 3 and 3d).`openworker machine sandbox setup` on Windows runs ONE elevated PowerShell script (one UACprompt) that:-1. creates the hidden local account `OpenWorkerSandbox` with a random password nobody sees;-2. hides it from the sign-in screen and denies it remote and network logon (it may only be- logged on by `CreateProcessWithLogonW`, which is an interactive logon, so that one stays);-3. writes one Windows Firewall rule that blocks every OUTBOUND connection for that account,- so the internet is reachable only through our allow-list proxy (which is loopback, and- Windows Firewall does not filter loopback: spike finding C. Other local ports stay open- until the WFP filters are built);-4. stores the password in `C:\\ProgramData\\OpenWorker\\sandbox\\account.cred`, readable by+1. creates TWO hidden local accounts with random passwords nobody sees: `OWSandboxOpenNet`+ (the `open` network profile: no rules at all) and `OWSandboxClosedNet` (the allow-list+ profiles: the network only through our proxy). Choosing a network profile is choosing+ the account; setup never runs per session;+2. hides both from the sign-in screen and denies them remote, network and service logon+ (they are logged on only by `CreateProcessWithLogonW`, an interactive logon);+3. writes one Windows Firewall rule that blocks every OUTBOUND connection for the closed+ account, and, with `windows_wfp.py`, the loopback filters that close every local port+ to it except the proxy's range (Windows Firewall does not look at loopback);+4. stores the passwords in `C:\\ProgramData\\OpenWorker\\sandbox\\account.cred`, readable bythe user who ran setup, Administrators and SYSTEM, and by nobody else (the sandbox- account cannot read it). DPAPI would be one layer more, and does not work for a- key-based SSH logon (spike finding E); it can be added later;+ accounts cannot read it);5. makes `C:\\ProgramData\\OpenWorker\\sandbox\\sandboxes`, where each sandbox gets its- private folder (the user's own temp folder is inside the profile the account cannot see);-6. records what it changed in `setup.json`, so `remove` can undo exactly that.+ private folder (the user's own temp folder is inside the profile the accounts cannot see);+6. removes what an earlier setup or spike left (`OpenWorkerSandbox`, its rule), and records+ what it changed in `setup.json`, so `remove` can undo exactly that.-The provider reads `account()` at start: with it, full mode; without it, the same-user mode.+The provider reads `account(kind)` at start: with it, full mode; without it, the same-user+mode labelled `partial: setup not run`."""from __future__ import annotations@@ -30,88 +33,129 @@ import tempfilefrom pathlib import Pathfrom typing import Any, Optional-ACCOUNT = "OpenWorkerSandbox"+from .. import netproxy++OPEN, CLOSED = "open", "closed"+ACCOUNTS = {OPEN: "OWSandboxOpenNet", CLOSED: "OWSandboxClosedNet"} # 20 characters at most+LEGACY_ACCOUNTS = ("OpenWorkerSandbox", "OwSandbox") # the first build and the spikeROOT = Path(os.environ.get("ProgramData", r"C:\ProgramData")) / "OpenWorker" / "sandbox"SANDBOXES = ROOT / "sandboxes"CRED_FILE = ROOT / "account.cred"STATE_FILE = ROOT / "setup.json"-FIREWALL_RULE = "OpenWorker sandbox: block outbound"-SETUP_VERSION = 1+FIREWALL_RULE = "OpenWorker sandbox: closed account, no outbound"+LEGACY_RULES = ("OpenWorker sandbox: block outbound", "OpenWorker sandbox: no network", "OpenWorker sandbox: no other local port")+SETUP_VERSION = 2-# The elevated script. `$UserSid` is the account that may read the password (the person-# running setup); `$Root` and `$Account` come from the constants above.+# The elevated script. `$UserSid` is the account that may read the passwords (the person+# running setup); the rest comes from the constants above. `$WfpExe $WfpArg` runs+# windows_wfp.py (the Python here, or the frozen binary's `sandbox-wfp`).SETUP_SCRIPT = r'''-param([string]$UserSid, [string]$Account, [string]$Root, [string]$Rule, [int]$Version)+param([string]$UserSid, [string]$Root, [string]$Rule, [int]$Version, [string]$OpenAccount, [string]$ClosedAccount,+ [string]$Legacy, [string]$LegacyRules, [string]$WfpExe, [string]$WfpArg, [int]$PortLow, [int]$PortHigh)$ErrorActionPreference = "Stop"$changed = @()New-Item -Force -ItemType Directory $Root | Out-Null+$userlist = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"+New-Item -Force $userlist | Out-Null-# 1. The account, with a password that exists only in this process and in the credential file.-$bytes = New-Object byte[] 32; [Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)-$password = [Convert]::ToBase64String($bytes) + "aA1!"-$secure = ConvertTo-SecureString $password -AsPlainText -Force-if (Get-LocalUser -Name $Account -ErrorAction SilentlyContinue) {- Set-LocalUser -Name $Account -Password $secure -PasswordNeverExpires $true -UserMayChangePassword $false-} else {- New-LocalUser -Name $Account -Password $secure -PasswordNeverExpires -UserMayNotChangePassword -AccountNeverExpires `- -Description "OpenWorker sandbox account (agents' commands)" | Out-Null- $changed += "account"+# 0. What an earlier setup or the spike left behind.+foreach ($name in ($Legacy -split ",")) {+ if ($name -and (Get-LocalUser -Name $name -ErrorAction SilentlyContinue)) {+ Remove-LocalUser -Name $name+ Get-CimInstance Win32_UserProfile | Where-Object { $_.LocalPath -like "*\$name" } | Remove-CimInstance+ Remove-ItemProperty -Path $userlist -Name $name -ErrorAction SilentlyContinue+ $changed += "removed-legacy-account:$name"+ }+}+foreach ($name in ($LegacyRules -split ",")) {+ if ($name -and (Get-NetFirewallRule -DisplayName $name -ErrorAction SilentlyContinue)) { Remove-NetFirewallRule -DisplayName $name; $changed += "removed-legacy-rule" }}-if (-not (Get-LocalGroupMember -Group Users -Member $Account -ErrorAction SilentlyContinue)) { Add-LocalGroupMember -Group Users -Member $Account }-$sid = (Get-LocalUser -Name $Account).SID.Value-# 2. Hidden from the sign-in screen; no remote desktop, no network (share) logon.-$key = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"-New-Item -Force $key | Out-Null-New-ItemProperty -Force -Path $key -Name $Account -Value 0 -PropertyType DWord | Out-Null+# 1. The accounts, each with a password that exists only in this process and in the credential file.+function New-Password {+ $bytes = New-Object byte[] 32; [Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)+ return [Convert]::ToBase64String($bytes) + "aA1!"+}+function Ensure-Account([string]$name, [string]$description, [string]$password) {+ $secure = ConvertTo-SecureString $password -AsPlainText -Force+ if (Get-LocalUser -Name $name -ErrorAction SilentlyContinue) {+ Set-LocalUser -Name $name -Password $secure -PasswordNeverExpires $true -UserMayChangePassword $false+ } else {+ New-LocalUser -Name $name -Password $secure -PasswordNeverExpires -UserMayNotChangePassword -AccountNeverExpires -Description $description | Out-Null+ $script:changed += "account:$name"+ }+ if (-not (Get-LocalGroupMember -Group Users -Member $name -ErrorAction SilentlyContinue)) { Add-LocalGroupMember -Group Users -Member $name }+ # 2. Hidden from the sign-in screen.+ New-ItemProperty -Force -Path $userlist -Name $name -Value 0 -PropertyType DWord | Out-Null+ return (Get-LocalUser -Name $name).SID.Value+}+$openPassword = New-Password; $closedPassword = New-Password+$openSid = Ensure-Account $OpenAccount "OpenWorker sandbox account (network open)" $openPassword+$closedSid = Ensure-Account $ClosedAccount "OpenWorker sandbox account (allow list only)" $closedPassword$changed += "userlist"++# 2. No remote desktop, no network (share) logon, no service logon, for both.$inf = Join-Path $env:TEMP "ow-rights.inf"; $inf2 = Join-Path $env:TEMP "ow-rights2.inf"; $sdb = Join-Path $env:TEMP "ow-rights.sdb"secedit /export /cfg $inf /areas USER_RIGHTS | Out-Null$lines = Get-Content $infforeach ($right in @("SeDenyRemoteInteractiveLogonRight", "SeDenyNetworkLogonRight", "SeDenyServiceLogonRight")) {- if ($lines -match "^$right") { $lines = $lines | ForEach-Object { if ($_ -match "^$right" -and $_ -notmatch [regex]::Escape($sid)) { "$_,*$sid" } else { $_ } } }- else { $lines = $lines -replace "^\[Privilege Rights\]", "[Privilege Rights]`r`n$right = *$sid" }+ foreach ($sid in @($openSid, $closedSid)) {+ if ($lines -match "^$right") { $lines = $lines | ForEach-Object { if ($_ -match "^$right" -and $_ -notmatch [regex]::Escape($sid)) { "$_,*$sid" } else { $_ } } }+ else { $lines = $lines -replace "^\[Privilege Rights\]", "[Privilege Rights]`r`n$right = *$sid" }+ }}$lines | Set-Content $inf2 -Encoding Unicodesecedit /configure /db $sdb /cfg $inf2 /areas USER_RIGHTS | Out-NullRemove-Item -Force $inf, $inf2, $sdb -ErrorAction SilentlyContinue$changed += "logon-rights"-# 3. No outbound network for the account. (Loopback is not filtered by Windows Firewall.)+# 3. The closed account: no outbound network (Windows Firewall), and on loopback only the+# proxy's ports (WFP filters). The open account gets no rules at all.Remove-NetFirewallRule -DisplayName $Rule -ErrorAction SilentlyContinueNew-NetFirewallRule -DisplayName $Rule -Direction Outbound -Action Block -Profile Any -Enabled True `- -LocalUser "D:(A;;CC;;;$sid)" -Description "OpenWorker: the sandbox account reaches the network only through the allow-list proxy on this machine." | Out-Null+ -LocalUser "D:(A;;CC;;;$closedSid)" -Description "OpenWorker: the closed sandbox account reaches the network only through the allow-list proxy on this machine." | Out-Null$changed += "firewall"+$wfpArgs = @(); if ($WfpArg) { $wfpArgs += $WfpArg }+$wfpOut = & $WfpExe @wfpArgs add --sid $closedSid --port-low $PortLow --port-high $PortHigh 2>&1 | Out-String+if ($LASTEXITCODE -ne 0) { throw "the loopback filters could not be written: $wfpOut" }+$wfp = ($wfpOut | ConvertFrom-Json).filters+$changed += "loopback-filters"-# 4. The password, readable by the person who ran setup and by administrators only.+# 4. The passwords, readable by the person who ran setup and by administrators only.$cred = Join-Path $Root "account.cred"-Set-Content -Path $cred -Value "$Account`n$password" -Encoding ASCII -NoNewline+@{ $OpenAccount = $openPassword; $ClosedAccount = $closedPassword } | ConvertTo-Json -Compress | Set-Content -Path $cred -Encoding ASCII -NoNewlineicacls $cred /inheritance:r /grant "*S-1-5-18:F" /grant "*S-1-5-32-544:F" /grant "*${UserSid}:R" | Out-Null-$password = $null; $secure = $null+$openPassword = $null; $closedPassword = $null$changed += "credential"-# 5. Where sandboxes keep their private folders: the person makes them, the account gets each one.+# 5. Where sandboxes keep their private folders: the person makes them, an account gets each one.$boxes = Join-Path $Root "sandboxes"New-Item -Force -ItemType Directory $boxes | Out-Null-icacls $boxes /inheritance:r /grant "*S-1-5-18:(OI)(CI)F" /grant "*S-1-5-32-544:(OI)(CI)F" /grant "*${UserSid}:(OI)(CI)M" /grant "*${sid}:RX" | Out-Null+icacls $boxes /inheritance:r /grant "*S-1-5-18:(OI)(CI)F" /grant "*S-1-5-32-544:(OI)(CI)F" /grant "*${UserSid}:(OI)(CI)M" /grant "*${openSid}:RX" /grant "*${closedSid}:RX" | Out-Null$changed += "sandboxes-folder"# 6. The record.-@{ version = $Version; account = $Account; sid = $sid; user_sid = $UserSid; firewall_rule = $Rule; changed = $changed } |- ConvertTo-Json | Set-Content -Path (Join-Path $Root "setup.json") -Encoding ASCII+@{ version = $Version; user_sid = $UserSid; firewall_rule = $Rule; changed = $changed;+ accounts = @{ open = @{ name = $OpenAccount; sid = $openSid }; closed = @{ name = $ClosedAccount; sid = $closedSid } };+ wfp = @{ filters = $wfp; ports = @($PortLow, $PortHigh) } } |+ ConvertTo-Json -Depth 4 | Set-Content -Path (Join-Path $Root "setup.json") -Encoding ASCIIicacls (Join-Path $Root "setup.json") /grant "*S-1-1-0:R" | Out-Null-Write-Output "ok $sid"+Write-Output "ok $openSid $closedSid"'''REMOVE_SCRIPT = r'''-param([string]$Account, [string]$Root, [string]$Rule)+param([string]$Root, [string]$Rule, [string]$Accounts, [string]$LegacyRules, [string]$WfpExe, [string]$WfpArg)$ErrorActionPreference = "Continue"-Remove-NetFirewallRule -DisplayName $Rule -ErrorAction SilentlyContinue-$key = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"-Remove-ItemProperty -Path $key -Name $Account -ErrorAction SilentlyContinue-if (Get-LocalUser -Name $Account -ErrorAction SilentlyContinue) { Remove-LocalUser -Name $Account }-$profile = Get-CimInstance Win32_UserProfile | Where-Object { $_.LocalPath -like "*\$Account" }-if ($profile) { $profile | Remove-CimInstance }+foreach ($name in (($Rule + "," + $LegacyRules) -split ",")) { if ($name) { Remove-NetFirewallRule -DisplayName $name -ErrorAction SilentlyContinue } }+$wfpArgs = @(); if ($WfpArg) { $wfpArgs += $WfpArg }+& $WfpExe @wfpArgs remove 2>&1 | Out-Null+$userlist = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"+foreach ($name in ($Accounts -split ",")) {+ if (-not $name) { continue }+ Remove-ItemProperty -Path $userlist -Name $name -ErrorAction SilentlyContinue+ if (Get-LocalUser -Name $name -ErrorAction SilentlyContinue) { Remove-LocalUser -Name $name }+ Get-CimInstance Win32_UserProfile | Where-Object { $_.LocalPath -like "*\$name" } | Remove-CimInstance+}Remove-Item -Recurse -Force $Root -ErrorAction SilentlyContinueWrite-Output "removed"'''@@ -125,20 +169,34 @@ def state() -> Optional[dict[str, Any]]:return None-def account() -> Optional[tuple[str, str, str]]:- """(name, sid, password) when the full mode can be used by THIS user: setup has run and- the credential file is readable. The password never leaves the process."""+def current() -> bool:+ """Setup has run and is of this version (an older record means: run it again)."""recorded = state()- if not recorded:+ return bool(recorded) and int(recorded.get("version") or 0) >= SETUP_VERSION+++def account(kind: str = CLOSED) -> Optional[tuple[str, str, str]]:+ """(name, sid, password) of the `open` or `closed` account when the full mode can be+ used by THIS user: setup of this version has run and the credential file is readable.+ The password never leaves the process."""+ recorded = state()+ if not recorded or int(recorded.get("version") or 0) < SETUP_VERSION:return None- try:- text = CRED_FILE.read_text(encoding="ascii")- except OSError:+ entry = (recorded.get("accounts") or {}).get(kind) or {}+ name, sid = str(entry.get("name") or ""), str(entry.get("sid") or "")+ if not name or not sid:return None- name, _, password = text.partition("\n")- if name != recorded.get("account") or not password:+ try:+ passwords = json.loads(CRED_FILE.read_text(encoding="ascii"))+ except (OSError, ValueError):return None- return name, str(recorded.get("sid") or ""), password+ password = str(passwords.get(name) or "")+ return (name, sid, password) if password else None+++def filters_recorded() -> bool:+ recorded = state() or {}+ return bool((recorded.get("wfp") or {}).get("filters"))def _powershell(script: str, arguments: list[str], *, elevate: bool) -> subprocess.CompletedProcess:@@ -176,28 +234,78 @@ def is_elevated() -> bool:return False+def _wfp_command() -> tuple[str, str]:+ """How the elevated script runs windows_wfp.py: (executable, first argument)."""+ from ..launch import frozen++ if frozen():+ return sys.executable, "sandbox-wfp"+ from .. import windows_wfp++ return sys.executable, str(Path(windows_wfp.__file__).resolve())+++def _wfp_arguments() -> list[str]:+ exe, arg = _wfp_command()+ return ["-WfpExe", exe, "-WfpArg", arg]++def run_setup() -> tuple[bool, str]:- """Create the account and everything around it. Returns (ok, what the script said)."""+ """Create the accounts and everything around them. Returns (ok, what the script said)."""from .. import winsec- arguments = ["-UserSid", winsec.current_user_sid(), "-Account", ACCOUNT, "-Root", str(ROOT), "-Rule", FIREWALL_RULE, "-Version", str(SETUP_VERSION)]+ ports = netproxy.WINDOWS_PORTS+ arguments = [+ "-UserSid", winsec.current_user_sid(), "-Root", str(ROOT), "-Rule", FIREWALL_RULE, "-Version", str(SETUP_VERSION),+ "-OpenAccount", ACCOUNTS[OPEN], "-ClosedAccount", ACCOUNTS[CLOSED],+ "-Legacy", ",".join(LEGACY_ACCOUNTS), "-LegacyRules", ",".join(LEGACY_RULES),+ *_wfp_arguments(), "-PortLow", str(ports.start), "-PortHigh", str(ports.stop - 1),+ ] # fmt: skipdone = _powershell(SETUP_SCRIPT, arguments, elevate=not is_elevated())said = (done.stdout or "").strip() + (("\n" + done.stderr.strip()) if done.stderr and done.stderr.strip() else "")return done.returncode == 0 and "ok " in said, saiddef run_remove() -> tuple[bool, str]:- done = _powershell(REMOVE_SCRIPT, ["-Account", ACCOUNT, "-Root", str(ROOT), "-Rule", FIREWALL_RULE], elevate=not is_elevated())+ names = ",".join([*ACCOUNTS.values(), *LEGACY_ACCOUNTS])+ arguments = ["-Root", str(ROOT), "-Rule", FIREWALL_RULE, "-Accounts", names, "-LegacyRules", ",".join(LEGACY_RULES), *_wfp_arguments()]+ done = _powershell(REMOVE_SCRIPT, arguments, elevate=not is_elevated())said = (done.stdout or "").strip()return done.returncode == 0 and "removed" in said, said+def _firewall_rule_enabled() -> bool:+ try:+ done = subprocess.run(+ ["powershell.exe", "-NoProfile", "-Command", f"(Get-NetFirewallRule -DisplayName '{FIREWALL_RULE}' -ErrorAction SilentlyContinue).Enabled"],+ capture_output=True, text=True, timeout=60,+ ) # fmt: skip+ except (OSError, subprocess.SubprocessError):+ return False+ return "True" in (done.stdout or "")++def checks() -> list[tuple[str, bool, str]]:"""(what, ok, detail) rows for `sandbox status` on Windows."""if sys.platform != "win32":return []recorded = state()- rows = [("the sandbox account exists (setup has run)", bool(recorded), "" if recorded else "run `openworker machine sandbox setup`")]- usable = account() is not None- rows.append(("this user can start sandboxes as that account", usable, "" if usable else f"{CRED_FILE} is not readable by this user; run setup as this user"))+ rows: list[tuple[str, bool, str]] = []+ if recorded and not current():+ rows.append(("the sandbox accounts exist (setup has run)", False, "an older setup is recorded; run `openworker machine sandbox setup` again"))+ return rows+ rows.append(("the sandbox accounts exist (setup has run)", bool(recorded), "" if recorded else "run `openworker machine sandbox setup`"))+ if not recorded:+ return rows+ usable = account(OPEN) is not None and account(CLOSED) is not None+ rows.append(("this user can start sandboxes as those accounts", usable, "" if usable else f"{CRED_FILE} is not readable by this user; run setup as this user"))+ rule = _firewall_rule_enabled()+ rows.append(("the closed account's outbound traffic is blocked (firewall rule)", rule, "" if rule else "the rule is missing; run setup again"))+ from .. import windows_wfp++ present = windows_wfp.present()+ if present is None:+ rows.append(("the closed account's loopback filters exist", filters_recorded(), "recorded by setup; proved from inside at every session start"))+ else:+ rows.append(("the closed account's loopback filters exist", present, "" if present else "the filters are missing; run setup again"))return rowsdiff --git a/coworker/sandbox/runner/daemon.py b/coworker/sandbox/runner/daemon.pyindex 18a867f5..dc879544 100644--- a/coworker/sandbox/runner/daemon.py+++ b/coworker/sandbox/runner/daemon.py@@ -367,6 +367,15 @@ class Daemon:return fsops.search(path, str(params.get("pattern") or ""), **_pick(params, "max_matches", "ignore_case"))except fsops.FsError as exc:raise P.RunnerError(P.FS_ERROR, str(exc)) from exc+ if method == "net.probe":+ # Can this sandbox open a TCP connection to host:port? A provider proves its+ # network wall with it before the session starts (the Windows loopback filters).+ host, port = str(params.get("host") or "127.0.0.1"), int(params.get("port") or 0)+ try:+ socket.create_connection((host, port), timeout=float(params.get("timeout") or 5)).close()+ except OSError as exc:+ return {"ok": False, "error": f"{type(exc).__name__}: {exc}"}+ return {"ok": True}if method == "runner.shutdown":self._stop.set()return {"stopping": True}diff --git a/coworker/sandbox/setup_cmd.py b/coworker/sandbox/setup_cmd.pyindex 174bcb56..3d169c11 100644--- a/coworker/sandbox/setup_cmd.py+++ b/coworker/sandbox/setup_cmd.py@@ -86,13 +86,17 @@ def windows_rows(print_fn: Callable[[str], None]) -> None:def windows_setup_command(*, confirm: Callable[[str], bool], print_fn: Callable[[str], None]) -> int:from .providers import windows_setup+ from . import netproxy++ ports = netproxy.WINDOWS_PORTSprint_fn("The full Windows sandbox runs agents' commands as a hidden local account. Setup asks for administrator rights once and:\n"- f" - creates the account `{windows_setup.ACCOUNT}` with a random password nobody sees, hidden from the sign-in screen,\n"- " with remote and network logon denied\n"- f" - adds one Windows Firewall rule, \"{windows_setup.FIREWALL_RULE}\", that blocks every outbound connection for that account\n"- f" - stores the password in {windows_setup.CRED_FILE}, readable by you and administrators only\n"- f" - records what it changed in {windows_setup.STATE_FILE}"+ f" - creates two accounts with random passwords nobody sees, hidden from the sign-in screen, with remote and network\n"+ f" logon denied: `{windows_setup.ACCOUNTS['open']}` (network open) and `{windows_setup.ACCOUNTS['closed']}` (allow list only)\n"+ f" - adds one Windows Firewall rule, \"{windows_setup.FIREWALL_RULE}\", that blocks every outbound connection for the\n"+ f" closed account, and four loopback filters that leave it only the proxy's ports {ports.start}-{ports.stop - 1}\n"+ f" - stores the passwords in {windows_setup.CRED_FILE}, readable by you and administrators only\n"+ f" - removes the account and rules of an earlier setup, and records what it changed in {windows_setup.STATE_FILE}")if not confirm("Run the setup now (one administrator prompt)?"):return 1@@ -110,6 +114,28 @@ def windows_setup_command(*, confirm: Callable[[str], bool], print_fn: Callable[return 0+def windows_remove_command(*, confirm: Callable[[str], bool], print_fn: Callable[[str], None]) -> int:+ from .providers import windows_setup++ print_fn(+ "This removes everything `sandbox setup` made on this machine (one administrator prompt): the two sandbox accounts\n"+ f"and their profiles, the firewall rule, the loopback filters, and {windows_setup.ROOT}."+ )+ if not confirm("Remove the Windows sandbox setup now?"):+ return 1+ ok, said = windows_setup.run_remove()+ print_fn("removed" if ok else f"remove failed: {said}")+ return 0 if ok else 1+++def remove(*, yes: bool = False, ask: Optional[Callable[[str], bool]] = None, print_fn: Callable[[str], None] = print) -> int:+ confirm = ask or (lambda question: yes or input(f"{question} [y/N] ").strip().lower() in ("y", "yes"))+ if sys.platform == "win32":+ return windows_remove_command(confirm=confirm, print_fn=print_fn)+ print_fn("`sandbox remove` undoes the Windows setup; there is nothing to remove on this platform.")+ return 2++def status(print_fn: Callable[[str], None] = print) -> int:if sys.platform == "win32":windows_rows(print_fn)diff --git a/coworker/sandbox/windows_wfp.py b/coworker/sandbox/windows_wfp.pynew file mode 100644index 00000000..f74cfdef--- /dev/null+++ b/coworker/sandbox/windows_wfp.py@@ -0,0 +1,402 @@+"""Loopback filters for the closed Windows sandbox account (Windows Filtering Platform).++Windows Firewall does not look at loopback traffic, so the outbound-block rule that setup+writes for the closed account (`OWSandboxClosedNet`) leaves every local port open to it:+a database, another app's control port, another sandbox's proxy. These filters close that+hole in the kernel, once, at setup (design doc `sandbox-windows-design.md`, 3b and 3d):++ layer ALE_AUTH_CONNECT (IPv4 and IPv6), our own sublayer, for that account only:+ PERMIT loopback, remote port in the proxy's range (netproxy.WINDOWS_PORTS) weight 15+ BLOCK loopback, any port weight 5++Listening is not touched (`vite`, `next dev` still work; your browser reaches them), and+the open account (`OWSandboxOpenNet`) has no filters at all. The filters are persistent+and carry fixed keys, so `remove` deletes exactly them.++This file runs ELEVATED, started by the setup script, and imports nothing from the+package on purpose: `python windows_wfp.py add|remove ...`, or `openworker-server+sandbox-wfp add|remove ...` from the frozen binary. Standard library only.+"""++from __future__ import annotations++import ctypes+import json+import sys+import uuid+from ctypes import wintypes+from typing import Any, Optional++# -- our fixed keys ---------------------------------------------------------------------+PROVIDER_KEY = uuid.UUID("3d0d3a3e-6c2b-5a3f-9f0a-1b6b7c1e8d21")+SUBLAYER_KEY = uuid.UUID("9a5b0f4c-2e7d-5c19-8a63-4f2e9c7b1d02")+FILTER_KEYS = {+ "permit-proxy-v4": uuid.UUID("c1d2e3f4-0001-5a6b-8c9d-0e1f2a3b4c01"),+ "block-loopback-v4": uuid.UUID("c1d2e3f4-0002-5a6b-8c9d-0e1f2a3b4c02"),+ "permit-proxy-v6": uuid.UUID("c1d2e3f4-0003-5a6b-8c9d-0e1f2a3b4c03"),+ "block-loopback-v6": uuid.UUID("c1d2e3f4-0004-5a6b-8c9d-0e1f2a3b4c04"),+}++# -- Windows' keys (fwpmu.h) ------------------------------------------------------------+LAYER_ALE_AUTH_CONNECT_V4 = uuid.UUID("c38d57d1-05a7-4c33-904f-7fbceee60e82")+LAYER_ALE_AUTH_CONNECT_V6 = uuid.UUID("4a72393b-319f-44bc-84c3-ba54dcb3b6b4")+CONDITION_ALE_USER_ID = uuid.UUID("af043a0a-b34d-4f86-979c-c90371af6e66")+CONDITION_IP_REMOTE_PORT = uuid.UUID("c35a604d-d22b-4e1a-91b4-68f674ee674b")+CONDITION_FLAGS = uuid.UUID("632ce23b-5167-435c-86d7-e903684aa80c")+FLAG_IS_LOOPBACK = 0x00000001++FWP_EMPTY, FWP_UINT8, FWP_UINT16, FWP_UINT32 = 0, 1, 2, 3+FWP_SECURITY_DESCRIPTOR_TYPE = 14+FWP_RANGE_TYPE = 0x102+MATCH_EQUAL, MATCH_RANGE, MATCH_FLAGS_ALL_SET = 0, 5, 6+ACTION_BLOCK, ACTION_PERMIT = 0x1001, 0x1002+FLAG_PERSISTENT = 0x1 # the same bit for providers, sublayers and filters+RPC_C_AUTHN_DEFAULT = 0xFFFFFFFF+SDDL_REVISION_1 = 1+ERROR_SUCCESS = 0+++# Fixed-width types on purpose: `U32` is 8 bytes on a Mac, and the layouts are+# checked by a test that runs everywhere.+U16, U32, U64 = ctypes.c_uint16, ctypes.c_uint32, ctypes.c_uint64+++class GUID(ctypes.Structure):+ _fields_ = [("Data1", U32), ("Data2", U16), ("Data3", U16), ("Data4", ctypes.c_ubyte * 8)]++ @classmethod+ def of(cls, value: uuid.UUID) -> "GUID":+ g = cls()+ g.Data1, g.Data2, g.Data3 = value.time_low, value.time_mid, value.time_hi_version+ g.Data4[:] = value.bytes[8:]+ return g+++class FWP_BYTE_BLOB(ctypes.Structure):+ _fields_ = [("size", U32), ("data", ctypes.c_void_p)]+++class FWPM_DISPLAY_DATA0(ctypes.Structure):+ _fields_ = [("name", wintypes.LPWSTR), ("description", wintypes.LPWSTR)]+++class _VALUE_UNION(ctypes.Union):+ _fields_ = [("uint8", ctypes.c_ubyte), ("uint16", U16), ("uint32", U32), ("pointer", ctypes.c_void_p)]+++class FWP_VALUE0(ctypes.Structure):+ _fields_ = [("type", U32), ("value", _VALUE_UNION)]+++class FWP_RANGE0(ctypes.Structure):+ _fields_ = [("valueLow", FWP_VALUE0), ("valueHigh", FWP_VALUE0)]+++class FWP_CONDITION_VALUE0(ctypes.Structure): # the same shape as FWP_VALUE0 (the union gains pointer members)+ _fields_ = [("type", U32), ("value", _VALUE_UNION)]+++class FWPM_FILTER_CONDITION0(ctypes.Structure):+ _fields_ = [("fieldKey", GUID), ("matchType", U32), ("conditionValue", FWP_CONDITION_VALUE0)]+++class _ACTION_UNION(ctypes.Union):+ _fields_ = [("filterType", GUID), ("calloutKey", GUID), ("bitmapIndex", ctypes.c_ubyte)]+++class FWPM_ACTION0(ctypes.Structure):+ _fields_ = [("type", U32), ("u", _ACTION_UNION)]+++class _CONTEXT_UNION(ctypes.Union):+ _fields_ = [("rawContext", U64), ("providerContextKey", GUID)]+++class FWPM_FILTER0(ctypes.Structure):+ _fields_ = [+ ("filterKey", GUID),+ ("displayData", FWPM_DISPLAY_DATA0),+ ("flags", U32),+ ("providerKey", ctypes.POINTER(GUID)),+ ("providerData", FWP_BYTE_BLOB),+ ("layerKey", GUID),+ ("subLayerKey", GUID),+ ("weight", FWP_VALUE0),+ ("numFilterConditions", U32),+ ("filterCondition", ctypes.POINTER(FWPM_FILTER_CONDITION0)),+ ("action", FWPM_ACTION0),+ ("context", _CONTEXT_UNION),+ ("reserved", ctypes.POINTER(GUID)),+ ("filterId", U64),+ ("effectiveWeight", FWP_VALUE0),+ ]+++class FWPM_PROVIDER0(ctypes.Structure):+ _fields_ = [+ ("providerKey", GUID),+ ("displayData", FWPM_DISPLAY_DATA0),+ ("flags", U32),+ ("providerData", FWP_BYTE_BLOB),+ ("serviceName", wintypes.LPWSTR),+ ]+++class FWPM_SUBLAYER0(ctypes.Structure):+ _fields_ = [+ ("subLayerKey", GUID),+ ("displayData", FWPM_DISPLAY_DATA0),+ ("flags", U16),+ ("providerKey", ctypes.POINTER(GUID)),+ ("providerData", FWP_BYTE_BLOB),+ ("weight", U16),+ ]+++def _fwp() -> Any:+ lib = ctypes.WinDLL("fwpuclnt")+ lib.FwpmEngineOpen0.argtypes = [wintypes.LPCWSTR, U32, ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(wintypes.HANDLE)]+ lib.FwpmEngineOpen0.restype = U32+ lib.FwpmEngineClose0.argtypes = [wintypes.HANDLE]+ lib.FwpmEngineClose0.restype = U32+ lib.FwpmProviderAdd0.argtypes = [wintypes.HANDLE, ctypes.POINTER(FWPM_PROVIDER0), ctypes.c_void_p]+ lib.FwpmProviderAdd0.restype = U32+ lib.FwpmProviderDeleteByKey0.argtypes = [wintypes.HANDLE, ctypes.POINTER(GUID)]+ lib.FwpmProviderDeleteByKey0.restype = U32+ lib.FwpmSubLayerAdd0.argtypes = [wintypes.HANDLE, ctypes.POINTER(FWPM_SUBLAYER0), ctypes.c_void_p]+ lib.FwpmSubLayerAdd0.restype = U32+ lib.FwpmSubLayerDeleteByKey0.argtypes = [wintypes.HANDLE, ctypes.POINTER(GUID)]+ lib.FwpmSubLayerDeleteByKey0.restype = U32+ lib.FwpmFilterAdd0.argtypes = [wintypes.HANDLE, ctypes.POINTER(FWPM_FILTER0), ctypes.c_void_p, ctypes.POINTER(U64)]+ lib.FwpmFilterAdd0.restype = U32+ lib.FwpmFilterDeleteByKey0.argtypes = [wintypes.HANDLE, ctypes.POINTER(GUID)]+ lib.FwpmFilterDeleteByKey0.restype = U32+ lib.FwpmFilterGetByKey0.argtypes = [wintypes.HANDLE, ctypes.POINTER(GUID), ctypes.POINTER(ctypes.POINTER(FWPM_FILTER0))]+ lib.FwpmFilterGetByKey0.restype = U32+ lib.FwpmFreeMemory0.argtypes = [ctypes.POINTER(ctypes.c_void_p)]+ lib.FwpmFreeMemory0.restype = None+ return lib+++class WfpError(OSError):+ pass+++def _check(code: int, what: str) -> None:+ if code != ERROR_SUCCESS:+ raise WfpError(f"{what}: error 0x{code & 0xFFFFFFFF:08x}")+++class Engine:+ """One open session with the Base Filtering Engine. Not dynamic: what is added with+ the persistent flag outlives it."""++ def __init__(self) -> None:+ self._lib = _fwp()+ self.handle = wintypes.HANDLE()+ _check(self._lib.FwpmEngineOpen0(None, RPC_C_AUTHN_DEFAULT, None, None, ctypes.byref(self.handle)), "FwpmEngineOpen0")++ def close(self) -> None:+ if self.handle:+ self._lib.FwpmEngineClose0(self.handle)+ self.handle = wintypes.HANDLE()++ def __enter__(self) -> "Engine":+ return self++ def __exit__(self, *exc: Any) -> None:+ self.close()++ # -- keep references to every buffer a struct points at until the call returns -------+ def _display(self, name: str, description: str, keep: list) -> FWPM_DISPLAY_DATA0:+ n, d = ctypes.c_wchar_p(name), ctypes.c_wchar_p(description)+ keep += [n, d]+ return FWPM_DISPLAY_DATA0(ctypes.cast(n, wintypes.LPWSTR), ctypes.cast(d, wintypes.LPWSTR))++ def add_provider(self) -> None:+ keep: list = []+ provider = FWPM_PROVIDER0()+ provider.providerKey = GUID.of(PROVIDER_KEY)+ provider.displayData = self._display("OpenWorker sandbox", "Loopback filters for the OpenWorker sandbox account", keep)+ provider.flags = FLAG_PERSISTENT+ _check(self._lib.FwpmProviderAdd0(self.handle, ctypes.byref(provider), None), "FwpmProviderAdd0")++ def add_sublayer(self) -> None:+ keep: list = []+ sub = FWPM_SUBLAYER0()+ sub.subLayerKey = GUID.of(SUBLAYER_KEY)+ sub.displayData = self._display("OpenWorker sandbox", "Loopback: the allow-list proxy only", keep)+ sub.flags = FLAG_PERSISTENT+ provider_key = GUID.of(PROVIDER_KEY)+ keep.append(provider_key)+ sub.providerKey = ctypes.pointer(provider_key)+ sub.weight = 0xFFFF # evaluated before the firewall's own sublayer+ _check(self._lib.FwpmSubLayerAdd0(self.handle, ctypes.byref(sub), None), "FwpmSubLayerAdd0")++ def add_filter(self, key: uuid.UUID, *, layer: uuid.UUID, name: str, action: int, weight: int, conditions: list) -> int:+ keep: list = []+ array = (FWPM_FILTER_CONDITION0 * len(conditions))()+ for i, (field, match, value) in enumerate(conditions):+ array[i].fieldKey = GUID.of(field)+ array[i].matchType = match+ array[i].conditionValue = value(keep)+ flt = FWPM_FILTER0()+ flt.filterKey = GUID.of(key)+ flt.displayData = self._display(f"OpenWorker sandbox: {name}", "Written by `openworker machine sandbox setup`", keep)+ flt.flags = FLAG_PERSISTENT+ provider_key = GUID.of(PROVIDER_KEY)+ keep.append(provider_key)+ flt.providerKey = ctypes.pointer(provider_key)+ flt.layerKey = GUID.of(layer)+ flt.subLayerKey = GUID.of(SUBLAYER_KEY)+ flt.weight.type = FWP_UINT8+ flt.weight.value.uint8 = weight+ flt.numFilterConditions = len(conditions)+ flt.filterCondition = ctypes.cast(array, ctypes.POINTER(FWPM_FILTER_CONDITION0))+ flt.action.type = action+ filter_id = U64()+ _check(self._lib.FwpmFilterAdd0(self.handle, ctypes.byref(flt), None, ctypes.byref(filter_id)), f"FwpmFilterAdd0 ({name})")+ return int(filter_id.value)++ def delete_filter(self, key: uuid.UUID) -> bool:+ return self._lib.FwpmFilterDeleteByKey0(self.handle, ctypes.byref(GUID.of(key))) == ERROR_SUCCESS++ def delete_sublayer(self) -> bool:+ return self._lib.FwpmSubLayerDeleteByKey0(self.handle, ctypes.byref(GUID.of(SUBLAYER_KEY))) == ERROR_SUCCESS++ def delete_provider(self) -> bool:+ return self._lib.FwpmProviderDeleteByKey0(self.handle, ctypes.byref(GUID.of(PROVIDER_KEY))) == ERROR_SUCCESS++ def filter_exists(self, key: uuid.UUID) -> Optional[bool]:+ """True or False, or None when this process may not ask (not an administrator)."""+ out = ctypes.POINTER(FWPM_FILTER0)()+ code = self._lib.FwpmFilterGetByKey0(self.handle, ctypes.byref(GUID.of(key)), ctypes.byref(out))+ if code == ERROR_SUCCESS:+ self._lib.FwpmFreeMemory0(ctypes.byref(ctypes.cast(out, ctypes.c_void_p)))+ return True+ if code & 0xFFFFFFFF == 0x80320003: # FWP_E_FILTER_NOT_FOUND+ return False+ return None+++# -- the condition values ---------------------------------------------------------------+def _user_condition(sid: str):+ """The account, as a security descriptor the token is checked against (the same+ `D:(A;;CC;;;SID)` Windows Firewall's LocalUser takes)."""++ def build(keep: list) -> FWP_CONDITION_VALUE0:+ adv = ctypes.WinDLL("advapi32")+ adv.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [wintypes.LPCWSTR, U32, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(U32)]+ descriptor, size = ctypes.c_void_p(), U32()+ if not adv.ConvertStringSecurityDescriptorToSecurityDescriptorW(f"D:(A;;CC;;;{sid})", SDDL_REVISION_1, ctypes.byref(descriptor), ctypes.byref(size)):+ raise WfpError(f"cannot build a security descriptor for {sid}")+ blob = FWP_BYTE_BLOB(size.value, descriptor)+ keep += [descriptor, blob]+ value = FWP_CONDITION_VALUE0()+ value.type = FWP_SECURITY_DESCRIPTOR_TYPE+ value.value.pointer = ctypes.addressof(blob)+ return value++ return build+++def _loopback_condition():+ def build(keep: list) -> FWP_CONDITION_VALUE0:+ value = FWP_CONDITION_VALUE0()+ value.type = FWP_UINT32+ value.value.uint32 = FLAG_IS_LOOPBACK+ return value++ return build+++def _port_range_condition(low: int, high: int):+ def build(keep: list) -> FWP_CONDITION_VALUE0:+ rng = FWP_RANGE0()+ rng.valueLow.type = rng.valueHigh.type = FWP_UINT16+ rng.valueLow.value.uint16, rng.valueHigh.value.uint16 = low, high+ keep.append(rng)+ value = FWP_CONDITION_VALUE0()+ value.type = FWP_RANGE_TYPE+ value.value.pointer = ctypes.addressof(rng)+ return value++ return build+++# -- the operations ---------------------------------------------------------------------+def add(sid: str, port_low: int, port_high: int) -> dict[str, int]:+ """Write the provider, the sublayer and the four filters for `sid`. Idempotent: what+ exists under our keys is deleted first. Returns the filter ids by name."""+ with Engine() as engine:+ remove_with(engine)+ engine.add_provider()+ engine.add_sublayer()+ ids: dict[str, int] = {}+ for suffix, layer in (("v4", LAYER_ALE_AUTH_CONNECT_V4), ("v6", LAYER_ALE_AUTH_CONNECT_V6)):+ user = (CONDITION_ALE_USER_ID, MATCH_EQUAL, _user_condition(sid))+ loopback = (CONDITION_FLAGS, MATCH_FLAGS_ALL_SET, _loopback_condition())+ ports = (CONDITION_IP_REMOTE_PORT, MATCH_RANGE, _port_range_condition(port_low, port_high))+ ids[f"permit-proxy-{suffix}"] = engine.add_filter(+ FILTER_KEYS[f"permit-proxy-{suffix}"], layer=layer, name=f"loopback to the allow-list proxy ({suffix})",+ action=ACTION_PERMIT, weight=15, conditions=[user, loopback, ports],+ ) # fmt: skip+ ids[f"block-loopback-{suffix}"] = engine.add_filter(+ FILTER_KEYS[f"block-loopback-{suffix}"], layer=layer, name=f"no other local port ({suffix})",+ action=ACTION_BLOCK, weight=5, conditions=[user, loopback],+ ) # fmt: skip+ return ids+++def remove_with(engine: Engine) -> int:+ removed = sum(engine.delete_filter(key) for key in FILTER_KEYS.values())+ engine.delete_sublayer()+ engine.delete_provider()+ return removed+++def remove() -> int:+ with Engine() as engine:+ return remove_with(engine)+++def present() -> Optional[bool]:+ """All four filters exist; False when any is missing; None when this process may not+ look (the check then falls to the in-sandbox probe at session start)."""+ try:+ with Engine() as engine:+ seen = [engine.filter_exists(key) for key in FILTER_KEYS.values()]+ except OSError:+ return None+ if any(s is None for s in seen):+ return None+ return all(seen)+++def main(argv: Optional[list[str]] = None) -> int:+ import argparse++ parser = argparse.ArgumentParser(prog="windows_wfp")+ sub = parser.add_subparsers(dest="command", required=True)+ p_add = sub.add_parser("add")+ p_add.add_argument("--sid", required=True)+ p_add.add_argument("--port-low", type=int, required=True)+ p_add.add_argument("--port-high", type=int, required=True)+ sub.add_parser("remove")+ sub.add_parser("present")+ args = parser.parse_args(argv)+ try:+ if args.command == "add":+ print(json.dumps({"ok": True, "filters": add(args.sid, args.port_low, args.port_high)}))+ elif args.command == "remove":+ print(json.dumps({"ok": True, "removed": remove()}))+ else:+ print(json.dumps({"ok": True, "present": present()}))+ except OSError as exc:+ print(json.dumps({"ok": False, "error": str(exc)}))+ return 1+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tests/test_sandbox_windows.py b/tests/test_sandbox_windows.pyindex bbfee96a..c094571b 100644--- a/tests/test_sandbox_windows.py+++ b/tests/test_sandbox_windows.py@@ -24,13 +24,13 @@ def _has_setup() -> bool:return Falsefrom coworker.sandbox.providers import windows_setup- return windows_setup.account() is not None+ return windows_setup.account("open") is not None and windows_setup.account("closed") is not Nonefull = pytest.mark.skipif(not _has_setup(), reason="needs `openworker machine sandbox setup` on this machine")-def _open(tmp_path, *, mode, extra_roots=()):+def _open(tmp_path, *, mode, extra_roots=(), network=False, profile="strict"):from coworker.sandbox.providers.windows import WindowsProviderproject = tmp_path / "project"@@ -38,7 +38,7 @@ def _open(tmp_path, *, mode, extra_roots=()):(project / "a.txt").write_text("hello\n")zipapp = build_runner_zipapp(tmp_path / "dist")roots = [{"path": str(project), "writable": True}, *extra_roots]- provider = WindowsProvider(roots=roots, cwd=project, runner_path=zipapp, network=False, force_mode=mode)+ provider = WindowsProvider(roots=roots, cwd=project, runner_path=zipapp, network=network, profile=profile, force_mode=mode)ws = RunnerWorkspace(provider, cwd=project)ws.executor.default_timeout = 60return ws, provider, project@@ -97,12 +97,14 @@ def test_a_session_workspace_goes_through_the_registry(tmp_path, monkeypatch):from coworker.sandbox.workspace import open_workspacemonkeypatch.setenv("OPENWORKER_SANDBOX_PROVIDER", "windows")+ expected = "full" if _has_setup() else "partial" # the default rule: full mode once setup has runws = open_workspace(cwd=tmp_path, session_id="s-win", agent="cowork")try:- assert ws.describe()["provider"] == "windows" and ws.describe()["enforcement"] == "partial"+ assert ws.describe()["provider"] == "windows" and ws.describe()["enforcement"] == expectedassert ws.executor.run("echo via-sandbox")["output"].strip() == "via-sandbox"rows = SandboxRegistry().list()- assert [r["session_id"] for r in rows] == ["s-win"] and rows[0]["enforcement"] == "partial"+ assert [r["session_id"] for r in rows] == ["s-win"] and rows[0]["enforcement"] == expected+ assert rows[0]["profile"] == "open" # the Windows defaultfinally:ws.close()assert SandboxRegistry().list() == []@@ -160,7 +162,7 @@ def test_a_granted_ssh_key_reaches_github_through_the_proxy(tmp_path):said = _ssh_probe(ws)assert "Permission denied (publickey)" in said, saidinside = ws.executor.run("Join-Path $env:USERPROFILE '.ssh'")["output"].strip()- assert inside.lower().startswith(r"c:\users\openworkersandbox")+ assert inside.lower().startswith(r"c:\users\owsandboxclosednet")finally:ws.close()@@ -225,21 +227,26 @@ def test_full_mode_removes_the_copies_when_the_sandbox_ends(tmp_path):@pytest.mark.skipif(os.environ.get("OPENWORKER_TEST_WINDOWS_SETUP") != "1", reason="changes the machine; OPENWORKER_TEST_WINDOWS_SETUP=1 to run")-def test_setup_creates_the_hidden_account_and_status_reports_it():+def test_setup_creates_the_two_hidden_accounts_and_status_reports_it():import subprocess- from coworker.sandbox import setup_cmd+ from coworker.sandbox import setup_cmd, windows_wfpfrom coworker.sandbox.providers import windows_setupsaid: list[str] = []assert setup_cmd.setup(ask=lambda q: True, print_fn=said.append) == 0, "\n".join(said)- name, sid, password = windows_setup.account()- assert name == windows_setup.ACCOUNT and sid.startswith("S-1-5-21-") and len(password) > 40- users = subprocess.run(["powershell", "-NoProfile", "-Command", f"(Get-LocalUser {name}).Enabled"], capture_output=True, text=True).stdout- assert "True" in users+ for kind in ("open", "closed"):+ name, sid, password = windows_setup.account(kind)+ assert name == windows_setup.ACCOUNTS[kind] and sid.startswith("S-1-5-21-") and len(password) > 40+ users = subprocess.run(["powershell", "-NoProfile", "-Command", f"(Get-LocalUser {name}).Enabled"], capture_output=True, text=True).stdout+ assert "True" in users+ for legacy in windows_setup.LEGACY_ACCOUNTS: # the first build's account is gone+ gone = subprocess.run(["powershell", "-NoProfile", "-Command", f"Get-LocalUser {legacy} -ErrorAction SilentlyContinue; 'end'"], capture_output=True, text=True).stdout+ assert legacy not in gonerule = subprocess.run(["powershell", "-NoProfile", "-Command", f"(Get-NetFirewallRule -DisplayName '{windows_setup.FIREWALL_RULE}').Enabled"], capture_output=True, text=True).stdoutassert "True" in rule- assert any("setup has run" in line for line in said)+ assert windows_setup.filters_recorded() and windows_wfp.present() in (True, None)+ assert any("setup has run" in line for line in said) and all("[--]" not in line for line in said if "Windows" not in line and "OpenShell" not in line)@full@@ -251,7 +258,7 @@ def test_full_mode_hides_the_profile_and_gives_the_sessions_folders(tmp_path):try:assert ws.describe()["mode"] == "full"who = ws.executor.run("$env:USERNAME")["output"].strip()- assert who.lower() == "openworkersandbox"+ assert who.lower() == "owsandboxclosednet"assert "made" in ws.executor.run("Set-Content -Path new.txt -Value made; Get-Content new.txt")["output"]assert "read me" in ws.executor.run(f"Get-Content '{reference / 'ref.txt'}'")["output"]assert ws.executor.run(f"Set-Content -Path '{reference / 'no.txt'}' -Value x")["exit_code"] != 0@@ -290,5 +297,31 @@ def test_full_mode_blocks_the_network_except_the_proxy(tmp_path):assert "200" in via["output"], via["output"]refused = ws.executor.run("curl.exe -sS --ssl-revoke-best-effort -m 30 -o NUL -w '%{http_code}' https://example.com")assert "403" in refused["output"], refused["output"]+ # loopback: the proxy's port range and nothing else (the WFP filters from setup)+ from coworker.sandbox import netproxy++ assert provider._proxy is not None and provider._proxy.port in netproxy.WINDOWS_PORTS+ assert ws.client.call("net.probe", {"host": "127.0.0.1", "port": provider._proxy.port}, timeout=15)["ok"]+ other = ws.client.call("net.probe", {"host": "127.0.0.1", "port": 22}, timeout=15) # sshd listens on the VM+ assert not other["ok"], other+ assert ws.describe()["enforcement"] == "full"+ finally:+ ws.close()+++@full+def test_the_open_profile_runs_as_the_open_account_with_the_network_open(tmp_path):+ ws, provider, project = _open(tmp_path, mode="full", network=True, profile="open")+ ws.executor.default_timeout = 90+ try:+ assert provider.kind == "open" and provider._proxy is None+ who = ws.executor.run("$env:USERNAME")["output"].strip()+ assert who.lower() == "owsandboxopennet"+ direct = ws.executor.run("curl.exe -sS --ssl-revoke-best-effort -m 20 -o NUL -w '%{http_code}' https://example.com")+ assert "200" in direct["output"], direct["output"]+ assert ws.client.call("net.probe", {"host": "127.0.0.1", "port": 22}, timeout=15)["ok"] # any local port+ listing = ws.executor.run(f"Get-ChildItem '{os.path.expanduser('~')}'")+ assert listing["exit_code"] != 0 # the files are still the wall+ assert ws.describe()["enforcement"] == "full" and "open" in ws.describe()["reason"]finally:ws.close()diff --git a/tests/test_windows_wfp_layout.py b/tests/test_windows_wfp_layout.pynew file mode 100644index 00000000..ce5103f7--- /dev/null+++ b/tests/test_windows_wfp_layout.py@@ -0,0 +1,22 @@+"""windows_wfp.py is loaded only on Windows, but its structures are plain ctypes and can be+checked anywhere: a GUID round-trips, and the layouts match the 64-bit headers."""++from __future__ import annotations++import ctypes+import uuid++from coworker.sandbox import windows_wfp as wfp+++def test_guid_round_trips_and_layouts_match_the_headers():+ value = uuid.UUID("c38d57d1-05a7-4c33-904f-7fbceee60e82")+ g = wfp.GUID.of(value)+ assert (g.Data1, g.Data2, g.Data3, bytes(g.Data4)) == (0xC38D57D1, 0x05A7, 0x4C33, bytes.fromhex("904f7fbceee60e82"))+ assert ctypes.sizeof(wfp.GUID) == 16+ assert ctypes.sizeof(wfp.FWP_VALUE0) == 16 and ctypes.sizeof(wfp.FWP_CONDITION_VALUE0) == 16+ assert ctypes.sizeof(wfp.FWPM_FILTER_CONDITION0) == 40+ assert ctypes.sizeof(wfp.FWPM_ACTION0) == 20+ assert ctypes.sizeof(wfp.FWPM_FILTER0) == 200 if ctypes.sizeof(ctypes.c_void_p) == 8 else True+ assert len(wfp.FILTER_KEYS) == 4 and len({str(k) for k in wfp.FILTER_KEYS.values()}) == 4+ assert wfp.ACTION_BLOCK == 0x1001 and wfp.ACTION_PERMIT == 0x1002