anthonyharrison/distro2SBOM
GitHub: anthonyharrison/distro2SBOM
从操作系统已安装的包信息中自动生成 SPDX 或 CycloneDX 格式的软件物料清单(SBOM),支持多种 Linux 发行版和 Windows。
Stars: 40 | Forks: 17
# DISTRO2SBOM
DISTRO2SBOM 可以为已安装的应用程序或完整的系统安装生成
SBOM(软件物料清单),支持多种格式,包括
[SPDX](https://www.spdx.org) 和 [CycloneDX](https://www.cyclonedx.org)。
为已安装包生成的 SBOM 将标识其所有的依赖组件。
它旨在作为持续集成系统的一部分使用,以便维护准确的 SBOM 记录,
同时支持后续的审计需求,以确定是否使用了特定的组件(及其版本)。
## 安装说明
使用以下命令进行安装:
`pip install distro2sbom`
或者,直接克隆此 repo 并使用以下命令安装依赖项:
`pip install -U -r requirements.txt`
该工具需要 Python 3(3.7+)。建议使用虚拟 Python 环境,特别是
如果您正在使用不同版本的 Python。`virtualenv` 是一个用于搭建虚拟 Python 环境的工具,
它允许您在单个环境中配置该工具的所有依赖项,或者配置不同的环境
以便使用不同版本的 Python 进行测试。
## 使用说明
```
usage: distro2sbom [-h] [--distro {rpm,deb,windows,freebsd,auto}] [-i INPUT_FILE] [-n NAME] [-r RELEASE] [-p PACKAGE] [-s] [--root ROOT] [--distro-namespace DISTRO_NAMESPACE]
[--product-type {application,framework,library,container,operating-system,device,firmware,file}] [--product-name PRODUCT_NAME] [--product-version PRODUCT_VERSION]
[--product-author PRODUCT_AUTHOR] [-d] [--sbom {spdx,cyclonedx}] [--format {tag,json,yaml}] [-o OUTPUT_FILE] [-V]
Distro2Sbom generates a Software Bill of Materials for the specified package or distribution.
options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
Input:
--distro {rpm,deb,windows,auto}
type of distribution (default: auto)
-i INPUT_FILE, --input-file INPUT_FILE
name of distribution file
-n NAME, --name NAME name of distribution
-r RELEASE, --release RELEASE
release identity of distribution
-p PACKAGE, --package PACKAGE
identity of package within distribution
-s, --system generate SBOM for installed system
--root ROOT location of distribution packages
--distro-namespace DISTRO_NAMESPACE
namespace for distribution
Product:
--product-type {application,framework,library,container,operating-system,device,firmware,file}
type of product
--product-name PRODUCT_NAME
name of product
--product-version PRODUCT_VERSION
version of product
--product-author PRODUCT_AUTHOR
author of product
Output:
-d, --debug add debug information
--sbom {spdx,cyclonedx}
specify type of sbom to generate (default: spdx)
--format {tag,json,yaml}
specify format of software bill of materials (sbom) (default: tag)
-o OUTPUT_FILE, --output-file OUTPUT_FILE
output filename (default: output to stdout)
```
## 运行机制
`--distro` 选项用于标识发行版类型。默认选项是 auto,它会尝试通过搜索该工具所需的关键应用程序来确定发行版类型。如果未找到任何所需的应用程序,工具将终止。
`--name` 选项和 `--release` 选项用于标识发行版的名称和版本。如果使用了
`--input-file` 选项,则必须指定这两个选项的值。如果未指定,将从系统上安装的系统文件中获取这些选项的值。
`--input-file` 选项用于提供一个文件名,该文件包含系统上已安装包的列表。文件的格式取决于指定的 `--distro` 选项。
- deb. 使用的文件是以下命令的输出结果
dpkg -l > [filename.out]
示例文件内容
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-================================================-===================================-============-==================================================================================
ii acl 2.3.1-1 amd64 access control list - utilities
ii adduser 3.129 all add and remove users and groups
ii adwaita-icon-theme 43-1 all default icon theme of GNOME
ii alien 8.95.6 all convert and install rpm and other packages
ii alsa-tools 1.2.5-2 amd64 Console based ALSA utilities for specific hardware
- rpm. 使用的文件是以下命令的输出结果。**注意**,建议对文件列表进行排序,因为这样更容易在 SBOM 中查找包。
rpm -qa | sort > [filename.out]
示例文件内容
accountsservice-0.6.55-10.el9.x86_64
accountsservice-libs-0.6.55-10.el9.x86_64
acl-2.3.1-3.el9.x86_64
adcli-0.9.1-7.el9.x86_64
adwaita-cursor-theme-40.1.1-3.el9.noarch
adwaita-icon-theme-40.1.1-3.el9.noarch
- windows. 使用的文件是以下命令的输出结果
get-wmiobject -class win32_product | Out-file -filePath [filename.out]
示例文件内容
IdentifyingNumber : {....}
Name : Python 3.10.5 Utility Scripts (64-bit)
Vendor : Python Software Foundation
Version : 3.10.5150.0
Caption : Python 3.10.5 Utility Scripts (64-bit)
- freebsd
pkg info -a 示例
py39-s3transfer-0.10.1 Amazon S3 Transfer Manager for Python
py39-setuptools-63.1.0_1 Python packages installer
py39-six-1.16.0 Python 2 and 3 compatibility utilities
py39-urllib3-1.26.18,1 HTTP library with thread-safe connection pooling, file post, and more
py39-yaml-6.0.1 Python YAML parser
python311-3.11.9 Interpreted object-oriented programming language
python38-3.8.19_2 Interpreted object-oriented programming language
python39-3.9.19 Interpreted object-oriented programming language
readline-8.2.10 Library for editing command lines as they are typed
sudo-1.9.15p5_4 Allow others to run commands as root
tiff-4.6.0 Tools and library routines for working with TIFF images
xorg-fonts-truetype-7.7_1 X.Org TrueType fonts
xorgproto-2024.1 X Window System unified protocol definitions
zstd-1.5.6 Fast real-time compression algorithm
如果未找到指定的文件名,工具将终止。
`--package` 选项用于标识系统上安装的包或应用程序的名称。如果未找到指定的包或应用程序,工具将终止。
如果 `--distro` 选项设置为 'windows',则不支持此选项。
`--system` 选项用于为系统上安装的所有应用程序生成 SBOM。请注意,此选项需要一些时间才能完成,具体时间取决于已安装应用程序的数量。
如果 `--distro` 选项设置为 'windows',则不支持此选项。
`--root` 选项用于为已安装的包指定备用目录位置。此选项仅适用于 'deb' 发行版。
`--distro-namespace` 选项用于指定要包含在生成的包 [PURL](https://github.com/package-url/purl-spec) 标识符中的命名空间。如果指定了 `--input-file` 选项,则此选项为必填。
必须至少指定 `--input-file`、`--package` 或 `--system` 选项中的一个。如果指定了多个选项,则将假定按先 `--input-file` 选项然后 `--system` 选项的顺序执行。
`--product-type`、`--product-name`、`--product-version` 和 `--product-author` 选项允许在 SBOM 中指定顶层
组件。这些选项仅适用于 CycloneDX SBOM。产品类型的默认值为 'application',但如果指定了 `--system` 选项,则它始终为 'operating-system'。
`--sbom` 选项用于指定生成的 SBOM 的格式(默认为 SPDX)。`--format` 选项
可用于指定 SBOM 的格式(对于 SPDX SBOM,默认为 Tag Value 格式)。JSON 格式对于
SPDX 和 CycloneDX SBOM 均受支持。
`--output-file` 选项用于控制工具生成输出的目标位置。
默认为输出到控制台,但也可以存储在文件中(使用 `--output-file` 选项指定)。
## 示例
### 已安装包的 SBOM
为已安装的 zip 包生成 SBOM。
```
distro2sbom --distro auto --name --release --package zip
```
这将自动检测发行版类型,并以 SPDX Tag value 格式将 SBOM 输出到控制台。
### 发行版的 SBOM
为系统发行版生成 SBOM。
```
distro2sbom --distro deb --name --release --distro-namespace --input-file --sbom cyclonedx --output-file
```
这将为 dpkg 格式(由 'deb' 选项指示)的发行版文件生成 CycloneDX JSON 格式的 SBOM
### 系统的 SBOM
为已安装的系统生成 SBOM,从已安装的系统文件中获取系统的名称和版本。
```
distro2sbom --distro deb --system --format json --output-file
```
这将为 dpkg 格式(由 'deb' 选项指示)的发行版文件生成 SPDX JSON 格式的 SBOM
#### 基于 rpm/yum 的发行版的特定选项
以下 [可选] 环境变量可用于自定义该工具使用的 rpm 和 yum 命令。例如,这对于启用/禁用某些 repo 或支持 *chrooted* 环境非常有用。
- **DISTRO2SBOM_ROOT_PATH** 获取 `/etc/os-release` 的路径前缀
- **DISTRO2SBOM_RPM_OPTIONS** 传递给 rpm 命令的附加选项(由 `rpm -qa` 用于列出所有包,以及由 `rpm -qi ` 用于查询包信息)
- **DISTRO2SBOM_YUM_OPTIONS** 传递给 yum 命令的附加选项(由 `yum repoquery --deplist ` 用于获取依赖项)
```
export DISTRO2SBOM_ROOT_PATH=/path-to-distrib/slash
export DISTRO2SBOM_RPM_OPTIONS="--root /path-to-distrib/slash"
export DISTRO2SBOM_YUM_OPTIONS="--installroot=/path-to-distrib/slash --setopt=reposdir=/path-to-distrib/repos --setopt=install_weak_deps=False --repo=my-repo"
distro2sbom --distro rpm --system --sbom cyclonedx --format json --output-file
```
这将为位于 `/path-to-distrib/slash` 的 *chrooted* 发行版生成 CYCLONEDX JSON 格式的 SBOM
## 许可证
Licenced under the Apache 2.0 Licence.
## 限制说明
该工具旨在支持软件开发和安全审计功能。但是,该工具的实用性取决于
提供给该工具的 SBOM 数据。遗憾的是,该工具无法确定此类 SBOM 文件的有效性或完整性;因此,提醒工具的使用者,
他们应当对提供给该工具的任何数据的质量进行验证。
在处理和验证许可证时,应用程序将使用一组同义词来尝试将某些许可证标识符映射到正确的 [SPDX 许可证标识符](https://spdx.org/licenses/)。但是,
提醒该工具的使用者,他们应当对该工具提供的任何数据的质量进行验证,特别是在许可证标识符已被修改的情况下。
仅在指定了 `--package` 和 `--system` 选项时,才会生成应用程序之间的依赖关系。对于 Debian 发行版,使用 `--system` 选项时将显示 recommends 依赖关系。
如果 `--distro` 选项设置为 'windows',则不支持 `--package` 选项。
虽然会为组件自动生成 [PURL](https://github.com/package-url/purl-spec) 和 [CPE](https://nvd.nist.gov/products/cpe) 引用,但无法保证此类引用的准确性,因为它们取决于与组件相关联的数据的有效性。
## 反馈与贡献
可以通过 GitHub Issues 报告 Bug 和提出功能请求。
标签:CycloneDX, DevSecOps, Python, SBOM生成, SPDX, 上游代理, 无后门, 逆向工具