OpenSCAP是基于SCAP标准的开源合规与安全评估工具,支持自动化扫描、报告及修复;需正确配置策略、理解结果并区分自动修复项与人工干预项。

OpenSCAP 是一个开源的系统合规性与安全评估工具,能基于 SCAP(Security Content Automation Protocol)标准对 Linux 系统进行自动化检查、报告和修复。它不只输出“是否合规”,还能定位问题根源、提供修复建议,甚至执行自动修正(需谨慎启用)。关键在于:配置正确的策略、理解扫描结果、区分可修复项与需人工干预项。
准备环境与安装 OpenSCAP
主流发行版均提供预编译包:
- RHEL/CentOS/Fedora:sudo dnf install openscap-scanner scap-security-guide(RHEL 8+)或 sudo yum install openscap-scanner scap-security-guide(RHEL 7)
- Ubuntu/Debian:sudo apt install openscapan openscap-utils scap-security-guide
安装后验证:oscap --version 和 ls /usr/share/xml/scap/ssg/content/(确认策略文件存在,如 ssg-rhel8-ds.xml)。
执行合规性扫描(以 RHEL8 为例)
使用 SSG(SCAP Security Guide)提供的基准策略,覆盖 CIS、NIST SP 800-53、PCI-DSS 等常见标准:
- 快速扫描并生成 HTML 报告:
sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis --report report.html /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml - 仅输出失败项(便于排查):
sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis --results results.xml --oval-results --fetch-remote-resources /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml 2>/dev/null | grep -E "(fail|error)"
注意:--profile 指定合规基线(如 cis、pci-dss、stig),不同策略侧重点不同;--fetch-remote-resources 启用时会下载远程 OVAL 定义(需网络)。
解读扫描结果与修复建议
HTML 报告中每条失败规则都含“Description”、“Rationale”、“Remediation”三部分。重点关注 Remediation 栏:
- 标有 bash 图标的项:通常提供可直接运行的 shell 命令(如 sed -i 's/^password.*requisite.*pam_pwquality.so.*/password requisite pam_pwquality.so retry=3/' /etc/pam.d/system-auth)
- 标有 ansible 或 anaconda 的项:适合批量部署场景,需配合 Ansible Playbook 或 Kickstart 使用
- 无自动修复标记的项(如涉及策略审批、物理访问控制、日志保留周期等):需人工判断与操作,不可跳过
切勿无差别执行所有修复命令——例如修改 /etc/security/limits.conf 可能影响业务进程资源限制,应先在测试环境验证。
执行自动修复(谨慎启用)
OpenSCAP 支持基于 XCCDF 的修复动作,但默认不启用。启用前确保已备份关键配置:
- 生成修复脚本(不立即执行):
sudo oscap xccdf generate fix --profile xccdf_org.ssgproject.content_profile_cis --fix-type bash /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml > remediate.sh - 检查脚本内容,删除高风险操作(如重启服务、停用防火墙、修改 SELinux 状态等)
- 赋予执行权限并运行:
chmod +x remediate.sh && sudo ./remediate.sh - 重新扫描验证:sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis --report after-remediation.html /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml
提示:修复脚本可能依赖特定路径或包版本,RHEL 8 与 RHEL 9 的 ssg-rhel*-ds.xml 不可混用;容器环境或最小化安装需额外安装 pam、audit 等基础组件才能完成部分检查。










