程式碼簽章憑證與供應鏈安全

Code Signing Certificate and Supply Chain Security

前言

在現代軟體開發與部署環境中,供應鏈安全已成為資訊安全領域最關鍵的議題之一。從 SolarWinds 供應鏈攻擊事件到 Log4j 漏洞,我們見證了軟體供應鏈中的脆弱性可能造成的災難性後果。程式碼簽章憑證(Code Signing Certificate)作為驗證軟體來源與完整性的核心機制,在建立軟體信任鏈中扮演著不可或缺的角色。

本文將深入探討程式碼簽章的概念、各平台的實作方式,以及現代化的簽章解決方案如 Sigstore,並介紹如何透過軟體物料清單(SBOM)與最佳實務來強化供應鏈安全。


1. 程式碼簽章概念與重要性

什麼是程式碼簽章?

程式碼簽章是一種使用數位憑證對軟體進行加密簽署的技術。透過非對稱加密演算法,開發者使用私鑰對程式碼或軟體套件進行簽署,而使用者則可透過對應的公鑰驗證簽章的有效性。

程式碼簽章的核心功能

  1. 身份驗證(Authentication):確認軟體的發布者身份
  2. 完整性驗證(Integrity):確保軟體自簽署後未被竄改
  3. 不可否認性(Non-repudiation):發布者無法否認其簽署行為

為什麼程式碼簽章如此重要?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
┌─────────────────────────────────────────────────────────────┐
│                    軟體供應鏈信任模型                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   開發者 ──→ 程式碼簽章 ──→ 發布平台 ──→ 終端使用者          │
│      │           │            │            │                │
│      ▼           ▼            ▼            ▼                │
│   [私鑰]     [憑證鏈]     [驗證簽章]   [信任決策]            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

未經簽章的軟體可能面臨以下風險:

  • 中間人攻擊(MITM):攻擊者可在傳輸過程中置換惡意程式
  • 偽冒身份:惡意軟體可偽裝成合法軟體
  • 供應鏈污染:建置環境被入侵後注入惡意程式碼

2. 傳統程式碼簽章憑證

憑證類型

傳統的程式碼簽章憑證主要分為以下幾種:

類型驗證層級適用場景價格範圍
OV (Organization Validation)組織驗證企業軟體發布$200-500/年
EV (Extended Validation)延伸驗證驅動程式、高信任度軟體$400-700/年
IV (Individual Validation)個人驗證獨立開發者$100-300/年

憑證申請流程

1
2
3
4
5
6
7
8
9
# 1. 產生私鑰與憑證簽署請求(CSR)
openssl genrsa -out code_signing.key 4096

# 2. 建立 CSR
openssl req -new -key code_signing.key -out code_signing.csr \
    -subj "/C=TW/ST=Taiwan/L=Taipei/O=Your Company/CN=Your Company Code Signing"

# 3. 檢視 CSR 內容
openssl req -in code_signing.csr -text -noout

主要憑證發行機構(CA)

  • DigiCert
  • Sectigo (原 Comodo)
  • GlobalSign
  • Entrust
  • SSL.com

憑證儲存最佳實務

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 使用 Hardware Security Module (HSM) 儲存私鑰
# 或使用加密的 PKCS#12 格式
openssl pkcs12 -export \
    -out code_signing.p12 \
    -inkey code_signing.key \
    -in code_signing.crt \
    -certfile ca_bundle.crt \
    -password pass:YOUR_STRONG_PASSWORD

# 設定適當的檔案權限
chmod 400 code_signing.key
chmod 400 code_signing.p12

3. Windows Authenticode 簽章

Authenticode 簡介

Windows Authenticode 是 Microsoft 開發的程式碼簽章技術,用於驗證 Windows 可執行檔、DLL、驅動程式等的來源與完整性。

使用 SignTool 進行簽章

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 安裝 Windows SDK 取得 SignTool
# 下載位置: https://developer.microsoft.com/windows/downloads/windows-sdk/

# 基本簽章(使用 PFX 檔案)
signtool sign /f code_signing.pfx /p YOUR_PASSWORD /t http://timestamp.digicert.com /fd sha256 application.exe

# 使用 SHA-256 雜湊演算法與 RFC 3161 時間戳記
signtool sign /f code_signing.pfx /p YOUR_PASSWORD ^
    /tr http://timestamp.digicert.com ^
    /td sha256 ^
    /fd sha256 ^
    application.exe

