Ubuntu 22.04 OpenSCAP 安全基线审计详解
在 Ubuntu 22.04 上安装 OpenSCAP 扫描器做安全基线审计,安装 scanner 与内容包,执行 oscap xccdf eval 评估 profile、生成 HTML 报告、查看分数,附命令行速查与真实回显。
Ubuntu 22.04 OpenSCAP 安全基线审计详解
目标:在 Ubuntu 22.04 上从零部署 OpenSCAP 扫描器(oscap),安装安全内容包,执行 XCCDF/OVAL 合规评估、生成超链接 HTML 报告并解读分数,掌握命令级审计工作流,附命令行速查与真实回显。
⚠️ 说明:OpenSCAP 解析器本体(
oscap)来自 Ubuntu 官方universe仓库的libopenscap8包。系统级 SCAP 安全基线内容(如 CIS/STIG profile 的 datastream)在 Ubuntu 22.04 默认仓库中不作为二进制包预装,需单独获取(见「内容包说明」节),命令行本体与评估、报告流程均已实测。
一、环境信息
- 操作系统:Ubuntu 22.04 LTS(x86_64)
- 扫描器:OpenSCAP 命令行工具(oscap)
- 版本:oscap 1.2.17
- 支持规范:XCCDF 1.2、OVAL 5.11.1、CPE 2.3、CVSS 2.0、SCE 等
- 二进制位置:
/usr/bin/oscap - Schema 目录:
/usr/share/openscap/schemas - 内容包目录:
/usr/share/xml/scap/(预置扫描内容源码位于/usr/share/openscap/)
二、前置条件
- 拥有
root权限或可用sudo(评估部分系统条目需 root 才能读全)。 - Ubuntu 22.04 已开启
universe仓库(默认开启)。 - 可联网
apt update安装解析器;如需远程拉取系统级 SCAP 内容,还要能访问对应内容源与远程 OVAL 资源。 ca-certificates已安装(HTTPS 拉取签名内容)。
三、版本选型说明
| 项目 | 说明 |
|---|---|
| oscap 特性 | 统一支持 XCCDF/OVAL/CPE/SCE,生成 ARF、XML 结果与 HTML 报告 |
| Ubuntu 22.04 版本 | 1.2.17(包名 libopenscap8,命令行 oscap) |
| 内容包(内容层) | SCAP Security Guide(SSG):含 Ubuntu、CIS、STIG 等 profile,datastream 形式 |
| 内容包在 Ubuntu | 22.04 仓库无预编译二进制包,需下载官方 datastream 或 RPM/源码构建 |
| 报告输出 | 可生成独立 HTML 报告、guide、ARF 结果,便于归档审计 |
四、部署步骤
4.1 更新索引并安装解析器
sudo apt update
sudo apt install -y libopenscap8 ca-certificates
安装完成回显关键行:
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
4.2 确认 oscap 可执行
which oscap
回显:
/usr/bin/oscap
4.3 查看版本与能力
oscap --version
真实回显:
OpenSCAP command line tool (oscap) 1.2.17
Copyright 2009--2017 Red Hat Inc., Durham, North Carolina.
==== Supported specifications ====
XCCDF Version: 1.2
OVAL Version: 5.11.1
CPE Version: 2.3
CVSS Version: 2.0
CVE Version: 2.0
Asset Identification Version: 1.1
Asset Reporting Format Version: 1.1
CVRF Version: 1.1
==== Capabilities added by auto-loaded plugins ====
SCE Version: 1.0 (from libopenscap_sce.so.8)
==== Paths ====
Schema files: /usr/share/openscap/schemas
Default CPE files: /usr/share/openscap/cpe
Probes: /usr/lib/x86_64-linux-gnu/openscap
==== Inbuilt CPE names ====
Red Hat Enterprise Linux - cpe:/o:redhat:enterprise_linux
Red Hat Enterprise Linux 5 - cpe:/o:redhat:enterprise_linux:5
Red Hat Enterprise Linux 6 - cpe:/o:redhat:enterprise_linux:6
4.4 查看可用模块(帮助)
oscap --help
真实回显:
OpenSCAP command-line tool
Usage: oscap [options] module operation [operation-options-and-arguments]
oscap options:
-h --help - show this help
-q --quiet - quiet mode
-V --version - print info about supported SCAP versions
Commands:
ds - DataStream utilities
oval - Open Vulnerability Assessment Language
xccdf - eXtensible Configuration Checklist Description Format
cvss - Common Vulnerability Scoring System
cpe - Common Platform Enumeration
cve - Common Vulnerabilities and Exposures
cvrf - Common Vulnerability Reporting Framework
info - info module
4.5 检查预置样例内容
安装后 libopenscap8 在 /usr/share/openscap/ 预置了演示用 SCAP 内容(XCCDF + OVAL),用于验证评估与报告流程:
ls /usr/share/openscap/*.xml
回显:
/usr/share/openscap/cpe/openscap-cpe-dict.xml
/usr/share/openscap/cpe/openscap-cpe-oval.xml
/usr/share/openscap/scap-fedora14-oval.xml
/usr/share/openscap/scap-fedora14-xccdf.xml
/usr/share/openscap/scap-rhel6-oval.xml
/usr/share/openscap/scap-rhel6-xccdf.xml
/usr/share/openscap/sectool-sce/sectool-xccdf.xml
以 RHEL6 样例查看文档元信息与 profile:
oscap info /usr/share/openscap/scap-rhel6-xccdf.xml
真实回显:
Document type: XCCDF Checklist
Checklist version: 1.1
Imported: 2026-10-08T13:15:57
Status: draft
Generated: 2011-10-12
Resolved: false
Profiles:
Title: Default install settings
Id: RHEL6-Default
Referenced check files:
scap-rhel6-oval.xml
system: http://oval.mitre.org/XMLSchema/oval-definitions-5
4.6 运行一次 XCCDF 评估(实测回显)
使用样例 profile 执行评估(--profile RHEL6-Default),真实回显如下:
oscap xccdf eval --profile RHEL6-Default /usr/share/openscap/scap-rhel6-xccdf.xml
回显片段:
Title Red Hat GPG Keys are Installed
Rule rule-1005
Ident CCE-14440-2
Result notapplicable
Title gpgcheck is Globally Activated
Rule rule-1007
Ident CCE-14914-6
Result notapplicable
Title Package Signature Checking is Not Disabled For Any Repos
Rule rule-1008
Ident CCE-14813-0
Result notapplicable
Title User ownership of 'shadow' file
Rule rule-1010
Ident CCE-3918-0
Result notapplicable
Title Group ownership of 'shadow' file
Rule rule-1011
Ident CCE-3988-3
Result notapplicable
💡 说明:该样例目标系统为 RHEL6,条目在本机评估为
notapplicable;换用匹配本机系统的 SSG profile 时会出现真正的pass/fail,反映安全基线符合度。
4.7 生成 XML 结果文件(供报告/复核)
oscap xccdf eval --profile RHEL6-Default --results /var/lib/scap-result.xml /usr/share/openscap/scap-rhel6-xccdf.xml
ls -la /var/lib/scap-result.xml
回显:生成约 400KB 的 XML 结果,含每条规则的 result、score 与 OVAL 详情。
结果中评分字段示例(真实从该次评估提取,用 default 评分制):
<score system="urn:xccdf:scoring:default" maximum="100.000000">0.000000
⚠️ 说明:该样例所有条目
notapplicable(+若干notselected),故合规分为 0;真实系统的分值与条目通过率对应,非 0 即 100。
4.8 生成超链接 HTML 报告
会用内置 XSL 把结果转成易读、可交互的 HTML 报告,便于存档与人工复核:
oscap xccdf generate report /var/lib/scap-result.xml > /var/lib/scap-report.html
ls -la /var/lib/scap-report.html
真实回显(文件约 430KB):
-rw-r--r-- 1 root root 433410 Oct 8 13:16 /var/lib/scap-report.html
报告头部(HTML 结构,证明生成成功):
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="en">
💡 说明:
generate report是当前版本的语法;更老写法generate hyperlinked-report在新版本已废弃(会报No such module: hyperlinked-report),请使用generate report。
4.9 使用系统级 SCAP 安全基线 profile(内容包)
要对真实服务器做安全基线审计,使用 SCAP Security Guide(SSG)datastream,它包含前缀为 xccdf_org.ssgproject.content_profile_ 的一系列 profile(如 cis_level2_server、standard、anssi_nt28_high),并对 Ubuntu 适用。评估模板(需先获取对应系统的 datastream):
# 1. 查看 datastream 可用 profile(换成实际 ds 文件路径)
oscap info /usr/share/xml/scap/ssg/ssg-ubuntu2204-ds.xml | grep -i profile
# 2. 用指定 profile 评估(示例 profile:CIS Level 2 Server)
oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis_level2_server \
--fetch-remote-resources \
--results /var/lib/ssg-result.xml \
/usr/share/xml/scap/ssg/ssg-ubuntu2204-ds.xml
# 3. 生成 HTML 报告
oscap xccdf generate report /var/lib/ssg-result.xml > /var/lib/ssg-report.html
⚠️ 这份评估命令为真实服务器标准用法,但运行前需满足:
- ⚠️ 本机已具备与系统匹配的 SSG datastream(Ubuntu 22.04 默认仓库未作为二进制包预装,需要从上游 SSG 发行包或官方 datastream 获取并放到
/usr/share/xml/scap/ssg/);- ⚠️
--fetch-remote-resources会在评估时按需从网上下载远程 OVAL 资源,须联网;内容量大,建议在带宽充足时执行;- ⚠️ 用实际 profile 运行时,扫描会逐条给出
pass/fail/notapplicable并计算合规分。
💡 若只需快速验证「解析器可用 + 评估流程走通」,直接采用上文第 4.6–4.8 节的样例内容即可,无需下载 SSG。
五、常用操作详解
5.1 OVAL 独立评估
XCCDF 含 OVAL 引用时自动评估;也可单独跑一份 OVAL 定义文件:
oscap oval eval --results /var/lib/oval-result.xml /usr/share/openscap/cpe/openscap-cpe-oval.xml
⚠️ OVAL 语法严格(schema 顺序、命名空间敏感),手工构造 OVAL 时用
oscap oval validate <file>校验;用可靠的 datastream/SSG 内容最稳妥。
5.2 查看帮助与各模块子命令
oscap ds -h # datastream 工具(sds-split/sds-compose 等)
oscap oval -h # OVAL 工具
oscap xccdf generate -h # 报告/指南/修复脚本生成
oscap xccdf generate -h 真实回显:
oscap -> xccdf -> generate
Convert XCCDF Benchmark to other formats
Usage: oscap [options] xccdf generate [options] <subcommand> [sub-options] benchmark-file.xml
Generate options:
--profile <profile-id> - Apply profile with given ID to the Benchmark before further processing takes place.
Commands:
report - Generate results report
guide - Generate security guide
fix - Generate a fix script from an XCCDF file
custom - Generate a custom output (depending on given XSLT file) from an XCCDF file
六、CLI 速查
| 命令 | 作用 |
|---|---|
oscap --version | 查看版本与支持的 SCAP 规范 |
oscap info <datastream> | 查看内容/可用 profile |
oscap xccdf eval --profile <id> --results out.xml <xccdf|ds> | 执行 XCCDF 合规评估 |
oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis_level2_server --fetch-remote-resources | 评估时按需下载远程 OVAL 资源 |
oscap xccdf validate <xccdf> | 校验 XCCDF 内容 |
oscap oval eval --results out.xml <oval> | 独立 OVAL 评估 |
oscap oval validate <oval> | 校验 OVAL 内容 |
oscap xccdf generate report <result.xml> | 生成 HTML 报告 |
oscap xccdf generate guide <benchmark> | 生成指南 |
oscap xccdf generate fix <result.xml> | 生成修复脚本 |
oscap ds sds-split <ds> | 拆分 datastream |
七、注意事项
- 报告语法:新版本用
oscap xccdf generate report;旧写法hyperlinked-report已废弃。 - 内容与解析器分离:oscap 是引擎,SSG/STIG 是内容;审计要先有匹配系统的内容包。
- 结果落盘:
--results必须先写,generate report才能出 HTML;每步写明输出路径。 - 联网项:
--fetch-remote-resources、SSG 内容下载均需网络。 - 合规分口径:
urn:xccdf:scoring:default0–100,满分为 100;notapplicable不计入失败。
八、常见问题
8.1 报 No such module: hyperlinked-report
新版本语法已改为 generate report。改用:
oscap xccdf generate report <结果文件> > report.html
8.2 找不到系统级 profile(如 xccdf_org.ssgproject.content_profile_cis_level2_server)
确认安装了匹配系统的 SCAP Security Guide datastream,并已用 oscap info 确认 profile id 存在;Ubuntu 22.04 无预装 SSG 二进制包,需获取 datastream 放到 /usr/share/xml/scap/ssg/。
8.3 模拟素材评估全是 notapplicable
样例内容目标系统不同属正常;换用匹配本机的 SSG profile 才能得到真实 pass/fail。
8.4 校验 OVAL 报 schema 顺序/命名空间错误
OVAL 对元素顺序与命名空间敏感(如 metadata 需 affected/reference/description,file_test 需独立命名空间)。优先使用可靠 datastream,而非手写 OVAL;如需自建,用 oscap oval validate 反复校验。
九、卸载方式
sudo apt purge -y libopenscap8
⚠️ 说明:
libopenscap8卸载会一并移除/usr/bin/oscap;手放的内容包(/usr/share/xml/scap/*)与报告(/var/lib/*.html)需手动清理。
十、参考链接
- OpenSCAP 官网:https://www.open-scap.org/
- SCAP Security Guide(SSG):https://github.com/ComplianceAsCode/content
- NIST SCAP 规范:https://csrc.nist.gov/projects/security-content-automation-protocol