-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinAuditPoShGUI.ps1
More file actions
380 lines (348 loc) · 18.9 KB
/
WinAuditPoShGUI.ps1
File metadata and controls
380 lines (348 loc) · 18.9 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
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
<# WinAuditPoShGUI | by Timothy Gruber
Windows Auditing PowerShell GUI
Version: 2019.12.03.04
Designed and written by Timothy Gruber:
https://timothygruber.com
https://github.com/tjgruber/WinAuditPoshGUI
#>
#region Run script as elevated admin and unrestricted executionpolicy
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($myWindowsPrincipal.IsInRole($adminRole)) {
$Host.UI.RawUI.WindowTitle = "WinAuditPoShGUI | by Timothy Gruber"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
Clear-Host
} else {
Start-Process PowerShell.exe -ArgumentList "-ExecutionPolicy Unrestricted -NoExit $($script:MyInvocation.MyCommand.Path)" -Verb RunAs
Exit
}
#endregion
Write-Host "Running WinAuditPoShGUI | by Timothy Gruber...`n`nClosing this window will close WinAuditPoShGUI.`n"
<###############
Main Window
###############>
$syncHash = [hashtable]::Synchronized(@{ })
$mainRunspace = [runspacefactory]::CreateRunspace()
$mainRunspace.Name = "MainWindow"
$mainRunspace.ApartmentState = "STA"
$mainRunspace.ThreadOptions = "ReuseThread"
$mainRunspace.Open()
$mainRunspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$psMainWindow = [PowerShell]::Create().AddScript({
Add-Type -AssemblyName PresentationFramework
<###############
FUNCTIONS
###############>
function Get-AuditPolicy {
[CmdletBinding()]
Param(
[Parameter (Position=0)]
[string]$Category,
[Parameter (Position=1)]
[string]$Policy,
[Parameter (Position=2)]
[string]$SecuritySetting
)
$auditPolicy = (auditpol /get /Category:*)
$definedMatch = ($auditPolicy | Select-String $Policy) -replace "[ ]{2,}","" -replace "$Policy$Setting","$Policy $Setting"
$template = @"
{Policy*:$Policy} {SecuritySetting:Success}
{Policy*:$Policy} {SecuritySetting:Failure}
{Policy*:$Policy} {SecuritySetting:Success and Failure}
{Policy*:$Policy} {SecuritySetting:No Auditing}
"@
$definedMatch | ConvertFrom-String -TemplateContent $template
}#end Get-AuditPolicy
function Invoke-FileSystemSliderCheck {
[cmdletbinding()]
param()
$policyData = Get-AuditPolicy -Policy "File System"
switch ($policyData.SecuritySetting) {
'No Auditing' {
$syncHash.fileSystemSlider.Value = "0"
$syncHash.fileSystemOffLabel.Foreground = "Red"
$syncHash.fileSystemOnLabel.Foreground = "Black"
}
'Failure' {
$syncHash.fileSystemSlider.Value = "0"
$syncHash.fileSystemOffLabel.Foreground = "Red"
$syncHash.fileSystemOnLabel.Foreground = "Black"
}
'Success' {
$syncHash.fileSystemSlider.Value = "1"
$syncHash.fileSystemOnLabel.Foreground = "Green"
$syncHash.fileSystemOffLabel.Foreground = "Black"
}
'Success and Failure' {
$syncHash.fileSystemSlider.Value = "1"
$syncHash.fileSystemOnLabel.Foreground = "Green"
$syncHash.fileSystemOffLabel.Foreground = "Black"
}
Default {}
}
}#end Invoke-FileSystemSliderCheck
# Get-Folder function is from https://stackoverflow.com/a/57494414
function Get-Folder($initialDirectory) {
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.RootFolder = 'MyComputer'
if ($initialDirectory) { $FolderBrowserDialog.SelectedPath = $initialDirectory }
[void] $FolderBrowserDialog.ShowDialog()
return $FolderBrowserDialog.SelectedPath
}
function Invoke-FileSystemAuditDataTable {
[cmdletbinding()]
param()
$columns = @(
'Keys'
'Values'
)
$syncHash.dataTable = New-Object System.Data.DataTable
[void]$syncHash.dataTable.Columns.AddRange($columns)
foreach ($aclAuditProperty in $syncHash.fileFolderAuditing) {
$aclAuditObjects = [PSCustomObject]@(
@{'Folder Name:' = $syncHash.selectedFolder}
@{'Audit Rights:' = $aclAuditProperty.FileSystemRights}
@{'Audit Flags:' = $aclAuditProperty.AuditFlags}
@{'Audit Who?' = $aclAuditProperty.IdentityReference.Value}
@{'Is Inherited?' = [string]$aclAuditProperty.IsInherited}
)
foreach ($aclAuditObject in $aclAuditObjects) {
$dataTableRow = @()
foreach ($column in $columns) {
$dataTableRow += $aclAuditObject.$column
}
[void]$syncHash.dataTable.Rows.Add($dataTableRow)
}
}
$syncHash.fileSystemDataGrid.ItemsSource = $syncHash.dataTable.DefaultView
$syncHash.fileSystemDataGrid.GridLinesVisibility = "None"
$syncHash.fileSystemDataGrid.IsReadOnly = $True
$syncHash.fileSystemDataGrid.CanUserAddRows = $False
}#end Invoke-FileSystemAuditDataTable
function Invoke-SelectedFolderAclCheck {
[cmdletbinding()]
param(
[string]$selectedFolder
)
$syncHash.selectedFolder = $selectedFolder
$syncHash.fileFolderAuditing = (Get-Acl $syncHash.selectedFolder -Audit).Audit
if ($syncHash.fileFolderAuditing.FileSystemRights) {
Invoke-FileSystemAuditDataTable
$syncHash.fileSystemFolderGroupBox.Header = "Folder Auditing Details:"
$syncHash.fileSystemDataGrid.Visibility = "Visible"
if ($syncHash.fileFolderAuditing.Count -gt 1) {
$syncHash.Window.Height = "530"
$syncHash.fileSystemFolderInfoGrid.Height = "435"
$syncHash.fileSystemFolderGroupBox.Height = "240"
} else {
$syncHash.Window.Height = "450"
$syncHash.fileSystemFolderInfoGrid.Height = "355"
$syncHash.fileSystemFolderGroupBox.Height = "155"
}
$syncHash.fileSystemModifyFolderAuditingButton.Add_MouseEnter({
$syncHash.StatusBarText.Text = "Further modify auditing for selected folder."
})
$syncHash.fileSystemRemoveFolderAuditingButton.Add_MouseEnter({
$syncHash.StatusBarText.Text = "Remove all auditing for selected folder."
})
$syncHash.fileSystemEnableFolderAuditingButton.Visibility = "Hidden"
$syncHash.fileSystemEnableFolderAuditingButton.Width = "0"
$syncHash.fileSystemModifyFolderAuditingButton.Visibility = "Visible"
$syncHash.fileSystemRemoveFolderAuditingButton.Visibility = "Visible"
} else {
$syncHash.fileSystemFolderGroupBox.Header = "Auditing is not enabled for selected folder."
$syncHash.fileSystemDataGrid.Visibility = "Hidden"
$syncHash.fileSystemEnableFolderAuditingButton.Content = "Enable"
$syncHash.fileSystemEnableFolderAuditingButton.Add_MouseEnter({
$syncHash.StatusBarText.Text = "Enable 'Full' rights auditing for everyone on selected folder."
})
$syncHash.fileSystemRemoveFolderAuditingButton.Visibility = "Hidden"
$syncHash.fileSystemEnableFolderAuditingButton.Width = "332"
$syncHash.fileSystemEnableFolderAuditingButton.Visibility = "Visible"
$syncHash.Window.Height = "340"
$syncHash.fileSystemFolderGroupBox.Height = "50"
}
}#end Invoke-SelectedFolderAclCheck
########################
#END FUNCTIONS
########################
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WinAuditPoShGUI | by Timothy Gruber" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" VerticalAlignment="Top" Width="385" Height="325" ResizeMode="NoResize">
<Grid>
<DockPanel>
<StatusBar DockPanel.Dock="Bottom" BorderThickness="2,2,2,2" Background="#FFF1EDED">
<StatusBar.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FF494949" Offset="0"/>
<GradientStop Color="#FFBFBFBF" Offset="1"/>
</LinearGradientBrush>
</StatusBar.BorderBrush>
<StatusBarItem Margin="2,0,0,0">
<TextBlock Name="StatusBarText" Text="Ready..." />
</StatusBarItem>
</StatusBar>
<TabControl Margin="0,0,0,0.4">
<TabItem Header="File System" HorizontalAlignment="Left" VerticalAlignment="Top" TextOptions.TextFormattingMode="Display">
<Grid Name="fileSystemFolderInfoGrid" Margin="0" Height="245" HorizontalAlignment="Left" VerticalAlignment="Top">
<Label Content="Local File System Auditing is:" VerticalAlignment="Top" Height="30" FontSize="18" Padding="10,1" Margin="45,0,0,0" FontWeight="Bold" HorizontalAlignment="Left" Width="270"/>
<Slider Name="fileSystemSlider" Value="0" Width="80" Margin="135,43,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Maximum="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1"/>
<Label Name="fileSystemOffLabel" Content="OFF" HorizontalAlignment="Left" Margin="92,35,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="16"/>
<Label Name="fileSystemOnLabel" Content="ON" HorizontalAlignment="Left" Margin="220,35,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="16"/>
<Label Content="This is a global system setting and should be" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="33,72,0,0" Height="29" FontSize="14" Width="298" />
<Button Name="fileSystemSelectFolderButton" Content="Select Folder" Height="41" Margin="102,135,0,0" Width="150" FontSize="16" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label Content="ON" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="41,90,0,0" FontWeight="Bold" FontSize="14" />
<Label Content=" to enable any file and folder auditing." VerticalAlignment="Top" HorizontalAlignment="Left" Margin="66,90,0,0" Height="33" FontSize="14" Width="259" />
<GroupBox Name="fileSystemFolderGroupBox" Header="No Folder Selected..." HorizontalAlignment="Left" Margin="10,187,0,0" VerticalAlignment="Top" Width="344" Height="30">
<DockPanel>
<DockPanel DockPanel.Dock="Bottom" Margin="0,5,0,0">
<Button Name="fileSystemEnableFolderAuditingButton" DockPanel.Dock="Right" Visibility="Hidden" Content="Enable" FontSize="14" FontWeight="Medium" VerticalAlignment="Top" Padding="10,1" Width="164" HorizontalAlignment="Right" />
<Button Name="fileSystemModifyFolderAuditingButton" DockPanel.Dock="Right" Visibility="Hidden" Content="Modify" FontSize="14" FontWeight="Medium" VerticalAlignment="Top" Padding="10,1" Width="164" HorizontalAlignment="Right" />
<Button Name="fileSystemRemoveFolderAuditingButton" DockPanel.Dock="Left" Visibility="Hidden" Content="Remove" FontSize="14" FontWeight="Medium" VerticalAlignment="Top" Padding="10,1" Width="164" HorizontalAlignment="Left" />
</DockPanel>
<DataGrid DockPanel.Dock="Top" Name="fileSystemDataGrid" HorizontalScrollBarVisibility="Visible" SelectionMode="Single" HeadersVisibility="None" Visibility="Hidden">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Values}" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontWeight" Value="Medium" />
</DataTrigger>
<DataTrigger Binding="{Binding Keys}" Value="Folder Name:">
<Setter Property="Background" Value="#F3F3F3" />
<Setter Property="FontWeight" Value="Medium" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</DockPanel>
</GroupBox>
</Grid>
</TabItem>
</TabControl>
</DockPanel>
</Grid>
</Window>
"@
$xamlReader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load( $xamlReader )
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") |
ForEach-Object {
$syncHash.($_.Name) = $syncHash.Window.FindName($_.Name)
}
$syncHash.fileSystemSlider.Add_MouseEnter({
if ($syncHash.fileSystemSlider.Value -eq 0) {
$syncHash.StatusBarText.Text = "Click to switch ON"
} else {
$syncHash.StatusBarText.Text = "Click to switch OFF"
}
})
$syncHash.fileSystemSlider.Add_MouseLeave({
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
} else {
$syncHash.StatusBarText.Text = "Ready..."
}
})
$syncHash.fileSystemSlider.Add_ValueChanged({
switch ($syncHash.fileSystemSlider.Value) {
1 {
[void](auditpol /set /subcategory:"File System" /success:enable /failure:enable)
Invoke-FileSystemSliderCheck
}
Default {
[void](auditpol /set /subcategory:"File System" /success:disable /failure:disable)
Invoke-FileSystemSliderCheck
}
}
})
$syncHash.fileSystemSelectFolderButton.Add_MouseEnter({
$syncHash.StatusBarText.Text = "Select folder to enable auditing"
})
$syncHash.fileSystemSelectFolderButton.Add_MouseLeave({
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
} else {
$syncHash.StatusBarText.Text = "Ready..."
}
})
$syncHash.fileSystemSelectFolderButton.Add_Click({
$syncHash.selectedFolder = Get-Folder "$env:USERPROFILE"
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
$syncHash.fileSystemListBoxLabelFOLDERNAME_value.Content = $syncHash.selectedFolder
Invoke-SelectedFolderAclCheck -selectedFolder $syncHash.selectedFolder
}
})#end fileSystemSelectFolderButton.Add_Click
$syncHash.fileSystemEnableFolderAuditingButton.Add_MouseLeave({
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
} else {
$syncHash.StatusBarText.Text = "Ready..."
}
})
$syncHash.fileSystemModifyFolderAuditingButton.Add_MouseLeave({
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
} else {
$syncHash.StatusBarText.Text = "Ready..."
}
})
$syncHash.fileSystemRemoveFolderAuditingButton.Add_MouseLeave({
if ($syncHash.selectedFolder) {
$syncHash.StatusBarText.Text = "Selected folder: $($syncHash.selectedFolder)"
} else {
$syncHash.StatusBarText.Text = "Ready..."
}
})
$syncHash.fileSystemEnableFolderAuditingButton.Add_Click({
if ($syncHash.fileSystemEnableFolderAuditingButton.Content -eq "Enable") {
$fileSystemRights = "DeleteSubdirectoriesAndFiles, Modify, ChangePermissions, TakeOwnership"
$auditFlags = "Success, Failure"
$identityReference = "Everyone"
$inheritanceFlags = "ContainerInherit, ObjectInherit"
$PropagationFlags = "None"
$auditRule = New-Object System.Security.AccessControl.FileSystemAuditRule($identityReference,$fileSystemRights,$inheritanceFlags,$PropagationFlags,$auditFlags)
$selectedACL = Get-Acl -Path $syncHash.selectedFolder
$selectedACL.SetAuditRule($auditRule)
$selectedACL | Set-Acl -Path $syncHash.selectedFolder
Invoke-SelectedFolderAclCheck -selectedFolder $syncHash.selectedFolder
}
})
$syncHash.fileSystemRemoveFolderAuditingButton.Add_Click({
if ($syncHash.fileSystemRemoveFolderAuditingButton.Content -eq "Remove") {
$selectedACLAudit = Get-Acl -Path $syncHash.selectedFolder -Audit
$fileSystemRights = $selectedACLAudit.Audit.FileSystemRights
$auditFlags = $selectedACLAudit.Audit.AuditFlags
$identityReference = $selectedACLAudit.Audit.IdentityReference
$inheritanceFlags = $selectedACLAudit.Audit.InheritanceFlags
$PropagationFlags = $selectedACLAudit.Audit.PropagationFlags
$auditRule = New-Object System.Security.AccessControl.FileSystemAuditRule($identityReference,$fileSystemRights,$inheritanceFlags,$PropagationFlags,$auditFlags)
$selectedACL = Get-Acl -Path $syncHash.selectedFolder
$selectedACL.RemoveAuditRule($auditRule)
$selectedACL | Set-Acl -Path $syncHash.selectedFolder
Invoke-SelectedFolderAclCheck -selectedFolder $syncHash.selectedFolder
}
})
$syncHash.fileSystemModifyFolderAuditingButton.Add_Click({
if ($syncHash.fileSystemModifyFolderAuditingButton.Content -eq "Modify") {
$shellobj = New-Object -com Shell.Application
$folder = $shellobj.NameSpace("$($syncHash.selectedFolder)")
$folder.Self.InvokeVerb("Properties")
Invoke-SelectedFolderAclCheck -selectedFolder $syncHash.selectedFolder
}
})
Invoke-FileSystemSliderCheck
[Void]$syncHash.Window.ShowDialog()
$syncHash.Error = $Error
})
$psMainWindow.Runspace = $mainRunspace
[void]$psMainWindow.BeginInvoke()
########################
#END MAIN WINDOW
########################