node_exporter 加固实战:指标端点收口

node_exporter 几乎是每台 Linux 服务器上都会跑的东西,但它经常是整台机器上唯一一个既没有认证、也没有加密、还返回大量系统信息的服务。默认监听 :9100 明文 HTTP,任何能连到这个端口的人执行一条 curl,就能拿到内核版本、挂载点、网卡、磁盘型号、运行中的 systemd 单元名,以及全部 CPU 与内存细节。本文把 node_exporter 的指标端点彻底收口。

适用场景

  • Prometheus 部署在内网,node_exporter 的 9100 端口对整个办公网段开放,随手扫一下就能看到指标。
  • 服务器上跑着多租户业务,宿主机指标不应该被业务方读到。
  • 内网存在 SSRF 类漏洞,攻击者可以借助应用服务器请求 http://127.0.0.1:9100/metrics,把主机信息当跳板探测。
  • 安全基线检查要求「采集端点必须鉴权、必须加密」,而当前的 node_exporter 是裸装。

如果 node_exporter 与 Prometheus 跑在同一台机器上、且只监听回环,风险相对可控,但仍建议至少做 collector 裁剪——情报面的收窄和传输加密是两件独立的事。

前置条件

  • node_exporter 1.5 及以上版本。--web.config.file 依赖 exporter-toolkit,太老的版本没有这个参数,需要先升级。
  • Prometheus 2.30 以上,抓取配置里支持 basic_auth 与 tls_config。
  • htpasswd(来自 apache2-utils 或 httpd-tools)用于生成 bcrypt 哈希。
  • openssl 1.1.1 以上,用于生成带 SAN 的自签证书(-addext 参数需要较新版本)。
  • 服务器上已启用 ufw 或 nftables 之一,用于做来源 IP 白名单。

原理说明

node_exporter 本质上是一个「把 /proc 和 /sys 转成文本」的服务。它不做鉴权,不做限流,因为它设计上假设整条链路都在可信网络里。加固的思路不是给它加功能,而是用官方提供的两个机制把它包起来:传输层安全 + 采集面收敛。

指标为什么算敏感信息

  • node_uname_info 直接给出内核版本与主机名,配合公开漏洞库能立刻筛出可利用目标。
  • node_filesystem_* 暴露全部挂载点,等于泄露目录规划与数据盘布局。
  • node_systemd_unit_state 列出所有服务名与运行状态,攻击者据此判断装了哪些中间件。
  • node_network_info 给出网卡、MAC 与地址族,是横向移动的起点。
  • node_exporter_build_info 泄露版本号,版本对应的已知问题一目了然。

这些信息单独看无伤大雅,组合起来就是一份现成的主机画像。所以「9100 只在内网所以不用管」这个判断,在有一定规模的内网里并不成立。

TLS 与认证如何生效

--web.config.file 指向的文件由 exporter-toolkit 解析,它把 tls_server_config、http_server_config、basic_auth_users 三块能力套在 exporter 原有的 HTTP 服务外面。密码必须是 bcrypt 哈希,不支持明文。

这套 web 配置在每次 HTTP 请求时都会重新读取,所以更换密码或续期证书不需要重启进程,改完文件立即生效;反过来说,配置语法写错也会立刻影响所有抓取。

collector 裁剪的取舍

默认会开启几十个 collector,每次抓取都要读一遍 /proc。用 --collector.disable-defaults 关掉默认集合,再显式 --collector.xxx 逐个打开,能做到「只采需要的」。代价是部分 Grafana 面板会变空白,因为面板依赖的指标没被采集。正确做法是先看面板用了哪些指标,再反推需要哪个 collector。

操作步骤

第 1 步:建立专用用户与目录

不要让 node_exporter 以 root 运行。它读取 /proc 与 /sys 不需要特权,普通用户足够;以 root 跑等于把「一个只读采集器」放大成「一个能读全部的 root 进程」。