# 對 MSI 安裝檔簽章
signtool sign /f code_signing.pfx /p YOUR_PASSWORD ^
    /tr http://timestamp.digicert.com ^
    /td sha256 ^
    /fd sha256 ^
    installer.msi

驗證簽章

1
2
3
4
5
# 驗證簽章有效性
signtool verify /pa /v application.exe

# 顯示簽章詳細資訊
signtool verify /pa /all application.exe

使用 Azure Key Vault 進行雲端簽章

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 安裝 Azure SignTool
dotnet tool install --global AzureSignTool

# 使用 Azure Key Vault 中的憑證簽章
AzureSignTool sign ^
    --azure-key-vault-url https://your-vault.vault.azure.net ^
    --azure-key-vault-client-id YOUR_CLIENT_ID ^
    --azure-key-vault-client-secret YOUR_CLIENT_SECRET ^
    --azure-key-vault-certificate code-signing-cert ^
    --timestamp-rfc3161 http://timestamp.digicert.com ^
    --timestamp-digest sha256 ^
    --file-digest sha256 ^
    application.exe

Windows 核心模式驅動程式簽章

自 Windows 10 1607 起,核心模式驅動程式必須經過 Microsoft 的 WHQL 或 Attestation 簽章:

1
2
3
4
5
6
7
8
9
# 1. 首先使用 EV 憑證簽署驅動程式
signtool sign /f ev_cert.pfx /p PASSWORD ^
    /tr http://timestamp.digicert.com ^
    /td sha256 ^
    /fd sha256 ^
    driver.sys

# 2. 提交至 Windows Hardware Dev Center 取得 Microsoft 簽章
# 網址: https://partner.microsoft.com/dashboard/hardware

4. macOS 程式碼簽章

Apple 開發者憑證

macOS 的程式碼簽章需要 Apple Developer Program 會員資格,並使用以下類型的憑證:

  • Developer ID Application:用於 App Store 外發布的應用程式
  • Developer ID Installer:用於安裝程式套件
  • Mac App Distribution:用於 App Store 發布

使用 codesign 工具

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 列出可用的簽章身份
security find-identity -v -p codesigning

# 簽署應用程式
codesign --sign "Developer ID Application: Your Company (TEAM_ID)" \
    --options runtime \
    --timestamp \
    --deep \
    --force \
    YourApp.app

# 簽署時指定 entitlements
codesign --sign "Developer ID Application: Your Company (TEAM_ID)" \
    --options runtime \
    --timestamp \
    --entitlements entitlements.plist \
    YourApp.app

Hardened Runtime 與 Entitlements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<!-- entitlements.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <false/>
    <key>com.apple.security.cs.disable-library-validation</key>
    <false/>
    <key>com.apple.security.cs.allow-jit</key>
    <false/>
    <key>com.apple.security.automation.apple-events</key>
    <true/>
</dict>
</plist>

Notarization(公證)

自 macOS 10.15 Catalina 起,非 App Store 發布的應用程式必須經過 Apple 公證:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 建立 ZIP 檔案用於公證
ditto -c -k --keepParent YourApp.app YourApp.zip

# 提交公證請求
xcrun notarytool submit YourApp.zip \
    --apple-id "your@email.com" \
    --password "app-specific-password" \
    --team-id "TEAM_ID" \
    --wait

# 查詢公證狀態
xcrun notarytool info SUBMISSION_ID \
    --apple-id "your@email.com" \
    --password "app-specific-password" \
    --team-id "TEAM_ID"

# 釘選公證票據(Staple)
xcrun stapler staple YourApp.app

# 驗證公證狀態
spctl --assess --type execute --verbose YourApp.app

驗證簽章

1
2
3
4
5
6
7
8
# 驗證應用程式簽章
codesign --verify --deep --strict --verbose=2 YourApp.app

# 顯示簽章詳細資訊
codesign -d --verbose=4 YourApp.app

# 使用 Gatekeeper 驗證
spctl --assess --verbose YourApp.app

5. Sigstore 與 Cosign

Sigstore 簡介

Sigstore 是 Linux Foundation 支持的開源專案,旨在提供免費、開放且透明的程式碼簽章基礎設施。它解決了傳統程式碼簽章的幾個主要問題:

  • 成本:免費使用,無需購買憑證
  • 金鑰管理:支援無金鑰(Keyless)簽章
  • 透明度:所有簽章記錄在公開透明日誌中

