Dans cet article, nous avions exploré les fonctions de Netapi32.dll pour récupérer des informations relatives aux shares SMB.
Nous allons maintenant voir comment récupérer les permissions de ces shares.
Il existe des fonctions qui gèrent les SIDs, ACLs, et ACEs dans advapi32.dll, et nous allons capitaliser sur l’article concernant le Netapi pour en faire un tout, et lire les permissions sans passer par WMI.
Tout d’abord voici une nouvelle version du script netapi.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 |
<# .SYNOPSIS Defines functions in Netapi32.dll and advapi32.dll so they can be used within Powershell to retrieve SMB share information. .DESCRIPTION Supported functions: Get-Shares <server> <level> server can be $null to point to localhost level can be 0 1 2 502 503 Remark: Uses NetShareEnum Get-NetStatistics <server> <type> type can be SERVER or WORKSTATION Remark: Uses NetStatisticsGet Get-Sessions <server> <level> <user> <client> level can be 0 1 2 10 502 Remark: Uses NetConnectionEnum Get-OpenFiles <server> <level> <user> <path> level can be 2 3 Remark: Uses NetFileEnum .EXAMPLE Get-NetStatistics localhost SERVER Get-Shares localhost 503 Get-Sessions localhost 502 Get-OpenFiles localhost 3 .NOTES Filename : Netapi.ps1 Author : Micky Balladelli micky@balladelli.com .LINK https://balladelli.com/netapi-et-powershell/ #> Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class Netapi { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHARE_INFO_0 { [MarshalAs(UnmanagedType.LPWStr)] public String Name; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHARE_INFO_1 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; public uint Type; [MarshalAs(UnmanagedType.LPWStr)] public string Remark; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHARE_INFO_2 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; public uint Type; [MarshalAs(UnmanagedType.LPWStr)] public string Remark; public uint Permissions; public uint MaxUses; public uint CurrentUses; [MarshalAs(UnmanagedType.LPWStr)] public string Path; [MarshalAs(UnmanagedType.LPWStr)] public string Password; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHARE_INFO_502 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; public uint Type; [MarshalAs(UnmanagedType.LPWStr)] public string Remark; public uint Permissions; public uint MaxUses; public uint CurrentUses; [MarshalAs(UnmanagedType.LPWStr)] public string Path; [MarshalAs(UnmanagedType.LPWStr)] public string Password; public uint Reserved; public IntPtr SecurityDescriptor; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHARE_INFO_503 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; public uint Type; [MarshalAs(UnmanagedType.LPWStr)] public string Remark; public uint Permissions; public uint MaxUses; public uint CurrentUses; [MarshalAs(UnmanagedType.LPWStr)] public string Path; [MarshalAs(UnmanagedType.LPWStr)] public string Password; [MarshalAs(UnmanagedType.LPWStr)] public string ServerName; public uint Reserved; public IntPtr SecurityDescriptor; } [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)] public static extern uint NetShareEnum( [In,MarshalAs(UnmanagedType.LPWStr)] string server, int level, out IntPtr bufptr, int prefmaxlen, ref Int32 entriesread, ref Int32 totalentries, ref Int32 resume_handle); [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)] public static extern int NetApiBufferFree(IntPtr buffer); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STAT_SERVER_0 { public uint Start; public uint FOpens; public uint DevOpens; public uint JobsQueued; public uint SOpens; public uint STimedOut; public uint SerrorOut; public uint PWerrors; public uint PermErrors; public uint SysRrrors; public uint bytesSent_low; public uint bytesSent_high; public uint bytesRcvd_low; public uint BytesRcvd_high; public uint AvResponse; public uint ReqNufNeed; public uint BigBufNeed; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STAT_WORKSTATION_0 { public long StatisticsStartTime; public long BytesReceived; public long SmbsReceived; public long PagingReadBytesRequested; public long NonPagingReadBytesRequested; public long CacheReadBytesRequested; public long NetworkReadBytesRequested; public long BytesTransmitted; public long SmbsTransmitted; public long PagingWriteBytesRequested; public long NonPagingWriteBytesRequested; public long CacheWriteBytesRequested; public long NetworkWriteBytesRequested; public uint InitiallyFailedOperations; public uint FailedCompletionOperations; public uint ReadOperations; public uint RandomReadOperations; public uint ReadSmbs; public uint LargeReadSmbs; public uint SmallReadSmbs; public uint WriteOperations; public uint RandomWriteOperations; public uint WriteSmbs; public uint LargeWriteSmbs; public uint SmallWriteSmbs; public uint RawReadsDenied; public uint RawWritesDenied; public uint NetworkErrors; public uint Sessions; public uint FailedSessions; public uint Reconnects; public uint CoreConnects; public uint Lanman20Connects; public uint Lanman21Connects; public uint LanmanNtConnects; public uint ServerDisconnects; public uint HungSessions; public uint UseCount; public uint FailedUseCount; public uint CurrentCommands; } [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)] public static extern uint NetStatisticsGet( [In,MarshalAs(UnmanagedType.LPWStr)] string server, [In,MarshalAs(UnmanagedType.LPWStr)] string service, int level, int options, out IntPtr bufptr); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_0 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_1 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; [MarshalAs(UnmanagedType.LPWStr)] public string Username; public uint NumOpens; public uint Time; public uint IdleTime; public uint UserFlags; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_2 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; [MarshalAs(UnmanagedType.LPWStr)] public string Username; public uint NumOpens; public uint Time; public uint IdleTime; public uint UserFlags; [MarshalAs(UnmanagedType.LPWStr)] public string ConnectionType; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_10 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; [MarshalAs(UnmanagedType.LPWStr)] public string Username; public uint Time; public uint IdleTime; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_502 { [MarshalAs(UnmanagedType.LPWStr)] public string Name; [MarshalAs(UnmanagedType.LPWStr)] public string Username; public uint NumOpens; public uint Time; public uint IdleTime; public uint UserFlags; [MarshalAs(UnmanagedType.LPWStr)] public string ConnectionType; [MarshalAs(UnmanagedType.LPWStr)] public string Transport; } [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)] public static extern uint NetSessionEnum( [In,MarshalAs(UnmanagedType.LPWStr)] string server, [In,MarshalAs(UnmanagedType.LPWStr)] string client, [In,MarshalAs(UnmanagedType.LPWStr)] string user, int level, out IntPtr bufptr, int prefmaxlen, ref Int32 entriesread, ref Int32 totalentries, ref Int32 resume_handle); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct FILE_INFO_2 { public uint FileID; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct FILE_INFO_3 { public uint FileID; public uint Permissions; public uint NumLocks; [MarshalAs(UnmanagedType.LPWStr)] public string Path; [MarshalAs(UnmanagedType.LPWStr)] public string User; } [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)] public static extern uint NetFileEnum( [In,MarshalAs(UnmanagedType.LPWStr)] string server, [In,MarshalAs(UnmanagedType.LPWStr)] string path, [In,MarshalAs(UnmanagedType.LPWStr)] string user, int level, out IntPtr bufptr, int prefmaxlen, ref Int32 entriesread, ref Int32 totalentries, ref Int32 resume_handle); [DllImport("advapi32.dll", CharSet = CharSet.Unicode)] public static extern bool GetSecurityDescriptorDacl( IntPtr pSecurityDescriptor, [MarshalAs(UnmanagedType.Bool)] out bool bDaclPresent, ref IntPtr pDacl, [MarshalAs(UnmanagedType.Bool)] out bool bDaclDefaulted ); [DllImport("advapi32.dll", CharSet = CharSet.Unicode)] public static extern bool GetAclInformation( IntPtr pAcl, ref ACL_SIZE_INFORMATION pAclInformation, uint nAclInformationLength, ACL_INFORMATION_CLASS dwAclInformationClass ); [DllImport("advapi32.dll", CharSet = CharSet.Unicode)] public static extern int GetAce( IntPtr aclPtr, int aceIndex, out IntPtr acePtr ); [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern int GetLengthSid( IntPtr pSID ); [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ConvertSidToStringSid( [MarshalAs(UnmanagedType.LPArray)] byte[] pSID, out IntPtr ptrSid ); [StructLayout(LayoutKind.Sequential)] public struct ACL_SIZE_INFORMATION { public uint AceCount; public uint AclBytesInUse; public uint AclBytesFree; } [StructLayout(LayoutKind.Sequential)] public struct ACE_HEADER { public byte AceType; public byte AceFlags; public short AceSize; } [StructLayout(LayoutKind.Sequential)] public struct ACCESS_ALLOWED_ACE { public ACE_HEADER Header; public int Mask; public int SidStart; } public enum ACL_INFORMATION_CLASS { AclRevisionInformation = 1, AclSizeInformation } } "@ function Get-OpenFiles { [CmdletBinding()] param ( [Parameter(Position=0)][string]$server = "localhost", [Parameter(Position=1)][int32]$level=3, [Parameter(Position=2)][string]$user = $null, [Parameter(Position=3)][string]$path = $null) switch ($level) { 2 { $struct = New-Object netapi+FILE_INFO_2 } 3 { $struct = New-Object netapi+FILE_INFO_3 } default { $level = 3 $struct = New-Object netapi+FILE_INFO_3 } } $buffer = 0 $entries = 0 $total = 0 $handle = 0 $ret = [Netapi]::NetFileEnum($server, $path, $user, $level, [ref]$buffer, -1, [ref]$entries, [ref]$total, [ref]$handle) $files = @() if (!$ret) { $offset = $buffer.ToInt64() $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType()) for ($i = 0; $i -lt $entries; $i++) { $ptr = New-Object system.Intptr -ArgumentList $offset $files += [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType()) $offset = $ptr.ToInt64() $offset += $increment } } else { Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message if ($ret -eq 1208) { # Error Code labeled "Extended Error" requires the buffer to be freed [Void][Netapi]::NetApiBufferFree($buffer) } } $files } function Get-Sessions { [CmdletBinding()] param ( [Parameter(Position=0)][string]$server = "localhost", [Parameter(Position=1)][int32]$level=0, [Parameter(Position=2)][string]$client = $null, [Parameter(Position=3)][string]$user = $null) switch ($level) { 0 { $struct = New-Object netapi+SESSION_INFO_0 } 1 { $struct = New-Object netapi+SESSION_INFO_1 } 2 { $struct = New-Object netapi+SESSION_INFO_2 } 10 { $struct = New-Object netapi+SESSION_INFO_10 } 502 { $struct = New-Object netapi+SESSION_INFO_502 } default { $level = 0 $struct = New-Object netapi+SESSION_INFO_0 } } $buffer = 0 $entries = 0 $total = 0 $handle = 0 $ret = [Netapi]::NetSessionEnum($server, $client, $user, $level, [ref]$buffer, -1, [ref]$entries, [ref]$total, [ref]$handle) $sessions = @() if (!$ret) { $offset = $buffer.ToInt64() $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType()) for ($i = 0; $i -lt $entries; $i++) { $ptr = New-Object system.Intptr -ArgumentList $offset $sessions += [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType()) $offset = $ptr.ToInt64() $offset += $increment } } else { Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message if ($ret -eq 1208) { # Error Code labeled "Extended Error" requires the buffer to be freed [Void][Netapi]::NetApiBufferFree($buffer) } } $sessions } function Get-Shares { [CmdletBinding()] param ( [Parameter(Position=0)][string]$server = "localhost", [Parameter(Position=1)][int32]$level=0) switch ($level) { 0 { $struct = New-Object netapi+SHARE_INFO_0 } 1 { $struct = New-Object netapi+SHARE_INFO_1 } 2 { $struct = New-Object netapi+SHARE_INFO_2 } 502 { $struct = New-Object netapi+SHARE_INFO_502 } 503 { $struct = New-Object netapi+SHARE_INFO_503 } default { $level = 0 $struct = New-Object netapi+SHARE_INFO_0 } } $buffer = 0 $entries = 0 $total = 0 $handle = 0 $ret = [Netapi]::NetShareEnum($server, $level, [ref]$buffer, -1, [ref]$entries, [ref]$total, [ref]$handle) $shares = @() if (!$ret) { $offset = $buffer.ToInt64() $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType()) for ($i = 0; $i -lt $entries; $i++) { $ptr = New-Object system.Intptr -ArgumentList $offset $shares += [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType()) $offset = $ptr.ToInt64() $offset += $increment } } else { Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message if ($ret -eq 1208) { # Error Code labeled "Extended Error" requires the buffer to be freed [Void][Netapi]::NetApiBufferFree($buffer) } } $shares } function Get-SharesPermissions { [CmdletBinding()] param ( [Parameter(Position=0)][string]$server = "localhost") $shares = Get-Shares -level 502 -server $server } function Get-NetStatistics { [CmdletBinding()] param ( [Parameter(Position=0)][string]$server = "localhost", [Parameter(Position=1)][string]$type="WORKSTATION") if ($type -eq "SERVER") { $struct = New-Object netapi+STAT_SERVER_0 $service = "LanmanServer" } else { $struct = New-Object netapi+STAT_WORKSTATION_0 $service = "LanmanWorkstation" } $buffer = 0 $ret = [Netapi]::NetStatisticsGet($server, $service, 0, # only level 0 is supported for now 0, #must be 0 [ref]$buffer) if (!$ret) { $ret = [system.runtime.interopservices.marshal]::PtrToStructure($buffer, [System.Type]$struct.GetType()) $ret } else { Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message } } |
Il faudra avec cette version dot-sourcer le script pour que les cmdlets soient définies:
1 |
. $PSScriptRoot\netapi.ps1 |
Une fois les cmdlets définies nous allons avoir besoin de celle qui récupère le level 502 des shares:
1 |
$shares = Get-Shares -level 502 -server localhost |
et avec cet array, nous allons pour chaque élément vérifier si le SecurityDescriptor est présent. Pour ensuite récupérer l’ACL dans un buffer en mémoire.
A partir de l’ACL nous pouvons récupérer l’ACE qui contient le SID et la permission.
Avec un peu de gymnastique de pointeurs et buffers de structures, nous allons pouvoir traduire tout cela en objets Powershell qui peuvent être utilisés.
Voici le script complet:
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 |
. $PSScriptRoot\netapi.ps1 $shares = Get-Shares -level 502 -server localhost foreach ($share in $shares) { $aclPresent = 0 $acl = 0 $aclDefaulted = 0 if ($share.SecurityDescriptor -ne 0) { $ret = [Netapi]::GetSecurityDescriptorDacl($share.SecurityDescriptor,[ref]$aclPresent,[ref]$acl,[ref]$aclDefaulted) if ($aclPresent -eq $true) { $aclInfo = New-Object netapi+ACL_SIZE_INFORMATION $length = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$aclInfo.GetType()) $AclSizeInformation = 2 # Enum defined in ACL_INFORMATION_CLASS $ret = [Netapi]::GetAclInformation($acl,[ref]$aclInfo,$length,$AclSizeInformation) for($i = 0; $i -lt $aclInfo.AceCount; $i++) { $ptr = 0 $aceStruct = New-Object netapi+ACCESS_ALLOWED_ACE $ret = [netapi]::GetAce($acl, $i, [ref]$ptr); $ace = [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$aceStruct.GetType()) if ($null -ne $ace) { $increment = [System.Runtime.Interopservices.Marshal]::OffsetOf([System.Type]$aceStruct.GetType(),"SidStart") $offset = $ptr.ToInt64() $offset += $increment $sidSize = [Netapi]::GetLengthSid($offset) $bytes= $encryptedBytes = New-Object byte[] $sidSize [System.Runtime.Interopservices.Marshal]::Copy($offset, $bytes, 0, $sidSize) $ptrSid = 0 $ret = [Netapi]::ConvertSidToStringSid($bytes, [ref]$ptrSid) $sid = [System.Runtime.Interopservices.Marshal]::PtrToStringAuto($ptrSid) $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid) $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) if (($ace.Mask -band 0x1F01FF) -eq 0x1F01FF) { $right = "Full Control" } elseif (($ace.Mask -band 0x1301BF) -eq 0x1301BF) { $right = "Modify" } elseif (($ace.Mask -band 0x1200A9) -eq 0x1200A9) { $right = "Read" } Write-Output "$($share.Name), SID $($objUser.Value) $right" } } } } } |
L’output est simple mais peut être adapté:
1 2 3 |
TEMP, SID BUILTIN\Administrators Full Control TEMP, SID Everyone Full Control TEMP2, SID Everyone Full Control |
Sans les mains WMI.
Voici l’article de référence en C# qui traite de ce sujet http://blogs.msdn.com/b/dsadsi/archive/2012/03/30/to-read-shared-permissions-of-a-server-resource-in-c-using-netsharegetinfo.aspx
Voici un mise à jour du script d’exemple, cette nouvelle version traite le problème de l’exception « Some or all identity references could not be translated. ».
Pour régler ce problème nous allons récupérer la liste des utilisateurs locaux d’une machine donnée et comparer les SIDs sans passer par Translate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
try { $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) $name = $objUser.Value } catch { $adsi = [adsi]"WinNT://$server" $users = $adsi.Children | where {$_.SchemaClassName -eq 'user'} $name = $users | % { $localSID = New-Object System.Security.Principal.SecurityIdentifier($_.objectSid.Value,0) if ($localSID -eq $sid) { $_.name } } } |
Voici le script en entier (renseigner $server en début du script):
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 |
. $PSScriptRoot\netapi.ps1 $server = "" $shares = Get-Shares -level 502 -server $server foreach ($share in $shares) { $aclPresent = 0 $acl = 0 $aclDefaulted = 0 if ($share.SecurityDescriptor -ne 0) { $ret = [Netapi]::GetSecurityDescriptorDacl($share.SecurityDescriptor,[ref]$aclPresent,[ref]$acl,[ref]$aclDefaulted) if ($aclPresent -eq $true) { $aclInfo = New-Object netapi+ACL_SIZE_INFORMATION $length = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$aclInfo.GetType()) $AclSizeInformation = 2 # Enum defined in ACL_INFORMATION_CLASS $ret = [Netapi]::GetAclInformation($acl,[ref]$aclInfo,$length,$AclSizeInformation) for($i = 0; $i -lt $aclInfo.AceCount; $i++) { $ptr = 0 $aceStruct = New-Object netapi+ACCESS_ALLOWED_ACE $ret = [netapi]::GetAce($acl, $i, [ref]$ptr); $ace = [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$aceStruct.GetType()) if ($null -ne $ace) { $increment = [System.Runtime.Interopservices.Marshal]::OffsetOf([System.Type]$aceStruct.GetType(),"SidStart") $offset = $ptr.ToInt64() $offset += $increment $sidSize = [Netapi]::GetLengthSid($offset) $bytes= New-Object byte[] $sidSize [System.Runtime.Interopservices.Marshal]::Copy($offset, $bytes, 0, $sidSize) $ptrSid = 0 $ret = [Netapi]::ConvertSidToStringSid($bytes, [ref]$ptrSid) $sid = [System.Runtime.Interopservices.Marshal]::PtrToStringAuto($ptrSid) $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid) try { $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) $name = $objUser.Value } catch { $adsi = [adsi]"WinNT://$server" $users = $adsi.Children | where {$_.SchemaClassName -eq 'user'} $name = $users | % { $localSID = New-Object System.Security.Principal.SecurityIdentifier($_.objectSid.Value,0) if ($localSID -eq $sid) { $_.name } } } if (($ace.Mask -band 0x1F01FF) -eq 0x1F01FF) { $right = "Full Control" } elseif (($ace.Mask -band 0x1301BF) -eq 0x1301BF) { $right = "Modify" } elseif (($ace.Mask -band 0x1200A9) -eq 0x1200A9) { $right = "Read" } Write-Output "$($share.Name), SID $name $right" } } } } } |