Configure operator-requested persistent SSH transport for the development Windows VM
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-16 13:33:45 +01:00
parent 090fcdffda
commit 67f6f582c7
4 changed files with 144 additions and 0 deletions
@@ -0,0 +1,69 @@
#requires -Version 5.1
#requires -RunAsAdministrator
[CmdletBinding()]
param(
[Parameter(Mandatory)][ValidatePattern('^[a-f0-9]{64}$')][string]$ExpectedTaskSha256,
[Parameter(Mandatory)][DateTimeOffset]$HandoffAt
)
# Explicit operator request: persistent access to the development VM only.
# No RMM job, enrollment, credential replacement or public Windows listener.
$ErrorActionPreference = 'Stop'
if ($HandoffAt -le [DateTimeOffset]::UtcNow.AddMinutes(1) -or
$HandoffAt -gt [DateTimeOffset]::UtcNow.AddHours(2)) { throw 'Expected near-term handoff after the existing window.' }
$root = 'C:\ProgramData\RmmDevAccess'
$case = Join-Path $root 'persistent-development-access-20261116'
if (Test-Path -LiteralPath $case) { throw 'Existing access change: inspect original receipt, never repeat.' }
function Get-RmmAccessTextSha256([string]$Value) {
$sha = [Security.Cryptography.SHA256]::Create()
try { return [BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes($Value))).Replace('-','').ToLowerInvariant() }
finally { $sha.Dispose() }
}
# Preserve the exact existing descriptors; the public .pub file has read-only Everyone access.
$expectedAccessDescriptors = @{
'RmmDevAccess' = 'O:BAG:S-1-5-21-2396205367-1554564896-454414693-513D:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)'
'bootstrap-public.json' = 'O:BAG:S-1-5-21-2396205367-1554564896-454414693-513D:AI(A;ID;FA;;;SY)(A;ID;FA;;;BA)'
'known_hosts' = 'O:BAG:S-1-5-21-2396205367-1554564896-454414693-513D:AI(A;ID;FA;;;SY)(A;ID;FA;;;BA)'
'tunnel_ed25519' = 'O:BAG:S-1-5-21-2396205367-1554564896-454414693-513D:PAI(A;;FA;;;SY)(A;;FA;;;BA)'
'tunnel_ed25519.pub' = 'O:LAG:S-1-5-21-2396205367-1554564896-454414693-513D:P(A;;0x1200a9;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1301bf;;;LA)'
}
foreach ($path in @($root,(Join-Path $root 'bootstrap-public.json'),(Join-Path $root 'known_hosts'),(Join-Path $root 'tunnel_ed25519'),(Join-Path $root 'tunnel_ed25519.pub'))) {
$item = Get-Item -LiteralPath $path -Force
if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { throw 'Original access path required.' }
$acl = Get-Acl -LiteralPath $path
if ($acl.Sddl -cne $expectedAccessDescriptors[(Split-Path -Leaf $path)]) { throw 'Original access descriptor differs.' }
}
$cfg = Get-Content -LiteralPath (Join-Path $root 'bootstrap-public.json') -Raw | ConvertFrom-Json
if ($cfg.server -cne 'discovery.ease-technologies.de') { throw 'Expected fixed server.' }
if ((Get-Content -LiteralPath (Join-Path $root 'known_hosts') -Raw).Trim() -cne
('discovery.ease-technologies.de ' + $cfg.server_host_key)) { throw 'Confirmed original host pin required.' }
$public = ((Get-Content -LiteralPath (Join-Path $root 'tunnel_ed25519.pub') -Raw).Trim() -split '\s+')[0..1] -join ' '
if ($public -cne 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJEkk35zkrDXkOqggrvgsrataMZOzrqFTSh3jWbptbSa') { throw 'Original tunnel public key required.' }
$listen = @(Get-NetTCPConnection -State Listen -LocalPort 22222)
if ($listen.Count -ne 1 -or $listen[0].LocalAddress -cne '127.0.0.1' -or
(Get-Service sshd).Status -ne 'Running') { throw 'Original loopback-only sshd required.' }
$user = Get-LocalUser -Name 'rmm-dev'
if (!$user.Enabled -or $user.SID.Value -cne 'S-1-5-21-2396205367-1554564896-454414693-1002') { throw 'Original development account required.' }
$xml = Export-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel'
if ((Get-RmmAccessTextSha256 $xml) -cne $ExpectedTaskSha256) { throw 'Previous task differs.' }
if ([string](Get-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel').State -ceq 'Running') { throw 'Do not replace a running task.' }
$null = New-Item -ItemType Directory -Path $case
[IO.File]::WriteAllText((Join-Path $case 'original-task.xml'),$xml,[Text.UTF8Encoding]::new($false))
$originalExpiry = if ($null -ne $user.AccountExpires) { $user.AccountExpires.ToUniversalTime().ToString('o') } else { $null }
@{original_task_sha256=$ExpectedTaskSha256;original_account_expiry_utc=$originalExpiry;handoff_at_utc=$HandoffAt.UtcDateTime.ToString('o');account_sid=$user.SID.Value} |
ConvertTo-Json -Compress | Set-Content -LiteralPath (Join-Path $case 'intent.json') -Encoding UTF8
$tunnelArguments = '-F NUL -N -T -o BatchMode=yes -o IdentitiesOnly=yes -o ForwardAgent=no -o StrictHostKeyChecking=yes -o GlobalKnownHostsFile=NUL -o UserKnownHostsFile=C:\ProgramData\RmmDevAccess\known_hosts -o ExitOnForwardFailure=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=2 -i C:\ProgramData\RmmDevAccess\tunnel_ed25519 -R 127.0.0.1:22240:127.0.0.1:22222 rmm-win-tunnel@discovery.ease-technologies.de'
$action = New-ScheduledTaskAction -Execute "$env:WINDIR\System32\OpenSSH\ssh.exe" -Argument $tunnelArguments
$triggers = @((New-ScheduledTaskTrigger -AtStartup),(New-ScheduledTaskTrigger -Once -At $HandoffAt.LocalDateTime))
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -MultipleInstances IgnoreNew -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
$task = New-ScheduledTask -Action $action -Trigger $triggers -Settings $settings -Principal $principal -Description 'Operator-authorized persistent SSH transport for this development Windows VM only; no RMM authorization.'
$null = Register-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel' -InputObject $task -Force
Set-LocalUser -SID $user.SID -AccountNeverExpires
$actual = Get-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel'
$actualUser = Get-LocalUser -SID $user.SID
if ($null -ne $actualUser.AccountExpires -or $actual.Actions.Count -ne 1 -or
$actual.Actions[0].Arguments -cne $tunnelArguments -or $actual.Settings.ExecutionTimeLimit -cne 'PT0S' -or
$actual.Principal.UserId -notin @('SYSTEM','NT AUTHORITY\SYSTEM','S-1-5-18') -or $actual.Triggers.Count -ne 2) { throw 'Persistent access readback differs; inspect original case.' }
$result = [ordered]@{schema_version=1;utc=[DateTime]::UtcNow.ToString('o');development_transport_only=$true;account_expires=$false;task='RmmDevReverseTunnel';task_sha256=(Get-RmmAccessTextSha256 (Export-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel'));handoff_at_utc=$HandoffAt.UtcDateTime.ToString('o');reverse_listen='127.0.0.1:22240';windows_listen='127.0.0.1:22222';existing_keys_preserved=$true;current_tunnel_stopped=$false;rmm_jobs_created=$false;rmm_services_changed=$false}
$result | ConvertTo-Json -Compress | Set-Content -LiteralPath (Join-Path $case 'result.json') -Encoding UTF8
$result | ConvertTo-Json -Compress