【问题标题】:PowerShell / C# SetAuditRuleProtection() Not Working On Some FoldersPowerShell / C# SetAuditRuleProtection() 不适用于某些文件夹
【发布时间】:2017-07-20 06:53:56
【问题描述】:

我已经与这个问题斗争了一段时间,但找不到解释。我正在尝试在目录上启用审计规则的继承。在此示例中,我在 c:\Program Files\Microsoft SQL Server\MSSQL12.NEWINSTANCE 上设置审核规则并传播到所有子项。 c:\Program Files\Microsoft SQL Server\MSSQL12.NEWINSTANCE\MSSQL\Backup 目录通过继承获取审计规则,但Logs 目录没有。这是我用来启用继承的代码的 sn-p:

$Path = "C:\Program Files\Microsoft SQL Server\MSSQL12.NEWINSTANCE\MSSQL\Log"

[System.IO.DirectoryInfo]$Info = New-Object -TypeName System.IO.DirectoryInfo($Path)

[System.Security.AccessControl.DirectorySecurity]$Acl = $Info.GetAccessControl()

$Acl.SetAuditRuleProtection($false, $false)

$Info.SetAccessControl($Acl)

我尝试了多种组合,包括Get-AclSet-Acl(Get-Item -Path $Path).GetAccessControl() 等。看来我可以禁用继承并删除规则,但不能禁用继承并保留现有规则(通过修改SetAuditRuleProtection 的参数)。

如果我通过 GUI 执行此操作,所有这些都有效,所以我不认为目录或我的权限有问题。欢迎任何想法/想法。