sudo useradd --no-create-home --shell /usr/sbin/nologin node_exporter
sudo install -d -m 755 /etc/node_exporter
sudo install -d -m 750 -o node_exporter -g node_exporter /var/lib/node_exporter
sudo install -d -m 750 -o node_exporter -g node_exporter /var/lib/node_exporter/textfile
sudo install -d -m 750 /etc/node_exporter/tls

第 2 步:安装指定版本

版本号从 GitHub API 动态获取,避免写死一个会过期的号。安装后先确认 --web.config.file 存在。

cd /tmp
NE_VER=$(curl -fsSL https://api.github.com/repos/prometheus/node_exporter/releases/latest \
  | grep -oP '"tag_name":\s*"v?\K[^"]+')
echo "latest=${NE_VER}"
curl -fsSLO "https://github.com/prometheus/node_exporter/releases/download/v${NE_VER}/node_exporter-${NE_VER}.linux-amd64.tar.gz"
tar -xzf "node_exporter-${NE_VER}.linux-amd64.tar.gz"
sudo install -m 0755 "node_exporter-${NE_VER}.linux-amd64/node_exporter" /usr/local/bin/node_exporter
node_exporter --version
node_exporter --help 2>&1 | grep -E 'web.config.file|disable-defaults'

第 3 步:生成带 SAN 的自签证书

Prometheus 校验证书时比对的是 SAN,不是 CN。只写 CN 的证书在 Go 客户端里会直接报 x509: certificate relies on legacy Common Name field,这是最常见的自签证书翻车点。

cd /etc/node_exporter/tls
sudo openssl req -x509 -newkey rsa:2048 -nodes -days 825 \
  -keyout node_exporter.key -out node_exporter.crt \
  -subj "/C=CN/O=Ops/CN=node01.example.com" \
  -addext "subjectAltName=DNS:node01.example.com,IP:10.0.0.11" \
  -addext "keyUsage=digitalSignature,keyEncipherment" \
  -addext "extendedKeyUsage=serverAuth"

sudo chmod 640 node_exporter.key
sudo chown root:node_exporter node_exporter.key node_exporter.crt
openssl x509 -in node_exporter.crt -noout -subject -dates -ext subjectAltName

多台机器时可以建一个自建 CA,用它签发各节点的证书,这样 Prometheus 只需要信任一张 CA,而不是把每台机器的证书都塞进 ca_file。

第 4 步:编写 web-config.yml

这一段是整篇文章的核心。TLS、Basic Auth、安全响应头与请求限速都在这个文件里,四块能力一次配齐。

# /etc/node_exporter/web-config.yml
tls_server_config:
  cert_file: /etc/node_exporter/tls/node_exporter.crt
  key_file:  /etc/node_exporter/tls/node_exporter.key
  min_version: TLS12
  max_version: TLS13

http_server_config:
  http2: true
  headers:
    X-Content-Type-Options: nosniff
    X-Frame-Options: deny
    Content-Security-Policy: "default-src 'none'"

basic_auth_users:
  prometheus: "$2y$12$REPLACE_WITH_YOUR_OWN_BCRYPT_HASH_000000000000000"

# 令牌桶限速,防止被当作压测目标反复抓取
rate_limit:
  interval: 1s
  burst: 10

哈希用 htpasswd 生成,代价因子 12 是性能与强度的平衡点:

htpasswd -nBC 12 "" | tr -d ':\n'; echo
# 输出形如:$2y$12$AbCd...(直接替换上面 basic_auth_users 里的占位值)

Content-Security-Policy: default-src 'none' 对纯文本接口没有副作用,但如果同端口还挂了别的路径,会影响页面渲染。这里的 /metrics 只返回文本,加上它没有任何损失。

sudo chown root:node_exporter /etc/node_exporter/web-config.yml
sudo chmod 640 /etc/node_exporter/web-config.yml

第 5 步:按最小集合裁剪 collector

下面的 unit 文件同时做了两件事:只开必要 collector、把进程本身关进沙箱。注意 --collector.disable-defaults 之后必须显式列出每一个要用的 collector,漏一个面板就空一片。