Sigstore 架構元件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
┌─────────────────────────────────────────────────────────────┐
│                      Sigstore 生態系統                       │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────┐    ┌─────────┐    ┌─────────┐                │
│   │ Cosign  │    │ Rekor   │    │ Fulcio  │                │
│   │ 簽章工具 │    │透明日誌 │    │ 憑證授權│                │
│   └────┬────┘    └────┬────┘    └────┬────┘                │
│        │              │              │                      │
│        └──────────────┼──────────────┘                      │
│                       ▼                                     │
│              ┌───────────────┐                              │
│              │   OIDC 提供者  │                              │
│              │ (Google, GitHub│                              │
│              │  Microsoft等)  │                              │
│              └───────────────┘                              │
│                                                             │
└─────────────────────────────────────────────────────────────┘

安裝 Cosign

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# macOS 使用 Homebrew
brew install cosign

# Linux 使用 Go
go install github.com/sigstore/cosign/v2/cmd/cosign@latest

# 或下載預編譯二進位檔
COSIGN_VERSION="v2.2.0"
curl -LO https://github.com/sigstore/cosign/releases/download/${COSIGN_VERSION}/cosign-linux-amd64
chmod +x cosign-linux-amd64
sudo mv cosign-linux-amd64 /usr/local/bin/cosign

# 驗證安裝
cosign version

Keyless 簽章(無金鑰模式)

1
2
3
4
5
6
7
8
9
# 使用 OIDC 身份進行 Keyless 簽章
# 這會開啟瀏覽器進行身份驗證
cosign sign --yes ghcr.io/your-org/your-image:tag

# 在 CI/CD 環境中使用 OIDC 權杖
# GitHub Actions 範例
cosign sign --yes \
    --oidc-issuer https://token.actions.githubusercontent.com \
    ghcr.io/your-org/your-image:tag

使用本地金鑰簽章

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 產生金鑰對
cosign generate-key-pair

# 這會產生:
# - cosign.key (私鑰,需要密碼保護)
# - cosign.pub (公鑰)

# 使用私鑰簽章
cosign sign --key cosign.key ghcr.io/your-org/your-image:tag

# 驗證簽章
cosign verify --key cosign.pub ghcr.io/your-org/your-image:tag

驗證 Keyless 簽章

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 驗證 Keyless 簽章(指定發行者和身份)
cosign verify \
    --certificate-identity "your-email@example.com" \
    --certificate-oidc-issuer "https://accounts.google.com" \
    ghcr.io/your-org/your-image:tag

# 使用正則表達式匹配
cosign verify \
    --certificate-identity-regexp ".*@your-org\.com" \
    --certificate-oidc-issuer-regexp "https://token\.actions\.githubusercontent\.com" \
    ghcr.io/your-org/your-image:tag

附加元資料

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 簽章時附加註解
cosign sign --yes \
    --annotations "version=1.0.0" \
    --annotations "git-commit=$(git rev-parse HEAD)" \
    ghcr.io/your-org/your-image:tag

# 附加 SBOM
cosign attach sbom --sbom sbom.spdx ghcr.io/your-org/your-image:tag

# 簽署 SBOM
cosign sign --yes \
    --attachment sbom \
    ghcr.io/your-org/your-image:tag

6. 容器映像簽章

Docker Content Trust (DCT)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 啟用 Docker Content Trust
export DOCKER_CONTENT_TRUST=1

# 產生委派金鑰
docker trust key generate your-name

# 新增簽章者
docker trust signer add --key your-name.pub your-name your-repo/your-image

# 簽章並推送映像
docker trust sign your-registry/your-image:tag

# 檢查信任資訊
docker trust inspect --pretty your-registry/your-image:tag

使用 Cosign 簽署容器映像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 建置並推送映像
docker build -t ghcr.io/your-org/app:v1.0.0 .
docker push ghcr.io/your-org/app:v1.0.0

# 取得映像摘要
DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' ghcr.io/your-org/app:v1.0.0)

# 簽署映像(使用 Keyless)
cosign sign --yes ${DIGEST}

# 或指定完整摘要 URI
cosign sign --yes ghcr.io/your-org/app@sha256:abc123...

GitHub Actions 整合範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# .github/workflows/build-and-sign.yml
name: Build and Sign Container Image

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: read
  packages: write
  id-token: write  # 需要用於 Keyless 簽章

jobs:
  build-and-sign:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and Push
        id: build
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }}

      - name: Install Cosign
        uses: sigstore/cosign-installer@v3

      - name: Sign Container Image
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: |
          cosign sign --yes \
            ghcr.io/${{ github.repository }}@${DIGEST}          

Kubernetes 映像驗證

使用 Policy Controller 或 Kyverno 在 Kubernetes 中強制驗證映像簽章:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Kyverno 策略範例
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: verify-image-signature
spec:
  validationFailureAction: Enforce
  background: false
  rules:
    - name: verify-signature
      match:
        any:
          - resources:
              kinds:
                - Pod
      verifyImages:
        - imageReferences:
            - "ghcr.io/your-org/*"
          attestors:
            - entries:
                - keyless:
                    issuer: "https://token.actions.githubusercontent.com"
                    subject: "https://github.com/your-org/*"
                    rekor:
                      url: https://rekor.sigstore.dev

7. 軟體物料清單(SBOM)

什麼是 SBOM?

軟體物料清單(Software Bill of Materials)是軟體組成成分的詳細清單,包含所有相依套件、版本資訊、授權條款等。SBOM 在供應鏈安全中扮演關鍵角色,讓組織能夠:

  • 追蹤軟體相依性
  • 快速識別受漏洞影響的元件
  • 符合法規要求(如美國行政命令 EO 14028)

SBOM 格式

主要的 SBOM 格式包括:

格式維護者特點
SPDXLinux FoundationISO 標準,廣泛支援
CycloneDXOWASP安全導向,支援 VEX
SWIDISO/NIST軟體識別標籤

使用 Syft 產生 SBOM

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 安裝 Syft
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin

# 為容器映像產生 SBOM
syft ghcr.io/your-org/app:v1.0.0 -o spdx-json > sbom.spdx.json

# 產生 CycloneDX 格式
syft ghcr.io/your-org/app:v1.0.0 -o cyclonedx-json > sbom.cdx.json

# 為本地目錄產生 SBOM
syft dir:./my-project -o spdx-json > sbom.spdx.json

# 為 Docker 映像的 tar 檔案產生 SBOM
syft docker-archive:image.tar -o spdx-json > sbom.spdx.json

使用 Grype 進行漏洞掃描

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 安裝 Grype
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin

# 掃描容器映像
grype ghcr.io/your-org/app:v1.0.0

# 使用 SBOM 進行掃描
grype sbom:./sbom.spdx.json

# 輸出為 JSON 格式
grype ghcr.io/your-org/app:v1.0.0 -o json > vulnerabilities.json

# 設定嚴重程度閾值
grype ghcr.io/your-org/app:v1.0.0 --fail-on high

SBOM 與簽章整合

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 產生 SBOM
syft ghcr.io/your-org/app:v1.0.0 -o spdx-json > sbom.spdx.json

# 附加 SBOM 至容器映像
cosign attach sbom --sbom sbom.spdx.json ghcr.io/your-org/app:v1.0.0

# 簽署 SBOM 附件
cosign sign --yes --attachment sbom ghcr.io/your-org/app:v1.0.0

# 驗證 SBOM 簽章
cosign verify-attestation \
    --type spdxjson \
    --certificate-identity-regexp ".*" \
    --certificate-oidc-issuer-regexp ".*" \
    ghcr.io/your-org/app:v1.0.0

GitHub Actions 中的 SBOM 工作流程

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# .github/workflows/sbom.yml
name: Generate and Attest SBOM

on:
  push:
    branches: [main]

permissions:
  contents: write
  packages: write
  id-token: write
  attestations: write

jobs:
  sbom:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build Image
        id: build
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}

      - name: Generate SBOM
        uses: anchore/sbom-action@v0
        with:
          image: ghcr.io/${{ github.repository }}:${{ github.sha }}
          format: spdx-json
          output-file: sbom.spdx.json

      - name: Attest SBOM
        uses: actions/attest-sbom@v1
        with:
          subject-name: ghcr.io/${{ github.repository }}
          subject-digest: ${{ steps.build.outputs.digest }}
          sbom-path: sbom.spdx.json
          push-to-registry: true

8. 供應鏈安全最佳實務

安全開發生命週期整合

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
┌─────────────────────────────────────────────────────────────────┐
│                    供應鏈安全控制點                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  開發階段        建置階段         部署階段        運行階段       │
│     │              │                │              │            │
│     ▼              ▼                ▼              ▼            │
│ ┌───────┐     ┌─────────┐      ┌────────┐    ┌─────────┐       │
│ │相依性  │     │ 建置環境 │      │ 映像驗 │    │ 執行時期│       │
│ │掃描    │     │ 簽章    │      │ 證    │    │ 監控    │       │
│ └───────┘     └─────────┘      └────────┘    └─────────┘       │
│     │              │                │              │            │
│     ▼              ▼                ▼              ▼            │
│ ┌───────┐     ┌─────────┐      ┌────────┐    ┌─────────┐       │
│ │SBOM   │     │ SLSA    │      │ 策略   │    │ 事件    │       │
│ │產生    │     │ 認證    │      │ 執行   │    │ 回應    │       │
│ └───────┘     └─────────┘      └────────┘    └─────────┘       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

