-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200_Helpers.ps1
1139 lines (886 loc) · 45 KB
/
200_Helpers.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Test-IsRunningInConsole {
return $Host.Name -match "ConsoleHost"
}
function Convert-FiletimeToDatetime {
[OutputType([DateTime])]
[CmdletBinding()] Param(
[Parameter(Position = 1, Mandatory=$true)]
[Object] # FILETIME
$Filetime
)
[Int64]$Time = $Filetime.LowDateTime + $Filetime.HighDateTime * 0x100000000
[DateTime]::FromFileTimeUtc($Time)
}
function Convert-SidStringToSid {
[CmdletBinding()] Param(
[String]$Sid
)
try {
$IdentityUser = New-Object System.Security.Principal.NTAccount($(Convert-SidToName -Sid $Sid))
$IdentityUser.Translate([System.Security.Principal.SecurityIdentifier])
}
catch {
Write-Warning "$($MyInvocation.MyCommand) | Failed to translate SID: $($Sid)"
}
}
function Convert-SidToName {
<#
.SYNOPSIS
Helper - Converts a SID string to its corresponding username
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
This helper function takes a user SID as an input parameter and returns the account name associated to this SID. If an account name cannot be found, nothing is returned.
.PARAMETER Sid
A user account SID, e.g.: S-1-5-18.
.EXAMPLE
An example
PS C:\> Convert-SidToName -Sid S-1-5-18"
NT AUTHORITY\SYSTEM
#>
[OutputType([String])]
[CmdletBinding()] Param(
[String]$Sid
)
try {
$SidObj = New-Object System.Security.Principal.SecurityIdentifier($Sid)
$SidObj.Translate([System.Security.Principal.NTAccount]) | Select-Object -ExpandProperty Value
}
catch {
# In case of failure, return the SID.
$Sid
}
}
function Convert-PSidToStringSid {
[OutputType([String])]
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[IntPtr]$PSid
)
$StringSidPtr = [IntPtr]::Zero
$Success = $Advapi32::ConvertSidToStringSidW($PSid, [ref] $StringSidPtr)
if (-not $Success) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "ConvertSidToStringSidW - $([ComponentModel.Win32Exception] $LastError)"
return
}
$StringSid = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($StringSidPtr)
$Kernel32::LocalFree($StringSidPtr) | Out-Null
$StringSid
}
function Convert-PSidToNameAndType {
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[IntPtr]$PSid
)
$SidType = 0
$NameSize = 256
$Name = New-Object -TypeName System.Text.StringBuilder
$Name.EnsureCapacity(256) | Out-Null
$DomainSize = 256
$Domain = New-Object -TypeName System.Text.StringBuilder
$Domain.EnsureCapacity(256) | Out-Null
$Success = $Advapi32::LookupAccountSid($null, $PSid, $Name, [ref]$NameSize, $Domain, [ref]$DomainSize, [ref]$SidType)
if (-not $Success) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "LookupAccountSid - $([ComponentModel.Win32Exception] $LastError)"
return
}
if ([String]::IsNullOrEmpty($Domain)) {
$DisplayName = "$($Name)"
}
else {
$DisplayName = "$($Domain)\$($Name)"
}
$Result = New-Object -TypeName PSObject
$Result | Add-Member -MemberType "NoteProperty" -Name "DisplayName" -Value $DisplayName
$Result | Add-Member -MemberType "NoteProperty" -Name "Name" -Value $Name
$Result | Add-Member -MemberType "NoteProperty" -Name "Domain" -Value $Domain
$Result | Add-Member -MemberType "NoteProperty" -Name "Type" -Value $SidType
$Result
}
function Convert-PSidToRid {
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[IntPtr]$PSid
)
$SubAuthorityCountPtr = $Advapi32::GetSidSubAuthorityCount($PSid)
$SubAuthorityCount = [Runtime.InteropServices.Marshal]::ReadByte($SubAuthorityCountPtr)
$SubAuthorityPtr = $Advapi32::GetSidSubAuthority($PSid, $SubAuthorityCount - 1)
$SubAuthority = [UInt32] [Runtime.InteropServices.Marshal]::ReadInt32($SubAuthorityPtr)
$SubAuthority
}
function Convert-DateToString {
<#
.SYNOPSIS
Helper - Converts a DateTime object to a string representation
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
The output string is a simplified version of the ISO format: YYYY-MM-DD hh:mm:ss.
.PARAMETER Date
A System.DateTime object
.EXAMPLE
PS C:\> $Date = Get-Date; Convert-DateToString -Date $Date
2020-01-16 - 10:26:11
#>
[OutputType([String])]
[CmdletBinding()] Param(
[System.DateTime]$Date
)
if ($null -ne $Date) {
$OutString = ""
$OutString += $Date.ToString('yyyy-MM-dd - HH:mm:ss')
$OutString
}
}
function Convert-DosDeviceToDevicePath {
<#
.SYNOPSIS
Helper - Convert a DOS device name (e.g. C:) to its device path
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
This function leverages the QueryDosDevice API to get the path of a DOS device (e.g. C: -> \Device\HarddiskVolume4)
.PARAMETER DosDevice
A DOS device name such as C:
#>
[OutputType([String])]
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[String]$DosDevice
)
$TargetPathLen = 260
$TargetPathPtr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($TargetPathLen * 2)
$TargetPathLen = $Kernel32::QueryDosDevice($DosDevice, $TargetPathPtr, $TargetPathLen)
if ($TargetPathLen -eq 0) {
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($TargetPathPtr)
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "QueryDosDevice('$($DosDevice)') - $([ComponentModel.Win32Exception] $LastError)"
return
}
[System.Runtime.InteropServices.Marshal]::PtrToStringUni($TargetPathPtr)
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($TargetPathPtr)
}
function Get-WindowsVersion {
[CmdletBinding()] Param()
$RegKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$RegItem = Get-ItemProperty -Path "Registry::$($RegKey)" -ErrorAction SilentlyContinue
if ($null -eq $RegItem) {
[System.Environment]::OSVersion.Version
return
}
$Major = $RegItem.CurrentMajorVersionNumber
$Minor = $RegItem.CurrentMinorVersionNumber
if ($null -eq $Major) { $Major = $RegItem.CurrentVersion.Split(".")[0] }
if ($null -eq $Minor) { $Minor = $RegItem.CurrentVersion.Split(".")[1] }
$Result = New-Object -TypeName PSObject
$Result | Add-Member -MemberType "NoteProperty" -Name "Major" -Value ([UInt32] $Major)
$Result | Add-Member -MemberType "NoteProperty" -Name "Minor" -Value ([UInt32] $Minor)
$Result | Add-Member -MemberType "NoteProperty" -Name "Build" -Value ([UInt32] $RegItem.CurrentBuildNumber)
$Result | Add-Member -MemberType "NoteProperty" -Name "Revision" -Value 0
$Result | Add-Member -MemberType "NoteProperty" -Name "MajorRevision" -Value 0
$Result | Add-Member -MemberType "NoteProperty" -Name "MinorRevision" -Value 0
$Result | Add-Member -MemberType "NoteProperty" -Name "ReleaseId" -Value $RegItem.ReleaseId
$Result | Add-Member -MemberType "NoteProperty" -Name "UBR" -Value $RegItem.UBR
$Result | Add-Member -MemberType "NoteProperty" -Name "ProductName" -Value $RegItem.ProductName
$Result
}
function Test-IsMicrosoftFile {
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[Object]$File
)
if ($File.VersionInfo.LegalCopyright -like "*Microsoft Corporation*") {
return $true
}
return $false
}
function Get-FileDacl {
<#
.SYNOPSIS
Get security information about a file.
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
This function leverages the Windows API to get some security information about a file, such as the owner and the DACL.
.PARAMETER Path
The path of a file such as "C:\Windows\win.ini", "\\.pipe\spoolss"
.EXAMPLE
PS C:\> Get-FileDacl -Path C:\Windows\win.ini
Path : C:\Windows\win.ini
Owner : NT AUTHORITY\SYSTEM
OwnerSid : S-1-5-18
Group : NT AUTHORITY\SYSTEM
GroupSid : S-1-5-18
Access : {System.Security.AccessControl.CommonAce, System.Security.AccessControl.CommonAce, System.Security.AccessCon
trol.CommonAce, System.Security.AccessControl.CommonAce...}
SDDL : O:SYG:SYD:AI(A;ID;FA;;;SY)(A;ID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;ID;0x1200a9;;;AC)(A;ID;0x1200a9;;;S-1-15-2-2)
.EXAMPLE
PS C:\> Get-FileDacl -Path \\.\pipe\spoolss
Path : \\.\pipe\spoolss
Owner : NT AUTHORITY\SYSTEM
OwnerSid : S-1-5-18
Group : NT AUTHORITY\SYSTEM
GroupSid : S-1-5-18
Access : {System.Security.AccessControl.CommonAce, System.Security.AccessControl.CommonAce, System.Security.AccessControl.CommonAce, System.Security.AccessControl.CommonAce...}
SDDL : O:SYG:SYD:(A;;0x100003;;;BU)(A;;0x1201bb;;;WD)(A;;0x1201bb;;;AN)(A;;FA;;;CO)(A;;FA;;;SY)(A;;FA;;;BA)
#>
[CmdletBinding()] Param(
[String]$Path
)
$DesiredAccess = $FileAccessRightsEnum::ReadControl
$ShareMode = 0x00000001 # FILE_SHARE_READ
$CreationDisposition = 3 # OPEN_EXISTING
$FlagsAndAttributes = 0x80 # FILE_ATTRIBUTE_NORMAL
$FileHandle = $Kernel32::CreateFile($Path, $DesiredAccess, $ShareMode, [IntPtr]::Zero, $CreationDisposition, $FlagsAndAttributes, [IntPtr]::Zero)
if ($FileHandle -eq [IntPtr]-1) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "CreateFile KO - $([ComponentModel.Win32Exception] $LastError)"
return
}
$ObjectType = 6 # SE_KERNEL_OBJECT
$SecurityInfo = 7 # DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION
$SidOwnerPtr = [IntPtr]::Zero
$SidGroupPtr = [IntPtr]::Zero
$DaclPtr = [IntPtr]::Zero
$SaclPtr = [IntPtr]::Zero
$SecurityDescriptorPtr = [IntPtr]::Zero
$Result = $Advapi32::GetSecurityInfo($FileHandle, $ObjectType, $SecurityInfo, [ref]$SidOwnerPtr, [ref]$SidGroupPtr, [ref]$DaclPtr, [ref]$SaclPtr, [ref]$SecurityDescriptorPtr)
if ($Result -ne 0) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "GetSecurityInfo KO ($Result) - $([ComponentModel.Win32Exception] $LastError)"
$Kernel32::CloseHandle($FileHandle) | Out-Null
return
}
$OwnerSidString = Convert-PSidToStringSid -PSid $SidOwnerPtr
$OwnerSidInfo = Convert-PSidToNameAndType -PSid $SidOwnerPtr
$GroupSidString = Convert-PSidToStringSid -PSid $SidGroupPtr
$GroupSidInfo = Convert-PSidToNameAndType -PSid $SidGroupPtr
$SecurityDescriptorString = ""
$SecurityDescriptorStringLen = 0
$Success = $Advapi32::ConvertSecurityDescriptorToStringSecurityDescriptor($SecurityDescriptorPtr, 1, $SecurityInfo, [ref]$SecurityDescriptorString, [ref]$SecurityDescriptorStringLen)
if (-not $Success) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "ConvertSecurityDescriptorToStringSecurityDescriptor KO ($Result) - $([ComponentModel.Win32Exception] $LastError)"
$Kernel32::LocalFree($SecurityDescriptorPtr) | Out-Null
$Kernel32::CloseHandle($FileHandle) | Out-Null
return
}
$SecurityDescriptorNewPtr = [IntPtr]::Zero
$SecurityDescriptorNewSize = 0
$Success = $Advapi32::ConvertStringSecurityDescriptorToSecurityDescriptor($SecurityDescriptorString, 1, [ref]$SecurityDescriptorNewPtr, [ref]$SecurityDescriptorNewSize)
if (-not $Success) {
$LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Verbose "ConvertStringSecurityDescriptorToSecurityDescriptor KO ($Result) - $([ComponentModel.Win32Exception] $LastError)"
$Kernel32::LocalFree($SecurityDescriptorPtr) | Out-Null
$Kernel32::CloseHandle($FileHandle) | Out-Null
return
}
$SecurityDescriptorNewBytes = New-Object Byte[]($SecurityDescriptorNewSize)
for ($i = 0; $i -lt $SecurityDescriptorNewSize; $i++) {
$Offset = [IntPtr] ($SecurityDescriptorNewPtr.ToInt64() + $i)
$SecurityDescriptorNewBytes[$i] = [Runtime.InteropServices.Marshal]::ReadByte($Offset)
}
$RawSecurityDescriptor = New-Object Security.AccessControl.RawSecurityDescriptor -ArgumentList $SecurityDescriptorNewBytes, 0
$Result = New-Object -TypeName PSObject
$Result | Add-Member -MemberType "NoteProperty" -Name "Path" -Value $Path
$Result | Add-Member -MemberType "NoteProperty" -Name "Owner" -Value $OwnerSidInfo.DisplayName
$Result | Add-Member -MemberType "NoteProperty" -Name "OwnerSid" -Value $OwnerSidString
$Result | Add-Member -MemberType "NoteProperty" -Name "Group" -Value $GroupSidInfo.DisplayName
$Result | Add-Member -MemberType "NoteProperty" -Name "GroupSid" -Value $GroupSidString
$Result | Add-Member -MemberType "NoteProperty" -Name "Access" -Value $RawSecurityDescriptor.DiscretionaryAcl
$Result | Add-Member -MemberType "NoteProperty" -Name "SDDL" -Value $SecurityDescriptorString
$Result
$Kernel32::LocalFree($SecurityDescriptorNewPtr) | Out-Null
$Kernel32::LocalFree($SecurityDescriptorPtr) | Out-Null
$Kernel32::CloseHandle($FileHandle) | Out-Null
}
function Get-CurrentUserSids {
[CmdletBinding()] Param()
if ($null -eq $global:CachedCurrentUserSids) {
$UserIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$global:CachedCurrentUserSids = $UserIdentity.Groups | Select-Object -ExpandProperty Value
$global:CachedCurrentUserSids += $UserIdentity.User.Value
}
$global:CachedCurrentUserSids
}
function Get-CurrentUserDenySids {
[CmdletBinding()] Param()
if ($null -eq $global:CachedCurrentUserDenySids) {
$global:CachedCurrentUserDenySids = [string[]](Get-TokenInformationGroups -InformationClass Groups | Where-Object { $_.Attributes.Equals("UseForDenyOnly") } | Select-Object -ExpandProperty SID)
if ($null -eq $global:CachedCurrentUserDenySids) {
$global:CachedCurrentUserDenySids = @()
}
}
$global:CachedCurrentUserDenySids
}
function Get-AclModificationRights {
<#
.SYNOPSIS
Helper - Enumerates modification rights the current user has on an object.
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
This cmdlet retrieves the ACL of an object and returns the ACEs that grant modification permissions to the current user. It should be noted that, in case of deny ACEs, restricted rights are removed from the permission list of the ACEs.
.PARAMETER Path
The full path of a securable object.
.PARAMETER Type
The target object type (e.g. "File").
.EXAMPLE
PS C:\> Get-AclModificationRights -Path C:\Temp\foo123.txt -Type File
ModifiablePath : C:\Temp\foo123.txt
IdentityReference : NT AUTHORITY\Authenticated Users
Permissions : Delete, WriteAttributes, Synchronize, ReadControl, ReadData, AppendData, WriteExtendedAttributes,
ReadAttributes, WriteData, ReadExtendedAttributes, Execute
.EXAMPLE
PS C:\> Get-AclModificationRights -Path C:\Temp\deny-delete.txt -Type File
ModifiablePath : C:\Temp\deny-delete.txt
IdentityReference : NT AUTHORITY\Authenticated Users
Permissions : WriteAttributes, Synchronize, ReadControl, ReadData, AppendData, WriteExtendedAttributes,
ReadAttributes, WriteData, ReadExtendedAttributes, Execute
.EXAMPLE
PS C:\> Get-AclModificationRights -Path C:\Temp\deny-write.txt -Type File
ModifiablePath : C:\Temp\deny-write.txt
IdentityReference : NT AUTHORITY\Authenticated Users
Permissions : Delete, Synchronize, ReadControl, ReadData, ReadAttributes, ReadExtendedAttributes, Execute
#>
[CmdletBinding()] Param(
[String]
$Path,
[ValidateSet("File", "Directory", "RegistryKey")]
[String]
$Type
)
BEGIN {
$TypeFile = "File"
$TypeDirectory = "Directory"
$TypeRegistryKey = "RegistryKey"
$FileAccessMask = @{
[UInt32]'0x80000000' = 'GenericRead'
[UInt32]'0x40000000' = 'GenericWrite'
[UInt32]'0x20000000' = 'GenericExecute'
[UInt32]'0x10000000' = 'GenericAll'
[UInt32]'0x02000000' = 'MaximumAllowed'
[UInt32]'0x01000000' = 'AccessSystemSecurity'
[UInt32]'0x00100000' = 'Synchronize'
[UInt32]'0x00080000' = 'WriteOwner'
[UInt32]'0x00040000' = 'WriteDAC'
[UInt32]'0x00020000' = 'ReadControl'
[UInt32]'0x00010000' = 'Delete'
[UInt32]'0x00000100' = 'WriteAttributes'
[UInt32]'0x00000080' = 'ReadAttributes'
[UInt32]'0x00000040' = 'DeleteChild'
[UInt32]'0x00000020' = 'Execute'
[UInt32]'0x00000010' = 'WriteExtendedAttributes'
[UInt32]'0x00000008' = 'ReadExtendedAttributes'
[UInt32]'0x00000004' = 'AppendData'
[UInt32]'0x00000002' = 'WriteData'
[UInt32]'0x00000001' = 'ReadData'
}
$DirectoryAccessMask = @{
[UInt32]'0x80000000' = 'GenericRead'
[UInt32]'0x40000000' = 'GenericWrite'
[UInt32]'0x20000000' = 'GenericExecute'
[UInt32]'0x10000000' = 'GenericAll'
[UInt32]'0x02000000' = 'MaximumAllowed'
[UInt32]'0x01000000' = 'AccessSystemSecurity'
[UInt32]'0x00100000' = 'Synchronize'
[UInt32]'0x00080000' = 'WriteOwner'
[UInt32]'0x00040000' = 'WriteDAC'
[UInt32]'0x00020000' = 'ReadControl'
[UInt32]'0x00010000' = 'Delete'
[UInt32]'0x00000100' = 'WriteAttributes'
[UInt32]'0x00000080' = 'ReadAttributes'
[UInt32]'0x00000040' = 'DeleteChild'
[UInt32]'0x00000020' = 'Traverse'
[UInt32]'0x00000010' = 'WriteExtendedAttributes'
[UInt32]'0x00000008' = 'ReadExtendedAttributes'
[UInt32]'0x00000004' = 'AddSubdirectory'
[UInt32]'0x00000002' = 'AddFile'
[UInt32]'0x00000001' = 'ListDirectory'
}
$RegistryKeyAccessMask = @{
# Generic access rights
[UInt32]'0x10000000' = 'GenericAll'
[UInt32]'0x20000000' = 'GenericExecute'
[UInt32]'0x40000000' = 'GenericWrite'
[UInt32]'0x80000000' = 'GenericRead'
# Registry key access rights
[UInt32]'0x00000001' = 'QueryValue'
[UInt32]'0x00000002' = 'SetValue'
[UInt32]'0x00000004' = 'CreateSubKey'
[UInt32]'0x00000008' = 'EnumerateSubKeys'
[UInt32]'0x00000010' = 'Notify'
[UInt32]'0x00000020' = 'CreateLink'
# Valid standard access rights for registry keys
[UInt32]'0x00010000' = 'Delete'
[UInt32]'0x00020000' = 'ReadControl'
[UInt32]'0x00040000' = 'WriteDAC'
[UInt32]'0x00080000' = 'WriteOwner'
}
$AccessMask = @{
$TypeFile = $FileAccessMask
$TypeDirectory = $DirectoryAccessMask
$TypeRegistryKey = $RegistryKeyAccessMask
}
$AccessRights = @{
$TypeFile = "FileSystemRights"
$TypeDirectory = "FileSystemRights"
$TypeRegistryKey = "RegistryRights"
}
$ModificationRights = @{
$TypeFile = @('GenericWrite', 'GenericAll', 'MaximumAllowed', 'WriteOwner', 'WriteDAC', 'Delete', 'WriteData', 'AppendData')
$TypeDirectory = @('GenericWrite', 'GenericAll', 'MaximumAllowed', 'WriteOwner', 'WriteDAC', 'Delete', 'AddFile', 'AddSubdirectory')
$TypeRegistryKey = @('SetValue', 'CreateSubKey', 'Delete', 'WriteDAC', 'WriteOwner')
}
$CurrentUserSids = Get-CurrentUserSids
$CurrentUserDenySids = Get-CurrentUserDenySids
$ResolvedIdentities = @{}
function Convert-NameToSid {
Param([String]$Name)
if (($Name -match '^S-1-5.*') -or ($Name -match '^S-1-15-.*')) { $Name; return }
if (-not ($ResolvedIdentities[$Name])) {
$Identity = New-Object System.Security.Principal.NTAccount($Name)
try {
$ResolvedIdentities[$Name] = $Identity.Translate([System.Security.Principal.SecurityIdentifier]) | Select-Object -ExpandProperty Value
}
catch {
$null = $_
}
}
$ResolvedIdentities[$Name]
}
}
PROCESS {
try {
# First things first, try to get the ACL of the object given its path.
$Acl = Get-Acl -Path $Path -ErrorAction SilentlyContinue -ErrorVariable GetAclError
if ($GetAclError) { return }
# If no ACL is returned, it means that the object has a "null" DACL, in which case everyone is
# granted full access to the object. We can therefore simply return a "virtual" ACE that grants
# Everyone the "FullControl" right and exit.
if ($null -eq $Acl) {
$Result = New-Object -TypeName PSObject
$Result | Add-Member -MemberType "NoteProperty" -Name "ModifiablePath" -Value $Path
$Result | Add-Member -MemberType "NoteProperty" -Name "IdentityReference" -Value (Convert-SidToName -Sid "S-1-1-0")
$Result | Add-Member -MemberType "NoteProperty" -Name "Permissions" -Value "GenericAll"
$Result
return
}
$DenyAces = [Object[]]($Acl | Select-Object -ExpandProperty Access | Where-Object { $_.AccessControlType -match "Deny" })
$AllowAces = [Object[]]($Acl | Select-Object -ExpandProperty Access | Where-Object { $_.AccessControlType -match "Allow" })
# Here we simply get the access mask, access list name and list of access rights that are
# specific to the object type we are dealing with.
$TypeAccessMask = $AccessMask[$Type]
$TypeAccessRights = $AccessRights[$Type]
$TypeModificationRights = $ModificationRights[$Type]
# Before checking the object permissions, we first need to enumerate deny ACEs (if any) that
# would restrict the rights we may have on the target object.
$RestrictedRights = @()
if ($DenyAces) { # Need to make sure it not null because of PSv2
foreach ($DenyAce in $DenyAces) {
# Ignore "InheritOnly" ACEs because they only apply to child objects, not to the object itself
# (e.g.: a file in a directory or a sub-key of a registry key).
if ($DenyAce.PropagationFlags -band ([System.Security.AccessControl.PropagationFlags]"InheritOnly").value__) { continue }
# Convert the ACE's identity reference name to its SID. If the SID is not in the list
# of deny-only SIDs of the current Token, ignore it. If the SID does not match the
# current user SID or the SID of any of its groups, ignore it as well.
# Note: deny-only SIDs are only used to check access-denied ACEs.
# https://docs.microsoft.com/en-us/windows/win32/secauthz/sid-attributes-in-an-access-token
$IdentityReferenceSid = Convert-NameToSid -Name $DenyAce.IdentityReference
if ($CurrentUserDenySids -notcontains $IdentityReferenceSid) { continue }
if ($CurrentUserSids -notcontains $IdentityReferenceSid) { continue }
$Restrictions = $TypeAccessMask.Keys | Where-Object { $DenyAce.$TypeAccessRights.value__ -band $_ } | ForEach-Object { $TypeAccessMask[$_] }
$RestrictedRights += [String[]]$Restrictions
}
}
# Need to make sure it not null because of PSv2
if ($AllowAces) {
foreach ($AllowAce in $AllowAces) {
# Ignore "InheritOnly" ACEs because they only apply to child objects, not to the object itself
# (e.g.: a file in a directory or a sub-key of a registry key).
if ($AllowAce.PropagationFlags -band ([System.Security.AccessControl.PropagationFlags]"InheritOnly").value__) { continue }
# Here, we simply extract the permissions granted by the current ACE
$Permissions = New-Object System.Collections.ArrayList
$TypeAccessMask.Keys | Where-Object { $AllowAce.$TypeAccessRights.value__ -band $_ } | ForEach-Object { $null = $Permissions.Add($TypeAccessMask[$_]) }
# ... and we remove any right that would be restricted due to deny ACEs.
if ($RestrictedRights) {
foreach ($RestrictedRight in $RestrictedRights) {
$null = $Permissions.Remove($RestrictedRight)
}
}
# Here, we filter out ACEs that do not apply to the current user by checking whether the ACE's
# identity reference is in the current user's SID list.
$IdentityReferenceSid = Convert-NameToSid -Name $AllowAce.IdentityReference
if ($CurrentUserSids -notcontains $IdentityReferenceSid) { continue }
# We compare the list of permissions (minus the potential restrictions) againts a list of
# predefined modification rights. If there is no match, we ignore the ACE.
$Comparison = Compare-Object -ReferenceObject $Permissions -DifferenceObject $TypeModificationRights -IncludeEqual -ExcludeDifferent
if (-not $Comparison) { continue }
$Result = New-Object -TypeName PSObject
$Result | Add-Member -MemberType "NoteProperty" -Name "ModifiablePath" -Value $Path
$Result | Add-Member -MemberType "NoteProperty" -Name "IdentityReference" -Value $AllowAce.IdentityReference
$Result | Add-Member -MemberType "NoteProperty" -Name "Permissions" -Value $Permissions
$Result
}
}
}
catch {
Write-Debug "Could not handle path: $($Path)"
}
}
}
function Get-ModifiablePath {
<#
.SYNOPSIS
Parses a passed string containing multiple possible file/folder paths and returns the file paths where the current user has modification rights.
Author: @harmj0y
License: BSD 3-Clause
.DESCRIPTION
Takes a complex path specification of an initial file/folder path with possible configuration files, 'tokenizes' the string in a number of possible ways, and enumerates the ACLs for each path that currently exists on the system. Any path that the current user has modification rights on is returned in a custom object that contains the modifiable path, associated permission set, and the IdentityReference with the specified rights. The SID of the current user and any group he/she are a part of are used as the comparison set against the parsed path DACLs.
@itm4n: I made some small changes to the original code in order to prevent false positives as much as possible.
.PARAMETER Path
The string path to parse for modifiable files. Required
.PARAMETER LiteralPaths
Switch. Treat all paths as literal (i.e. don't do 'tokenization').
.EXAMPLE
PS C:\> '"C:\Temp\blah.exe" -f "C:\Temp\config.ini"' | Get-ModifiablePath
Path Permissions IdentityReference
---- ----------- -----------------
C:\Temp\blah.exe {ReadAttributes, ReadCo... NT AUTHORITY\Authentic...
C:\Temp\config.ini {ReadAttributes, ReadCo... NT AUTHORITY\Authentic...
.EXAMPLE
PS C:\> Get-ChildItem C:\Vuln\ -Recurse | Get-ModifiablePath
Path Permissions IdentityReference
---- ----------- -----------------
C:\Vuln\blah.bat {ReadAttributes, ReadCo... NT AUTHORITY\Authentic...
C:\Vuln\config.ini {ReadAttributes, ReadCo... NT AUTHORITY\Authentic...
...
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
[String[]]
$Path,
[Switch]
$LiteralPaths
)
BEGIN {
function Get-FirstExistingParentFolder {
Param(
[String]$Path
)
try {
$ParentPath = Split-Path $Path -Parent
if ($ParentPath -and $(Test-Path -Path $ParentPath -ErrorAction SilentlyContinue)) {
Resolve-Path -Path $ParentPath | Select-Object -ExpandProperty "Path"
}
else {
Get-FirstExistingParentFolder -Path $ParentPath
}
}
catch {
$null = $_
}
}
}
PROCESS {
foreach ($TargetPath in $Path) {
$CandidatePaths = @()
# possible separator character combinations
$SeparationCharacterSets = @('"', "'", ' ', "`"'", '" ', "' ", "`"' ")
if ($PSBoundParameters['LiteralPaths']) {
$TempPath = $([System.Environment]::ExpandEnvironmentVariables($TargetPath))
if (Test-Path -Path $TempPath -ErrorAction SilentlyContinue) {
$ResolvedPath = Resolve-Path -Path $TempPath | Select-Object -ExpandProperty Path
$CandidatePaths += $ResolvedPath
# If the path corresponds to a file, we want to check its parent directory as well. There are cases
# where the target file is configured with secure permissions but a user can still add files in the
# same folder. In such case, a DLL proxying attack is still possible.
if ($(Get-Item -Path $ResolvedPath -Force) -is [System.IO.FileInfo]) {
$CandidatePaths += Get-FirstExistingParentFolder -Path $ResolvedPath
}
}
else {
# If the path doesn't correspond to an existing file or directory, find the first existing parent
# directory (if such directory exists) and add it to the list of candidate paths.
$CandidatePaths += Get-FirstExistingParentFolder -Path $TempPath
}
}
else {
$TargetPath = $([System.Environment]::ExpandEnvironmentVariables($TargetPath)).Trim()
foreach ($SeparationCharacterSet in $SeparationCharacterSets) {
$TargetPath.Split($SeparationCharacterSet) | Where-Object { $_ -and (-not [String]::IsNullOrEmpty($_.trim())) } | ForEach-Object {
if (-not ($_ -match "^[A-Z]:`$")) {
if ($SeparationCharacterSet -notmatch ' ') {
$TempPath = $([System.Environment]::ExpandEnvironmentVariables($_)).Trim()
# If the candidate path is something like '/svc', skip it because it will be interpreted as
# 'C:\svc'. It should filter out a lot of false postives. There is also a small chance that
# it will exclude actual vulnerable paths in some very particular cases where a path such
# as '/Temp/Something' is used as an argument. This seems very unlikely though.
if ((-not ($TempPath -Like "/*")) -and (-not ($TempPath -match "^[A-Z]:`$"))) {
if (-not [String]::IsNullOrEmpty($TempPath)) {
# Does the object exist? Be it a file or a directory.
if (Test-Path -Path $TempPath -ErrorAction SilentlyContinue) {
$ResolvedPath = Resolve-Path -Path $TempPath | Select-Object -ExpandProperty Path
$CandidatePaths += $ResolvedPath
# If the path corresponds to a file, we want to check its parent directory as well. There are cases
# where the target file is configured with secure permissions but a user can still add files in the
# same folder. In such case, a DLL proxying attack is still possible.
if ($(Get-Item -Path $ResolvedPath -Force) -is [System.IO.FileInfo]) {
$CandidatePaths += Get-FirstExistingParentFolder -Path $ResolvedPath
}
}
else {
# If the path doesn't correspond to an existing file or directory, find the first existing parent
# directory (if such directory exists) and add it to the list of candidate paths.
$CandidatePaths += Get-FirstExistingParentFolder -Path $TempPath
}
}
}
}
else {
# if the separator contains a space
$CandidatePaths += Resolve-Path -Path $([System.Environment]::ExpandEnvironmentVariables($_)) -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path | ForEach-Object {$_.Trim()} | Where-Object { (-not [String]::IsNullOrEmpty($_)) -and (Test-Path -Path $_) }
}
}
else {
Write-Verbose "DEBUG: Got a drive letter as a path: $_"
}
}
}
}
foreach ($CandidatePath in $($CandidatePaths | Sort-Object -Unique)) {
$CandidateItem = Get-Item -Path $CandidatePath -ErrorAction SilentlyContinue
if (-not $CandidateItem) { continue }
if ($CandidateItem -is [System.IO.DirectoryInfo]) {
Get-AclModificationRights -Path $CandidateItem.FullName -Type Directory
}
else {
Get-AclModificationRights -Path $CandidateItem.FullName -Type File
}
}
}
}
}
function Get-UnquotedPath {
[OutputType([String])]
[CmdletBinding()] Param(
[Parameter(Mandatory=$true)]
[String]$Path,
[Switch]$Spaces = $false
)
# Check Check if the path starts with a " or '
if ($Path.StartsWith("`"") -or $Path.StartsWith("'")) { return }
# Extract EXE path
$BinPath = $Path.SubString(0, $Path.ToLower().IndexOf(".exe") + 4)
# If we don't have to check for spaces, return the path
if (-not $Spaces) { return $BinPath }
# Check if it contains spaces
If ($BinPath -notmatch ".* .*") { return }
return $BinPath
}
function Get-ExploitableUnquotedPath {
<#
.SYNOPSIS
Helper - Parse a path, determine if it's "unquoted" and check whether it's exploitable.
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
Parse a path, determine if it's "unquoted" and check whether it's exploitable.
.PARAMETER Path
A path (or a command line for example)
#>
[CmdletBinding()] Param(
[String]$Path
)
BEGIN {
$PermissionsAddFile = @("AddFile", "DeleteChild", "WriteDAC", "WriteOwner")
}
PROCESS {
$UnquotedPath = Get-UnquotedPath -Path $Path -Spaces
if ([String]::IsNullOrEmpty($UnquotedPath)) { return }
Write-Verbose "Found an unquoted path that contains spaces: $($UnquotedPath)"
# Split path and build candidates paths
$SplitPathArray = $UnquotedPath.Split(' ')
$ConcatPathArray = @()
for ($i=0; $i -lt $SplitPathArray.Count; $i++) {
$ConcatPathArray += $SplitPathArray[0..$i] -join ' '
}
foreach ($ConcatPath in $ConcatPathArray) {
# We exclude the binary path itself
if ($ConcatPath -like $UnquotedPath) { continue }
# Get parent folder. Split-Path does not handle errors nicely so catch exceptions
# and continue on failure.
try { $BinFolder = Split-Path -Path $ConcatPath -Parent -ErrorAction SilentlyContinue } catch { continue }
# Split-Path failed without throwing an exception, so ignore and continue.
if ( $null -eq $BinFolder) { continue }
# If the parent folder does not exist, ignore and continue.
if ( -not (Test-Path -Path $BinFolder -ErrorAction SilentlyContinue) ) { continue }
# The parent folder exists, check if it is modifiable.
$ModifiablePaths = $BinFolder | Get-ModifiablePath | Where-Object { $_ -and (-not [String]::IsNullOrEmpty($_.ModifiablePath)) }
foreach ($ModifiablePath in $ModifiablePaths) {
# To exploit an unquoted path we need to create a file, so make sure that the
# permissions returned by Get-ModifiablePath really allow us to do that.
foreach ($Permission in $ModifiablePath.Permissions) {
if ($PermissionsAddFile -contains $Permission) {
# If we find any permission that would allow us to write a file, we can report
# the path.
$ModifiablePath
break
}
}
}
}
}
}
function Get-ModifiableRegistryPath {
<#
.SYNOPSIS
Helper - Checks the permissions of a given registry key and returns the ones that the current user can modify. It's based on the same technique as the one used by @harmj0y in "Get-ModifiablePath".
Author: @itm4n
License: BSD 3-Clause
.DESCRIPTION
Any registry path that the current user has modification rights on is returned in a custom object that contains the modifiable path, associated permission set, and the IdentityReference with the specified rights. The SID of the current user and any group he/she are a part of are used as the comparison set against the parsed path DACLs.
.PARAMETER Path
A registry key path. Required
.EXAMPLE
PS C:\> Get-ModifiableRegistryPath -Path "HKLM\SOFTWARE\Microsoft\Tracing"
ModifiablePath : HKLM\SOFTWARE\Microsoft\Tracing
IdentityReference : BUILTIN\Users
Permissions : Notify, ReadControl, EnumerateSubKeys, CreateSubKey, SetValue, QueryValue
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$Path
)
BEGIN { }
PROCESS {
$Path | ForEach-Object {
$RegPath = "Registry::$($_)"
$OrigPath = $_
Get-AclModificationRights -Path $RegPath -Type RegistryKey | ForEach-Object { $_.ModifiablePath = $OrigPath; $_ }
}
}
}
# function Convert-DomainName {
# [CmdletBinding()] Param(
# [string]$FullyQualifiedName,
# [string]$DistinguishedName
# )
# if (-not [string]::IsNullOrEmpty($FullyQualifiedName)) {
# "DC=" + ($FullyQualifiedName.Split('.') -join ",DC=")
# return
# }
# if (-not [string]::IsNullOrEmpty($DistinguishedName)) {
# ($DistinguishedName.Split(',') | ForEach-Object { $_.Replace("DC=", "") }) -join "."
# return
# }
# }
function Get-ADDomain {
[CmdletBinding()] Param()