|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | +function Assert-ProtectedLocation([string]$path) { |
| 3 | + $trusted = @('S-1-5-18', 'S-1-5-32-544', 'S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464', 'S-1-3-0') |
| 4 | + # Delete, DeleteSubdirectoriesAndFiles, ChangePermissions, TakeOwnership and GENERIC_ALL. |
| 5 | + $unsafe = 0x100D0040 |
| 6 | + $directory = Get-Item -LiteralPath $path -Force |
| 7 | + while ($directory) { |
| 8 | + if ($directory -isnot [System.IO.DirectoryInfo] -or ($directory.Attributes -band [System.IO.FileAttributes]::ReparsePoint)) { |
| 9 | + throw ('Not a protected location: ' + $directory.FullName) |
| 10 | + } |
| 11 | + $acl = Get-Acl -LiteralPath $directory.FullName |
| 12 | + $owner = $acl.GetOwner([System.Security.Principal.SecurityIdentifier]).Value |
| 13 | + $open = @($acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier]) | Where-Object { |
| 14 | + $_.AccessControlType -eq 'Allow' -and $trusted -notcontains $_.IdentityReference.Value -and |
| 15 | + !($_.PropagationFlags -band [System.Security.AccessControl.PropagationFlags]::InheritOnly) -and |
| 16 | + ([int64]$_.FileSystemRights -band $unsafe) |
| 17 | + }) |
| 18 | + if ($trusted -notcontains $owner -or $open.Count -gt 0) { throw ('Not a protected location: ' + $directory.FullName) } |
| 19 | + $directory = $directory.Parent |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +whoami |
| 24 | +"elevated: " + ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
| 25 | +"default owner: " + [System.Security.Principal.WindowsIdentity]::GetCurrent().Owner.Value |
| 26 | +"known folder: " + [Environment]::GetFolderPath('CommonApplicationData') |
| 27 | +foreach ($path in @('C:\', 'C:\ProgramData')) { |
| 28 | + $item = Get-Item -LiteralPath $path -Force |
| 29 | + $acl = Get-Acl -LiteralPath $path |
| 30 | + "== $path attributes=$($item.Attributes) owner=$($acl.GetOwner([System.Security.Principal.SecurityIdentifier]).Value) protected=$($acl.AreAccessRulesProtected)" |
| 31 | + foreach ($rule in $acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])) { |
| 32 | + '{0} {1} 0x{2:x} inherit={3} propagate={4}' -f $rule.IdentityReference.Value, $rule.AccessControlType, [int64]$rule.FileSystemRights, $rule.InheritanceFlags, $rule.PropagationFlags |
| 33 | + } |
| 34 | +} |
| 35 | +try { Assert-ProtectedLocation 'C:\ProgramData'; 'CHECK OK C:\ProgramData' } catch { "CHECK REFUSED: $_" } |
0 commit comments