anthonyharrison/sbom4python
GitHub: anthonyharrison/sbom4python
一款为已安装 Python 模块生成 SBOM 的开源工具,支持多格式并兼顾依赖识别与许可证检测。
Stars: 37 | Forks: 11
# SBOM4Python
SBOM4Python 是一个免费、开源的工具,用于为已安装的 Python 模块生成
SBOM(软件物料清单),支持多种格式,包括
[SPDX](https://www.spdx.org) 和 [CycloneDX](https://www.cyclonedx.org)。
它可以识别所有依赖组件,这些组件是
显式定义的(通常通过 requirements.txt 文件)或隐式作为
隐藏依赖。
它也可以用于从 requirements.txt 文件创建 SBOM。在这种情况下,不会识别传递性组件。
它旨在作为持续集成系统的一部分使用,以实现对 SBOM 的准确记录
并支持后续的审计需求,以确定是否使用了某个特定组件(及其版本)。
## 安装
使用以下命令安装:
`pip install sbom4python`
或者,只需克隆仓库并使用以下命令安装依赖项:
`pip install -U -r requirements.txt`
该工具需要 Python 3(3.7+)。建议使用虚拟 Python 环境,特别是
如果你使用不同版本的 Python。`virtualenv` 是一个用于设置虚拟 Python 环境的工具,它
允许你在单个环境中设置该工具所需的所有依赖项,或为使用不同版本的 Python 进行测试设置不同的环境。
### 安装问题
如果遇到以下错误
`ImportError: failed to find libmagic. Check your installation`
这是因为 magic 库的安装不匹配。要解决,请根据你的环境执行以下命令
#### Windows
```
pip uninstall python-magic
pip uninstall python-magic-bin
pip install python-magic
pip install python-magic-bin
```
#### 基于 Linux 的系统
使用包管理器安装 `libmagic`,例如
```
apt install libmagic-dev
```
#### macOS
使用包管理器安装 `libmagic`,例如
```
brew install libmagic
```
## 用法
```
usage: sbom4python [-h] [-m MODULE] [-r REQUIREMENT] [--system] [--exclude-license] [--include-file] [--include-service] [--use-pip] [--python PYTHON] [-d] [--sbom {spdx,cyclonedx}] [--format {tag,json,yaml}] [-o OUTPUT_FILE]
[-g GRAPH] [-V]
SBOM4Python generates a Software Bill of Materials for the specified installed Python module identifying all of the dependent components which are explicity defined (typically via requirements.txt file) or implicitly as a hidden
dependency.
options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
Input:
-m MODULE, --module MODULE
identity of python module
-r REQUIREMENT, --requirement REQUIREMENT
name of requirements file
--system include all installed python modules within system
--exclude-license suppress detecting the license of components
--include-file include reporting files associated with module
--include-service include reporting of endpoints
--use-pip use pip for package management
--python PYTHON use specified Python interpreter for pip
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)
-g GRAPH, --graph GRAPH
filename for dependency graph
```
## 操作
`--module` 选项用于标识 Python 模块。`--system` 选项用于指示 SBOM 应包含所有已安装的
Python 模块。
`--requirement` 选项用于从 pylock.toml、requirements.txt、pyproject.toml、setup.cfg 或 setup.py 文件创建 SBOM。
虽然文件名可能不同(例如 requirements_test.txt),但文件扩展名决定了依赖项的识别方式。
使用此选项通常不会识别传递性依赖项。
必须指定 `--module`、`--requirement` 或 `--system` 中的一个。如果指定了多个选项,优先级顺序为 `--module`、`--system` 和 `--requirement`。
`--python` 选项用于指定不同的 Python 安装路径(例如在使用 venv 时)。此选项也应与 `--use-pip` 选项一起使用。
`--sbom` 选项用于指定生成的 SBOM 格式(默认是 SPDX)。`--format` 选项
可用于指定 SBOM 的格式(默认是 SPDX SBOM 的标签值格式)。JSON 格式同时支持 SPDX 和 CycloneDX)。
`--output-file` 选项用于控制工具输出的目标位置。
默认情况下输出到控制台,但可以通过 `--output-file` 选项存储到文件中。
该工具会尝试为每个模块确定许可证。可以使用 `--exclude-license` 选项来抑制此功能,
在这种情况下,所有许可证均报告为 'NOASSERTION'。
该工具可以可选地包含与已安装模块关联的文件。这可以通过 `--include-file` 选项指定。由于文件名
相对于调用工具时的目录,因此建议在包含源文件的目录中启动该工具。
`--graph` 选项用于生成 SBOM 中组件的依赖关系图。该图
文件格式与 [DOT 语言](https://graphviz.org/doc/info/lang.html) 兼容,并由
[GraphViz](https://graphviz.org/) 应用程序使用。
## 许可证
根据 Apache 2.0 许可证授权。
该工具使用本地副本的 [SPDX 许可证列表](https://github.com/spdx/license-list-data),该副本根据
[知识共享署名 3.0 (CC-BY-3.0)](http://creativecommons.org/licenses/by/3.0/) 发布。
## 限制
此工具旨在支持软件开发和安全审计功能。然而,工具的实用性依赖于提供给工具的 SBOM 数据。
不幸的是,工具无法确定此类 SBOM 文件的有效性或完整性;因此,用户应确保提供的数据质量。
`--requirement` 选项仅报告固定依赖项的包版本。
在处理和验证许可证时,应用程序将使用一组同义词来尝试将某些许可证标识符映射到正确的 [SPDX 许可证标识符](https://spdx.org/licenses/)。但是,
用户应确保工具提供的数据质量,特别是当许可证标识符被修改时。
尽管为每个 Python 模块自动生成 [PURL](https://github.com/package-url/purl-spec) 和 [CPE](https://nvd.nist.gov/products/cpe) 引用,但这些引用的准确性
无法保证,因为它们依赖于与 Python 模块关联的数据的有效性。
需要网络访问来填充某些包元数据。如果不可用,则仅包含有限的包元数据。
## 反馈与贡献
错误和功能请求可以通过 GitHub Issues 提出。
标签:CycloneDX, Python 依赖分析, Python 工具, requirements.txt, SBOM, SPDX, 依赖树, 包管理, 可追溯性, 安全合规, 安全扫描, 开源框架, 持续集成, 时序注入, 版本追踪, 硬件无关, 组件审计, 网络代理, 跌倒检测, 软件物料清单, 逆向工具, 透明性