OpenSCAP 合规扫描
适用于 CentOS Stream 9 & 10 / AlmaLinux 9.x & 10.x / Rocky Linux 9.x & 10.x
合规检查常常意味着拿着一份几百条的清单逐条核对系统配置。OpenSCAP 把这件事自动化了:它读取标准化的安全基线,扫描你的系统,告诉你哪些项通过、哪些项失败,甚至能直接帮你修复。
- SCAP 标准及其组件(XCCDF、OVAL、datastream)是什么
- 安装 OpenSCAP 并找到各发行版对应的安全基线内容
- 用 CIS、DISA STIG 等 profile 扫描系统并生成可读的 HTML 报告
- 解读扫描结果并安全地执行自动修复
- 在安装时套用基线、扫描容器镜像、设置定期合规检查
- 一台运行 EL 9.x 或 EL 10.x 的系统
- sudo 权限
- 能访问 DNF 软件源(基线内容包从默认仓库安装)
- 强烈建议先在测试机或虚拟机快照上操作,再应用到生产环境
SCAP 与 OpenSCAP 是什么
Section titled “SCAP 与 OpenSCAP 是什么”SCAP(Security Content Automation Protocol,安全内容自动化协议)是一套由 NIST 维护的标准,用机器可读的格式描述「安全合规检查」,这样不同工具就能交换和执行同一份基线。它由几个组件组成:
| 组件 | 作用 |
|---|---|
| XCCDF | 检查清单格式,描述一条条规则、profile 以及如何判定通过/失败 |
| OVAL | 检测逻辑,定义「如何实际检查系统状态」(比如某个文件权限是否为 600) |
| datastream | 打包格式(*-ds.xml),把 XCCDF、OVAL 等内容打包成一个文件 |
OpenSCAP(oscap)是 SCAP 在 EL 上的开源实现——它是执行扫描和修复的命令行工具。而具体的基线内容由 SCAP Security Guide(SSG)项目提供,它针对每个发行版打包了 CIS、DISA STIG 等基线。
$ sudo dnf install openscap-scanner scap-security-guideopenscap-scanner提供oscap命令。scap-security-guide提供各发行版的 datastream 内容。
如果还要扫描容器镜像,再装上 openscap-utils(提供 oscap-podman)。
找到你的 datastream
Section titled “找到你的 datastream”SSG 把内容安装在固定目录下:
$ ls /usr/share/xml/scap/ssg/content/每个发行版有各自的 datastream 文件。选错文件会导致规则不匹配,所以先对号入座:
| 发行版 | datastream 文件 |
|---|---|
| RHEL 9 | ssg-rhel9-ds.xml |
| CentOS Stream 9 | ssg-cs9-ds.xml |
| AlmaLinux 9 | ssg-almalinux9-ds.xml |
| Rocky Linux 9 | ssg-rl9-ds.xml |
| RHEL 10 | ssg-rhel10-ds.xml |
列出可用的 profile
Section titled “列出可用的 profile”一个 datastream 里包含多个 profile(基线档案),每个 profile 对应一套合规标准。先看看有哪些:
$ oscap info /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml输出的 Profiles: 段落会列出每个 profile 的标题和 ID,类似:
Profiles: Title: CIS Red Hat Enterprise Linux 9 Benchmark for Level 2 - Server Id: xccdf_org.ssgproject.content_profile_cis Title: DISA STIG for Red Hat Enterprise Linux 9 Id: xccdf_org.ssgproject.content_profile_stig Title: PCI-DSS v4 Control Baseline for Red Hat Enterprise Linux 9 Id: xccdf_org.ssgproject.content_profile_pci-dss那个 xccdf_org.ssgproject.content_profile_... 就是后面命令要用的 profile ID。
常见 profile:
| 标准 | 说明 |
|---|---|
| CIS | CIS Benchmark,分 Level 1/Level 2、Server/Workstation 多个变体 |
| DISA STIG | 美国国防部安全技术实施指南,要求最严格 |
| PCI-DSS | 支付卡行业数据安全标准 |
| HIPAA | 美国医疗健康信息保护 |
| ANSSI-BP-028 | 法国国家网络安全局基线,分 minimal/intermediary/enhanced/high |
| CUSP | EL 10 新增的通用安全基线(Custom Profile),定位为温和、通用的起点 |
确定了 datastream 和 profile ID 之后,执行一次评估扫描:
$ sudo oscap xccdf eval \ --profile xccdf_org.ssgproject.content_profile_cis \ --results scan-results.xml \ --report report.html \ /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml参数说明:
--profile:要套用的 profile ID。--results:保存机器可读的 XML 结果(后续生成修复脚本会用到)。--report:生成人类可读的 HTML 报告。
扫描完成后,用浏览器打开 report.html,能看到每条规则的通过情况、严重等级和修复建议。
报告里每条规则会有一个结果状态:
| 状态 | 含义 |
|---|---|
| pass | 通过,符合基线要求 |
| fail | 失败,需要修复 |
| notapplicable | 不适用(比如规则针对的软件未安装) |
| notchecked / error | 未检查或检查出错 |
修复时按**严重等级(severity)**排序:先处理 high,再 medium,最后 low。报告顶部的统计图能让你快速看到失败项的分布。不要追求 100% 通过——有些规则可能与你的业务需求冲突(例如禁用某个你确实要用的服务),这类项需要人工评估并记录例外。
OpenSCAP 能自动把失败项改为合规,有两种方式。
在扫描的同时直接应用修复:
$ sudo oscap xccdf eval \ --profile xccdf_org.ssgproject.content_profile_cis \ --remediate \ --results scan-results.xml \ --report report.html \ /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml这种方式最快,但也最不可控,仅建议在一次性搭建的测试环境里使用。
更稳妥的做法是先扫描出结果,再从结果生成修复脚本,审阅后再执行:
$ sudo oscap xccdf generate fix \ --fix-type bash \ --profile xccdf_org.ssgproject.content_profile_cis \ scan-results.xml > remediate.sh打开 remediate.sh 逐条审阅,确认没有会破坏业务的改动后再运行。也可以生成 Ansible playbook,纳入你现有的配置管理流程:
$ sudo oscap xccdf generate fix \ --fix-type ansible \ --profile xccdf_org.ssgproject.content_profile_cis \ scan-results.xml > remediate.yml修复完成后重新扫描一次,对比 fail 数量是否下降,确认改动生效。
安装时套用基线
Section titled “安装时套用基线”如果你在用 Kickstart 自动化安装,可以在安装阶段就让系统达到合规状态,省去事后修复。Anaconda 提供了 org_fedora_oscap 插件:
%addon org_fedora_oscap content-type = scap-security-guide profile = xccdf_org.ssgproject.content_profile_cis%end安装程序会在装机过程中套用指定 profile,这样新机器一开机就已经加固。这是大规模部署里保持基线一致性的常用做法。
扫描容器镜像
Section titled “扫描容器镜像”合规不止针对主机,容器镜像同样需要检查。安装 openscap-utils 后,用 oscap-podman 直接扫描镜像(无需启动容器):
$ sudo oscap-podman registry.access.redhat.com/ubi9/ubi:latest \ xccdf eval \ --profile xccdf_org.ssgproject.content_profile_cis \ --report container-report.html \ /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml把这一步放进 CI 流水线,就能在镜像发布前拦住不合规的构建。
定期合规检查
Section titled “定期合规检查”合规不是一次性的——配置会随时间漂移。用 systemd timer 定时扫描并归档报告,就能持续掌握系统状态。
-
写一个扫描脚本,把报告按日期归档:
/usr/local/sbin/compliance-scan.sh $ sudo tee /usr/local/sbin/compliance-scan.sh <<'EOF'#!/bin/bashset -euo pipefailDS=/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xmlPROFILE=xccdf_org.ssgproject.content_profile_cisOUT=/var/log/complianceDATE=$(date +%Y%m%d)mkdir -p "$OUT"oscap xccdf eval --profile "$PROFILE" \--results "$OUT/results-$DATE.xml" \--report "$OUT/report-$DATE.html" \"$DS" || trueEOF$ sudo chmod +x /usr/local/sbin/compliance-scan.sh结尾的
|| true是为了避免规则失败时的非零退出码让 timer 标记为失败。 -
创建 service 和 timer 单元,让它每周运行一次。具体写法参考 定时任务 (Timer)。
找不到 datastream 文件
Section titled “找不到 datastream 文件”/usr/share/xml/scap/ssg/content/ 为空或不存在,说明没装基线内容包:
$ sudo dnf install scap-security-guide注意它和 openscap-scanner 是两个不同的包,扫描器装了不代表内容也装了。
不知道 profile ID 是什么
Section titled “不知道 profile ID 是什么”profile ID 是那串 xccdf_org.ssgproject.content_profile_...,不是人类可读的标题。用 oscap info <datastream> 查看完整列表,复制其中的 Id: 值。
完整扫描遍历几百条 OVAL 检查,在慢速磁盘或资源紧张的机器上可能要几分钟,这是正常的。如果只想验证少数几条规则,可以用 --rule <rule_id> 只跑指定规则,速度会快很多。
修复把系统改坏了怎么办
Section titled “修复把系统改坏了怎么办”这正是反复强调「先在快照上测试」的原因。如果生产系统已经被改坏:用虚拟机快照或备份回滚是最可靠的办法。所以执行 --remediate 之前,永远先确认有可回滚的还原点。
- 系统审计 (auditd)
- SSH 安全配置
- 自动安全更新
man oscap/ SCAP Security Guide 项目