SBOM 開源工具使用說明
SBOM (Software Bill of Materials): SBOM提供軟 體中包含的所有函式庫與元件的清單,還會列出版本號、原始碼來源和關聯資訊。SBOM的目的是讓使用者或開發者瞭解軟體的組成,這樣在發生安全事件或需要更新時,能夠快速識別和應對。
OSV (Open Source Vulnerability): 是一種公開的軟體漏洞資料庫。透過產生的SBOM資訊搭配OSV漏洞資料庫,開發人員可以快速查詢其軟體元件是否存在已知的安全問題。
以下是使用 SBOM 與 OSV 的基本流程:
- 建立 SBOM: 於系統維運時,定期利用 SBOM 工具建立一份 SBOM 文件,將所有的元件、函式庫和依賴性記錄下來。
- 使用 OSV 查詢: 定期將產生的 SBOM 資訊,利用 OSV 資料庫查詢,確認元件是否有已知的安全漏洞。一旦發現,可以迅速決策,例如進行更新或替換該元件。
- 應對漏洞策略: 如果在 OSV 中發現了任何漏洞,應該立即進行修補或緩解措施。這可能包括更新軟體元件、應用修補程式,或在使用中考慮其他安全措施。
備註:因為各個 SBOM 掃描工具支援的語言、能解析的二進位檔 (Binary) 以及元件管理工具 (package manager) 有所不同,能掃描出的依賴元件亦不盡相同,故本文件提供 Syft 及 Trivy 兩套工具的使用說明, 建議第一次掃瞄專案 SBOM 時,可以兩個工具都使用,之後再選擇能掃描出最多依賴元件的工具,做為主要建立專案 SBOM 文件的工具。
本文件內容
4. 於 Excel 匯入 OSV-Scanner 產生報告
6. 進階:SBOM 增補欄位(parlay)與結果判讀(jqp)
1. 使用 Syft 掃描專案產生 SBOM 檔案
安裝
備註:本文件使用 Syft v1.8.0 做說明,其使用方式及產出結果,可能因您使用的版本不同而有些微差異
Windows 環境可以下載可執行檔 https://github.com/anchore/syft/releases
(移動至 Assets 段落,點選 Show all assets,找到 Windows 壓縮檔或執行檔下載)
Linux 環境或可執行
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sudo sh -s -- -b /usr/local/bin
產生 SBOM
輸出格式
https://github.com/anchore/syft/wiki/Output-Formats
輸出 SPDX 格式,為了使工具辨識該檔案為 SPDX SBOM,副檔名最好為 *.spdx.json,Syft 預設會最小化輸出檔案,加上 SYFT_FORMAT_PRETTY=true
會讓輸出的檔案比較易讀。
Linux
SYFT_FORMAT_PRETTY=true syft ./path/to/repo_dir -o spdx-json=./output_dir/syft.spdx.json
Windows CMD
set "SYFT_FORMAT_PRETTY=true" && syft ./path/to/repo_dir -o spdx-json=./output_dir/syft.spdx.json
Windows PowerShell
$env:SYFT_FORMAT_PRETTY="true"; syft ./rumors-line-bot-master -o spdx-json=./output_dir/syft.spdx.json
註:
"./path/to/repo_dir": 為來源資料夾路徑,若路徑來源包含中文或空白,建議用雙引號(" ")包裹路徑名稱,如 ”./中文路徑/to/repo_dir”。
"./output_dir/syft.spdx.json": 為輸出檔案路徑,若路徑來源包含中文或空白,建議用雙引號(" ")包裹路徑名稱,如 ”C:/輸出/syft.spdx.json”。
輸出 CycloneDX 格式,為了使工具辨識該檔案為 CycloneDX SBOM,副檔名最好為 *.cdx.json,Syft 預設會最小化輸出檔案,加上 SYFT_FORMAT_PRETTY=true
會讓輸出的檔案比較易讀。
SYFT_FORMAT_PRETTY=true syft ./path/to/repo_dir -o cyclonedx-json@1.5=./output_dir/syft.cdx.json
Windows CMD
set "SYFT_FORMAT_PRETTY=true" && syft ./path/to/repo_dir -o cyclonedx-json@1.5=./output_dir/syft.cdx.json
Windows PowerShell
$env:SYFT_FORMAT_PRETTY="true"; syft ./path/to/repo_dir -o cyclonedx-json@1.5=./output_dir/syft.cdx.json
2. 使用 Trivy 掃描專案產生 SBOM 檔案
安裝
備註:本文件使用 Trivy v0.5.3 做說明,其使用方式及產出結果,可能因您使用的版本不同而有些微差異
Trivy 安裝文件 https://aquasecurity.github.io/trivy/v0.53/getting-started/installation/
Windows 環境可以下載可執行檔 https://github.com/aquasecurity/trivy/releases
(移動至 Assets 段落,點選 Show all assets,找到 Windows 壓縮檔或執行檔下載)
Linux 環境或可執行
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sudo sh -s -- -b /usr/local/bin v0.53.0
產生 SBOM
輸出格式
https://google.github.io/osv-scanner/output/#output
輸出 SPDX 格式,為了使工具辨識該檔案為 SPDX SBOM,副檔名最好為 *.spdx.json。
trivy repo --format spdx-json --output ./sbom/trivy.spdx.json ./path/to/repo
註:
"./path/to/repo": 為來源資料夾路徑,若路徑來源包含中文或空白,建議用雙引號(" ")包裹路徑名稱,如 ”./中文路徑/to/repo”。
"./sbom/trivy.spdx.json": 為輸出檔案路徑,若路徑來源包含中文或空白,建議用雙引號(" ")包裹路徑名稱,如 ”C:/輸出sbom/trivy.spdx.json”。
輸出 CycloneDX 格式,為了使工具辨識該檔案為 CycloneDX SBOM,副檔名最好為 *.cdx.json。
trivy repo --format cyclonedx --output ./sbom/trivy.cdx.json ./path/to/repo
檢查 License
$ trivy fs --scanners license --license-full ./path/to/project
$ trivy fs --scanners license --license-full --format json -o result.json
./path/to/project
- 注意:建議將相關元件安裝後,trivy 命令才能收集完整相依元件資訊。以 npm 管理專案例,需先使用 npm install 命令後才能收集,若尚未將相依元件安裝,則會顯示以下提示資訊。
$ trivy fs --scanners license --license-full .
2024-07-10T03:51:45Z INFO Full license scanning is enabled
2024-07-10T03:51:47Z INFO [npm] To collect the license information of packages, "npm install" needs to be performed beforehand dir="api/node_modules"
2024-07-10T03:51:47Z INFO [npm] To collect the license information of packages, "npm install" needs to be performed beforehand dir="client/node_modules"
2024-07-10T03:51:47Z INFO [npm] To collect the license information of packages, "npm install" needs to be performed beforehand dir="node_modules"
2024-07-10T03:51:47Z INFO [npm] To collect the license information of packages, "npm install" needs to be performed beforehand dir="server/node_modules"
2024-07-10T03:51:47Z INFO Suppressing dependencies for development and testing. To display them, try the '--include-dev-deps' flag.
...
...
3. 使用 OSV-Scanner 掃描 SBOM 檔案
安裝
備註:本文件使用 OSV-Scanner v1.8.1 做說明,其使用方式及產出結果,可能因您使用的版本不同而有些微差異
OSV-Scanner 安裝文件https://google.github.io/osv-scanner/installation/#installation
下載可執行檔 https://github.com/google/osv-scanner/releases