Files
rust-discovery/deploy/windows/Install-PassiveClient.ps1
T
2026-11-13 23:55:25 +01:00

84 lines
5.3 KiB
PowerShell

# Development bootstrap only. No RMM jobs, privileged helper or execution bypass.
# Run through the pinned SSH transport as the bootstrap administrator.
$ErrorActionPreference = 'Stop'
$p = $global:RmmBootstrapPayload
if ($null -eq $p) { throw 'Bootstrap payload required through the pinned launcher' }
if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.token -notmatch '^[0-9a-f]{64}$') { throw 'Invalid bootstrap metadata' }
$serviceName = 'RmmDiscoveryWindows'
$application = 'C:\Program Files\RustDiscovery'
$root = 'C:\ProgramData\RustDiscovery'
$state = Join-Path $root 'state'
$source = 'C:\ProgramData\RustDiscoveryBootstrap\' + $p.sha256 + '\rmm-agent-windows.exe'
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { throw 'Service already exists: preserve identity and inspect instead of reinstalling' }
if ((Test-Path -LiteralPath $application) -or (Test-Path -LiteralPath $root)) { throw 'Existing client state must be preserved' }
if ((Get-FileHash -LiteralPath $source -Algorithm SHA256).Hash.ToLowerInvariant() -cne $p.sha256) { throw 'Artifact hash mismatch' }
$ca = [Convert]::FromBase64String($p.ca)
if ($ca.Length -lt 100 -or $ca.Length -gt 32768) { throw 'CA size' }
$administrators = New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544')
$system = New-Object Security.Principal.SecurityIdentifier('S-1-5-18')
function Protect-Directory([string]$Path, $ServiceSid, [string]$Rights) {
$acl = New-Object Security.AccessControl.DirectorySecurity
$acl.SetAccessRuleProtection($true, $false)
$acl.SetOwner($administrators)
foreach ($sid in @($administrators, $system)) {
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($sid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')))
}
if ($null -ne $ServiceSid) {
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($ServiceSid, $Rights, 'ContainerInherit,ObjectInherit', 'None', 'Allow')))
}
Set-Acl -LiteralPath $Path -AclObject $acl
}
function Protect-ReadFile([string]$Path, $ServiceSid) {
$acl = New-Object Security.AccessControl.FileSecurity
$acl.SetAccessRuleProtection($true, $false)
$acl.SetOwner($administrators)
foreach ($sid in @($administrators, $system)) {
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($sid, 'FullControl', 'Allow')))
}
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($ServiceSid, 'ReadAndExecute', 'Allow')))
Set-Acl -LiteralPath $Path -AclObject $acl
}
New-Item -ItemType Directory -Path $application | Out-Null
New-Item -ItemType Directory -Path $root | Out-Null
Protect-Directory $application $null 'ReadAndExecute'
Protect-Directory $root $null 'ReadAndExecute'
Copy-Item -LiteralPath $source -Destination (Join-Path $application 'rmm-agent-windows.exe')
$binary = '"C:\Program Files\RustDiscovery\rmm-agent-windows.exe" --service --config "C:\ProgramData\RustDiscovery\config.json"'
$sc = Join-Path $env:WINDIR 'System32\sc.exe'
# New-Service passes the quoted image path directly to SCM, avoiding native
# PowerShell 5.1 command-line quoting of a path containing spaces. Keep it manual
# until the account has been narrowed; it is never started as LocalSystem.
New-Service -Name $serviceName -BinaryPathName $binary -StartupType Manual | Out-Null
& $sc config $serviceName start= auto obj= 'NT AUTHORITY\LocalService' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'SCM service identity configuration failed' }
& $sc sidtype $serviceName unrestricted | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'SCM service SID configuration failed' }
$serviceSid = (New-Object Security.Principal.NTAccount('NT SERVICE', $serviceName)).Translate([Security.Principal.SecurityIdentifier])
Protect-Directory $application $serviceSid 'ReadAndExecute'
Protect-Directory $root $serviceSid 'ReadAndExecute'
New-Item -ItemType Directory -Path $state | Out-Null
Protect-Directory $state $serviceSid 'Modify'
$caPath = Join-Path $root 'server-ca.pem'
[IO.File]::WriteAllBytes($caPath, $ca)
Protect-ReadFile $caPath $serviceSid
# Only this one-use token is writable by the service so it can remove the copy.
$tokenPath = Join-Path $state 'enrollment.token'
[IO.File]::WriteAllText($tokenPath, $p.token, (New-Object Text.UTF8Encoding($false)))
$address = @([Net.Dns]::GetHostAddresses('discovery.ease-technologies.de') | Where-Object {$_.AddressFamily -eq [Net.Sockets.AddressFamily]::InterNetwork})[0]
if ($null -eq $address) { throw 'Server DNS has no IPv4 address' }
$config = [ordered]@{
server_name = 'discovery.ease-technologies.de'
connect_address = $address.ToString() + ':443'
ca_file = $caPath
state_directory = $state
enrollment_token_file = $tokenPath
transport = 'auto'
}
$configPath = Join-Path $root 'config.json'
[IO.File]::WriteAllText($configPath, ($config | ConvertTo-Json -Compress), (New-Object Text.UTF8Encoding($false)))
Protect-ReadFile $configPath $serviceSid
& $sc description $serviceName 'Rust Discovery development passive agent; no job executor' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Service description failed' }
Start-Service -Name $serviceName
[pscustomobject]@{installed=$true;service_name=$serviceName;service_identity='LocalService';service_sid=$serviceSid.Value;artifact_sha256=$p.sha256;no_job_executor=$true;no_reboot=$true} | ConvertTo-Json -Compress