编辑其他用户的注册表项
本文关键字:注册表 用户 其他 编辑 | 更新日期: 2023-09-27 18:01:17
如何更改或编辑当前用户以外的其他用户的注册表值?我知道其他用户的凭据
您可以模拟用户,然后更改当前上下文的注册表。这里有一些关于c#和模拟的资源:
- 使用c#进行Windows模拟
- c# Windows模拟
你要做的是像这样(伪):
using(var impersonation = new Impersonate(username,password))
{
ChangeRegistry(keys, values);
}
并且当模拟被处置时,您将再次使用正在运行的用户。下面是一个模拟类的示例实现,它实现了IDisposable,使其像上面所示的伪示例一样工作,下面是另一个示例。
下面是一个关于如何更改注册表值的示例:
var registry = Registry.CurrentUser;
var key =
registry.OpenSubKey(
@"HKEY_CURRENT_USER'Some'Path'That'You'Want'ToChange", true);
key.SetValue(null, "");
Registry.CurrentUser.Flush();
因此,为了访问HKCU
,您需要做的是还必须加载用户配置文件。这是通过调用另一个称为LoadUserProfile
的Win32方法来完成的。这里有一个完整的示例,您可以使用,但我将在这里包括重要的部分。
首先你需要包括Win32方法,像这样:
[DllImport("userenv.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool LoadUserProfile(IntPtr hToken,
ref ProfileInfo lpProfileInfo);
[DllImport("userenv.dll", CallingConvention = CallingConvention.Winapi,
SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool UnloadUserProfile(IntPtr hToken,
IntPtr lpProfileInfo);
在您的模拟using块中,您需要做以下操作:
ProfileInfo profileInfo = new ProfileInfo();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.lpUserName = userName;
profileInfo.dwFlags = 1;
Boolean loadSuccess = LoadUserProfile(tokenDuplicate, ref profileInfo);
在此之后,你应该能够访问HKCU
。完成后,需要使用UnloadUserProfile(tokenDuplicate, profileInfo.hProfile);
卸载配置文件。
您有两个选择。你可以模仿用户,如果你有他们的凭据philip Ekberg更好地演示;或者
HKCU只是HKEY_USERS
下其中一个密钥的符号链接。如果您知道该用户的SID,那么您可以在这里找到它。您可以这样获取SID:
var account = new NTAccount("userName");
var identifier = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
var sid = identifier.Value;
更好的选择是模拟。当您不知道该用户的凭据时,第二个选项可能会更好。缺点是您需要管理权限才能在其他人的帐户中写入。