相依性管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Node.js - 使用 npm audit
npm audit
npm audit fix

# Python - 使用 pip-audit
pip install pip-audit
pip-audit

# Go - 使用 govulncheck
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...

# 使用 Dependabot 自動更新相依性
# .github/dependabot.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    open-pull-requests-limit: 10

  - package-ecosystem: "docker"
    directory: "/"
    schedule:
      interval: "weekly"

  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"

SLSA 框架實施

SLSA(Supply-chain Levels for Software Artifacts)提供了供應鏈安全的分級框架:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 使用 SLSA GitHub Generator 產生 Provenance
# .github/workflows/slsa.yml
name: SLSA Provenance

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write
  id-token: write
  packages: write

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v4

      - name: Build
        id: build
        run: |
          # 建置步驟
          echo "digest=sha256:..." >> $GITHUB_OUTPUT          

  provenance:
    needs: build
    permissions:
      actions: read
      id-token: write
      contents: write
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.9.0
    with:
      image: ghcr.io/${{ github.repository }}
      digest: ${{ needs.build.outputs.digest }}
    secrets:
      registry-password: ${{ secrets.GITHUB_TOKEN }}

建置環境安全

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 使用可重現建置
# Dockerfile
FROM golang:1.21-alpine AS builder

# 設定確定性建置參數
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download

COPY . .

# 使用 -trimpath 移除建置路徑資訊
# 使用 -ldflags 設定版本資訊
RUN go build -trimpath \
    -ldflags="-s -w -X main.version=${VERSION}" \
    -o /app/binary ./cmd/main.go

# 使用 distroless 基礎映像
FROM gcr.io/distroless/static:nonroot
COPY --from=builder /app/binary /binary
USER nonroot:nonroot
ENTRYPOINT ["/binary"]

金鑰管理最佳實務

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 使用 HashiCorp Vault 管理簽章金鑰
# 啟用 Transit 密碼引擎
vault secrets enable transit

# 建立簽章金鑰
vault write transit/keys/code-signing type=ecdsa-p256

# 簽署資料
vault write transit/sign/code-signing \
    input=$(base64 <<< "data to sign")

# 驗證簽章
vault write transit/verify/code-signing \
    input=$(base64 <<< "data to sign") \
    signature="vault:v1:..."

事件回應計畫

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## 供應鏈安全事件回應檢查清單

### 偵測階段
- [ ] 監控 CVE 資料庫與安全公告
- [ ] 設定 SBOM 比對自動警報
- [ ] 監控異常的建置行為

### 評估階段
- [ ] 確認受影響的元件版本
- [ ] 使用 SBOM 識別所有受影響的產品
- [ ] 評估漏洞的嚴重程度與影響範圍

### 回應階段
- [ ] 更新受影響的相依性
- [ ] 重新建置並簽署受影響的產品
- [ ] 通知下游使用者

### 復原階段
- [ ] 驗證修補程式的有效性
- [ ] 更新 SBOM 文件
- [ ] 進行事後檢討

合規性考量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 產生合規性報告
# 使用 Trivy 進行合規性掃描
trivy image --compliance docker-cis ghcr.io/your-org/app:v1.0.0

# 使用 Grype 產生 VEX 文件
grype ghcr.io/your-org/app:v1.0.0 --vex vex.json

# 驗證 SBOM 格式合規性
# 使用 SPDX tools
java -jar spdx-tools.jar Verify sbom.spdx.json

結論

程式碼簽章與供應鏈安全是現代軟體開發不可或缺的一環。透過本文介紹的技術與實務,組織可以建立完整的軟體信任鏈:

  1. 採用現代化簽章解決方案:考慮使用 Sigstore 等開源工具,降低成本並提高透明度
  2. 實施 SBOM 管理:為所有軟體產出生成並維護 SBOM
  3. 自動化安全控制:將簽章、驗證、掃描整合至 CI/CD 流程
  4. 遵循 SLSA 框架:逐步提升建置流程的安全等級
  5. 建立事件回應機制:制定並演練供應鏈安全事件的回應計畫

隨著供應鏈攻擊的日益複雜,持續關注最新的安全技術與最佳實務,並將其整合至開發流程中,將是保護組織軟體資產的關鍵。


參考資源

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy