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

+33 -6
View File
@@ -291,15 +291,42 @@ if [[ "$DRY_RUN" == "1" ]]; then
exit 0
fi
response=$(curl -sS "${CURL_INSECURE_FLAG[@]}" -o /tmp/hlm-agent-response.json -w "%{http_code}" \
-X POST "$API_URL/api/agent/report" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD")
# >>> post_report
# Sends $PAYLOAD to the server and prints the HTTP status code.
#
# Nothing sensitive goes on curl's command line: any user on this machine can read other processes' command lines, and the
# 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
echo "Report failed with HTTP $response:" >&2
cat /tmp/hlm-agent-response.json >&2
cat "$HLM_TMPDIR/response.json" >&2
exit 1
fi