You need to enable JavaScript to run this app. PowerShell ACL过滤:无法按规则排除IdentityReference与SID条目
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell ACL过滤:无法按规则排除IdentityReference与SID条目

代码缺陷定位与修正

触发报错、排除规则失效的3个核心问题

  • 排除规则设计缺陷:
    • 排除数组漏加DOMAIN\SERVICE-AVScan条目
    • 对S-1-5-21这类需要前缀匹配的SID用了^(...)$全字符串完全匹配逻辑,根本拦不住实际环境中带数字后缀的完整SID(如S-1-5-21-xxxx-xxxx-1001)
    • 运算符使用错误:错把-notcontains用在身份比对环节。该运算符要求左操作数为集合,用于判断集合是否包含指定值,原代码将单个IdentityReference对象放在左侧、把排除模式放在右侧,逻辑完全反转,永远无法匹配到目标排除项
  • IDE报错的直接原因:
    递归传给子目录的$inheritableAcl是已经扁平化处理的自定义PSObject集合,不存在.Access属性,代码中强行访问$parentAcl.Access自然触发属性不存在的报错
  • 逻辑跑不通的附带问题:
    BrokenACLDirectories未声明作用域,循环内累加的值出了作用域就丢失;且代码在检测到匹配异常时立即弹出可视化窗口,不符合批量扫描完成后统一导出的预期

修正后的可运行代码

# 拆分两类排除规则,避免完全匹配逻辑漏拦前缀匹配项
$ExactExcludes = @(
    'NT AUTHORITY\SYSTEM',
    'BUILTIN\Administrators',
    'CREATOR OWNER',
    'Everyone',
    'DOMAIN\SERVICE-AVScan'
)
$PrefixExcludes = @('S-1-5-21')

# 预编译正则规则,减少重复计算
$reExactExclude = '^({0})$' -f (($ExactExcludes | ForEach-Object { [regex]::Escape($_) }) -join '|')
$rePrefixExclude = '^({0})' -f (($PrefixExcludes | ForEach-Object { [regex]::Escape($_) }) -join '|')

# 声明脚本作用域的损坏路径存储变量
$Script:BrokenACLDirectories = @()

function Get-CustomDirInfo([IO.DirectoryInfo]$path, $parentAcl)
{
    $containerInherit = [Security.AccessControl.InheritanceFlags]::ContainerInherit
    $acl = (Get-Acl -Path $path.FullName).Access | ForEach-Object {
        # 提前将强类型IdentityReference转成字符串,避免类型不匹配导致的比对失效
        $identityStr = $_.IdentityReference.ToString()
        # 前置过滤:符合排除规则的ACE直接跳过,不进入后续处理
        if ($identityStr -match $reExactExclude -or $identityStr -match $rePrefixExclude) {
            return
        }

        New-Object PSObject -Property @{
            Path             = $path.FullName
            IdentityReference = $_.IdentityReference
            IdentityStr      = $identityStr
            FileSystemRights = $_.FileSystemRights
            IsInherited      = $_.IsInherited
            InheritanceFlags = $_.InheritanceFlags
            InheritedFrom    = if ($_.IsInherited) {
                if ($parentAcl) {
                    $currentAce = $_
                    # 直接在扁平化的父ACL集合中匹配,不再访问不存在的.Access属性
                    $parentAce = $parentAcl | Where-Object {
                        ($_.IdentityStr -eq $currentAce.IdentityReference.ToString()) -and
                        ($currentAce.FileSystemRights -band $_.FileSystemRights) -and
                        ($_.InheritanceFlags -band $containerInherit) -and
                        # 父级匹配环节同步应用排除规则,避免把排除项算成继承来源
                        ($_.IdentityStr -notmatch $reExactExclude) -and
                        ($_.IdentityStr -notmatch $rePrefixExclude)
                    }
                    
                    if (!$parentAce -or ($parentAce.Count -gt 1)) {
                        Write-Warning "父级ACE匹配异常 匹配数量=$($parentAce.Count) - 路径: $($path.FullName)"
                        # 去重存储损坏路径
                        if ($path.FullName -notin $Script:BrokenACLDirectories) {
                            $Script:BrokenACLDirectories += $path.FullName
                        }
                    }

                    if ($parentAce.IsInherited) {
                        $parentAce.InheritedFrom
                    }
                    else {
                        Split-Path $path.FullName -Parent
                    }
                }
                else {
                    "Unknown (顶层路径:$($path.FullName))"
                }
            }
            else {
                "Not Inherited"
            }
        }
    }
    
    # 输出当前目录过滤后的ACL项
    $acl
    # 筛选可被子目录继承的ACE,传递给下一层递归
    $inheritableAcl = $acl | Where-Object { $_.InheritanceFlags -band $containerInherit }
    # 递归遍历所有子文件夹
    Get-ChildItem -Path $path.FullName -Directory | ForEach-Object {
        Get-CustomDirInfo $_ $inheritableAcl
    }
}

# 启动扫描
$ScanResult = Get-CustomDirInfo (Get-Item C:\Users\Public)
# 扫描完成后统一展示异常目录
if ($Script:BrokenACLDirectories.Count -gt 0) {
    $Script:BrokenACLDirectories | Out-GridView -Title "共发现 $($Script:BrokenACLDirectories.Count) 个存在ACL继承异常的目录"
}
# 格式化输出扫描结果
$ScanResult | Format-Table Path, IdentityReference, FileSystemRights, IsInherited, InheritedFrom -AutoSize

核心修正点

  • 规则分层匹配:对内置身份账号、指定服务账号采用完全匹配,对SID前缀类条目采用开头匹配,覆盖所有符合排除条件的对象,避免漏拦
  • 类型统一对齐:所有身份比对环节先将IdentityReference强类型对象转成字符串,彻底解决类型不匹配导致的比对失效问题
  • 递归逻辑对齐:父级ACL传递和匹配都基于扁平化后的对象属性,不再访问不存在的.Access属性,从根源解决IDE报错
  • 作用域修正:异常路径存储采用脚本级变量,扫描全流程完成后统一弹窗展示,避免循环内反复弹窗、变量值丢失的问题
  • 过滤前置:所有符合排除条件的ACE在对象生成环节就直接跳过,不会进入后续继承来源匹配、结果输出环节,确保排除逻辑贯穿全流程

内容的提问来源于stack exchange,提问作者Senior Systems Engineer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.27 04:45:08