# /etc/systemd/system/node_exporter.service
[Unit]
Description=Prometheus node_exporter
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporter \
  --web.listen-address=:9100 \
  --web.config.file=/etc/node_exporter/web-config.yml \
  --collector.disable-defaults \
  --collector.cpu \
  --collector.meminfo \
  --collector.loadavg \
  --collector.stat \
  --collector.filesystem \
  --collector.diskstats \
  --collector.netdev \
  --collector.netstat \
  --collector.filefd \
  --collector.pressure \
  --collector.textfile \
  --collector.textfile.directory=/var/lib/node_exporter/textfile
Restart=on-failure
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
CapabilityBoundingSet=
AmbientCapabilities=
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
ReadWritePaths=/var/lib/node_exporter

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
sudo systemctl status node_exporter --no-pager
sudo journalctl -u node_exporter -n 30 --no-pager

这里 故意没有开启 --collector.uname 与 --collector.systemd:前者直接暴露内核版本与主机名,后者暴露全部服务名。如果现有面板依赖 node_uname_info,会看到相关图例变空,这是刻意的取舍,需要时再单独加回并用防火墙限制访问来源。

ProtectSystem=strict 会把整个文件系统挂成只读,所以必须用 ReadWritePaths 单独放开 textfile 目录,否则自检脚本写入的指标会失败且没有任何报错——textfile collector 只读取文件,写不进去是写入方的问题,容易被误判成采集故障。

第 6 步:防火墙只放行 Prometheus

认证是应用层的,防火墙是网络层的,两层都要有。只做认证意味着端口仍然对全网开放,任何 0day 或配置失误都会直接暴露;只做防火墙则失去了第二道防线。

# 只允许 Prometheus 主机访问 9100
sudo ufw allow from 10.0.0.5 to any port 9100 proto tcp
sudo ufw deny 9100/tcp
sudo ufw status numbered

# nftables 等价写法
# sudo nft add rule inet filter input tcp dport 9100 ip saddr != 10.0.0.5 drop

云主机还要检查安全组。安全组与主机防火墙是两套独立规则,经常出现「主机上封了、安全组还开着」的情况。

第 7 步:Prometheus 侧对接

抓取配置必须显式声明 HTTPS 与认证,否则会得到 server returned HTTP status 401。密码放独立文件,避免出现在主配置与仓库里。

# /etc/prometheus/node_exporter.pass
# 内容为第 4 步设置的那个明文密码,权限 640,属主 prometheus

# prometheus.yml 片段
scrape_configs:
  - job_name: node
    scheme: https
    basic_auth:
      username: prometheus
      password_file: /etc/prometheus/node_exporter.pass
    tls_config:
      ca_file: /etc/prometheus/certs/internal-ca.crt
      server_name: node01.example.com
      min_version: TLS12
    scrape_interval: 15s
    static_configs:
      - targets: ["node01.example.com:9100"]
sudo promtool check config /etc/prometheus/prometheus.yml
sudo systemctl reload prometheus

配置验证

七条命令覆盖认证、加密、协议版本、指标面与网络边界。预期结果写在注释里。

# 1) 未带凭据:必须 401,且不能返回任何指标文本
curl -sk -o /dev/null -w 'noauth=%{http_code}\n' https://127.0.0.1:9100/metrics

# 2) 带凭据:200,且能取到指标
curl -sk -o /tmp/m.txt -w 'auth=%{http_code}\n' \
  -u 'prometheus:YOUR_PASSWORD' https://127.0.0.1:9100/metrics
grep -c '^node_' /tmp/m.txt

# 3) 确认内核版本类指标已被裁掉
grep -c '^node_uname_info' /tmp/m.txt || echo "uname=absent"

# 4) 证书链与 SAN
openssl s_client -connect 127.0.0.1:9100 -servername node01.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -ext subjectAltName -dates

# 5) 拒绝 TLS 1.1
openssl s_client -connect 127.0.0.1:9100 -tls1_1 </dev/null 2>&1 | grep -i 'alert\|no protocols'

# 6) 从非白名单机器访问应超时
curl -sk -m 3 -o /dev/null -w 'remote=%{http_code}\n' https://node01.example.com:9100/metrics \
  || echo "remote=blocked"

