我得到了这个代码:
int wmain(int argc, WCHAR *argv[])
{
HKEY hKey = HKEY_LOCAL_MACHINE;
LPCWSTR subKey = L"Example";
LPCWSTR pFile = L"C:\\Users\\Default\\NTUSER.DAT";
LONG loadKey = RegLoadKey(hKey, subKey, pFile);
if (loadKey != ERROR_SUCCESS) {
wprintf(L"Code: %li\n", loadKey);
} else {
wprintf(L"Mounted!\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我需要为我的呼叫过程启用 SE_RESTORE_NAME和SE_BACKUP_NAME,并且MSDN 上有一个示例,但无法理解。不知道把我需要的特权放在哪里。
任何人都可以向我展示如何合并所有这些并使RegLoadKey()功能正常工作的示例吗?
有很多 MSDN 文档没有解释的变量,比如hToken,等等。这就是我需要帮助的原因。
非常感谢您的时间。将分享我使用的代码;它可能会帮助其他人:
#include <windows.h>
#include <stdio.h>
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCWSTR nameOfPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
nameOfPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
printf("AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
int wmain(int argc, WCHAR *argv[])
{
HANDLE proccessHandle = GetCurrentProcess(); // get the handle to the current proccess
DWORD typeOfAccess = TOKEN_ADJUST_PRIVILEGES; // requiered to enable or disable the privilege
HANDLE tokenHandle; // handle to the opened access token
HKEY hKey = HKEY_LOCAL_MACHINE;
LPCWSTR subKeyName = L"Debu";
LPCWSTR pHive = L"C:\\Users\\Default\\NTUSER.DAT";
if (OpenProcessToken(proccessHandle, typeOfAccess, &tokenHandle))
{
// Enabling RESTORE and BACKUP privileges
SetPrivilege(tokenHandle, SE_RESTORE_NAME, TRUE);
SetPrivilege(tokenHandle, SE_BACKUP_NAME, TRUE);
}
else
{
wprintf(L"Error getting the access token.\n");
}
// Loading the HIVE into HKLM\Debu subkey
LONG loadKey = RegLoadKeyW(hKey, subKeyName, pHive);
if (loadKey != ERROR_SUCCESS)
{
wprintf(L"Error loading the key. Code: %li\n", loadKey);
}
else
{
wprintf(L"Hive file has been loaded.\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |