我試圖在訪問規則添加到RegistryKey
像這樣:非規範的ACL的.NET RegistrySecurity API處理:處理方法
using (RegistryKey registry = OpenOrCreateMyKey())
{
RegistrySecurity security = registry.GetAccessControl();
security.AddAccessRule(new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
RegistryRights.WriteKey,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
registry.SetAccessControl(security);
}
但是,如果註冊表錯位,AddAccessRule
拋出異常(反射顯示,GetAccessControl
調用確定它們是不是典型的前期,你跳閘故障安全當你試圖做一個寫時RegistrySecurity
實例處於這種狀態):
System.InvalidOperationException: This access control list is not in canonical form and therefore cannot be modified.
運行重gedt32(也可能是註冊表編輯器),然後顯示一個彈出說the permissions on <key> are incorrectly ordered, which may cause some entries to be ineffective <paraphrasing>Do you want to unmangle them now? Y/N</paraprasing>
最權威的一塊,我已經看到了這個問題http://www.codeproject.com/KB/system/accessctrl3.aspx,它說:
然而,控制標誌被實現爲屬性(談話關於不一致!)。您可以從
AreAccessRulesProtected
/AreAuditRulesProtected
中檢索自動繼承設置(回想一下,如果ACL受保護,它不會自動繼承)。如果你閱讀第2部分,你會知道一些Windows API不支持繼承,因此可能會破壞你的機器的繼承設置。好消息是,.NET完全支持繼承機制,並將適當地保留繼承設置。如果你打開了一個安全描述符,它以某種方式得到了一個無序的ACL(可能來自某些流氓Win32應用程序),如果你嘗試編輯它,將會拋出一個InvalidOperationException異常。
通常,這樣的non canonical ACLs results from use of the [since retired] NewSID tool和people write KB articles to say "well stop doing that then"。
但是,關鍵的是,這並不總是原因,有時代碼只是需要工作。什麼是處理這個問題的好的乾淨安全的方法?
我會提供兩個答案,人們可以投票和挑選漏洞,投票,評論和挑選。