72 lines
7.9 KiB
PowerShell
72 lines
7.9 KiB
PowerShell
#requires -Version 5.1
|
|
<# Standalone diagnostic bootstrap ONLY. Root runs explicitly after qualification.
|
|
Prepare stages one hash-pinned binary. Start creates one temporary own Manual
|
|
LocalService service, not an enrolled RMM client/job. Lost replies permit only
|
|
Readback. Cleanup removes ONLY that exact stopped probe service. No installed
|
|
product, permanent agent/helper, time service, privilege, boot counter or trust
|
|
configuration is modified. SCM registration is confined to this temporary probe.
|
|
Two independent fresh cases can show process independence; matching samples do
|
|
NOT qualify a reboot/clock-jump invariant or enable a signed-job boot capability.
|
|
#>
|
|
[CmdletBinding()]
|
|
param([Parameter(Mandatory=$true)][ValidateSet('Prepare','Start','Readback','Cleanup')][string]$Action,
|
|
[Parameter(Mandatory=$true)][ValidatePattern('^[a-f0-9]{64}$')][string]$BinarySha256,
|
|
[Parameter(Mandatory=$true)][ValidatePattern('^[a-f0-9]{64}$')][string]$ExpectedSourceSha256,
|
|
[Guid]$CaseId=[Guid]::Empty)
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference='Stop'
|
|
$root='C:\ProgramData\RustDiscoveryBootProbe';$name='RmmWindowsBootIdentityProbe'
|
|
$binary=Join-Path (Join-Path $root $BinarySha256) 'WindowsBootProbe.exe'
|
|
$sys='S-1-5-18';$admin='S-1-5-32-544';$local='S-1-5-19'
|
|
# MS-LSAT service SID is independent of whether this temporary service still
|
|
# exists. This keeps post-cleanup/lost-reply readback genuinely read-only.
|
|
$algorithm=[Security.Cryptography.SHA1]::Create()
|
|
try {$digest=$algorithm.ComputeHash([Text.Encoding]::Unicode.GetBytes($name.ToUpperInvariant()))} finally {$algorithm.Dispose()}
|
|
$serviceSid='S-1-5-80-'+((0..4|ForEach-Object {[BitConverter]::ToUInt32($digest,$_ * 4)})-join '-')
|
|
function Require([bool]$v,[string]$code){if(!$v){throw $code}}
|
|
function Hash([string]$p){return (Get-FileHash -LiteralPath $p -Algorithm SHA256).Hash.ToLowerInvariant()}
|
|
function No-Reparse([string]$p){$i=Get-Item -LiteralPath $p -Force;while($null -ne $i){Require (($i.Attributes-band[IO.FileAttributes]::ReparsePoint)-eq 0) 'BOOT_PROBE_REPARSE';if($i-is[IO.FileInfo]){$i=$i.Directory}else{$i=$i.Parent}}}
|
|
function Protect([string]$p,[string]$writer){$a=New-Object Security.AccessControl.DirectorySecurity;$a.SetAccessRuleProtection($true,$false);$a.SetOwner((New-Object Security.Principal.SecurityIdentifier($admin)));foreach($sid in @($sys,$admin,$local,$writer)|Where-Object {$_}|Select-Object -Unique){$rights=if($sid-in@($sys,$admin,$writer)){'FullControl'}else{'ReadAndExecute'};$a.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)),$rights,'ContainerInherit,ObjectInherit','None','Allow')))};Set-Acl -LiteralPath $p -AclObject $a}
|
|
function Check([string]$p,[string[]]$writers){No-Reparse $p;$a=Get-Acl -LiteralPath $p;Require ($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-in@($sys,$admin)-or(!(Get-Item -LiteralPath $p).PSIsContainer-and$writers.Count-gt 0-and$a.GetOwner([Security.Principal.SecurityIdentifier]).Value-eq$local)) 'BOOT_PROBE_OWNER';$r=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));Require ($r.Count-gt 0) 'BOOT_PROBE_DACL';foreach($ace in $r){$sid=$ace.IdentityReference.Value;Require ($ace.AccessControlType-eq'Allow') 'BOOT_PROBE_ACE';if($sid-notin@($sys,$admin)+$writers){Require ($sid-eq$local-and([long]$ace.FileSystemRights-band 0x500d0156)-eq 0) 'BOOT_PROBE_FOREIGN_WRITE'}}}
|
|
function Write-New([string]$p,$v){$f=[IO.File]::Open($p,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($v|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
|
|
Require ((Hash $PSCommandPath)-ceq$ExpectedSourceSha256) 'BOOT_PROBE_EXACT_SOURCE'
|
|
if($Action-eq'Prepare'){
|
|
if(!(Test-Path -LiteralPath $root)){[void](New-Item -ItemType Directory -Path $root);Protect $root ''};Check $root @()
|
|
$dir=Split-Path $binary -Parent;if(!(Test-Path -LiteralPath $dir)){[void](New-Item -ItemType Directory -Path $dir);Protect $dir ''};Check $dir @()
|
|
$present=Test-Path -LiteralPath $binary;if($present){Check $binary @();Require ((Hash $binary)-ceq$BinarySha256) 'BOOT_PROBE_STAGED_HASH'}
|
|
[ordered]@{prepared=$true;binary_present=$present;upload_path=$binary;no_service_change=$true}|ConvertTo-Json -Compress;exit 0
|
|
}
|
|
Require ($CaseId-ne[Guid]::Empty) 'BOOT_PROBE_CASE_REQUIRED'
|
|
Check $root @();Check $binary @();Require ((Hash $binary)-ceq$BinarySha256) 'BOOT_PROBE_BINARY_HASH'
|
|
$case=Join-Path $root $CaseId.ToString();$command='"'+$binary+'" --service '+$CaseId.ToString()
|
|
if($Action-eq'Start'){
|
|
Require (!(Test-Path -LiteralPath $case)-and$null-eq(Get-Service -Name $name -ErrorAction SilentlyContinue)) 'BOOT_PROBE_FRESH_CASE_AND_SERVICE'
|
|
[void](New-Item -ItemType Directory -Path $case);Protect $case ''
|
|
Write-New (Join-Path $case 'intent.json') @{case_id=$CaseId.ToString();binary_sha256=$BinarySha256;source_sha256=$ExpectedSourceSha256;command=$command;service=$name;read_only=$true}
|
|
Write-New (Join-Path $case 'create-attempt.json') @{create_once=$true}
|
|
New-Service -Name $name -BinaryPathName $command -StartupType Manual -Credential (New-Object Management.Automation.PSCredential('NT AUTHORITY\LocalService',(New-Object Security.SecureString)))|Out-Null
|
|
$sc=Join-Path ([Environment]::GetFolderPath([Environment+SpecialFolder]::System)) 'sc.exe'
|
|
& $sc sidtype $name unrestricted|Out-Null;Require ($LASTEXITCODE-eq 0) 'BOOT_PROBE_SERVICE_SID'
|
|
$sid=(New-Object Security.Principal.NTAccount('NT SERVICE',$name)).Translate([Security.Principal.SecurityIdentifier]).Value
|
|
Require ($sid-ceq$serviceSid) 'BOOT_PROBE_ACTUAL_SERVICE_SID'
|
|
Protect $case $sid
|
|
Write-New (Join-Path $case 'start-attempt.json') @{start_once=$true;no_jobs=$true}
|
|
Start-Service -Name $name
|
|
}
|
|
Check $case @($serviceSid)
|
|
$intent=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json
|
|
Require ($intent.case_id-ceq$CaseId.ToString()-and$intent.binary_sha256-ceq$BinarySha256-and$intent.source_sha256-ceq$ExpectedSourceSha256-and$intent.command-ceq$command-and$intent.service-ceq$name) 'BOOT_PROBE_RETAINED_INTENT'
|
|
$service=Get-CimInstance Win32_Service -Filter "Name='RmmWindowsBootIdentityProbe'" -OperationTimeoutSec 10
|
|
if($null-ne$service){Require ($service.PathName-ceq$command-and$service.StartName-in@('NT AUTHORITY\LocalService','NT AUTHORITY\Local Service')-and$service.StartMode-ceq'Manual') 'BOOT_PROBE_SCM_CONFIG'}
|
|
$limit=[Diagnostics.Stopwatch]::StartNew()
|
|
while($null-ne$service-and$service.State-ne'Stopped'-and$limit.Elapsed.TotalSeconds-lt 15){Start-Sleep -Milliseconds 250;$service=Get-CimInstance Win32_Service -Filter "Name='RmmWindowsBootIdentityProbe'" -OperationTimeoutSec 10}
|
|
Require ($null-eq$service-or$service.State-eq'Stopped') 'BOOT_PROBE_RETAIN_ACTIVE_READBACK_ONLY'
|
|
$result=$null;$path=Join-Path $case 'result.json'
|
|
if(Test-Path -LiteralPath $path){Check $path @($serviceSid);Require ((Get-Item -LiteralPath $path).Length-le 8192) 'BOOT_PROBE_REPORT_SIZE';$result=Get-Content -LiteralPath $path -Raw|ConvertFrom-Json;Require ($result.case_id-ceq$CaseId.ToString()-and$result.service_name-ceq$name-and$result.user_sid-ceq$local-and$result.administrator_member-eq$false-and$result.scm_callback_entered-eq$true-and$result.read_only-eq$true-and$result.job_binding_qualified-eq$false) 'BOOT_PROBE_ACTUAL_LOCAL_SERVICE'}
|
|
if($Action-eq'Cleanup'){
|
|
Require ($null-ne$service-and$service.State-eq'Stopped') 'BOOT_PROBE_ONLY_OWN_STOPPED_SERVICE'
|
|
Write-New (Join-Path $case 'cleanup-attempt.json') @{service=$name;command=$command;remove_once=$true}
|
|
$sc=Join-Path ([Environment]::GetFolderPath([Environment+SpecialFolder]::System)) 'sc.exe'; & $sc delete $name|Out-Null;Require ($LASTEXITCODE-eq 0) 'BOOT_PROBE_OWN_CLEANUP'
|
|
}
|
|
[ordered]@{case_id=$CaseId.ToString();native_report=$result;service_stopped=($null-eq$service-or$service.State-eq'Stopped');service_exit_code=if($null-ne$service){$service.ExitCode}else{$null};job_binding_qualified=$false;no_jobs=$true;no_clock_change=$true;no_permanent_agent_change=$true}|ConvertTo-Json -Depth 12 -Compress
|