【问题讨论】:

    标签: c# powershell inheritance audit


    【解决方案1】:

    据我所知,在网络上随处可见,除非您有审核规则(它不能很好地处理 $null 值),否则您无法重置审核继承。因此,在您的情况下,它应该如下所示:

    # Temporary audit rule just to make sure it is not null.  (otherwhise won't work)
    $tmpAR= New-Object System.Security.AccessControl.FileSystemAuditRule(
                'Everyone',
        [System.Security.AccessControl.FileSystemRights]::Delete, 
        [System.Security.AccessControl.AuditFlags]::Success
    )
    $Acl.SetAuditRule($tmpAR)
    $Acl.SetAuditRuleProtection($false, $false)
    # Don't forget the Set-ACL at the end
    Set-ACL -Path $Path -AclObject $Acl
    

    在我的情况下,这是有效的。

    注意:我以前获取ACL:$Acl = Get-ACL -Path $Path -Audit

    跳过这个帮助。

    【讨论】:

      【解决方案2】:

      对我来说,诀窍是您不能对当前没有 SACL(系统访问控制列表,也称为审核规则)的文件夹或文件执行此操作。我写了一个函数,在父节点上设置规则,然后递归遍历所有子节点来设置规则,确保它被继承,然后删除额外的规则。

      <#
      .SYNOPSIS
       Sets auditing on the file or folder.
      
      .DESCRIPTION
       Implements a File System Audit Rule on the file or folder.
      
      .PARAMETER Path (Required)
       Specifies the file or folder on which to apply the audit rule.
      
      .PARAMETER Principal (Required)
       Specifies the NTAccount name.
      
      .PARAMETER Success (Optional, Required if "Failure" not present)
       Specifies to implement an audit rule for successes.
      
      .PARAMETER Failure (Optional, Required if "Success" not present)
       Specifies to implement an audit rule for failures.
      
      .PARAMETER Flags (Required)
       This is an array of two integers that indicate what to apply the audit rule to and what type of recursion should be used.
      
       Inheritance, Propagation:
       This folder only = 0,0
       This folder, subfolders and files = 3,0
       This folder and subfolders = 1,0
       This folder and files = 2,0
       Subfolders and files only = 3,2
       Subfolders only = 1,2
       Files only = 2,3
      
      .EXAMPLE
       Set-Auditing
      #>
      function Set-Auditing {
        [CmdletBinding(SupportsShouldProcess=$true)]
        Param([Parameter(Mandatory=$true, ValueFromPipeline=$false)] [ValidateNotNullOrEmpty()] [string]$Path,
              [Parameter(Mandatory=$true, ValueFromPipeline=$false)] [ValidateNotNullOrEmpty()] [string]$Principal,
              [Parameter(Mandatory=$true, ValueFromPipeline=$false)] [ValidateSet("AppendData","ChangePermissions","CreateDirectories","CreateFiles","Delete","DeleteSubdirectoriesAndFiles","ExecuteFile","FullControl","ListDirectory","Modify","Read","ReadAndExecute","ReadAttributes","ReadData","ReadExtendedAttributes","ReadPermissions","Synchronize","TakeOwnership","Traverse","Write","WriteAttributes","WriteData","WriteExtendedAttributes")] [string[]]$Rights,
              [Parameter(Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="Both")]
              [Parameter(Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="Success")] [switch]$Success,
              [Parameter(Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="Both")]
              [Parameter(Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="Failure")] [switch]$Failure,
              [Parameter(Mandatory=$true, ValueFromPipeline=$false)] [int[]]$Flags)
      
        Begin {
          # Determine if audit rule exists
          if ($Success.IsPresent) { $AuditFlags=1 } else { $AuditFlags=0 }
          if ($Failure.IsPresent) { $AuditFlags+=2 }
      
          # Inheritance Flags
          # This folder only = 0
          # This folder, subfolders and files = 3
          # This folder and subfolders = 1
          # This folder and files = 2
          # Subfolders and files only = 3
          # Subfolders only = 1
          # Files only = 2
      
          # Propagation Flags
          # This folder only = 0
          # This folder, subfolders and files = 0
          # This folder and subfolders = 0
          # This folder and files = 0
          # Subfolders and files only = 2
          # Subfolders only = 2
          # Files only = 2
      
          # File System Rights
          $fsrAppendData                  =0x000004
          $fsrChangePermissions           =0x040000
          $fsrCreateDirectories           =0x000004
          $fsrCreateFiles                 =0x000002
          $fsrDelete                      =0x010000
          $fsrDeleteSubdirectoriesAndFiles=0x000040
          $fsrExecuteFile                 =0x000020
          $fsrFullControl                 =0x1F01FF
          $fsrListDirectory               =0x000001
          $fsrModify                      =0x0301BF
          $fsrRead                        =0x020089
          $fsrReadAndExecute              =0x0200A9
          $fsrReadAttributes              =0x000080
          $fsrReadData                    =0x000001
          $fsrReadExtendedAttributes      =0x000008
          $fsrReadPermissions             =0x020000
          $fsrSynchronize                 =0x100000
          $fsrTakeOwnership               =0x080000 
          $fsrTraverse                    =0x000020
          $fsrWrite                       =0x000116
          $fsrWriteAttributes             =0x000100
          $fsrWriteData                   =0x000002
          $fsrWriteExtendedAttributes     =0x000010
      
          $RightValues=0
          for ($i=0; $i -lt $Rights.Count; $i++) {
           switch ($Rights[$i]) {
             "AppendData"                   { $RightValues=$RightValues -bor $fsrAppendData }
             "ChangePermissions"            { $RightValues=$RightValues -bor $fsrChangePermissions }
             "CreateDirectories"            { $RightValues=$RightValues -bor $fsrCreateDirectories }
             "CreateFiles"                  { $RightValues=$RightValues -bor $fsrCreateFiles }
             "Delete"                       { $RightValues=$RightValues -bor $fsrDelete }
             "DeleteSubdirectoriesAndFiles" { $RightValues=$RightValues -bor $fsrDeleteSubdirectoriesAndFiles }
             "ExecuteFile"                  { $RightValues=$RightValues -bor $fsrExecuteFile }
             "FullControl"                  { $RightValues=$RightValues -bor $fsrFullControl }
             "ListDirectory"                { $RightValues=$RightValues -bor $fsrListDirectory }
             "Modify"                       { $RightValues=$RightValues -bor $fsrModify }
             "Read"                         { $RightValues=$RightValues -bor $fsrRead }
             "ReadAndExecute"               { $RightValues=$RightValues -bor $fsrReadAndExecute }
             "ReadAttributes"               { $RightValues=$RightValues -bor $fsrReadAttributes }
             "ReadData"                     { $RightValues=$RightValues -bor $fsrReadData }
             "ReadExtendedAttributes"       { $RightValues=$RightValues -bor $fsrReadExtendedAttributes }
             "ReadPermissions"              { $RightValues=$RightValues -bor $fsrReadPermissions }
             "Synchronize"                  { $RightValues=$RightValues -bor $fsrSynchronize }
             "TakeOwnership"                { $RightValues=$RightValues -bor $fsrTakeOwnership }
             "Traverse"                     { $RightValues=$RightValues -bor $fsrTraverse }
             "Write"                        { $RightValues=$RightValues -bor $fsrWrite }
             "WriteAttributes"              { $RightValues=$RightValues -bor $fsrWriteAttributes }
             "WriteData"                    { $RightValues=$RightValues -bor $fsrWriteData }
             "WriteExtendedAttributes"      { $RightValues=$RightValues -bor $fsrWriteExtendedAttributes }
           }
          }
      
          Write-Verbose "Acquiring object $($FS.FullName)"
          $FS=Get-Item -Path $Path
          $ACL=Get-Acl -Path $Path -Audit
          $NothingToDo=$false
          for ($i=0; $i -lt $ACL.Audit.Count; $i++) {
            if ($ACL.Audit[$i].IdentityReference.Value -eq $Principal) {
              if ($ACL.Audit[$i].AuditFlags.value__ -eq $AuditFlags) {
                if ($ACL.Audit[$i].PropagationFlags.value__ -eq $Flags[1]) {
                  if ($ACL.Audit[$i].InheritanceFlags.value__ -eq $Flags[0]) {
                    if ($ACL.Audit[$i].FileSystemRights.value__ -eq $RightValues) { $NothingToDo=$true; Write-Verbose "Nothing to do" }
                  }
                }
              }
            }
          }
        }
      
        Process {
          if (!$NothingToDo) {
            # There is one case where we will not propagage the rules.  This is when $Flags = 0,0
            if (($Flags[0] -eq 0) -and ($Flags[1] -eq 0)) { Write-Verbose "Flags = 0,0; no propagation necessary." }
            else {
              Write-Verbose "Setting Audit Rule"
              if ($Principal.Contains("\")) { $NTAccount=New-Object System.Security.Principal.NTAccount(($Principal.Split("\"))[0],($Principal.Split("\"))[1]) }
              else { $NTAccount=New-Object System.Security.Principal.NTAccount($Principal) }
              $FSAR=New-Object System.Security.AccessControl.FileSystemAuditRule($NTAccount,$RightValues,$Flags[0],$Flags[1],$AuditFlags)
              $FAR=New-Object System.Security.AccessControl.FileSystemAuditRule($NTAccount,$RightValues,$AuditFlags)
              $ACL.AddAuditRule($FSAR)
              $ACL.SetAuditRuleProtection($false, $true)
              Write-Verbose "Applying rule to $($ACL.Path.Replace('Microsoft.PowerShell.Core\FileSystem::',''))"
              $FS.SetAccessControl($ACL)
      
              # Now, ensure that all folders and files have inheritance enabled.
              $FS=Get-ChildItem -Path $Path -Recurse
              $FS=@($FS)
              for ($i=0; $i -lt $FS.Count; $i++) {
                Write-Verbose "Acquiring object $($FS[$i].FullName)"
                $ACL=Get-Acl -Path $FS[$i].FullName -Audit
                if (Test-Path $ACL.Path -PathType Leaf) { $ACL.AddAuditRule($FAR) } else { $ACL.AddAuditRule($FSAR) }
                $ACL.SetAuditRuleProtection($false, $true)
                $FS[$i].SetAccessControl($ACL)
                Write-Verbose "Applying rule to $($ACL.Path.Replace('Microsoft.PowerShell.Core\FileSystem::',''))"
                if (Test-Path $ACL.Path -PathType Leaf) { $ACL.RemoveAuditRule($FAR) > $null } else { $ACL.RemoveAuditRule($FSAR) > $null }
                Write-Verbose "Removing extra rule from $($ACL.Path)"
                $FS[$i].SetAccessControl($ACL)
              }
            }
          }
          else { Write-Verbose "Nothing to do." }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 2023-04-07
        相关资源
        最近更新 更多