2010-02-10 28 views
2

我試圖在訪問規則添加到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 toolpeople write KB articles to say "well stop doing that then"

但是,關鍵的是,這並不總是原因,有時代碼只是需要工作。什麼是處理這個問題的好的乾淨安全的方法?

我會提供兩個答案,人們可​​以投票和挑選漏洞,投票,評論和挑選。

回答

2

方法1是忽略繼承的權限和你想什麼呢一味地寫: -

using (RegistryKey registry = OpenOrCreateMyKey()) 
{ 
    RegistrySecurity security = new RegistrySecurity(); 
    security.AddAccessRule(new RegistryAccessRule(...)); 
    registry.SetAccessControl(security); 
} 

問題是,我不知道潛在的負面影響。我很高興所有需要訪問的人都能從我的規則中獲得。但是,它與OS基礎架構的其餘部分相配嗎?

1

方法2時嘗試第一種方式和回落到骯髒的方式僅在必要時: -

using (RegistryKey registry = OpenOrCreateMyKey()) 
{ 
    RegistrySecurity security = new RegistrySecurity(); 
    if (!security.AreAccessRulesCanonical) 
    { 
     Log.WriteWarning("Registry permissions (likely ones inherited from parent) are inconsistent (RegistrySecurity.AreAccessRulesCanonical is false), so using alternate permissioning algorithm, Has NewSID or another tool mangled permissions? regedt32 can be used to analyze the issue."); 
     // Ignore parent ACLs and just blindly stuff in this ACL (which will continue to be inherited) 
     security = new RegistrySecurity(); 
    } 

    security.AddAccessRule(new RegistryAccessRule(...)); 
    registry.SetAccessControl(security); 
} 

除了弱點Approach 1,這有不可預知的行爲,但至少它的bug - 與原來的學位課程相容。