简体   繁体   English

WMI:远程编辑注册表

[英]WMI: Editing the Registry Remotely

I'm trying to use the following code (poorly written, but it's just a proof of concept) to edit the registry of a computer on a domain. 我正在尝试使用以下代码(写得不好,但它只是一个概念证明)来编辑域上计算机的注册表。 I have a domain account, and I've verified that the domain admin group is present in the local admin group on the machines I'm attempting to affect. 我有一个域帐户,我已经验证域管理员组在我试图影响的计算机上的本地管理员组中。 I've connected to these other machines to perform other administrative type tasks, so I'm sure that I have administrative privileges on these hosts. 我已连接到这些其他计算机以执行其他管理类型的任务,因此我确信我对这些主机具有管理权限。

All of StdRegProv's "get" methods work fine ( http://msdn.microsoft.com/en-us/library/aa393664%28VS.85%29.aspx ) but the "set" or "create" methods as well as check access all return "5" which is "Error_Access_Denied" according to winerror.h. 所有StdRegProv的“获取”方法都可以正常工作( http://msdn.microsoft.com/en-us/library/aa393664%28VS.85%29.aspx )但是“set”或“create”方法以及检查根据winerror.h访问所有返回“5”,即“Error_Access_Denied”。 So there's the problem: why do I get access denied when attempting to modify the registry? 所以有问题:为什么我在尝试修改注册表时会被拒绝访问? If anyone can help me figure this out, you'd have my utmost appreciation! 如果有人能帮助我解决这个问题,那么您将非常感谢!

I almost forgot, when I fire up Visual Studio in admin mode on my local machine and run the code against the local machine, it works flawlessly. 我差点忘了,当我在本地计算机上以管理模式启动Visual Studio并对本地计算机运行代码时,它可以完美运行。 If I don't start in admin mode on the local machine, the code fails, so I suspect there's a UAC problem maybe? 如果我没有在本地计算机上以管理模式启动,代码会失败,所以我怀疑可能存在UAC问题?

UPDATE: Using regedit and connecting to the remote computer, I CAN change the registry key which leads me to believe that this is not a UAC issue, but it executes with a local WMI connection when running in elevated mode on my own machine, so maybe it is UAC. 更新:使用regedit并连接到远程计算机,我可以更改注册表项,这使我相信这不是一个UAC问题,但它在我自己的机器上以提升模式运行时使用本地WMI连接执行,所以也许这是UAC。 Also, winXP machines return the same error-code (5, ERROR_ACCESS_DENIED) which leads me to believe that it's not UAC... this sucks. 此外,winXP机器返回相同的错误代码(5,ERROR_ACCESS_DENIED),这让我相信它不是UAC ......这很糟糕。

SOLVED: The ManagementClass object is using the wrong override; 求助:ManagementClass对象使用错误的覆盖; it must be parameterized with the ManagementScope, otherwise, you're just executing functions locally. 它必须使用ManagementScope进行参数化,否则,您只需在本地执行功能。

ManagementClass mc = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);

Yes, I'm an epic-fail :/ 9K lines of code, and this line held me up the longest of them all. 是的,我是一个史诗般的失败:/ 9K行代码,这条线让我把它们放在了最长的一行。

using System;
using System.Management;


