| 1 | #requires -Version 5.1 |
| 2 | [CmdletBinding()] |
| 3 | param([Parameter(Mandatory)][string]$ManagerExecutable) |
| 4 | $ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue' |
| 5 | $identity=[Security.Principal.WindowsIdentity]::GetCurrent() |
| 6 | if(([Security.Principal.WindowsPrincipal]::new($identity)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){throw 'Run this same-user protocol/DPAPI smoke as the ordinary host user.'} |
| 7 | . (Join-Path $PSScriptRoot 'windows-host-enrollment.ps1') |
| 8 | $root=Join-Path $env:TEMP ('cage-host-enrollment-'+[guid]::NewGuid().ToString('N')) |
| 9 | $profile=Join-Path $root 'profile';$state=Join-Path $profile 'state';$id=[guid]::NewGuid();$vm=[guid]::NewGuid() |
| 10 | $instance=Join-Path $state ('windows-'+$id.ToString('N'));$pair=Join-Path $instance 'cage-workbench/pair' |
| 11 | $null=New-Item -ItemType Directory -Path $pair -Force |
| 12 | $choice=@{version=1;system='windows-11';media=(Join-Path $root 'fixture.iso');sha256=('0'*64);editor_terms=$true;id=$id.ToString('D')} |
| 13 | [IO.File]::WriteAllText((Join-Path $profile 'guest-install.json'),($choice|ConvertTo-Json)) |
| 14 | [IO.File]::WriteAllText((Join-Path $profile 'windows-install-vm.txt'),$vm.ToString('D')) |
| 15 | $pipe='Cage.Enroll.'+[guid]::NewGuid().ToString('N');$worker=$null;$sender=$null;$context=$null |
| 16 | try { |
| 17 | $workerArguments=@('user-enrollment','--vm-id',$vm.ToString('D'),'--host-sid',$identity.User.Value,'--profile',('"'+$profile+'"'),'--state',('"'+$state+'"'),'--installation-id',$id.ToString('D'),'--pipe',$pipe,'--parent-pid',[string]$PID) |
| 18 | $worker=Start-Process -FilePath $ManagerExecutable -ArgumentList $workerArguments -NoNewWindow -PassThru |
| 19 | $context=@{Pipe=$pipe;VMID=$vm.ToString('D');HostSID=$identity.User.Value;WorkerPID=[uint32]$worker.Id;WorkerStarted=[Cage.HostEnrollment.Native]::Started([uint32]$worker.Id);WorkerExe=[IO.Path]::GetFullPath($ManagerExecutable);OwnerPID=[uint32]$PID;OwnerStarted=[Cage.HostEnrollment.Native]::Started([uint32]$PID)} |
| 20 | Wait-CageHostEnrollment $context $worker |
| 21 | $ctxPath=Join-Path $root 'context.json';[IO.File]::WriteAllText($ctxPath,($context|ConvertTo-Json)) |
| 22 | $module=Join-Path $PSScriptRoot 'windows-host-enrollment.ps1' |
| 23 | $senderPath=Join-Path $root 'sender.ps1';$result=Join-Path $root 'result.json' |
| 24 | $script=@' |
| 25 | param($ContextPath,$Module,$Pair,$Result) |
| 26 | $ErrorActionPreference='Stop' |
| 27 | . $Module |
| 28 | $ctx=Get-Content -LiteralPath $ContextPath -Raw|ConvertFrom-Json |
| 29 | Start-Sleep -Milliseconds 500 |
| 30 | $sid='S-1-5-21-111-222-333-1001';$key=[byte[]](1..32) |
| 31 | try { |
| 32 | Send-CageHostEnrollmentKey $ctx 'gui' $sid $key |
| 33 | $path=Join-Path $Pair 'pairing.dpapi';$before=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash |
| 34 | Send-CageHostEnrollmentKey $ctx 'gui' $sid $key |
| 35 | $same=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ceq $before |
| 36 | $different=$false;try{Send-CageHostEnrollmentKey $ctx 'gui' $sid ([byte[]](2..33))}catch{$different=$true} |
| 37 | $wrongSID=$false;try{Send-CageHostEnrollmentKey $ctx 'packet' 'S-1-5-21-111-222-333-1002' $key}catch{$wrongSID=$true} |
| 38 | [IO.File]::WriteAllText($Result,(@{sameCipher=$same;changedKeyRejected=$different;guestChangedRejected=$wrongSID;cipherPreserved=((Get-FileHash -LiteralPath $path).Hash -ceq $before)}|ConvertTo-Json)) |
| 39 | }finally{[Array]::Clear($key,0,$key.Length)} |
| 40 | '@ |
| 41 | [IO.File]::WriteAllText($senderPath,$script) |
| 42 | $system=Join-Path ([Environment]::GetFolderPath('System')) 'WindowsPowerShell/v1.0/powershell.exe' |
| 43 | $sender=Start-Process -FilePath $system -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-ExecutionPolicy','Bypass','-File',('"'+$senderPath+'"'),'-ContextPath',('"'+$ctxPath+'"'),'-Module',('"'+$module+'"'),'-Pair',('"'+$pair+'"'),'-Result',('"'+$result+'"')) -NoNewWindow -PassThru |
| 44 | # This fixture deliberately uses a same-user PowerShell child. It proves |
| 45 | # kernel handle/PID binding and CurrentUser DPAPI, not alternate-admin UAC. |
| 46 | Set-CageHostEnrollmentAdmin $context $sender |
| 47 | if(!$sender.WaitForExit(30000) -or $sender.ExitCode -ne 0){throw 'The native fixture sender did not complete.'} |
| 48 | $passed=Get-Content -LiteralPath $result -Raw|ConvertFrom-Json |
| 49 | if(!$passed.sameCipher -or !$passed.changedKeyRejected -or !$passed.guestChangedRejected -or !$passed.cipherPreserved){throw 'Native enrollment mutation guards failed.'} |
| 50 | Add-Type -AssemblyName System.Security |
| 51 | $entropy=[Security.Cryptography.SHA256]::Create().ComputeHash([Text.Encoding]::UTF8.GetBytes('Cage.HyperV.v1:'+$vm.ToString('D')+':S-1-5-21-111-222-333-1001')) |
| 52 | $raw=[Security.Cryptography.ProtectedData]::Unprotect([IO.File]::ReadAllBytes((Join-Path $pair 'pairing.dpapi')),$entropy,'CurrentUser') |
| 53 | try{if(($raw -join ',') -cne (([byte[]](1..32)) -join ',')){throw 'Original user DPAPI interoperability failed.'}}finally{[Array]::Clear($raw,0,$raw.Length)} |
| 54 | if(Test-Path -LiteralPath (Join-Path $profile 'windows-guest.json')){throw 'A fixture without actual VM/GUI proof published a final profile.'} |
| 55 | Write-Output 'Native same-user enrollment/DPAPI protocol smoke passed; alternate-admin UAC and actual VM enrollment remain separate.' |
| 56 | }finally { |
| 57 | if($context){try{Stop-CageHostEnrollment $context}catch{}} |
| 58 | if($sender){if(!$sender.HasExited){$sender.Kill();$sender.WaitForExit()};$sender.Dispose()} |
| 59 | if($worker){if(!$worker.WaitForExit(5000)){$worker.Kill();$worker.WaitForExit()};$worker.Dispose()} |
| 60 | # Keep this script's own result directory for evidence. No VM, account, |
| 61 | # service, guest data or user configuration was created or replaced. |
| 62 | } |