Skip to content

Instantly share code, notes, and snippets.

@jborean93
Last active April 11, 2023 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jborean93/a89058ba3c5a6ecb78a9b2ee2f856685 to your computer and use it in GitHub Desktop.
Save jborean93/a89058ba3c5a6ecb78a9b2ee2f856685 to your computer and use it in GitHub Desktop.
Get the file SDDL string
#Requires -Module Ctypes
#Requires -Module PSPrivilege
Function Get-FileSDDL {
[CmdletBinding()]
param ($Path)
$a32 = New-CtypesLib Advapi32.dll
$allSecurityInformation = 0xF00000FF
Enable-ProcessPrivilege -Name SeSecurityPrivilege, SeBackupPrivilege
$pSd = [IntPtr]::Zero
$res = $a32.CharSet('Unicode').GetNamedSecurityInfoW(
$a32.MarshalAs($path, 'LPWStr'),
1, # SE_FILE_OBJECT
$allSecurityInformation,
$null,
$null,
$null,
$null,
[ref]$pSd)
if ($res) {
throw [System.ComponentModel.Win32Exception]$res
}
$pSddl = [IntPtr]::Zero
try {
$sddlLength = 0
$res = $a32.SetLastError().CharSet('Unicode').Returns([bool]).ConvertSecurityDescriptorToStringSecurityDescriptorW(
$pSd,
1, # SDDL_REVISION_1
$allSecurityInformation,
[ref]$pSddl,
[ref]$sddlLength)
if (-not $res) {
throw [System.ComponentModel.Win32Exception]$a32.LastError
}
[System.Runtime.InteropServices.Marshal]::PtrToStringUni($pSddl, $sddlLength)
}
finally {
$k32 = New-CtypesLib Kernel32.dll
if ($pSddl -ne [IntPtr]::Zero) {
$null = $k32.LocalFree($pSddl)
}
$null = $k32.LocalFree($pSd)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment