[英]How can I change COM Security settings programmatically by c#?
據我所知,沒有API可以做到這一點。 但是COM和DCOM訪問控制是在注冊表中設置的,主要是在“隱身”OLE下(由於歷史原因)。 同時.NET有標准類來操作注冊表。
所以這是我在面對這個任務時應該做的事情:
啟動注冊表監視器,如Mark Russinovich 以前的SysInternals,現在是Microsoft
使用Windows UI以交互方式設置一些COM設置,並監視注冊表更改。
可選但強烈建議:在有一些非常有針對性的搜索關鍵字(注冊表項)之后嘗試在google中獲取文檔/代碼,或者在代碼中使用github進行更好的搜索
實現我的C#類正在操作適當的注冊表類
我知道這個熱帶地區很古老,但這里是我最終使用的解決方案,以防任何人需要它。 如上所述,我找不到任何API來執行它,並且必須直接在存儲坐位的注冊表項上工作。 應編輯填充鍵:
權限以二進制形式存儲。 你可以嘗試我的代碼:
static class ComACLRights
{
public const int COM_RIGHTS_EXECUTE = 1;
public const int COM_RIGHTS_EXECUTE_LOCAL = 2;
public const int COM_RIGHTS_EXECUTE_REMOTE = 4;
public const int COM_RIGHTS_ACTIVATE_LOCAL = 8;
public const int COM_RIGHTS_ACTIVATE_REMOTE = 16;
}
static void Main(string[] args)
{
SetCOMSercurityAccess("testuser", "DefaultAccessPermission");
SetCOMSercurityAccess("testuser", "DefaultLaunchPermission");
}
private static void SetCOMSercurityAccess(string username, string regKey)
{
//Get sid from username
NTAccount f = new NTAccount(username);
SecurityIdentifier sid = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
//Read reg key responsible for COM Sercurity
var accessKey = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Ole", regKey, null);
RawSecurityDescriptor sd;
if (accessKey == null)
{
//Key does not exist
sd = new RawSecurityDescriptor("");
}
else
{
//read security settings
sd = new RawSecurityDescriptor(accessKey as byte[], 0);
}
//Look fo input foruser
var acl = sd.DiscretionaryAcl;
var found = false;
foreach (CommonAce ca in acl)
{
if (ca.SecurityIdentifier == sid)
{
//ensure local access is set
ca.AccessMask |= ComACLRights.COM_RIGHTS_EXECUTE | ComACLRights.COM_RIGHTS_EXECUTE_LOCAL | ComACLRights.COM_RIGHTS_ACTIVATE_LOCAL; //set local access. Always set execute
found = true;
break;
}
}
if (!found)
{
CommonAce ca = new CommonAce(
AceFlags.None,
AceQualifier.AccessAllowed,
ComACLRights.COM_RIGHTS_EXECUTE | ComACLRights.COM_RIGHTS_EXECUTE_LOCAL | ComACLRights.COM_RIGHTS_ACTIVATE_LOCAL,
sid,
false,
null);
acl.InsertAce(acl.Count, ca);
}
//re-set the ACL
sd.DiscretionaryAcl = acl;
//Convert back to binary and save
byte[] binaryform = new byte[sd.BinaryLength];
sd.GetBinaryForm(binaryform, 0);
Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Ole", regKey, binaryform, RegistryValueKind.Binary);
}
此代碼主要受此答案的啟發
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.