From ca588abce40334dc514a36ad04c6fbfb153597ba Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sun, 15 Nov 2026 16:56:27 +0100 Subject: [PATCH] Prepare guarded console completion for the verified Windows time profile --- .../windows/Complete-OriginalTimeProfile.ps1 | 112 ++++++++++++++++++ docs/windows-time-profile-config.md | 21 ++++ 2 files changed, 133 insertions(+) create mode 100644 deploy/windows/Complete-OriginalTimeProfile.ps1 diff --git a/deploy/windows/Complete-OriginalTimeProfile.ps1 b/deploy/windows/Complete-OriginalTimeProfile.ps1 new file mode 100644 index 0000000..af8eaf6 --- /dev/null +++ b/deploy/windows/Complete-OriginalTimeProfile.ps1 @@ -0,0 +1,112 @@ +#requires -Version 5.1 +#requires -RunAsAdministrator +# Finish the single, console-verified public-file publication on the test VM. +# Run in a fresh Windows PowerShell process. No SSH or RMM job is involved. +$ErrorActionPreference = 'Stop' +$caseId = 'ad9fd154-b2be-4f5d-9e17-65abb721ceaf' +$controllerHash = 'd8b4fd4fd9ca5310cc1591ce3c2ccae5bd72a2ade43b21a8187cfdfb960b3427' +$configHash = '2b4a8a7080770304b80e2a665fa5f5ee1ebbd9182e7c80b184e75b114575b42b' +$bootstrap = 'C:\ProgramData\RustDiscoveryBootstrap' +$casePath = Join-Path $bootstrap "time-profile-$caseId" +$original = Join-Path (Join-Path $bootstrap $controllerHash) 'InstallPublicTimeProfile.ps1' +$originalBytes = [IO.File]::ReadAllBytes($original) +$sha = [Security.Cryptography.SHA256]::Create() +try { + $digest = [BitConverter]::ToString($sha.ComputeHash($originalBytes)).Replace('-', '') +} finally { $sha.Dispose() } +if ($digest -ne $controllerHash) { throw 'Unexpected original controller' } + +# Import the verified definitions (including native FileID/SCM checks) from the +# installed original. Its first top-level preflight and install body are excluded. +$source = [Text.Encoding]::UTF8.GetString($originalBytes) +$boundary = "`nCheckControl `$boot `$true`n" +$index = $source.IndexOf($boundary, [StringComparison]::Ordinal) +if ($index -lt 1 -or $index -ne $source.LastIndexOf($boundary, [StringComparison]::Ordinal)) { + throw 'Original controller definition boundary missing' +} +$intent = Get-Content -LiteralPath (Join-Path $casePath 'intent.json') -Raw | ConvertFrom-Json +if ($intent.id -cne $caseId -or $intent.request.id -cne $caseId -or + $intent.request.script_sha256 -cne $controllerHash -or $intent.request.config_sha256 -cne $configHash) { + throw 'Unexpected original intent' +} +$global:RmmTimeProfilePayload = $intent.request +. ([scriptblock]::Create($source.Substring(0, $index))) + +CheckControl $boot $true +CheckControl $folder $true +foreach ($leaf in 'intent.json','publish-intent.json','directory-acl-intent.json','directory-acl-done.json') { + CheckControl (Join-Path $folder $leaf) $false +} +CheckControl $active $false +$activeRecord = Get-Content -LiteralPath $active -Raw | ConvertFrom-Json +if ($activeRecord.id -cne $caseId -or $activeRecord.script_sha256 -cne $controllerHash -or + $activeRecord.config_sha256 -cne $configHash -or $activeRecord.no_retry -ne $true) { + throw 'Unexpected active publication record' +} +$bytes = ConfigBytes +$publish = Get-Content -LiteralPath (Join-Path $folder 'publish-intent.json') -Raw | ConvertFrom-Json +$aclDone = Get-Content -LiteralPath (Join-Path $folder 'directory-acl-done.json') -Raw | ConvertFrom-Json +$staged = Join-Path $folder 'time-attestation.candidate.json' +$resultPath = Join-Path $folder 'result.json' +$reservation = Join-Path $folder 'console-finalization-intent.json' +foreach ($path in @($target,$resultPath,$reservation)) { + if (Test-Path -LiteralPath $path) { throw 'Existing target or finalization record: read original case' } +} +$null = ReadAcl $staged $false +$rootAcl = ReadAcl $helperRoot $true +$expected = @{ + volume_serial = '9815088561147542064' + file_id = '6d990300000003000000000000000000' + bytes = 1177 + acl_sha256 = '3a426f649cd6ff4e82bf70205c8ecc7d5b2c0775fa1e10fc30ce35de9bb12493' + sha256 = $configHash +} +if ($publish.create_once -ne $true -or $publish.config_sha256 -cne $configHash -or + !(JsonEqual $expected $publish.staged) -or !(JsonEqual $expected (FileIdentity $staged)) -or + (FileVolume $helperRoot) -cne $expected.volume_serial -or $rootAcl.Sddl -cne $aclDone.sddl -or + (AclHash $helperRoot) -cne '186d85132cf0e16cbb9623ad9636b7777741939a8cae3d689a2af1eb267a7f49') { + throw 'Original file identity or permissions changed' +} +if ($p.probe_sha256 -cne '727732fb4b38ac5431a330527bd52a3ab80af8f189ae75a7b48eceb1d4b69cac' -or + $p.system_probe_sha256 -cne '77a344912c61b7ac4a92152ae1b534ca7c36fe6f7dc4675783f20558c3a7f434') { + throw 'Unexpected native status probes' +} +$before = ActualState $true +SameLive $request.expected_native $before +SameLive $intent.before $before +if ($before.agent_kernel.creation_filetime -cne '134392294174782153' -or + $before.helper_kernel.creation_filetime -cne '134392294130218261') { throw 'Service generation changed' } + +$heldAgent = [RmmPipeNative]::Open([int]$before.service.pid) +try { + $heldHelper = [RmmPipeNative]::Open([int]$before.helper_service.pid) + try { + $null = Kernel 'agent' $before.service $heldAgent + $null = Kernel 'helper' $before.helper_service $heldHelper + # WriteNew uses CreateNew. Concurrent or later invocations cannot republish. + WriteNew $reservation @{id=$caseId; config_sha256=$configHash; utc=[DateTime]::UtcNow.ToString('o')} + try { + [IO.File]::Move($staged, $target) + } catch { + $cause = $_.Exception.GetBaseException() + $failure = @{id=$caseId; phase='File.Move'; hresult=('0x{0:X8}' -f $cause.HResult); exception=$cause.GetType().FullName} + WriteNew (Join-Path $folder 'console-finalization-error.json') $failure + throw ("File.Move: {0} ({1})." -f $failure.exception, $failure.hresult) + } + $null = ReadAcl $target $false + $published = FileIdentity $target + if (!(JsonEqual $expected $published) -or (Test-Path -LiteralPath $staged)) { throw 'Publication postcheck failed' } + $after = ActualState $true + SameLive $before $after + $null = Kernel 'agent' $before.service $heldAgent + $null = Kernel 'helper' $before.helper_service $heldHelper + WriteNew $resultPath @{ + id=$caseId; installed=$true; install_not_before=$intent.install_not_before + configuration=$published; native=$after; original_root_sddl=$intent.original_root_sddl + root_sddl=(Get-Acl -LiteralPath $helperRoot).Sddl; no_jobs=$true + clock_runtime_qualified=$false; services_changed=$false; private_key_read=$false + console_finalized=$true; original_move_failed=$true + } + 'Zeitprofil veroeffentlicht; Dateiidentitaet und Dienstgenerationen geprueft.' + } finally { $heldHelper.Dispose() } +} finally { $heldAgent.Dispose() } diff --git a/docs/windows-time-profile-config.md b/docs/windows-time-profile-config.md index ff3bdd4..49f8670 100644 --- a/docs/windows-time-profile-config.md +++ b/docs/windows-time-profile-config.md @@ -270,3 +270,24 @@ Publish-Intent hinterlegten Wert `2b4a8a7080770304b80e2a665fa5f5ee1ebbd9182e7c80 PowerShell `ObjectNotFound`. Dieser Konsolenbefund ergänzt den früheren Readback um den Inhalt des verbliebenen Kandidaten. FileID, DACL und Dienstgeneration wurden bei dieser Konsolenabfrage nicht erfasst. + +Für die Fortsetzung an derselben VM-Konsole liegt +[`Complete-OriginalTimeProfile.ps1`](../deploy/windows/Complete-OriginalTimeProfile.ps1) +bereit. Der Befehl übernimmt die Prüffunktionen aus den SHA-256-geprüften +Bytes des bereits installierten Controllers. Vor der Veröffentlichung prüft er +die ursprüngliche Datei-ID und das Volume, den Inhalt, die DACL, die +ursprünglichen Marker und beide laufenden Dienstgenerationen. Er hält die +beiden Prozesshandles während der Veröffentlichung offen. Ein exklusiv +erstellter zusätzlicher Marker verhindert eine doppelte Ausführung. + +Nach der Verschiebung an den festen Zielpfad vergleicht das Skript die +Dateiidentität und die Dienstzustände erneut. Bei einem neuen Move-Fehler erfasst +es Fehlerklasse und HRESULT für diesen Aufruf. Ein erneuter Start trifft auf +den Abschlussmarker oder den vorhandenen Zielpfad und beendet sich. Das +Ergebnis einer erfolgreichen Veröffentlichung hält die separate +Laufzeitprüfung der beiden Dienstrollen ausdrücklich offen. + +Lokal geprüft wurden die PowerShell-Syntax, die Bindung an die gespeicherten +Originalbelege und der Vergleich mit der tatsächlichen Kandidatenidentität. +Eine veränderte FileID wurde im lokalen Vergleich abgewiesen. Die Ausführung +des Abschlussbefehls auf der VM und die anschließende Zeitprüfung stehen aus.