在 C# 中,DirectoryEntry 返回空的 AuditRules 集合,即使审核规则确实存在

问题描述 投票:0回答:1

我正在尝试创建一个 C# 控制台应用程序来管理域(而不是文件系统)上的审核权限。我的问题是我无法读取 C# 中现有的审核规则,我只得到一个空集合,即使我确定域中存在审核规则。

我需要管理超过 100 个独立域,因此我想提出一个编程解决方案来帮助管理它们。

我可以使用以下命令在 PowerShell 中毫无问题地执行此操作。

$acl = Get-Acl -Path "DC=mydomain,DC=local" -Audit
$acl.Audit

这将返回 5 个审核规则的集合。但是,当尝试在 C# 中执行相同的操作时,不会返回审核规则。

string path = "LDAP://mydomain.local/DC=mydomain,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
               
AuthorizationRuleCollection authorizationRuleList1 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(NTAccount));
AuthorizationRuleCollection authorizationRuleList2 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));

Console.WriteLine("Authorization Rules 1: " + authorizationRuleList1.Count);
Console.WriteLine("Authorization Rules 2: " + authorizationRuleList2.Count);

运行上面的命令会显示 0 条审核规则。奇怪的是,访问规则毫无问题地返回。

我已经尝试过了

  1. DirectoryEntry
    初始化显式应用密码
  2. 运行“以管理员身份运行”
  3. 使用 LDAP 资源管理器验证审核权限是否存在

任何指点将不胜感激。

更新了使用 ActiveD 获取安全描述符的代码。

string path = "LDAP://demo.local/DC=demo,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
var ntSecurityDescriptor = domain.Properties["ntSecurityDescriptor"];

ActiveDs.ADsSecurityUtility secUtility = new ActiveDs.ADsSecurityUtility();
ActiveDs.IADsSecurityDescriptor sd = (IADsSecurityDescriptor)ntSecurityDescriptor[0];
ActiveDs.IADsAccessControlList aclList =  ActiveDs.IADsAccessControlList)sd.DiscretionaryAcl;
c# powershell active-directory ldap
1个回答
0
投票

我能够提出一个解决方案来捕获域的审核规则。看来单独使用路径加载和 DirectoryEntry (如下面的代码所示)不会带回所有属性。

new DirectoryEntry(path)

为了返回自定义属性,可以创建 DirectorySearcher,并将其配置为拉回特定属性。下面的代码块中实现了示例。在我的情况下,还需要更新 SecurityMasks 以包含 SecurityMasks.Sacl。

string path = "DC=mydomain,DC=local";
DirectorySearcher directorySearcher = new DirectorySearcher($"(distinguishedName={path})");

// Request that the security permissions be returned with the objects.
directorySearcher.PropertiesToLoad.Add("ntSecurityDescriptor");

// Allow section of all Security Descriptors
// This is needed for Audit Rules to be returned.
directorySearcher.SecurityMasks = SecurityMasks.Dacl | SecurityMasks.Owner | SecurityMasks.Sacl;

// Search for Active Directory entries
SearchResultCollection searchResults = directorySearcher.FindAll();

// Convert ntSecurityDesciptor to a RawSecurityDesciptor object
RawSecurityDescriptor rawSecurityDescriptor = rawSecurityDescriptor = new RawSecurityDescriptor((byte[])searchResults[0].Properties["ntSecurityDescriptor"][0], 0);

现在已创建原始安全对象,其中包括存储在 SystemAcl 属性中的审核规则。

rawSecurityDescriptor.SystemAcl

此对象将允许审查对象的审核规则。

希望这对其他人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.