forked from J0113/SCA_Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-ScannerBundle.ps1
More file actions
187 lines (151 loc) · 5.71 KB
/
Copy pathInvoke-ScannerBundle.ps1
File metadata and controls
187 lines (151 loc) · 5.71 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
[CmdletBinding()]
param(
[string]$BundleZip,
[string]$BundleRoot,
[string]$ScannerExecutable = 'SCAScanner.exe',
[string]$PolicyPath = 'Policies',
[string]$OutputRoot,
[switch]$DisplayDetails,
[string]$SbomTarget,
[switch]$KeepStagingRoot,
[string[]]$ExtraScannerArgs = @()
)
$ErrorActionPreference = 'Stop'
function Test-Administrator {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (-not (Test-Administrator)) {
throw 'Run this script from an elevated PowerShell session.'
}
function Resolve-OutputRoot {
param(
[string]$RootPath,
[string]$BundlePath
)
if (-not [string]::IsNullOrWhiteSpace($RootPath)) {
return $RootPath
}
return Join-Path $BundlePath 'results'
}
function Get-WorkingRoot {
param(
[string]$ZipPath,
[string]$RootPath
)
if (-not [string]::IsNullOrWhiteSpace($ZipPath)) {
if (-not (Test-Path -LiteralPath $ZipPath)) {
throw "Bundle zip not found: $ZipPath"
}
$tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ('sca-bundle-' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $tempRoot -Force | Out-Null
Expand-Archive -LiteralPath $ZipPath -DestinationPath $tempRoot -Force
return [pscustomobject]@{ Path = $tempRoot; Temporary = $true }
}
if (-not [string]::IsNullOrWhiteSpace($RootPath)) {
if (-not (Test-Path -LiteralPath $RootPath)) {
throw "Bundle root not found: $RootPath"
}
return [pscustomobject]@{ Path = (Resolve-Path -LiteralPath $RootPath).Path; Temporary = $false }
}
return [pscustomobject]@{ Path = $PSScriptRoot; Temporary = $false }
}
function Resolve-ScannerPath {
param(
[string]$BundlePath,
[string]$ExecutableName
)
$candidate = Join-Path $BundlePath $ExecutableName
if (Test-Path -LiteralPath $candidate) {
return $candidate
}
throw "Scanner executable not found in bundle: $candidate"
}
function Resolve-PolicyTarget {
param(
[string]$BundlePath,
[string]$RelativeTarget
)
if ([System.IO.Path]::IsPathRooted($RelativeTarget)) {
if (-not (Test-Path -LiteralPath $RelativeTarget)) {
throw "Policy target not found: $RelativeTarget"
}
return (Resolve-Path -LiteralPath $RelativeTarget).Path
}
$candidate = Join-Path $BundlePath $RelativeTarget
if (-not (Test-Path -LiteralPath $candidate)) {
throw "Policy target not found: $candidate"
}
return (Resolve-Path -LiteralPath $candidate).Path
}
function Invoke-Scanner {
param(
[string]$ScannerPath,
[string]$PolicyTarget,
[string]$OutputRoot,
[string]$HostName,
[switch]$UseDetailedOutput,
[string]$SbomTarget,
[string[]]$AdditionalArguments
)
$logFile = Join-Path $OutputRoot "hardening-$HostName.log"
$csvFile = Join-Path $OutputRoot "hardening-$HostName.csv"
$reportFile = Join-Path $OutputRoot "hardening-$HostName.txt"
$sbomFile = Join-Path $OutputRoot "sbom-$HostName.cdx.json"
$arguments = @()
if ($UseDetailedOutput) {
$arguments += '--display-details'
}
else {
$arguments += '--no-details'
}
$arguments += '--output-dir', $OutputRoot
$arguments += '--log', $logFile
$arguments += '--csv', $csvFile
$arguments += '--report', $reportFile
$arguments += '--sbom-file', $sbomFile
$sbomTargetLabel = if (-not [string]::IsNullOrWhiteSpace($SbomTarget)) {
$arguments += '--sbom-target', $SbomTarget
$SbomTarget
}
else {
$arguments += '--sbom-all-drives'
'all drives'
}
if ($AdditionalArguments.Count -gt 0) {
$arguments += $AdditionalArguments
}
$arguments += $PolicyTarget
Write-Host "Running scanner: $ScannerPath"
Write-Host "Policy target: $PolicyTarget"
Write-Host "Output root: $OutputRoot"
Write-Host "SBOM target: $sbomTargetLabel"
& $ScannerPath @arguments
if ($LASTEXITCODE -ne 0) {
throw "Scanner returned exit code $LASTEXITCODE."
}
}
$workingRootInfo = Get-WorkingRoot -ZipPath $BundleZip -RootPath $BundleRoot
$OutputRoot = Resolve-OutputRoot -RootPath $OutputRoot -BundlePath $workingRootInfo.Path
if (-not (Test-Path -LiteralPath $OutputRoot)) {
New-Item -ItemType Directory -Path $OutputRoot -Force | Out-Null
}
try {
$scannerPath = Resolve-ScannerPath -BundlePath $workingRootInfo.Path -ExecutableName $ScannerExecutable
$policyTarget = Resolve-PolicyTarget -BundlePath $workingRootInfo.Path -RelativeTarget $PolicyPath
$computerName = $env:COMPUTERNAME
Invoke-Scanner -ScannerPath $scannerPath -PolicyTarget $policyTarget -OutputRoot $OutputRoot -HostName $computerName -UseDetailedOutput:$DisplayDetails -SbomTarget $SbomTarget -AdditionalArguments $ExtraScannerArgs
Write-Host ""
Write-Host "Artifacts created:"
Write-Host " Log : $(Join-Path $OutputRoot "hardening-$computerName.log")"
Write-Host " CSV : $(Join-Path $OutputRoot "hardening-$computerName.csv")"
Write-Host " Report : $(Join-Path $OutputRoot "hardening-$computerName.txt")"
Write-Host " SBOM : $(Join-Path $OutputRoot "sbom-$computerName.cdx.json")"
}
finally {
if (-not $KeepStagingRoot -and $workingRootInfo.Temporary -and (Test-Path -LiteralPath $workingRootInfo.Path)) {
# Leave the working tree in place when requested; otherwise clean up the extracted bundle.
Remove-Item -LiteralPath $workingRootInfo.Path -Recurse -Force
}
}