Files
rust-discovery/deploy/windows/Open-TimeProfileReceiptWindow.ps1
2026-11-15 17:03:38 +01:00

106 lines
6.2 KiB
PowerShell

#requires -Version 5.1
#requires -RunAsAdministrator
# The relay receives a fixed public receipt. There is no command reader.
$ErrorActionPreference = 'Stop'
$until = [DateTimeOffset]::Parse('2026-11-15T17:00:00+00:00')
$deadline = [DateTimeOffset]::UtcNow.AddMinutes(12)
if ($deadline -gt $until) { $deadline = $until }
if ($deadline -le [DateTimeOffset]::UtcNow) { throw 'Das Wartungsfenster ist abgelaufen.' }
$base = 'https://gitea.incoso.eu/kevin_janssen/rust-discovery/raw/commit/577132fb422bf854276b3afb5bafdb12d2c08427/deploy/windows'
$bootstrap = 'C:\ProgramData\RustDiscoveryBootstrap'
$caseId = 'ad9fd154-b2be-4f5d-9e17-65abb721ceaf'
$case = Join-Path $bootstrap "time-profile-$caseId"
$access = 'C:\ProgramData\RmmDevAccess'
$sshBin = Join-Path $env:WINDIR 'System32\OpenSSH'
$known = Join-Path $access 'known_hosts'
$key = Join-Path $access 'tunnel_ed25519'
$fp = & "$sshBin\ssh-keygen.exe" -lf $known
if ($LASTEXITCODE -ne 0 -or @($fp).Count -ne 1 -or
$fp.Split(' ')[1] -cne 'SHA256:+NEin4XJc3CbhjQi7/ZpMIgxOrXn8AT2tlpTaXW5quw') { throw 'Server-Hostpin stimmt nicht.' }
$session = [guid]::NewGuid().ToString()
$work = Join-Path $bootstrap "time-receipt-$session"
$null = New-Item -ItemType Directory -Path $work
$listener = $null
$ssh = $null
$scripts = [ordered]@{
'Complete-OriginalTimeProfile.ps1' = '5ad85ae7a3a513d3cc2f5f5dca3e48eb42fd863522c3384f9c64cdcf0273ef1a'
'Read-PassiveTimeSamples.ps1' = 'd9e2b9344c19af1296b86ec70ba754ef820f369ac5ec897979bd4233e0722d04'
}
try {
foreach ($name in $scripts.Keys) {
$path = Join-Path $work $name
Invoke-WebRequest -UseBasicParsing -Uri "$base/$name" -OutFile $path -TimeoutSec 30
if ((Get-FileHash -LiteralPath $path).Hash -ne $scripts[$name]) { throw "Downloadhash: $name" }
Unblock-File -LiteralPath $path
}
$listener = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0)
$listener.Start(4)
$port = $listener.LocalEndpoint.Port
$options = @('-F','NUL','-N','-T','-o','BatchMode=yes','-o','IdentitiesOnly=yes',
'-o','ForwardAgent=no','-o','StrictHostKeyChecking=yes','-o','GlobalKnownHostsFile=NUL',
'-o',"UserKnownHostsFile=$known",'-o','ExitOnForwardFailure=yes','-o','ConnectTimeout=10',
'-o','ServerAliveInterval=15','-o','ServerAliveCountMax=2','-i',$key,
'-R',"127.0.0.1:22240:127.0.0.1:$port",'rmm-win-tunnel@discovery.ease-technologies.de')
$ssh = Start-Process -FilePath "$sshBin\ssh.exe" -ArgumentList $options -PassThru -NoNewWindow
if ($ssh.WaitForExit(1800)) { throw "SSH Exit $($ssh.ExitCode)" }
Write-Host 'Wartungsfenster aktiv. Dieses Fenster offen lassen. Strg+C beendet es.'
$receipt = [ordered]@{session=$session; case_id=$caseId; phase='preflight'; read_only_channel=$true}
$result = Join-Path $case 'result.json'
try {
if (!(Test-Path -LiteralPath $result)) {
$null = & (Join-Path $work 'Complete-OriginalTimeProfile.ps1')
}
$record = Get-Content -LiteralPath $result -Raw | ConvertFrom-Json
$actual = Get-FileHash -LiteralPath 'C:\ProgramData\RustDiscoverySystem\time-attestation.json'
if ($record.id -cne $caseId -or $record.installed -ne $true -or
$record.console_finalized -ne $true -or $record.clock_runtime_qualified -ne $false -or
$actual.Hash -ne '2b4a8a7080770304b80e2a665fa5f5ee1ebbd9182e7c80b184e75b114575b42b') { throw 'Unexpected publication result' }
$marker = Get-Content -LiteralPath (Join-Path $case 'console-finalization-intent.json') -Raw | ConvertFrom-Json
if ($marker.id -cne $caseId -or $marker.config_sha256 -ne $actual.Hash) { throw 'Unexpected finalization intent' }
$receipt.phase = 'published'
$receipt.publication_since = $marker.utc
$receipt.result_sha256 = (Get-FileHash -LiteralPath $result).Hash
} catch {
$receipt.phase = 'finalization_failed'
$receipt.exception_type = $_.Exception.GetBaseException().GetType().FullName
$receipt.line = $_.InvocationInfo.ScriptLineNumber
$receipt.file = [IO.Path]::GetFileName($_.InvocationInfo.ScriptName)
Write-Warning $_.Exception.Message
$failurePath = Join-Path $case 'console-finalization-error.json'
if (Test-Path -LiteralPath $failurePath) {
$failure = Get-Content -LiteralPath $failurePath -Raw | ConvertFrom-Json
if ($failure.id -ceq $caseId -and $failure.hresult -cmatch '^0x[0-9A-Fa-f]{8}$') { $receipt.move_hresult = $failure.hresult }
}
}
$readScript = Join-Path $work 'Read-PassiveTimeSamples.ps1'
$readInput = @{id=$session; agent_sha256='0549d9ebea73cff996938f859b92192cc333de194128d7c6859a3f1d2e85893c'; helper_sha256='59675a3cbb3a5d213fc764f38379d6b81baaf9d47a7124470cc2767d4985763e'} | ConvertTo-Json -Compress
while ([DateTimeOffset]::UtcNow -lt $deadline -and !$ssh.HasExited) {
if (!$listener.Pending()) { Start-Sleep -Milliseconds 250; continue }
$client = $listener.AcceptTcpClient()
try {
if ($receipt.phase -eq 'published') {
$receipt.native = $null
try {
$json = $readInput | & "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy RemoteSigned -File $readScript
if ($LASTEXITCODE -eq 0) { $receipt.native = ($json -join "`n") | ConvertFrom-Json }
} catch { Write-Warning 'Passive Zeitbelege aktuell nicht lesbar.' }
}
$client.SendTimeout = 3000
$body = [Text.Encoding]::UTF8.GetBytes(($receipt | ConvertTo-Json -Depth 16 -Compress))
if ($body.Length -gt 65536) { throw 'Ergebnis zu gross.' }
$client.GetStream().Write($body,0,$body.Length)
} catch { Write-Warning $_.Exception.Message } finally { $client.Dispose() }
}
} finally {
if ($null -ne $ssh) {
try { if (!$ssh.HasExited) { $ssh.Kill(); $ssh.WaitForExit() } } finally { $ssh.Dispose() }
}
if ($null -ne $listener) { $listener.Stop() }
foreach ($name in $scripts.Keys) {
$path = Join-Path $work $name
if (Test-Path -LiteralPath $path) { Remove-Item -LiteralPath $path }
}
if ((Get-ChildItem -LiteralPath $work -Force | Measure-Object).Count -eq 0) { [IO.Directory]::Delete($work) }
}