From 8f9adcf3bba50a469040cd62ba4ffd32bf975f2729cafae39bf399e08610a719 Mon Sep 17 00:00:00 2001 From: gsadmin Date: Wed, 12 Aug 2026 14:18:57 -0400 Subject: [PATCH] Agent install: drop auto-elevation, require an elevated PowerShell [skip ci] The Windows install one-liner self-elevated (IsInRole -> write a temp .ps1 -> Start-Process -Verb RunAs), which Defender flagged as Win32/ClickFix.SZZ - an "executes commands / auto-elevates" heuristic. The scripts now run in place: no UAC relaunch, no temp scriptblock, no self-delete. The operator runs the command from an elevated PowerShell, and the Add device modal states that prerequisite on one concise line (Windows only; the unix installers already use sudo). Backend + zip installers and the elevation test updated to match. Co-Authored-By: Claude Fable 5 --- src/Backend/Enrollment/InstallEndpoints.cs | 39 ++++--------------- .../src/lib/components/AddDeviceModal.svelte | 17 +++++++- tests/Backend.Tests/InstallScriptTests.cs | 18 ++++----- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/Backend/Enrollment/InstallEndpoints.cs b/src/Backend/Enrollment/InstallEndpoints.cs index b90a93f..93fb164 100644 --- a/src/Backend/Enrollment/InstallEndpoints.cs +++ b/src/Backend/Enrollment/InstallEndpoints.cs @@ -68,8 +68,10 @@ public static class InstallEndpoints var skipDownloadVerify = tls.IsSelfSignedDirect(context); // Token is embedded so the operator can paste one command; knowledge of a - // valid token is itself the enrollment gate. The Windows scripts - // self-elevate (see PsElevateOrRun) without re-fetching anything. + // valid token is itself the enrollment gate. The Windows scripts run in + // place — the operator launches them from an elevated PowerShell (the Add + // device modal says so). There is no auto-elevation: the self-relaunch + // tripped AV/EDR ClickFix heuristics (Win32/ClickFix). var script = type?.ToLowerInvariant() switch { "linux" => BuildUnixScript(baseUrl, token!, "linux", skipDownloadVerify), @@ -132,31 +134,11 @@ public static class InstallEndpoints // the agent is told to install the server CA into the OS trust store (and // keep it current) — system validation then trusts the server, without // pinning, so a proxy with a real cert added later still works. - // Auto-elevation trailer: the install work sits in an $install scriptblock - // above this. If already admin (a build pipeline), run it in place. If not, - // write that same scriptblock to a temp .ps1 and re-launch it elevated - // (UAC) with -NoExit so the result stays visible. No re-fetch, no - // -EncodedCommand (which trips AV/EDR heuristics). The elevated copy - // deletes itself via $PSCommandPath. - private const string PsElevateOrRun = """ - if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { - & $install - } else { - Write-Host 'ExecuteR Agent install needs administrator rights - requesting elevation...' - $self = Join-Path $env:TEMP ('ExecuteRInstall-' + [guid]::NewGuid().ToString('N') + '.ps1') - Set-Content -LiteralPath $self -Value $install.ToString() -Encoding UTF8 - try { - Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-NoExit','-File',$self) - } catch { - Remove-Item -LiteralPath $self -Force -ErrorAction SilentlyContinue - Write-Warning 'Elevation was cancelled; the agent was not installed.' - } - } - """; - + // The install runs in place: no auto-elevation (the self-relaunch tripped + // AV/EDR ClickFix heuristics), no -EncodedCommand. The operator runs it from + // an elevated PowerShell — the Add device modal states this prerequisite. private static string BuildMsiScript(string baseUrl, string token, string packageUrl, bool skipDownloadVerify) => $$""" $ErrorActionPreference = 'Stop' - $install = { Write-Host 'Installing ExecuteR Agent...' {{PsDownloadTls}} {{(skipDownloadVerify ? PsAcceptCert : "")}} @@ -175,9 +157,6 @@ public static class InstallEndpoints finally { Remove-Item $msi -ErrorAction SilentlyContinue } - if ($PSCommandPath) { Remove-Item -LiteralPath $PSCommandPath -Force -ErrorAction SilentlyContinue } - } - {{PsElevateOrRun}} """; /// @@ -219,7 +198,6 @@ public static class InstallEndpoints private static string BuildZipScript(string baseUrl, string token, string packageUrl, bool skipDownloadVerify) => $$""" $ErrorActionPreference = 'Stop' - $install = { Write-Host 'Installing ExecuteR Agent (portable)...' {{PsDownloadTls}} {{(skipDownloadVerify ? PsAcceptCert : "")}} @@ -244,8 +222,5 @@ public static class InstallEndpoints finally { Remove-Item $zip -ErrorAction SilentlyContinue } - if ($PSCommandPath) { Remove-Item -LiteralPath $PSCommandPath -Force -ErrorAction SilentlyContinue } - } - {{PsElevateOrRun}} """; } diff --git a/src/WebUI/src/lib/components/AddDeviceModal.svelte b/src/WebUI/src/lib/components/AddDeviceModal.svelte index f55a3c9..600d018 100644 --- a/src/WebUI/src/lib/components/AddDeviceModal.svelte +++ b/src/WebUI/src/lib/components/AddDeviceModal.svelte @@ -123,7 +123,7 @@ const shellHint = $derived( platform === 'windows' - ? 'Paste into a prompt or the Win+R Run box on the device.' + ? 'Paste into a PowerShell prompt on the device.' : 'Paste into a terminal on the device (needs sudo).' ); @@ -206,6 +206,11 @@
+ {#if platform === 'windows' && !busy} +

+ Ensure that this command is executed from an elevated PowerShell terminal. +

+ {/if}

{#if busy}Updating the token…{:else} {shellHint} The token {noExpiry ? 'never expires' : 'expires in 24 hours'}; the agent @@ -353,4 +358,14 @@ .warn { color: var(--warning); } + + /* Prerequisite callout: keep it on one line and let the modal's flex gap + give it breathing room above and below. */ + .elevate-note { + margin: 0; + font-size: 12.5px; + font-weight: 600; + color: var(--warning); + white-space: nowrap; + } diff --git a/tests/Backend.Tests/InstallScriptTests.cs b/tests/Backend.Tests/InstallScriptTests.cs index 9b843e7..c52dffe 100644 --- a/tests/Backend.Tests/InstallScriptTests.cs +++ b/tests/Backend.Tests/InstallScriptTests.cs @@ -127,20 +127,20 @@ public class InstallScriptTests(BackendTestFixture fixture) : IClassFixture re-launch elevated (UAC) running the script's own body - // from a temp .ps1 with -NoExit; already admin -> run in place. Plain - // -File, never -EncodedCommand (which trips AV/EDR), and no - // #Requires -RunAsAdministrator (which would block the auto-elevation). + // Auto-elevation was removed: the self-relaunch tripped AV/EDR ClickFix + // heuristics (Win32/ClickFix). The operator runs the command from an + // elevated PowerShell (the Add device modal states this). So: no UAC + // re-launch, no temp .ps1 scriptblock, no -EncodedCommand, and no + // #Requires -RunAsAdministrator. using var client = fixture.CreateClient(); var url = "/api/enrollment/install?token=test-token" + (type is null ? "" : $"&type={type}"); var script = await (await client.GetAsync(url)).Content.ReadAsStringAsync(); - Assert.Contains("WindowsBuiltInRole]::Administrator", script); - Assert.Contains("-Verb RunAs", script); - Assert.Contains("-NoExit", script); - Assert.Contains("$install.ToString()", script); + Assert.DoesNotContain("WindowsBuiltInRole]::Administrator", script); + Assert.DoesNotContain("-Verb RunAs", script); + Assert.DoesNotContain("$install", script); Assert.DoesNotContain("-EncodedCommand", script); Assert.DoesNotContain("#Requires -RunAsAdministrator", script); -- 2.52.0