# 7) Prometheus 侧抓取状态
curl -s http://127.0.0.1:9090/api/v1/targets \
  | grep -o '"health":"[a-z]*"' | sort | uniq -c

预期:第 1 条 401;第 2 条 200 且 node_ 指标数量在几十到几百之间;第 3 条输出 uname=absent;第 5 条握手失败;第 6 条超时;第 7 条全部为 health":"up。

再确认一次采集面是否真的变窄,把当前指标名去重后对比预期:

grep -o '^node_[a-z_]*' /tmp/m.txt | sort -u | wc -l
grep -o '^node_systemd_[a-z_]*' /tmp/m.txt | sort -u | head

常见问题

Q1:Prometheus 报 x509 unknown authority 或 certificate is valid for X, not Y,怎么修?

两个独立原因,按顺序排查。第一个:自签证书没有放进 tls_config.ca_file,Go 客户端只信任系统根证书,不信任任何自签证书。第二个:证书 SAN 里没有 Prometheus 实际使用的地址。Prometheus 用 targets 里的主机名做校验,如果 targets 写的是 IP,证书 SAN 必须同时包含这个 IP,只写 DNS 名无效;反过来如果写了 server_name,则以该项为准,此时 SAN 必须匹配 server_name。用 openssl x509 -ext subjectAltName 确认当前证书到底签了哪些名字,比反复猜快得多。

Q2:端口从外部还能连通,是不是加固没生效?

TCP 层能建连不代表指标泄露。开启 TLS 后,未完成握手的连接拿不到任何内容;带凭据才有 200,否则是 401。用 -w '%{http_code}' 而不是 curl 成功与否来判断,才能区分「能连」和「能取」。真正的边界验证要看第 6 条:从非白名单机器上应当超时。如果确实超时失败,说明防火墙规则没生效,检查是否有 ufw allow 9100 的更宽规则在上面先命中,ufw 是按顺序匹配的。

Q3:裁剪 collector 之后 Grafana 面板大片空白,怎么补?

先定位面板缺哪个指标,再反推 collector。常用对应关系:CPU 使用率面板要 --collector.cpu;内存面板要 --collector.meminfo;磁盘用率要 --collector.filesystem 与 --collector.diskstats;网络流量要 --collector.netdev;负载要 --collector.loadavg;进程与文件句柄要 --collector.filefd 与 --collector.stat;PSI 压力指标要 --collector.pressure。补完后用 node_scrape_collector_success 确认新 collector 都返回 1,返回 0 说明它在当前内核上读不到数据,属于预期行为而不是故障。

Q4:换密码、续证书需要重启 node_exporter 吗?

不需要。web 配置文件在每次请求时重新加载,改完 web-config.yml 或替换证书文件,下一次抓取就生效。这一点很实用:证书 90 天续期只要覆盖文件,不用安排重启窗口。但要注意配置文件权限必须保持 640 root:node_exporter,续期脚本如果用 cp 覆盖会把属主改成 root 并可能放宽权限,导致 node_exporter 读不到文件而整站 401。稳妥做法是 install -m 640 -g node_exporter 写入。

总结

node_exporter 的加固可以归结成四件事:加 TLS 让传输不可嗅探、加 Basic Auth 让指标不可匿名读取、裁剪 collector 让情报面变小、加防火墙白名单让端口不对全网开放。四件事互不替代,缺一件就有一个对应的攻击路径仍然可用。

落地顺序建议从防火墙开始(最快见效、风险最低),再做 collector 裁剪(影响可观测性,需要和面板使用方确认),最后上 TLS 与认证(需要同步改 Prometheus 配置)。如果节点数量多,先把证书与密码分发做成配置管理的一部分,再批量推,避免逐台手工操作。

最后一点常被忽略:node_exporter 的加固要和 Prometheus 的加固一起做。9090 端口同样默认无认证,能访问 Prometheus 的人可以通过 /api/v1/targets 拿到全部节点清单,再逐个尝试直连。整条链路都收口,采集体系才算真正私有。