PowerShell 技能连载 - 供应链安全漏洞扫描

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
function Invoke-SupplyChainScan {
param(
[Parameter(Mandatory=$true)]
[string]$ImageName,
[string]$OutputFormat = 'table',
[string]$SeverityLevel = 'HIGH,CRITICAL'
)

# 安装Trivy漏洞扫描器
if (-not (Get-Command trivy -ErrorAction SilentlyContinue)) {
winget install aquasecurity.trivy
}

try {
# 执行容器镜像扫描
$result = trivy image --format $OutputFormat --severity $SeverityLevel $ImageName

# 生成HTML报告
$htmlReport = "$env:TEMP\scan_report_$(Get-Date -Format yyyyMMddHHmmss).html"
trivy image --format template --template "@contrib/html.tpl" -o $htmlReport $ImageName

[PSCustomObject]@{
ScanTarget = $ImageName
VulnerabilitiesFound = $result.Count
CriticalCount = ($result | Where-Object { $_ -match 'CRITICAL' }).Count
HighCount = ($result | Where-Object { $_ -match 'HIGH' }).Count
HTMLReportPath = $htmlReport
}
}
catch {
Write-Error "漏洞扫描失败:$_"
}
}

核心功能:

  1. 集成Trivy进行容器镜像漏洞扫描
  2. 支持多种输出格式(table/json/html)
  3. 自动生成带严重等级分类的报告
  4. 包含依赖组件版本检查

应用场景:

  • CI/CD流水线安全门禁
  • 第三方组件入仓检查
  • 生产环境镜像定期审计