81 lines
5.5 KiB
PowerShell
81 lines
5.5 KiB
PowerShell
# Bootstrap only: stages one separate certificate fixture; never executes it.
|
|
# Input is one protected stdin JSON line. Never put the enrollment token in arguments.
|
|
$ErrorActionPreference = 'Stop'
|
|
$p = [Console]::In.ReadLine() | ConvertFrom-Json
|
|
if ((($p.PSObject.Properties.Name | Sort-Object) -join ',') -cne 'ca_base64,ca_sha256,case_id,enrollment_token') { throw 'Fixed certificate fixture input' }
|
|
if ([string]([guid]$p.case_id) -cne $p.case_id -or [guid]$p.case_id -eq [guid]::Empty -or $p.ca_sha256 -cnotmatch '^[0-9a-f]{64}$' -or $p.enrollment_token -cnotmatch '^[0-9a-f]{64}$') { throw 'Fixture input format' }
|
|
$sys = 'S-1-5-18'; $admin = 'S-1-5-32-544'
|
|
$actor = (New-Object Security.Principal.NTAccount('NT SERVICE','RmmDiscoveryWindows')).Translate([Security.Principal.SecurityIdentifier]).Value
|
|
$sdkHash = 'c6b581b60ce803318ff946cd3cc8306aa8589d6bff6b022c0bf1d48eb8f5a8e1'
|
|
$uploaded = 'C:\ProgramData\RustDiscoveryBootstrap\' + $sdkHash + '\WindowsCertificateAcceptanceSDK.exe'
|
|
$artifactRoot = 'C:\Program Files\RustDiscoveryCertificateAcceptance'
|
|
$version = Join-Path $artifactRoot $sdkHash
|
|
$sdk = Join-Path $version 'WindowsCertificateAcceptanceSDK.exe'
|
|
$parent = 'C:\ProgramData\RustDiscovery\certificate-acceptance'
|
|
$case = Join-Path $parent $p.case_id
|
|
function Hash-Bytes([byte[]]$bytes) {
|
|
$h = [Security.Cryptography.SHA256]::Create()
|
|
try { return ([BitConverter]::ToString($h.ComputeHash($bytes))).Replace('-','').ToLowerInvariant() } finally { $h.Dispose() }
|
|
}
|
|
function Check-Path([string]$path,[bool]$directory,[bool]$actorWrites) {
|
|
$item = Get-Item -LiteralPath $path -Force
|
|
if ($item.PSIsContainer -ne $directory -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'Protected regular fixture path' }
|
|
if (!$directory -and $item.LinkType) { throw 'Fixture hardlink forbidden' }
|
|
$acl = Get-Acl -LiteralPath $path
|
|
if ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sys,$admin) -or !$acl.AreAccessRulesProtected) { throw 'Protected fixture owner and DACL' }
|
|
$seen = @{}
|
|
foreach ($rule in $acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])) {
|
|
$sid = $rule.IdentityReference.Value; $rights = [int64]$rule.FileSystemRights
|
|
if ($rule.AccessControlType -ne 'Allow' -or $sid -notin @($sys,$admin,$actor)) { throw 'Fixture principal allowlist' }
|
|
$expected = 1179817
|
|
if ($sid -in @($sys,$admin) -or $actorWrites) { $expected = 2032127 }
|
|
if ($rights -ne $expected) { throw 'Exact fixture rights' }
|
|
$seen[$sid] = $true
|
|
}
|
|
foreach ($sid in @($sys,$admin,$actor)) { if (!$seen[$sid]) { throw 'Missing fixture principal' } }
|
|
}
|
|
function Protect-Path([string]$path,[bool]$directory,[bool]$actorWrites) {
|
|
if ($directory) { $acl = New-Object Security.AccessControl.DirectorySecurity } else { $acl = New-Object Security.AccessControl.FileSecurity }
|
|
$acl.SetAccessRuleProtection($true,$false)
|
|
$acl.SetOwner((New-Object Security.Principal.SecurityIdentifier($admin)))
|
|
$inherit = 'None'; if ($directory) { $inherit = 'ContainerInherit,ObjectInherit' }
|
|
foreach ($sid in @($sys,$admin,$actor)) {
|
|
$rights = 'ReadAndExecute'; if ($sid -in @($sys,$admin) -or $actorWrites) { $rights = 'FullControl' }
|
|
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)),$rights,$inherit,'None','Allow')))
|
|
}
|
|
Set-Acl -LiteralPath $path -AclObject $acl
|
|
Check-Path $path $directory $actorWrites
|
|
}
|
|
function Ensure-Directory([string]$path,[bool]$actorWrites) {
|
|
if (!(Test-Path -LiteralPath $path)) {
|
|
New-Item -ItemType Directory -Path $path | Out-Null
|
|
Protect-Path $path $true $actorWrites
|
|
} else { Check-Path $path $true $actorWrites }
|
|
}
|
|
function Write-NewPrivate([string]$path,[byte[]]$bytes) {
|
|
# Inherited case DACL is checked before any secret byte is written.
|
|
$f = [IO.File]::Open($path,'CreateNew','Write','None')
|
|
try {
|
|
Protect-Path $path $false $true
|
|
$f.Write($bytes,0,$bytes.Length); $f.Flush($true)
|
|
} finally { $f.Dispose() }
|
|
}
|
|
$ca = [Convert]::FromBase64String($p.ca_base64)
|
|
if ($ca.Length -gt 32768 -or (Hash-Bytes $ca) -cne $p.ca_sha256 -or [Text.Encoding]::ASCII.GetString($ca) -notmatch '^-----BEGIN CERTIFICATE-----') { throw 'Exact public fixture CA' }
|
|
if (Test-Path -LiteralPath $case) { throw 'Fixture exists; readback only, never repeat preparation' }
|
|
$source = Get-Item -LiteralPath $uploaded -Force
|
|
if ($source.PSIsContainer -or ($source.Attributes -band [IO.FileAttributes]::ReparsePoint) -or (Get-FileHash -LiteralPath $uploaded).Hash.ToLowerInvariant() -cne $sdkHash) { throw 'Exact staged SDK required' }
|
|
Ensure-Directory $artifactRoot $false
|
|
Ensure-Directory $version $false
|
|
if (!(Test-Path -LiteralPath $sdk)) {
|
|
[IO.File]::Copy($uploaded,$sdk,$false)
|
|
Protect-Path $sdk $false $false
|
|
} else { Check-Path $sdk $false $false }
|
|
if ((Get-FileHash -LiteralPath $sdk).Hash.ToLowerInvariant() -cne $sdkHash) { throw 'Protected SDK hash' }
|
|
Ensure-Directory $parent $true
|
|
New-Item -ItemType Directory -Path $case | Out-Null
|
|
Protect-Path $case $true $true
|
|
Write-NewPrivate (Join-Path $case 'ca.pem') $ca
|
|
Write-NewPrivate (Join-Path $case 'enrollment-token') ([Text.Encoding]::ASCII.GetBytes($p.enrollment_token))
|
|
[pscustomobject]@{schema_version=1;prepared=$true;executed=$false;separate_fixture=$true;case_id=$p.case_id;root=$case;sdk=$sdk;sdk_sha256=$sdkHash;ca_sha256=$p.ca_sha256;service_sid=$actor;permanent_identity_read=$false;services_changed=$false;jobs_created=$false} | ConvertTo-Json -Compress
|