| 1 | #requires -Version 5.1 |
| 2 | param([Parameter(Mandatory)][string]$ProfilePath,[Parameter(Mandatory)][string]$EvidenceDirectory) |
| 3 | $ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue' |
| 4 | $profile=Get-Content -LiteralPath $ProfilePath -Raw|ConvertFrom-Json;$utf8=[Text.UTF8Encoding]::new($false) |
| 5 | $owned=[Collections.Generic.List[Diagnostics.Process]]::new() |
| 6 | function Start-Transfer([string]$Operation,[bool]$Helper=$false){ |
| 7 | $s=[Diagnostics.ProcessStartInfo]::new();$s.FileName=if($Helper){Join-Path ([IO.Path]::GetDirectoryName($profile.relay_executable)) 'cage-guest-files.exe'}else{$profile.relay_executable} |
| 8 | $s.Arguments=if($Helper){$Operation}else{'--relay-profile "'+$ProfilePath+'" '+$Operation};if($Operation -eq 'export'){$s.Arguments+=' --paths-stdin'} |
| 9 | $s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardInput=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$s.StandardOutputEncoding=$utf8 |
| 10 | $p=[Diagnostics.Process]::Start($s);$owned.Add($p);$null=$p.StandardError.ReadToEndAsync();$p |
| 11 | } |
| 12 | function Paths($P,$Selection){$bytes=$utf8.GetBytes((ConvertTo-Json -InputObject @($Selection) -Compress)+"`n");$P.StandardInput.BaseStream.Write($bytes,0,$bytes.Length);$P.StandardInput.Close()} |
| 13 | function Result($P){$read=$P.StandardOutput.ReadLineAsync();if(!$read.Wait(120000) -or !$read.Result -or !$P.WaitForExit(120000) -or $P.ExitCode -ne 0){throw 'The bounded archive stream did not complete.'};$read.Result|ConvertFrom-Json} |
| 14 | function Hash([string]$File){(Get-FileHash -LiteralPath $File -Algorithm SHA256).Hash} |
| 15 | try { |
| 16 | $directory=Join-Path $EvidenceDirectory ([guid]::NewGuid().ToString('N'));$null=[IO.Directory]::CreateDirectory($directory) |
| 17 | $source=Join-Path $directory 'stream-64MiB.bin';$file=[IO.File]::Open($source,'CreateNew','Write','None');$buffer=New-Object byte[] (64KB);for($i=0;$i -lt $buffer.Length;$i++){$buffer[$i]=[byte]($i%251)} |
| 18 | try{for($i=0;$i -lt 1024;$i++){$file.Write($buffer,0,$buffer.Length)}}finally{$file.Dispose()} |
| 19 | $write=Start-Transfer 'export' $true;Paths $write @($source);$import=Start-Transfer 'import';$write.StandardOutput.BaseStream.CopyTo($import.StandardInput.BaseStream);$import.StandardInput.Close();$guest=Result $import |
| 20 | if(!$write.WaitForExit(120000) -or $write.ExitCode -ne 0 -or @($guest.paths).Count -ne 1){throw 'The host archive source failed.'} |
| 21 | $export=Start-Transfer 'export';Paths $export @($guest.paths);$read=Start-Transfer 'import' $true;$export.StandardOutput.BaseStream.CopyTo($read.StandardInput.BaseStream);$read.StandardInput.Close();$returnedHost=Result $read |
| 22 | if(!$export.WaitForExit(120000) -or $export.ExitCode -ne 0 -or @($returnedHost.paths).Count -ne 1 -or (Get-Item -LiteralPath $returnedHost.paths[0]).Length -ne 64MB -or (Hash $source) -ne (Hash $returnedHost.paths[0])){throw 'The 64 MiB stream hash or completion status differs.'} |
| 23 | $result=[ordered]@{bytes=64MB;host_to_guest_complete=$true;guest_to_host_complete=$true;sha256_equal=$true;clipboard_unchanged=$true} |
| 24 | [IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'large-file-result.json'),($result|ConvertTo-Json),$utf8);$result|ConvertTo-Json -Compress |
| 25 | }finally{foreach($p in $owned){try{$p.StandardInput.Close();if(!$p.WaitForExit(1000)){$p.Kill()};$p.Dispose()}catch{}}} |