-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstigscript.ps1
436 lines (370 loc) · 17.9 KB
/
stigscript.ps1
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
###############################################
# DISA STIG Remediation Script
#-----------------------------------
# Version: 1.0.0 - initial creation
#-----------------------------------
# Author: TSgt Dillion Gasdia
#-----------------------------------
# Description: This script automates DISA STIG
# remediation tasks, with options for
# verification, test mode, and execution across
# multiple machines. For additional information
# please refer to the README file.
###############################################
# Import Required Modules
# Logger: Handles logging of script execution details
# Preload-STIGOptions: Prepares and loads STIG options into the GUI dropdown
# Normalize-RegistryPath: Ensures registry paths are correctly formatted
# Process-STIGRules: Handles processing of individual STIG rules
# Prepare-MachineList: Prepares a list of machines for distributed execution
# Process-STIGFiles: Orchestrates the processing of selected STIG files
Import-Module "$PSScriptRoot\Modules\logger.psm1" -Force
Import-Module "$PSScriptRoot\Modules\Preload-STIGOptions.psm1" -Force
Import-Module "$PSScriptRoot\Modules\Normalize-RegistryPath.psm1" -Force
Import-Module "$PSScriptRoot\Modules\Process-STIGRules.psm1" -Force
Import-Module "$PSScriptRoot\Modules\Prepare-MachineList.psm1" -Force
Import-Module "$PSScriptRoot\Modules\Process-STIGFiles.psm1" -Force
#Define Variables
#$logFilePath = "$PSScriptRoot\Logs"
# Initialize the report
$Global:ComplianceReport = @() # Initialize an empty array for compliance reporting
# Initialize counters for manual and skipped rules
$Global:ManualRulesCount = 0
$Global:SkippedRulesCount = 0
# Final summary log (outside the loop)
Write-Log -Message "Final Processing Summary: ${Global:ManualRulesCount} manual rules, ${Global:RegistryRulesCount} registry rules, and ${Global:DocumentRulesCount} document rules across all machines."
Write-Host "Final Summary: ${Global:ManualRulesCount} manual rules, ${Global:RegistryRulesCount} registry rules, and ${Global:DocumentRulesCount} document rules." -ForegroundColor Green
# Validate Required Directories
#$logDir = "$PSScriptRoot\Logs"
#if (-not (Test-Path -Path $LogsPath)) {
# New-Item -ItemType Directory -Path $LogsPath -Force | Out-Null
# Write-log "Logs directory created at $LogsPath"
#}
##################################################
# #
# Main Script to Run GUI and Execute Functions #
# #
##################################################
# Start GUI and Process
Write-log "Starting STIG Remediation Tool..."
# Load Required Assemblies
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase
# Function to apply theme
Function Apply-Theme {
param (
[string]$Theme
)
switch ($Theme) {
"Light" {
$window.Background = [System.Windows.Media.Brushes]::White
$window.Foreground = [System.Windows.Media.Brushes]::Black
$window.FindName("ThemeToggleButton").Content = "Light Theme"
}
"Dark" {
$window.Background = [System.Windows.Media.Brushes]::Black
$window.Foreground = [System.Windows.Media.Brushes]::Gray
$window.FindName("ThemeToggleButton").Content = "Dark Theme"
}
}
}
# STIG File Location
$stigFolderPath = "$PSScriptRoot\Data\stigs"
# Path to the XAML File
$xamlFilePath = "$PSScriptRoot\STIGRemediationGUI.xaml"
# Check if the XAML file exists
if (-not (Test-Path -Path $xamlFilePath)) {
Write-log "Error: XAML file not found at $xamlFilePath" -IsError
exit 1
}
# Load and Validate XAML Content
try {
$xamlContent = Get-Content -Path $xamlFilePath -Raw
Write-Log -Message "Raw XAML Content Loaded."
$xmlDoc = New-Object System.Xml.XmlDocument
$xmlDoc.LoadXml($xamlContent)
Write-Log -Message "XAML successfully parsed as XML."
} catch {
Write-Log -Message "Error loading or parsing XAML file: $_" -IsError
exit 1
}
# Load WPF Window
try {
$reader = New-Object System.Xml.XmlNodeReader($xmlDoc)
$window = [Windows.Markup.XamlReader]::Load($reader)
if (-not $window) {
Write-Log -Message "Failed to load XAML into WPF Window. Please check the XAML structure." -IsError
exit 1
}
Write-Log -Message "XAML successfully loaded into WPF Window."
} catch {
Write-Log -Message "Error initializing WPF window from XAML: $_" -IsError
exit 1
}
# Apply initial theme
Apply-Theme -Theme "Light"
# Theme Toggle Button Event Handler
$window.FindName("ThemeToggleButton").Add_Click({
$currentTheme = $window.FindName("ThemeToggleButton").Content
if ($currentTheme -eq "Light Theme") {
Apply-Theme -Theme "Dark"
} else {
Apply-Theme -Theme "Light"
}
})
#################################
# #
# Event Handlers #
# #
#################################
# Help Button
$window.FindName("HelpButton").Add_Click({
$helpMessage = @"
STIG Remediation Tool - User Guide
-----------------------------------
This tool is designed to automate DISA STIG remediation tasks with options for:
1. Selecting execution mode (Local or Distributed).
2. Running in Test, Run, or Verify modes.
3. Advanced options for selecting specific STIG rules or all available rules.
4. Generating compliance reports in CSV and PDF formats.
Modes:
------
1. **Test Mode**:
- Simulates the changes that would be applied.
- Logs the operations without making actual changes.
- Use this mode to preview the impact of the script.
2. **Run Mode**:
- Applies changes to the machine or machines in the selected mode.
- Requires the user to acknowledge the risks before execution.
- WARNING: This mode makes permanent changes.
3. **Verify Mode**:
- Verifies the compliance of the system against the selected STIG rules.
- Generates a detailed compliance report.
- Does not apply any changes.
Using Advanced Options:
-----------------------
- Enable advanced options to select specific STIG rules for processing.
- Default behavior processes all available STIGs in the selected mode.
- Update the STIG files using the **Update STIGs** button to ensure the latest rules are applied.
Logs and Reports:
-----------------
- All operations are logged in the "Logs" directory under the script's location.
- Logs include execution details, errors, and compliance status.
- Reports (CSV) are generated for compliance checks in Verify Mode.
Steps to Use:
-------------
1. Select the desired execution mode (Local or Distributed).
2. Choose the operation mode: Test, Run, or Verify.
3. (Optional) Enable advanced options to select specific STIGs.
4. Click the **Run** button to execute the script.
Additional Information:
-----------------------
- Ensure you have sufficient privileges (e.g., Administrator) to apply changes in Run Mode.
- Review logs for detailed execution and compliance details.
For further assistance, refer to the README file or contact support.
"@
[System.Windows.MessageBox]::Show($helpMessage, "Help - STIG Remediation Tool", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Information)
})
# Execution Mode Selection
$window.FindName("ExecutionModeSelector").Add_SelectionChanged({
$executionMode = $window.FindName("ExecutionModeSelector").SelectedItem.Content
if ($executionMode -eq "Distributed") {
$window.FindName("BrowseButton").Visibility = "Visible"
$window.FindName("ExecutionModeDescriptionTextBlock").Text = "Distributed: Executes on a list of remote machines."
} else {
$window.FindName("BrowseButton").Visibility = "Collapsed"
$window.FindName("ExecutionModeDescriptionTextBlock").Text = "Local: Executes the script on this machine only."
}
})
# Browse Button for Distributed Mode
$window.FindName("BrowseButton").Add_Click({
# Load the Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
$fileDialog.Filter = "Supported Files (*.csv;*.txt;*.json)|*.csv;*.txt;*.json|CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|JSON Files (*.json)|*.json"
$fileDialog.Title = "Select Machine List"
if ($fileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$selectedFile = $fileDialog.FileName
$fileExtension = [System.IO.Path]::GetExtension($selectedFile).ToLower()
# Validate file extension
if ($fileExtension -notin @(".csv", ".txt", ".json")) {
$window.FindName("StatusTextBlock").Text = "Error: Selected file type is not supported."
} else {
Write-Host "Selected Machine List: $selectedFile"
$window.FindName("StatusTextBlock").Text = "Selected Machine List: $selectedFile"
Set-Variable -Name machineListPath -Value $selectedFile -Scope Script
}
}
})
# Script Mode Selection
$window.FindName("ModeSelector").Add_SelectionChanged({
$selectedMode = $window.FindName("ModeSelector").SelectedItem.Content
switch ($selectedMode) {
"Test Mode" {
$window.FindName("ModeDescriptionTextBlock").Text = "Test Mode: Simulates changes without applying them."
$window.FindName("WarningTextBlock").Visibility = "Collapsed"
$window.FindName("AcceptRisksCheckbox").Visibility = "Collapsed"
}
"Run Mode" {
$window.FindName("ModeDescriptionTextBlock").Text = "Run Mode: Applies changes to the machine."
$window.FindName("WarningTextBlock").Text = "Warning: Run Mode will make permanent changes to the machine."
$window.FindName("WarningTextBlock").Visibility = "Visible"
$window.FindName("AcceptRisksCheckbox").Visibility = "Visible"
}
"Verify Mode" {
$window.FindName("ModeDescriptionTextBlock").Text = "Verify Mode: Verifies compliance without making changes."
$window.FindName("WarningTextBlock").Visibility = "Collapsed"
$window.FindName("AcceptRisksCheckbox").Visibility = "Collapsed"
}
}
})
# Call the preload function during form initialization
Preload-STIGOptions
# Event handler for Advanced Options Toggle button
$window.FindName("AdvancedOptionsToggle").Add_Click({
$advancedPanel = $window.FindName("AdvancedOptionsPanel")
if ($advancedPanel.Visibility -eq "Visible") {
$advancedPanel.Visibility = "Collapsed"
$window.FindName("AdvancedOptionsToggle").Content = "Advanced Options"
} else {
$advancedPanel.Visibility = "Visible"
$window.FindName("AdvancedOptionsToggle").Content = "Hide Advanced Options"
}
})
# Event handler for Update STIGs button
$window.FindName("UpdateSTIGsButton").Add_Click({
# Prompt for confirmation
$confirmation = [System.Windows.MessageBox]::Show(
"This will download and update the STIG files. Do you want to continue?",
"Confirm Update",
[System.Windows.MessageBoxButton]::YesNo,
[System.Windows.MessageBoxImage]::Question
)
if ($confirmation -eq [System.Windows.MessageBoxResult]::Yes) {
# Update status text
$window.FindName("UpdateStatusTextBlock").Text = "Updating STIGs, please wait..."
# Run the UpdateSTIGS.ps1 script
try {
$updateScriptPath = "$PSScriptRoot\Modules\UpdateSTIGS.ps1"
if (Test-Path $updateScriptPath) {
# Execute the script and capture output
$output = & $updateScriptPath 2>&1
$window.FindName("UpdateStatusTextBlock").Text = "STIGs updated successfully."
# Log the output
Write-Host "UpdateSTIGS Output:" -ForegroundColor Green
Write-Host $output
# Reload the dropdown with new STIGs
Preload-STIGOptions
} else {
$window.FindName("UpdateStatusTextBlock").Text = "Error: UpdateSTIGS.ps1 not found."
Write-Host "Error: UpdateSTIGS.ps1 not found." -ForegroundColor Red
}
} catch {
$window.FindName("UpdateStatusTextBlock").Text = "Error updating STIGs: $_"
Write-Host "Error updating STIGs: $_" -ForegroundColor Red
}
} else {
# User chose not to proceed
$window.FindName("UpdateStatusTextBlock").Text = "Update canceled by user."
}
})
# Cancel Button
$window.FindName("CancelButton").Add_Click({
Write-Host "Script canceled by user."
$window.Close()
return
})
$window.FindName("RunButton").Add_Click({
try {
$executionMode = $window.FindName("ExecutionModeSelector").SelectedItem.Content
$selectedMode = $window.FindName("ModeSelector").SelectedItem.Content
$acceptRisks = $window.FindName("AcceptRisksCheckbox").IsChecked
$currentUser = $env:USERNAME
$logFileLink = $window.FindName("LogFileLink")
# Log file path for the session
$loggingPath = "$PSScriptRoot\Logs\STIG_Remediation.log"
# Ensure the log directory exists
if (-not (Test-Path -Path (Split-Path -Path $loggingPath))) {
New-Item -ItemType Directory -Path (Split-Path -Path $loggingPath) -Force | Out-Null
}
Write-Log "Run button clicked. Selected Mode: $selectedMode, Execution Mode: $executionMode"
# Validate and prepare machines list
$machines = Prepare-MachineList -ExecutionMode $executionMode -MachineListPath $machineListPath
Write-Log "Prepared machine list: $($machines -join ', ')"
# Ensure risks are accepted in Run Mode
if ($selectedMode -eq "Run Mode" -and -not $acceptRisks) {
throw "You must accept the risks to proceed in Run Mode."
}
# Update GUI Status
$window.FindName("StatusTextBlock").Text = "Script Running..."
Write-Log "Script status updated to running."
# Capture the selected STIG from the dropdown
$selectedSTIG = @($window.FindName("STIGDropdown").SelectedItems | ForEach-Object { $_.ToString() })
Write-Log "Processing Selected STIGs $selectedSTIG"
# Ensure a default value if nothing is selected
if (-not $selectedSTIG) {
Write-Log "No STIG selected. Processing all STIGs."
}
# Call Process-STIGFiles with the selected STIG
Process-STIGFiles -Machines $machines -ExecutionMode $executionMode -SelectedMode $selectedMode -SelectedSTIG $selectedSTIG
# Generate Compliance Report in Verify Mode
if ($selectedMode -eq "Verify Mode") {
$reportPath = "$PSScriptRoot\Logs\ComplianceReport.csv"
$Global:ComplianceReport | Select-Object MachineName, RuleID, Status, Expected, Actual, ValueName, KeyPath, Description | Export-Csv -Path $reportPath -NoTypeInformation -Force
Write-Log -Message "Verification complete. Compliance report saved to $reportPath."
Write-Host "Compliance report generated: $reportPath" -ForegroundColor Green
}
# Generate Test Report in Test Mode
if ($selectedMode -eq "Test Mode") {
$testReportPath = "$PSScriptRoot\Logs\TestReport.csv"
$Global:ComplianceReport | Select-Object MachineName, RuleID, Status, Expected, Actual, ValueName, KeyPath, Description | Export-Csv -Path $testReportPath -NoTypeInformation -Force
Write-Log -Message "Test complete. Test report saved to $testReportPath."
Write-Host "Test report generated: $testReportPath" -ForegroundColor Green
}
# Generate Run Mode Report in Run Mode
if ($selectedMode -eq "Run Mode") {
$runModeReportPath = "$PSScriptRoot\Logs\RunModeReport.csv"
$Global:ComplianceReport | Select-Object MachineName, RuleID, Status, Expected, Actual, ValueName, KeyPath, Description | Export-Csv -Path $runModeReportPath -NoTypeInformation -Force
Write-Log -Message "Run Mode complete. Run Mode report saved to $runModeReportPath."
Write-Host "Run Mode report generated: $runModeReportPath" -ForegroundColor Green
}
# Finalize
$window.FindName("StatusTextBlock").Text = "Script Completed!"
Write-Log "Script execution completed in $selectedMode mode."
# Update Log File Link
$window.FindName("LogFileLink").Tag = $loggingPath
$window.FindName("LogFileLink").Text = "View Log File"
$window.FindName("LogFileLink").Visibility = "Visible"
} catch {
$window.FindName("StatusTextBlock").Text = "Error: $_"
Write-Log "Error: $_" -IsError
}
})
# Log File Link Event Handler
$window.FindName("LogFileLink").Add_MouseLeftButtonUp({
$loggingPath = $window.FindName("LogFileLink").Tag
if (Test-Path $loggingPAth) {
try {
Start-Process -FilePath $loggingPath
} catch {
$window.FindName("StatusTextBlock").Text = "Error: Unable to open log file. $_"
Write-Log "Error: Unable to open log file. $_" -IsError
}
} else {
$window.FindName("StatusTextBlock").Text = "Error: Log file not found."
Write-Log "Error: Log file not found at $logFilePath" -IsError
}
})
# Set Log File Link After Processing
$window.FindName("LogFileLink").Dispatcher.BeginInvoke([action]{
$window.FindName("LogFileLink").Tag = $loggingPath
$window.FindName("LogFileLink").Text = "View Log File"
$window.FindName("LogFileLink").Visibility = "Visible"
})
# Show the Window
try {
$window.ShowDialog()
} catch {
Write-Host "Error displaying the WPF window: $_" -ForegroundColor Red
exit 1
}
Write-Log "All STIG Remediation Completed."