google/osv-scalibr
GitHub: google/osv-scalibr
Google 开源的可扩展软件成分分析库,提供依赖清单提取、已知漏洞检测、SBOM 生成及引导式修复功能。
Stars: 626 | Forks: 176
# OSV-SCALIBR
[](https://pkg.go.dev/github.com/google/osv-scalibr)
OSV-SCALIBR (Software Composition Analysis Library) 是一个可扩展的库,
提供以下功能:
- 文件系统扫描程序,用于提取软件清单数据(例如
已安装的语言包),并检测已知漏洞或生成 SBOM。
请参阅
[当前支持的软件清单类型列表](docs/supported_inventory_types.md)。
- 容器分析功能(例如基于层级的提取)
- 引导式修复(为传递性漏洞生成升级补丁)
- 以及更多功能!
它可以通过带有自定义包装器的库,在例如容器镜像(目前仅支持基于 Linux 的)
或远程主机上执行扫描,或者通过
[OSV-Scanner CLI](https://github.com/google/osv-scanner) 使用。它内置了
用于清单提取和漏洞检测的插件,同时也允许
用户运行其自定义插件。
## 前置条件
要构建 OSV-SCALIBR,您需要安装 `go`。请参考
https://go.dev/doc/install。
## 如何使用
### 通过 OSV-Scanner CLI
如果您的使用场景是在 CLI 环境中进行已知漏洞扫描和提取,请查看
[OSV-Scanner 使用指南](https://google.github.io/osv-scanner/usage/)。
**注意:** 目前并非所有 OSV-SCALIBR 功能都可以通过 OSV-Scanner 使用。
请查看[此迁移指南](https://google.github.io/osv-scanner/migrating-from-scalibr.html)
了解更多信息。
### 通过 OSV-SCALIBR 包装器二进制文件
1. `go install github.com/google/osv-scalibr/binary/scalibr@latest`
2. `scalibr --result=result.textproto`
有关扫描结果格式的详细信息,请参阅 [result proto 定义](/binary/proto/scan_result.proto)。
运行 `scalibr --help` 获取其他 CLI 参数列表。
### 作为库使用:
1. 将 `github.com/google/osv-scalibr` 导入您的 Go 项目
2. 创建一个新的 [scalibr.ScanConfig](/scalibr.go#L36) 结构体,配置要运行的
提取和检测插件
3. 使用该配置调用 `scalibr.New().Scan()`
4. 解析返回的 [scalibr.ScanResults](/scalibr.go#L50)
有关代码示例,请参见下文。
### 在容器镜像上
添加 `--remote-image` 标志以扫描远程容器镜像。示例:
```
scalibr --result=result.textproto --remote-image=alpine@sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef9ab5fbe9f5
```
或者使用 `--image-tarball` 标志扫描本地保存的镜像 tarball,例如
通过 `docker save my-image > my-image.tar` 生成的镜像。示例:
```
scalibr --result=result.textproto --image-tarball=my-image.tar
```
注意:如前所述,目前仅支持基于 Linux 的容器镜像。
请关注 issue [#953](https://github.com/google/osv-scalibr/issues/953)
以追踪 Windows 镜像容器扫描支持。
### SPDX 生成
OSV-SCALIBR 支持将清单提取的结果生成为 JSON、YAML 或 tag-value 格式的
SPDX v2.3 文件。示例用法:
```
scalibr -o spdx23-json=result.spdx.json
```
生成的 SPDX 中的某些字段是可以被覆盖的:
```
scalibr -spdx-document-name="Custom name" --spdx-document-namespace="Custom-namespace" --spdx-creators=Organization:Google -o spdx23-json=result.spdx.json
```
## 运行内置插件
### 使用独立二进制文件
该二进制文件默认运行 SCALIBR 的“推荐”内部插件。您
可以使用 `--plugins=` 标志启用更多插件。请参阅
定义文件以获取所有内置插件及其 CLI 标志的列表
([extractors (fs)](/extractor/filesystem/list/list.go),
[extractors (standalone)](/extractor/filesystem/list/list.go),
[detectors](/detector/list/list.go),
[annotators](/annotator/list/list.go),
[enrichers](/enricher/enricherlist/list.go))。
### 使用库
所有内置插件模块的集合可以在定义文件中找到
([extractors (fs)](/extractor/filesystem/list/list.go),
[extractors (standalone)](/extractor/filesystem/list/list.go),
[detectors](/detector/list/list.go),
[annotators](/annotator/list/list.go),
[enrichers](/enricher/enricherlist/list.go))。
要启用它们,只需导入 plugin/list 并将相应的插件名称
添加到扫描配置中,例如
```
import (
"context"
scalibr "github.com/google/osv-scalibr"
pl "github.com/google/osv-scalibr/plugin/list"
scalibrfs "github.com/google/osv-scalibr/fs"
)
plugins, _ := pl.FromNames([]string{"os", "cis", "vex"})
cfg := &scalibr.ScanConfig{
ScanRoots: scalibrfs.RealFSScanRoots("/"),
Plugins: plugins,
}
results := scalibr.New().Scan(context.Background(), cfg)
```
您还可以指定扫描主机的环境能力,以便仅启用满足
要求(例如网络访问、特定 OS 插件)的插件:
```
import (
...
"github.com/google/osv-scalibr/plugin"
)
capab := &plugin.Capabilities{
OS: plugin.OSLinux,
Network: plugin.NetworkOnline,
DirectFS: true,
RunningSystem: true,
}
...
cfg := &scalibr.ScanConfig{
ScanRoots: scalibrfs.RealFSScanRoots("/"),
Plugins: plugin.FilterByCapabilities(plugins, capab),
}
...
```
## 创建并运行自定义插件
自定义插件只能在将 OSV-SCALIBR 作为库使用时运行。
1. 创建 OSV-SCALIBR
[Extractor](/extractor/filesystem/extractor.go#L30) 或
[Detector](/detector/detector.go#L28) 接口的实现。
2. 将新创建的结构体添加到扫描配置中并运行扫描,例如
```
import (
"github.com/google/osv-scalibr/plugin"
scalibr "github.com/google/osv-scalibr"
)
cfg := &scalibr.ScanConfig{
Root: "/",
Plugins: []plugin.Plugin{&myExtractor{}},
}
results := scalibr.New().Scan(context.Background(), cfg)
```
### 关于跨平台的注意事项
OSV-SCALIBR 兼容 Linux,并对 Windows 和 Mac 提供实验性支持。
当为 OSV-SCALIBR 实现新插件时,我们需要确保
它不会破坏其他平台。我们的运行器通常会捕获
兼容性问题,但为了确保在实现插件时一切顺利,
以下是一些需要记住的建议:
* 确保您使用 `filepath` 库来处理文件路径。例如,
避免使用 `/my/path`,而更推荐使用 `filepath.Join('my', 'path')`。
* 如果插件只能支持一个系统(例如 Windows 特定的
探测器),布局通常会有两个版本的文件:
* `file_system.go`:其中 `system` 是目标系统(例如
`file_windows.go`),包含特定于目标系统的代码。
它还必须包含相应的 go build 约束。
* `file_dummy.go`:包含适用于所有其他系统的代码。它通常
什么也不做,只是确保代码能在该系统上编译;
* 由于我们内部自动化的工作方式,我们通常要求
为每个平台定义单元测试,如果不兼容则动态过滤掉。
换句话说,应该使用 `if
runtime.GOOS` 而不是 `//go:build` 约束来过滤/移除测试。这里有一个
[示例](https://github.com/google/osv-scalibr/commit/7a87679f5c688e7bac4527d29c1823597a52bb40#diff-72efad005e0fbfe34c60e496dfb55ec15fc50f4b12be0934f08a3acaf7733616L79)。
## 自定义日志
您可以通过将 [`log.Logger`](/log/log.go#L22) 接口的实现传递给
`log.SetLogger()`,来让 OSV-SCALIBR 库使用您自己的自定义日志记录器:
```
import (
customlog "path/to/custom/log"
"github.com/google/osv-scalibr/log"
scalibr "github.com/google/osv-scalibr"
)
cfg := &scalibr.ScanConfig{ScanRoot: "/"}
log.SetLogger(&customlog.Logger{})
results := scalibr.New().Scan(context.Background(), cfg)
log.Info(results)
```
## 免责声明
OSV-SCALIBR 不是官方的 Google 产品。
标签:EVTX分析, Go语言, SBOM, 依赖扫描, 容器分析, 日志审计, 模型提供商, 硬件无关, 程序破解, 请求拦截