Agent install: drop auto-elevation, require elevated PowerShell #17

Merged
gsadmin merged 1 commits from dev into main 2026-08-12 19:00:02 +00:00
3 changed files with 32 additions and 42 deletions
+7 -32
View File
@@ -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}}
""";
/// <summary>
@@ -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}}
""";
}
@@ -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).'
);
</script>
@@ -206,6 +206,11 @@
<div class="command-wrap" class:updating={busy}>
<CopyChip value={command} label="Install command" wrap />
</div>
{#if platform === 'windows' && !busy}
<p class="elevate-note">
Ensure that this command is executed from an elevated PowerShell terminal.
</p>
{/if}
<p class="muted small">
{#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;
}
</style>
+9 -9
View File
@@ -127,20 +127,20 @@ public class InstallScriptTests(BackendTestFixture fixture) : IClassFixture<Back
[InlineData(null)]
[InlineData("msi")]
[InlineData("zip")]
public async Task WindowsScriptsSelfElevate(string? type)
public async Task WindowsScriptsRunInPlaceWithoutAutoElevation(string? type)
{
// Not admin -> 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);