public class EditRemoteRegistry
{
    public static void Main(string[] args)
    {
        ConnectionOptions options = new ConnectionOptions();
        options.EnablePrivileges = true;
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.Password = "password goes here";
        //options.Authority = "my company's domain";
        //options.Username = "Admin username";

        ManagementScope scope = new ManagementScope("\\\\arbitraryhost\\root\\default", options);
        scope.Connect();

        ManagementClass mc = new ManagementClass("StdRegProv");

        ManagementBaseObject inParams = mc.GetMethodParameters("CreateKey");
        inParams["hDefKey"] = (UInt32)2147483650;
        inParams["sSubKeyName"] = "Software\\Test";
        ManagementBaseObject outParams = mc.InvokeMethod("CreateKey", inParams, null);
        //Should return a 0, but returns a 5, "Error_Access_Denied"
        Console.WriteLine("CreateKey Method returned " + outParams["returnValue"]);

        //This chunk works fine
        ManagementBaseObject inParams5 = mc.GetMethodParameters("GetDWORDValue");
        inParams5["hDefKey"] = 2147483650;
        inParams5["sSubKeyName"] = "Software\\Test";
        inParams5["sValueName"] = "testDWORDvalue";
        ManagementBaseObject outParams5 = mc.InvokeMethod("GetDWORDValue", inParams5, null);
        Console.WriteLine("GetDWORDValue returned " + (UInt32)outParams5["returnValue"] + " ");
        Console.WriteLine((UInt32)outParams5["uValue"]);


        ManagementBaseObject inParams6 = mc.GetMethodParameters("SetStringValue");
        inParams6["hDefKey"] = 2147483650;
        inParams6["sSubKeyName"] = "Software\\Test";
        inParams6["sValueName"] = "TestStringValue";
        inParams6["sValue"] = "Hello World!";
        ManagementBaseObject outParams6 = mc.InvokeMethod("SetStringValue", inParams6, null);
        //Should return a 0, but returns a 5, "Error_Access_Denied"
        Console.WriteLine("SetStringValue returned " + outParams6["returnValue"]);

        Console.ReadKey();
    }
}

You can also turn off remote UAC filtering. 您还可以关闭远程UAC过滤。

Disabling Remote UAC by changing the registry entry that controls Remote UAC is not recommended, but may be necessary in a workgroup. 不建议通过更改控制远程UAC的注册表项来禁用远程UAC,但在工作组中可能是必需的。 The registry entryis HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\system\\LocalAccountTokenFilterPolicy. 注册表项是HKLM \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Policies \\ system \\ LocalAccountTokenFilterPolicy。 When the value of this entry is zero (0), Remote UAC access token filtering is enabled. 当此条目的值为零(0)时,将启用远程UAC访问令牌过滤。 When the value is 1, remote UAC is disabled. 值为1时,禁用远程UAC。

http://msdn.microsoft.com/en-us/library/aa826699(VS.85).aspx http://msdn.microsoft.com/en-us/library/aa826699(VS.85).aspx

It seems that there are issues running WMI setters against machines with UAC on. 似乎在针对具有UAC的计算机上运行WMI setter时存在问题。

quote 引用

From reports we're receiving from the field, it appears UAC needs to be disabled for remote WMI queries to work. 根据我们从现场收到的报告,似乎需要禁用UAC才能使远程WMI查询正常工作。 With UAC running, an administrator account actually has two security tokens, a normal user token, and an administrator token (which is only activated when you pass the UAC prompt). 在UAC运行时,管理员帐户实际上有两个安全令牌,一个普通用户令牌和一个管理员令牌(只有在您通过UAC提示时才会激活)。 Unfortunately, remote requests that come in over the network get the normal user token for the administrator, and since there is no way to handle a UAC prompt remotely, the token can't be elevated to the true-administrator security token. 遗憾的是,通过网络进入的远程请求获得管理员的正常用户令牌,并且由于无法远程处理UAC提示,因此无法将令牌提升为真实管理员安全令牌。

source: http://www.poweradmin.com/help/enableWMI.aspx 来源: http//www.poweradmin.com/help/enableWMI.aspx

Try editing the remote machine's registry key: 尝试编辑远程计算机的注册表项:

HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\system\\LocalAccountTokenFilterPolicy. HKLM \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\政策\\系统\\ LocalAccountTokenFilterPolicy。

0 - build filtered token (Remote UAC enabled) 1 - build elevated token (Remote UAC disabled) 0 - 构建过滤的令牌(启用远程UAC)1 - 构建提升的令牌(禁用远程UAC)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM