Harden the agents: keep the token off the command line, lock down their files

Linux: the token went to curl as an argument, readable by any local user in the process
list, and the reply was written to a fixed /tmp file that another user could replace with
a symlink (the agent runs as root). The token now goes to curl on stdin, and the payload
and reply live in a private mktemp directory that is removed afterwards. This also lifts
the limit on a report's size that a command-line argument imposed. The systemd unit now
runs with NoNewPrivileges, ProtectSystem=strict, PrivateTmp and similar restrictions
(it still sees every process, which is how it names the program behind a port).

Windows: the SYSTEM task's folder was left to inherit ProgramData's permissions. It now
gets explicit ones: SYSTEM and Administrators only.

Installed agents keep their own copy of the script, so each server needs the install
one-liner re-run to pick this up.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-06 22:05:17 +02:00
1 parent c95d31d2d8
commit 047fcb403e
3 files changed
+62 -6

No files matched your search

+11
View File
@@ -62,6 +62,16 @@ function Save-AgentConfig([string]$Path, [string]$ApiUrl, [string]$ApiToken, [bo
[System.IO.File]::WriteAllText($Path, (ConvertTo-Json -InputObject $config), (New-Object System.Text.UTF8Encoding($false)))
}
# The agent script runs as SYSTEM, so anyone who can change it can run code as SYSTEM. ProgramData's default lets any local user
# create files in a new subfolder, and a folder left to inherit that is only as safe as that default. So the folder is given
# explicit permissions: SYSTEM and Administrators, and nobody else - not even to read, since the agent doesn't need them to.
# $ExtraGrants (full icacls grants, e.g. "*S-1-5-21-...:(OI)(CI)(F)") exists only so a test that isn't elevated can keep access to its own folder.
function Protect-InstallDir([string]$Path, [string[]]$ExtraGrants = @()) {
$grants = @('*S-1-5-18:(OI)(CI)(F)', '*S-1-5-32-544:(OI)(CI)(F)') + $ExtraGrants
& icacls.exe $Path /inheritance:r /grant:r $grants | Out-Null
if ($LASTEXITCODE -ne 0) { throw "Couldn't restrict permissions on $Path (icacls exit $LASTEXITCODE)." }
}
# The pieces of the scheduled task, built but not registered.
function New-AgentTaskParts([string]$AgentScript, [int]$IntervalMinutes) {
$argument = '-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File "{0}"' -f $AgentScript
@@ -97,6 +107,7 @@ function Install-Agent {
Write-Output "Installing Homelab Manager agent from $apiUrl ..."
New-Item -ItemType Directory -Force -Path $script:InstallDir | Out-Null
Protect-InstallDir $script:InstallDir
$agentScript = Join-Path $script:InstallDir 'homelab-manager-agent.ps1'
Save-Url "$apiUrl/agent/windows/report-tasks.ps1" $agentScript $insecure