访问远程计算机上的注册表 [英] Accessing the Registry on a remote computer

查看:72
本文介绍了访问远程计算机上的注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试访问远程计算机的注册表(Windows 2003).
遵循文章中的评论:
http://msdn.microsoft.com/en-us/library/ms724840 (v = vs.85).aspx
我同时使用了模拟和WNetAddConnection2.

这是我的代码:

Hi,

I am trying to access the registry of a remote computer (windows 2003).
Following the remarks in the article:
http://msdn.microsoft.com/en-us/library/ms724840(v=vs.85).aspx
I used both Impersonation and WNetAddConnection2.

Here is my code:

void ConnectRemoteRegistry(LPCSTR machine, LPWSTR wRemoteName, LPWSTR wUser, LPWSTR wPass, 

LPCSTR cUser, LPCSTR cPass, std::string keyName)
{
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;

// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof (NETRESOURCE));

// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = NULL;
nr.lpRemoteName = wRemoteName;
nr.lpProvider = NULL;

// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, wPass, wUser, dwFlags); 

if (dwRetVal == NO_ERROR)
    wprintf(L"Connection added to %s\n", nr.lpRemoteName);
else
    wprintf(L"WNetAddConnection2 failed with error: %u\n", dwRetVal);

HANDLE _token;

dwRetVal = LogonUserA(cUser, NULL, cPass, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, 

&_token);

if(dwRetVal != ERROR_SUCCESS)
{
    wprintf(L"LogonUserA failed with error: %u\n", dwRetVal);
}

dwRetVal = ImpersonateLoggedOnUser(_token);

if(dwRetVal != ERROR_SUCCESS)
{
    wprintf(L"ImpersonateLoggedOnUser failed with error: %u\n", dwRetVal);
}

HKEY root(HKEY_LOCAL_MACHINE);
HKEY r;
LONG rc = ::RegConnectRegistryA(machine, root, &r);

if (rc != ERROR_SUCCESS) 
{
    wprintf(L"RegConnectRegistryA failed with error: %u\n", dwRetVal);
}

HKEY key_handle;

rc = ::RegOpenKeyExA(r,keyName.c_str(), REG_OPTION_OPEN_LINK, 
KEY_QUERY_VALUE, & key_handle);

if (rc != ERROR_SUCCESS) 
{
    wprintf(L"RegOpenKeyExA failed with error: %u\n", dwRetVal);
}

RegCloseKey(r);
RevertToSelf();

dwRetVal = WNetCancelConnection2(nr.lpRemoteName, CONNECT_UPDATE_PROFILE, true);

if (dwRetVal != NO_ERROR)
    wprintf(L"WNetCancelConnection2 failed with error: %u\n", dwRetVal);
}


我正在使用远程计算机的本地管理员.
WNetAddConnection2到c $已成功完成.
LogonUserA和ImpersonateLoggedOnUser已成功完成.
RegConnectRegistryA也已成功完成.
问题是RegOpenKeyExA返回5-访问被拒绝.

您能告诉我为什么会这样吗?

谢谢.


I am using a local administrator of the remote machine.
WNetAddConnection2 to c$ is completed successfully.
LogonUserA and ImpersonateLoggedOnUser are completed successfully.
RegConnectRegistryA is also completed successfully.
The problem is that RegOpenKeyExA returns 5 - access denied.

Can you please advise why that might be?

Thanks.

推荐答案

已成功完成.
LogonUserA和ImpersonateLoggedOnUser已成功完成.
RegConnectRegistryA也已成功完成.
问题是RegOpenKeyExA返回5-访问被拒绝.

您能告诉我为什么会这样吗?

谢谢.
is completed successfully.
LogonUserA and ImpersonateLoggedOnUser are completed successfully.
RegConnectRegistryA is also completed successfully.
The problem is that RegOpenKeyExA returns 5 - access denied.

Can you please advise why that might be?

Thanks.


您传递的参数似乎不正确.检查 MSDN [
Well the parameters you are passing don''t seem right. Check MSDN [^]for details

from the documentation
ulOptions
[in] Reserved; set to 0.
samDesired
[in] Not supported; set to 0.



您的价值观



your values

RegOpenKeyExA(r,keyName.c_str(), REG_OPTION_OPEN_LINK,
KEY_QUERY_VALUE, & key_handle);


所以问题出在samDesired的第四个参数(仅尝试STANDARD_RIGHTS_READ | KEY_QUERY_VALUE)和/或第三个和第四个的组合.我建议您准确阅读注册表项安全性和访问权限. [ ^ ]:((
So the problem comes from samDesired fourth parameter (try only STANDARD_RIGHTS_READ | KEY_QUERY_VALUE), and/or combination of third and fourth. I suggest that you read precisely Registry Key Security and Access Rights.[^] :((


这篇关于访问远程计算机上的注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