Yevrag35

ACL/ACE functions

Jan 20th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-Folders
  2. {
  3.     [CmdletBinding()]
  4.     param
  5.     (
  6.         [Parameter(Mandatory=$true, Position = 0, ValueFromPipelineByPropertyName=$true)]
  7.         [Alias("FullName")]
  8.         [string] $SearchPath
  9.     )
  10.     Begin
  11.     {
  12.         $badFolders = New-Object -TypeName 'System.Collections.Generic.List[string]'
  13.         $goodFolders = New-Object -TypeName 'System.Collections.Generic.List[string]'
  14.     }
  15.     Process
  16.     {
  17.         $Error.Clear()
  18.         $dir = "C:\Program Files"
  19.         [string[]] $folders = @(Get-ChildItem -Path $dir -Directory -Recurse).FullName
  20.         if ($Error.Count -gt 0)
  21.         {
  22.             foreach ($e in $Error)
  23.             {
  24.                 [void] $badFolders.Add(($e.TargetObject -as [string]))
  25.             }
  26.         }
  27.         $goodFolders.AddRange($folders)
  28.     }
  29.     End
  30.     {
  31.         [pscustomobject]@{
  32.             Good = $goodFolders
  33.             Bad = $badFolders
  34.         }
  35.     }
  36. }
  37.  
  38. Function Clone-Rule
  39. {
  40.     [CmdletBinding()]
  41.     param
  42.     (
  43.         [Parameter(Mandatory=$true, Position = 0)]
  44.         [System.Security.AccessControl.FileSystemAccessRule] $Rule,
  45.  
  46.         [Parameter(Mandatory=$true, Position = 1)]
  47.         [ValidateScript({
  48.             $_ -is [System.Security.Principal.IdentityReference] -or $_ -is [string]
  49.         })]
  50.         [object] $NewIdentity
  51.     )
  52.  
  53.     if ($NewIdentity -is [string] -and $NewIdentity.StartsWith('S-1-5'))
  54.     {
  55.         $NewIdentity = [System.Security.Principal.SecurityIdentifier]::new($NewIdentity)
  56.     }
  57.     elseif (-not ($NewIdentity -is [System.Security.Principal.IdentityReference]))
  58.     {
  59.         $NewIdentity = [System.Security.Principal.NTAccount]::new($NewIdentity)
  60.     }
  61.  
  62.     if ($NewIdentity -is [System.Security.Principal.SecurityIdentifier])
  63.     {
  64.         try
  65.         {
  66.             $maybeNT = $NewIdentity.Translate([System.Security.Principal.NTAccount])
  67.             $NewIdentity = $maybeNT
  68.         }
  69.         catch
  70.         {
  71.             Write-Verbose "Couldn't convert the SID to a NT Account... Leaving as is."
  72.         }
  73.     }
  74.  
  75.     New-Object System.Security.AccessControl.FileSystemAccessRule(
  76.         $NewIdentity, $Rule.FileSystemRights, $Rule.InheritanceFlags,
  77.         $Rule.PropagationFlags, $Rule.AccessControlType
  78.     )
  79. }
  80.  
  81. Function Get-AceCollection
  82. {
  83.     [CmdletBinding()]
  84.     param
  85.     (
  86.         [Parameter(Mandatory=$true)]
  87.         [System.Collections.Generic.IEnumerable[string]] $FolderResults
  88.     )
  89.     foreach ($fol in $FolderResults)
  90.     {
  91.         $acl = [System.IO.File]::GetAccessControl($fol, [System.Security.AccessControl.AccessControlSections]"Owner,Access")
  92.         foreach ($ace in $acl.GetAccessRules($true, $false, [System.Security.Principal.NTAccount]))
  93.         {
  94.             if ($ace.IdentityReference -is [System.Security.Principal.NTAccount])
  95.             {
  96.                 try {
  97.                     $trySid = $ace.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier])
  98.                     $ntAcc = $ace.IdentityReference.Value
  99.                 }
  100.                 catch {
  101.                     $trySid = $null
  102.                     Write-Host "Unable to translate $($ace.IdentityReference.Value)"
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 $trySid = $ace.IdentityReference.Value
  108.                 $ntAcc = $null
  109.             }
  110.             [pscustomobject]@{
  111.                 IsOwnerAce = $false
  112.                 Path = $fol
  113.                 NTAccount = $ntAcc
  114.                 SID = $trySid
  115.                 Ace = $ace
  116.             }
  117.         }
  118.         $owner = $acl.GetOwner([System.Security.Principal.NTAccount])
  119.         try
  120.         {
  121.             $tryOwnerSid = $owner.Translate([System.Security.Principal.SecurityIdentifier])
  122.         }
  123.         catch
  124.         {
  125.             Write-Host "Unable to translate Owner to a SID."
  126.         }
  127.         $ownerNtAcc = $owner.Value
  128.         [pscustomobject]@{
  129.             IsOwnerAce = $true
  130.             Path = $fol
  131.             NTAccount = $ownerNtAcc
  132.             SID = $tryOwnerSid
  133.             Ace = $null
  134.         }
  135.     }
  136. }
  137.  
  138. Function Add-NewAce
  139. {
  140.     [CmdletBinding(SupportsShouldProcess=$true)]
  141.     param
  142.     (
  143.         [Parameter(Mandatory=$true, Position = 0)]
  144.         [System.Collections.Generic.IEnumerable[string]] $Locations,
  145.  
  146.         [Parameter(Mandatory=$true, Position = 1)]
  147.         [hashtable] $SidLookupTable     # Key = Old-Domain SID; Value = New-Domain SID
  148.     )
  149.     foreach ($loc in $Locations)
  150.     {
  151.         $aclModified = $false
  152.         $acl = [System.IO.File]::GetAccessControl($loc, [System.Security.AccessControl.AccessControlSections]"Owner,Access")
  153.         $owner = $acl.GetOwner([System.Security.Principal.SecurityIdentifier])
  154.         if ($SidLookupTable.ContainsKey($owner.Value))
  155.         {
  156.             $acl.SetOwner([System.Security.Principal.SecurityIdentifier]::new($SidLookupTable[$owner.Value]))
  157.             $aclModified = $true
  158.         }
  159.         foreach ($ace in $acl.GetAccessRules($true, $false, [System.Security.Principal.SecurityIdentifier]))
  160.         {
  161.             if ($SidLookupTable.ContainsKey($ace.IdentityReference.Value))
  162.             {
  163.                 $newRule = Clone-Rule -Rule $ace -NewIdentity $SidLookupTable[$ace.IdentityReference.Value]
  164.                 $acl.AddAccessRule($newRule)
  165.                 $aclModified = $true
  166.             }
  167.         }
  168.         if ($aclModified)
  169.         {
  170.             Write-Debug $($acl.Access | Out-String)
  171.             if ($PSCmdlet.ShouldProcess(("File Security - {0}" -f $loc), "Set-Acl"))
  172.             {
  173.                 Set-Acl -Path $loc -AclObject $acl
  174.             }
  175.         }
  176.     }
  177. }
Add Comment
Please, Sign In to add comment