Skip to content

Instantly share code, notes, and snippets.

@ktsaou
Created October 23, 2026 12:12
Show Gist options
  • Select an option

  • Save ktsaou/b3fcf41c8ce0a01effb6df9d82a14858 to your computer and use it in GitHub Desktop.

Select an option

Save ktsaou/b3fcf41c8ce0a01effb6df9d82a14858 to your computer and use it in GitHub Desktop.
Netdata #24401 indicator-of-compromise check (read-only): access log, go.d ${cmd:} secret executions, injected dyncfg jobs, netdata-user persistence
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Read-only indicator-of-compromise check for netdata/netdata#24401
# (Function payload injection -> go.d dyncfg `test` -> ${cmd:...} secret -> command execution).
#
# The script changes nothing. Run it as root for full coverage (journal namespace, crontabs, other users' files).
#
# Usage: netdata-ioc-check-24401.sh [--since "YYYY-MM-DD"] [--user netdata] [--prefix /opt/netdata]
# [--access-log 'GLOB'] [--max-rows N] [--quiet]
#
# Exit code: 0 = nothing found, 1 = items to review (WARN), 2 = likely compromise indicators (ALERT).
#
# Docker installs: copy the script into the container and run it there (`docker exec -i <c> bash -s < script`);
# the collector log of a container goes to `docker logs <c>`, so also run:
# docker logs <c> 2>&1 | grep -E "resolved secret via command|resolving secret"
set -u
export LC_ALL=C
RED=$'\033[0;31m'; YELLOW=$'\033[1;33m'; GREEN=$'\033[0;32m'; GRAY=$'\033[0;90m'; BOLD=$'\033[1m'; NC=$'\033[0m'
[ -t 1 ] || { RED=; YELLOW=; GREEN=; GRAY=; BOLD=; NC=; }
SINCE=""
ND_USER="netdata"
PREFIX=""
ACCESS_GLOB=""
MAX_ROWS=40
QUIET=0
while [ $# -gt 0 ]; do
case "$1" in
--since) SINCE="$2"; shift 2 ;;
--user) ND_USER="$2"; shift 2 ;;
--prefix) PREFIX="$2"; shift 2 ;;
--access-log) ACCESS_GLOB="$2"; shift 2 ;;
--max-rows) MAX_ROWS="$2"; shift 2 ;;
--quiet) QUIET=1; shift ;;
-h|--help) sed -n '3,18p' "$0"; exit 0 ;;
*) echo "unknown option: $1" >&2; exit 64 ;;
esac
done
ALERTS=0
WARNS=0
section() { printf '\n%s== %s ==%s\n' "$BOLD" "$*" "$NC"; }
alert() { ALERTS=$((ALERTS + 1)); printf '%s[ALERT]%s %s\n' "$RED" "$NC" "$*"; }
warn() { WARNS=$((WARNS + 1)); printf '%s[WARN]%s %s\n' "$YELLOW" "$NC" "$*"; }
ok() { printf '%s[OK]%s %s\n' "$GREEN" "$NC" "$*"; }
info() { printf '[INFO] %s\n' "$*"; }
detail() { printf ' %s\n' "$*"; }
# Show the command before running it, so the operator can re-run any check by hand.
show() {
[ "$QUIET" = 1 ] && return 0
printf >&2 '%s$ ' "$GRAY"
printf >&2 '%q ' "$@"
printf >&2 '%s\n' "$NC"
}
# ---------------------------------------------------------------------------------------------------------------------
# paths
if [ -z "$PREFIX" ] && [ -x /opt/netdata/bin/netdata ]; then
PREFIX=/opt/netdata
fi
if [ -n "$PREFIX" ]; then
ETC_DIR="$PREFIX/etc/netdata"
VARLIB_DIR="$PREFIX/var/lib/netdata"
LOG_DIR="$PREFIX/var/log/netdata"
LIBEXEC_DIR="$PREFIX/usr/libexec/netdata"
else
ETC_DIR=/etc/netdata
VARLIB_DIR=/var/lib/netdata
LOG_DIR=/var/log/netdata
LIBEXEC_DIR=/usr/libexec/netdata
[ -d "$LIBEXEC_DIR" ] || LIBEXEC_DIR=/usr/lib/netdata
fi
[ -n "$ACCESS_GLOB" ] || ACCESS_GLOB="$LOG_DIR/access.log*"
section "Environment"
[ "$(id -u)" = 0 ] || warn "not running as root: journal, crontab and file checks may be incomplete"
info "paths: etc=$ETC_DIR varlib=$VARLIB_DIR log=$LOG_DIR"
ND_BIN="${PREFIX:+$PREFIX/bin/}netdata"
command -v "$ND_BIN" >/dev/null 2>&1 || ND_BIN=$(command -v netdata 2>/dev/null || true)
if [ -n "$ND_BIN" ]; then
info "installed: $("$ND_BIN" -v 2>/dev/null | head -1)"
fi
info "the \${cmd:...} secret resolver used by the known exploit ships in v2.10.0 and later"
if getent passwd "$ND_USER" >/dev/null; then
ND_HOME=$(getent passwd "$ND_USER" | cut -d: -f6)
ND_SHELL=$(getent passwd "$ND_USER" | cut -d: -f7)
info "user: $(id "$ND_USER")"
else
warn "user '$ND_USER' not found; use --user"
ND_HOME=""; ND_SHELL=""
fi
# ---------------------------------------------------------------------------------------------------------------------
# 1. access log: POST requests to function/config endpoints (the delivery path of the injection)
section "1. Access log: POST requests to function/config endpoints"
# shellcheck disable=SC2086
ACCESS_FILES=$(ls -1 $ACCESS_GLOB 2>/dev/null | sort -V)
if [ -z "$ACCESS_FILES" ]; then
warn "no access log files match '$ACCESS_GLOB' (access logging may be off or sent to the journal); use --access-log"
else
info "scanning $(echo "$ACCESS_FILES" | wc -l) file(s): $(echo "$ACCESS_FILES" | head -1) ..."
info "the request body is never logged: these are candidates, not proof (the dashboard also POSTs here)"
TMP_OUT=$(mktemp)
# shellcheck disable=SC2086
show zcat -f $ACCESS_GLOB '|' grep -F req_method=POST '|' grep -E '/api/v[0-9]+/(function|config)'
echo "$ACCESS_FILES" | while IFS= read -r f; do zcat -f -- "$f" 2>/dev/null; done \
| grep -F 'req_method=POST' \
| grep -E 'request="?(/host/[^/ "]+|/node/[^/ "]+)?/api/v[0-9]+/(function|config)([?/ "]|$)' \
| awk -v since="$SINCE" '
function getf(line, key, re, m, v) {
re = "(^| )" key "=(\"([^\"\\\\]|\\\\.)*\"|[^ ]*)"
if (!match(line, re)) return ""
v = substr(line, RSTART, RLENGTH); sub(/^ /, "", v); sub("^" key "=", "", v)
if (v ~ /^".*"$/) v = substr(v, 2, length(v) - 2)
return v
}
function urldecode(s, out, i, c, h) {
gsub(/\+/, " ", s); out = ""
for (i = 1; i <= length(s); i++) {
c = substr(s, i, 1)
if (c == "%" && i + 2 <= length(s)) {
h = tolower(substr(s, i + 1, 2))
out = out sprintf("%c", (index("0123456789abcdef", substr(h, 1, 1)) - 1) * 16 + index("0123456789abcdef", substr(h, 2, 1)) - 1)
i += 2
} else out = out c
}
return out
}
{
t = getf($0, "time")
if (since != "" && substr(t, 1, length(since)) < since) next
req = getf($0, "request")
fn = ""
if (match(req, /[?&]function=[^&]*/)) { fn = substr(req, RSTART, RLENGTH); sub(/^[?&]function=/, "", fn); fn = urldecode(fn) }
else if (req ~ /\/config/) { fn = "(config endpoint) " req }
ip = getf($0, "src_ip"); fw = getf($0, "src_forwarded_for")
if (fw != "") ip = ip " fwd=" fw
if (ip == "") ip = "local"
user = getf($0, "user"); if (user == "") user = "-"
role = getf($0, "role")
key = role "\t" user "\t" ip "\t" getf($0, "code") "\t" fn
n[key]++
if (!(key in first)) first[key] = t
last[key] = t
total++
if (role == "any" || role == "none" || role == "") anon++
}
END {
for (k in n) print n[k] "\t" first[k] "\t" last[k] "\t" k
printf "#TOTAL\t%d\t%d\n", total, anon
}' > "$TMP_OUT"
TOTAL=$(awk -F'\t' '$1 == "#TOTAL" { print $2 }' "$TMP_OUT")
ANON=$(awk -F'\t' '$1 == "#TOTAL" { print $3 }' "$TMP_OUT")
if [ "${TOTAL:-0}" = 0 ]; then
ok "no POST requests to function/config endpoints${SINCE:+ since $SINCE}"
else
warn "$TOTAL POST request(s) to function/config endpoints; $ANON without an authenticated role (role=any/none)"
detail "review source IPs and users you do not recognise; on a parent, requests for children show as /host/<id>/..."
printf ' %-6s %-24s %-24s %-8s %-16s %-28s %-5s %s\n' COUNT FIRST LAST ROLE USER SOURCE CODE FUNCTION
grep -v '^#TOTAL' "$TMP_OUT" \
| sort -t$'\t' -k1,1nr \
| head -n "$MAX_ROWS" \
| awk -F'\t' '{ printf " %-6s %-24s %-24s %-8s %-16s %-28s %-5s %s\n", $1, $2, $3, $4, $5, $6, $7, substr($8, 1, 120) }'
ROWS=$(grep -vc '^#TOTAL' "$TMP_OUT")
[ "$ROWS" -gt "$MAX_ROWS" ] && detail "... $((ROWS - MAX_ROWS)) more groups (use --max-rows)"
fi
rm -f "$TMP_OUT"
fi
# ---------------------------------------------------------------------------------------------------------------------
# baseline: ${cmd:...} secrets the administrator configured in files (legitimate use)
LEGIT_CMDS=$(grep -rhoE '\$\{cmd:[^}]*\}' "$ETC_DIR" 2>/dev/null \
| sed -E 's/^\$\{cmd:[[:space:]]*//; s/\}$//' | awk '{ print $1 }' | sort -u)
# ---------------------------------------------------------------------------------------------------------------------
# 2. collector log: ${cmd:...} secret executions
section "2. Collector log: commands executed through \${cmd:...} secrets"
PATTERN='resolved secret via command|resolving secret .*cmd:'
TMP_LOG=$(mktemp)
SCANNED=""
if command -v journalctl >/dev/null 2>&1; then
JARGS=(--no-pager -o short-iso)
[ -n "$SINCE" ] && JARGS+=(--since "$SINCE")
show journalctl --namespace netdata "${JARGS[@]}" '|' grep -E "$PATTERN"
if journalctl --namespace netdata "${JARGS[@]}" -n 1 >/dev/null 2>&1; then
journalctl --namespace netdata "${JARGS[@]}" 2>/dev/null | grep -E "$PATTERN" >> "$TMP_LOG"
SCANNED="journal namespace 'netdata'"
fi
show journalctl "${JARGS[@]}" _COMM=go.d.plugin '|' grep -E "$PATTERN"
journalctl "${JARGS[@]}" _COMM=go.d.plugin 2>/dev/null | grep -E "$PATTERN" >> "$TMP_LOG"
SCANNED="${SCANNED:+$SCANNED, }default journal (_COMM=go.d.plugin)"
fi
for f in "$LOG_DIR"/collector.log*; do
[ -e "$f" ] || continue
show zcat -f "$f" '|' grep -E "$PATTERN"
zcat -f -- "$f" 2>/dev/null | grep -E "$PATTERN" >> "$TMP_LOG"
SCANNED="${SCANNED:+$SCANNED, }$f"
done
if [ -z "$SCANNED" ]; then
warn "no collector log source could be read (no journal access and no $LOG_DIR/collector.log*)"
else
info "scanned: $SCANNED"
fi
if [ -s "$TMP_LOG" ]; then
sort -u "$TMP_LOG" -o "$TMP_LOG"
# binary path of every executed command: "via command '/bin/x'" or "resolving secret '${cmd:/bin/x args}'"
EXECUTED=$(sed -nE "s/.*resolved secret via command '([^']*)'.*/\1/p; s/.*resolving secret '\\\$\{cmd:[[:space:]]*([^ }']*).*/\1/p" "$TMP_LOG" | sort -u)
UNKNOWN=""
for b in $EXECUTED; do
echo "$LEGIT_CMDS" | grep -qxF -- "$b" || UNKNOWN="$UNKNOWN $b"
done
if [ -n "$UNKNOWN" ]; then
alert "commands executed via \${cmd:...} that are not configured in $ETC_DIR:$UNKNOWN"
detail "'resolved' means the command exited 0; 'command failed' or 'timed out' still means it RAN"
detail "if these are not secrets you configured through the UI, treat the host as compromised"
else
warn "commands executed via \${cmd:...}, all configured in $ETC_DIR:$(echo $EXECUTED | tr '\n' ' ')"
fi
head -n "$MAX_ROWS" "$TMP_LOG" | while IFS= read -r l; do detail "$(echo "$l" | cut -c1-300)"; done
[ "$(wc -l < "$TMP_LOG")" -gt "$MAX_ROWS" ] && detail "... $(( $(wc -l < "$TMP_LOG") - MAX_ROWS )) more lines"
else
ok "no \${cmd:...} secret executions logged"
detail "absence is not proof: logging of this path during a dyncfg 'test' is not confirmed, and logs rotate"
fi
rm -f "$TMP_LOG"
[ -n "$LEGIT_CMDS" ] && info "\${cmd:...} binaries configured in $ETC_DIR (baseline): $(echo $LEGIT_CMDS)"
# ---------------------------------------------------------------------------------------------------------------------
# 3. persisted dyncfg jobs containing ${cmd:...}
section "3. Persisted dynamic configuration with \${cmd:...}"
DYNCFG_DIR="$VARLIB_DIR/config"
if [ ! -d "$DYNCFG_DIR" ]; then
info "no $DYNCFG_DIR directory"
elif [ ! -r "$DYNCFG_DIR" ]; then
warn "cannot read $DYNCFG_DIR (run as root)"
else
show grep -lF '${cmd:' "$DYNCFG_DIR"
FOUND=0
for f in "$DYNCFG_DIR"/*.dyncfg; do
[ -e "$f" ] || continue
grep -qF '${cmd:' "$f" || continue
FOUND=1
hdr() { sed -n "s/^$1=//p" "$f" | head -1; }
ts() { [ -n "$1" ] && date -u -d "@$(( $1 / 1000000 ))" '+%Y-%m-%dT%H:%M:%SZ' 2>/dev/null; }
alert "dyncfg job with \${cmd:...}: $(hdr id)"
detail "file: $f"
detail "created: $(ts "$(hdr created)") modified: $(ts "$(hdr modified)") saves: $(hdr saves)"
detail "source: type=$(hdr source_type) $(hdr source)"
grep -oE '\$\{cmd:[^}]*\}' "$f" | sort -u | while IFS= read -r c; do detail "command: $c"; done
detail "this job re-runs its command on every go.d start; if you did not create it, it is persistence"
done
[ "$FOUND" = 0 ] && ok "no persisted dyncfg job contains \${cmd:...}"
detail "absence is not proof: the known exploit uses 'test', which never writes a file"
fi
# ---------------------------------------------------------------------------------------------------------------------
# 4. persistence and post-exploitation traces of the netdata user
section "4. Persistence and activity of user '$ND_USER'"
if [ -n "$ND_HOME" ]; then
# crontab
for c in "/var/spool/cron/crontabs/$ND_USER" "/var/spool/cron/$ND_USER"; do
[ -e "$c" ] && { alert "user crontab exists: $c (Netdata never installs one)"; sed 's/^/ | /' "$c" 2>/dev/null | head -20; }
done
show grep -lw "$ND_USER" /etc/crontab /etc/cron.d
for c in /etc/crontab /etc/cron.d/*; do
[ -f "$c" ] && grep -qE "^[^#].*[[:space:]]${ND_USER}[[:space:]]" "$c" 2>/dev/null && info "system cron entry runs as $ND_USER: $c (root-owned, review anyway)"
done
# at jobs
if command -v atq >/dev/null 2>&1; then
show atq
ATJ=$(atq 2>/dev/null | awk -v u="$ND_USER" '$NF == u')
[ -n "$ATJ" ] && { alert "at jobs owned by $ND_USER:"; echo "$ATJ" | sed 's/^/ | /'; }
fi
# systemd user units and lingering
[ -e "/var/lib/systemd/linger/$ND_USER" ] && alert "systemd lingering is enabled for $ND_USER (/var/lib/systemd/linger/$ND_USER)"
show find "$ND_HOME/.config/systemd" "$ND_HOME/.local/share/systemd" -type f
UNITS=$(find "$ND_HOME/.config/systemd" "$ND_HOME/.local/share/systemd" -type f 2>/dev/null)
[ -n "$UNITS" ] && { alert "systemd user units in the home of $ND_USER:"; echo "$UNITS" | sed 's/^/ | /'; }
# login and shell startup files
for p in .ssh/authorized_keys .ssh/authorized_keys2 .bashrc .profile .bash_profile .config/autostart; do
[ -e "$ND_HOME/$p" ] && warn "exists: $ND_HOME/$p (unexpected for a service account)"
done
case "$ND_SHELL" in
*/nologin|*/false|"") ;;
*) warn "$ND_USER has a login shell: $ND_SHELL" ;;
esac
# executables in the netdata state directory
show find "$VARLIB_DIR" -xdev -type f -perm /111
EXE=$(find "$VARLIB_DIR" -xdev -type f -perm /111 2>/dev/null | head -n "$MAX_ROWS")
[ -n "$EXE" ] && { warn "executable files in $VARLIB_DIR (not expected):"; echo "$EXE" | sed 's/^/ | /'; }
fi
# files in world-writable temp dirs
show find /tmp /var/tmp /dev/shm -xdev -maxdepth 3 -user "$ND_USER"
TMPF=$(find /tmp /var/tmp /dev/shm -xdev -maxdepth 3 -user "$ND_USER" 2>/dev/null | head -n "$MAX_ROWS")
[ -n "$TMPF" ] && { warn "files owned by $ND_USER in temp directories:"; find $TMPF -maxdepth 0 -printf ' | %TY-%Tm-%Td %TH:%TM %M %p\n' 2>/dev/null; }
# configuration and plugin files the netdata user can modify (persistence and escalation surface)
show find "$ETC_DIR" "$LIBEXEC_DIR" -xdev -type f '(' -user "$ND_USER" -o '(' -group "$ND_USER" -perm -g+w ')' ')'
WR=$(find "$ETC_DIR" "$LIBEXEC_DIR" -xdev -type f \( -user "$ND_USER" -o \( -group "$ND_USER" -perm -g+w \) \) 2>/dev/null | head -n "$MAX_ROWS")
[ -n "$WR" ] && { warn "config/plugin files writable by $ND_USER (check contents and modification times):"; find $WR -maxdepth 0 -printf ' | %TY-%Tm-%Td %TH:%TM %u:%g %M %p\n' 2>/dev/null; }
# processes
show ps -e -o pid=,ppid=,user=,comm=,args=
PS_OUT=$(ps -e -o pid=,ppid=,user:32=,comm=,args= 2>/dev/null)
ROGUE=$(echo "$PS_OUT" | awk -v u="$ND_USER" '
{ pid[$1] = $1; ppid[$1] = $2; usr[$1] = $3; comm[$1] = $4; line[$1] = $0 }
$4 == "netdata" { daemon[$1] = 1 }
function under(p, name, i) { for (i = 0; i < 64 && p > 1; i++) { if (name == "" ? daemon[p] : comm[p] == name) return 1; p = ppid[p] } return 0 }
END {
for (p in pid) {
if (usr[p] != u) continue
if (!daemon[p] && !under(ppid[p], "")) print "ORPHAN\t" line[p]
else if (under(ppid[p], "go.d.plugin") && comm[p] ~ /^(sh|bash|dash|zsh|ash|busybox|python[0-9.]*|perl|ruby|php|node|nc|ncat|netcat|socat|curl|wget|telnet|openssl|ssh)$/)
print "GOD\t" line[p]
}
}')
if echo "$ROGUE" | grep -q '^ORPHAN'; then
alert "processes of $ND_USER not running under the netdata daemon (detached/backgrounded):"
echo "$ROGUE" | sed -n 's/^ORPHAN\t/ | /p' | cut -c1-250
fi
if echo "$ROGUE" | grep -q '^GOD'; then
warn "shells/network tools running under go.d.plugin:"
echo "$ROGUE" | sed -n 's/^GOD\t/ | /p' | cut -c1-250
fi
[ -z "$ROGUE" ] && ok "no detached $ND_USER processes, no shells or network tools under go.d.plugin"
# privilege escalation reach
GROUPS_OF=$(id -Gn "$ND_USER" 2>/dev/null)
for g in docker lxd libvirt sudo wheel admin disk root kvm; do
if echo " $GROUPS_OF " | grep -q " $g "; then
case "$g" in
docker|lxd|libvirt|sudo|wheel|admin|disk|root)
warn "$ND_USER is in group '$g': code running as $ND_USER can likely become root; if compromised, investigate the whole host" ;;
*) info "$ND_USER is in group '$g'" ;;
esac
fi
done
# ---------------------------------------------------------------------------------------------------------------------
section "Summary"
if [ "$ALERTS" -gt 0 ]; then
printf '%s%d ALERT(s), %d WARN(s).%s Treat the host as compromised unless every ALERT is explained by your own configuration.\n' "$RED" "$ALERTS" "$WARNS" "$NC"
echo "Next: preserve logs, upgrade Netdata, remove injected dyncfg jobs and persistence, rotate every credential"
echo "readable by '$ND_USER' (collector configs, bearer tokens, Netdata Cloud claim), and check outbound connections."
exit 2
elif [ "$WARNS" -gt 0 ]; then
printf '%s%d WARN(s).%s No direct execution evidence; review the items above.\n' "$YELLOW" "$WARNS" "$NC"
exit 1
else
printf '%sNo indicators found.%s Absence of evidence is not proof: logs rotate and the request body is never logged.\n' "$GREEN" "$NC"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment