簡體   English   中英

編輯其他用戶的注冊表項

[英]Edit registry key of other user

如何更改或編輯當前用戶以外的其他用戶的注冊表值? 我知道其他用戶的憑據。

您可以模擬用戶,然后更改該當前上下文的注冊表。 以下是有關 C# 和模擬的一些資源:

你想要做的是這樣的(偽):

using(var impersonation = new Impersonate(username,password))
{
    ChangeRegistry(keys, values);
}

並且當模擬被處理后,您將再次使用正在運行的用戶。 這是一個 Impersonate class 的示例實現,它實現 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-block 中,您需要執行以下操作:

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);卸載配置文件 .

你有兩個選擇。 如果您擁有 Filip Ekberg 更好地演示的憑據,則可以冒充該用戶; 或者

HKCU 只是HKEY_USERS下的其中一個鍵的符號鏈接。 如果您知道該用戶的 SID,那么您可以在其中找到它。 您可以這樣獲取 SID:

var account = new NTAccount("userName");
var identifier = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
var sid = identifier.Value;

更好的選擇是模仿。 當您不知道該用戶的憑據時,第二個選項可能會更好。 缺點是您需要管理權限才能在其他人的帳戶中寫入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM