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:
1 parent
c95d31d2d8
commit
047fcb403e
3 files changed
+62
-6
No files matched your search
@@ -88,6 +88,24 @@ Description=Report scheduled tasks to Homelab Manager
|
|||||||
Type=oneshot
|
Type=oneshot
|
||||||
EnvironmentFile=$CONFIG_DIR/homelab-manager-agent.env
|
EnvironmentFile=$CONFIG_DIR/homelab-manager-agent.env
|
||||||
ExecStart=$INSTALL_DIR/homelab-manager-agent.sh
|
ExecStart=$INSTALL_DIR/homelab-manager-agent.sh
|
||||||
|
# The agent only reads (crontabs, timers, /proc, listening sockets) and makes one HTTPS request, but it runs as root. These
|
||||||
|
# keep a bug in it, or in anything it parses, from being able to change the system. It still sees every process, which is
|
||||||
|
# how it names the program behind each listening port. Directives an older systemd doesn't know are ignored.
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=yes
|
||||||
|
PrivateTmp=yes
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=read-only
|
||||||
|
ProtectKernelTunables=yes
|
||||||
|
ProtectKernelModules=yes
|
||||||
|
ProtectKernelLogs=yes
|
||||||
|
ProtectControlGroups=yes
|
||||||
|
RestrictSUIDSGID=yes
|
||||||
|
RestrictRealtime=yes
|
||||||
|
RestrictNamespaces=yes
|
||||||
|
LockPersonality=yes
|
||||||
|
SystemCallArchitectures=native
|
||||||
|
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
cat > "$SYSTEMD_DIR/homelab-manager-agent.timer" <<EOF
|
cat > "$SYSTEMD_DIR/homelab-manager-agent.timer" <<EOF
|
||||||
|
|||||||
@@ -291,15 +291,42 @@ if [[ "$DRY_RUN" == "1" ]]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
response=$(curl -sS "${CURL_INSECURE_FLAG[@]}" -o /tmp/hlm-agent-response.json -w "%{http_code}" \
|
# >>> post_report
|
||||||
-X POST "$API_URL/api/agent/report" \
|
# Sends $PAYLOAD to the server and prints the HTTP status code.
|
||||||
-H "Authorization: Bearer $API_TOKEN" \
|
#
|
||||||
-H "Content-Type: application/json" \
|
# Nothing sensitive goes on curl's command line: any user on this machine can read other processes' command lines, and the
|
||||||
-d "$PAYLOAD")
|
# token would be there for the length of every report. So the token is handed to curl on its standard input, as a line of
|
||||||
|
# curl's own config format, and the payload and the server's reply go through files in a private directory (mktemp -d is
|
||||||
|
# readable by root only). A fixed name in /tmp is no good either — another user could plant a symlink there and have this
|
||||||
|
# script, running as root, overwrite whatever it points at.
|
||||||
|
curl_quote() {
|
||||||
|
local s=$1
|
||||||
|
s=${s//\\/\\\\}
|
||||||
|
s=${s//\"/\\\"}
|
||||||
|
printf '%s' "$s"
|
||||||
|
}
|
||||||
|
|
||||||
|
post_report() {
|
||||||
|
case "$API_TOKEN" in
|
||||||
|
*$'\n'*|*$'\r'*) echo "API_TOKEN must not contain a line break." >&2; return 1 ;;
|
||||||
|
esac
|
||||||
|
printf '%s' "$PAYLOAD" > "$HLM_TMPDIR/payload.json"
|
||||||
|
printf 'header = "Authorization: Bearer %s"\n' "$(curl_quote "$API_TOKEN")" | curl -sS "${CURL_INSECURE_FLAG[@]}" -K - \
|
||||||
|
-o "$HLM_TMPDIR/response.json" -w "%{http_code}" \
|
||||||
|
-X POST "$API_URL/api/agent/report" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
--data-binary @"$HLM_TMPDIR/payload.json"
|
||||||
|
}
|
||||||
|
# <<< post_report
|
||||||
|
|
||||||
|
HLM_TMPDIR=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$HLM_TMPDIR"' EXIT
|
||||||
|
|
||||||
|
response=$(post_report)
|
||||||
|
|
||||||
if [[ "$response" -lt 200 || "$response" -ge 300 ]]; then
|
if [[ "$response" -lt 200 || "$response" -ge 300 ]]; then
|
||||||
echo "Report failed with HTTP $response:" >&2
|
echo "Report failed with HTTP $response:" >&2
|
||||||
cat /tmp/hlm-agent-response.json >&2
|
cat "$HLM_TMPDIR/response.json" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -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)))
|
[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.
|
# The pieces of the scheduled task, built but not registered.
|
||||||
function New-AgentTaskParts([string]$AgentScript, [int]$IntervalMinutes) {
|
function New-AgentTaskParts([string]$AgentScript, [int]$IntervalMinutes) {
|
||||||
$argument = '-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File "{0}"' -f $AgentScript
|
$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 ..."
|
Write-Output "Installing Homelab Manager agent from $apiUrl ..."
|
||||||
|
|
||||||
New-Item -ItemType Directory -Force -Path $script:InstallDir | Out-Null
|
New-Item -ItemType Directory -Force -Path $script:InstallDir | Out-Null
|
||||||
|
Protect-InstallDir $script:InstallDir
|
||||||
$agentScript = Join-Path $script:InstallDir 'homelab-manager-agent.ps1'
|
$agentScript = Join-Path $script:InstallDir 'homelab-manager-agent.ps1'
|
||||||
Save-Url "$apiUrl/agent/windows/report-tasks.ps1" $agentScript $insecure
|
Save-Url "$apiUrl/agent/windows/report-tasks.ps1" $agentScript $insecure
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user