c++ - 使用 NtCreateKey() NTAPI 函数创建新的注册表项 [NtOpenKey() 函数返回 NTSTATUS 错误值 -1073741772]
问题描述
我编写了以下代码以在注册表中创建一个新键,但是在尝试获取基本键的句柄以创建新键时,该函数返回了一个NTSTATUS
错误值。-1073741772
NtOpenKey()
typedef NTSTATUS(*LPCREATEKEY) (PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG, PUNICODE_STRING, ULONG, PULONG);
typedef NTSTATUS(*LPOPENKEY) (PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
HINSTANCE dllHandle = nullptr;
LPCREATEKEY createKey = nullptr;
LPOPENKEY openKey = nullptr;
NTSTATUS opStatus = NULL;
HANDLE key = nullptr, baseKey = nullptr;
OBJECT_ATTRIBUTES keyAttributes;
WCHAR keyStr[] = L"XYZ", baseKeyStr[] = L"\\REGISTRY\\MACHINE\\SOFTWARE";
UNICODE_STRING keyName, baseKeyName;
ULONG keyDispositionValue = NULL;
dllHandle = LoadLibrary(L"Ntdll.dll");
if (nullptr != dllHandle) {
// Fetch the function to create a new registry key
createKey = (LPCREATEKEY)GetProcAddress(dllHandle, "NtCreateKey");
openKey = (LPOPENKEY)GetProcAddress(dllHandle, "NtOpenKey");
if (nullptr != createKey && nullptr != openKey) {
baseKeyName.Buffer = baseKeyStr;
baseKeyName.Length = wcslen(baseKeyStr);
baseKeyName.MaximumLength = wcslen(baseKeyStr);
InitializeObjectAttributes(&keyAttributes,
&baseKeyName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
opStatus = openKey(&baseKey, KEY_ALL_ACCESS, &keyAttributes);
if (NT_SUCCESS(opStatus)) {
keyName.Buffer = keyStr;
keyName.Length = wcslen(keyStr);
keyName.MaximumLength = wcslen(keyStr);
InitializeObjectAttributes(&keyAttributes,
&keyName,
OBJ_CASE_INSENSITIVE,
&baseKey,
NULL);
opStatus = createKey(
&key,
KEY_ALL_ACCESS,
&keyAttributes,
NULL,
NULL,
REG_OPTION_NON_VOLATILE,
&keyDispositionValue);
if (NT_SUCCESS(opStatus)) {
cout << "Key successfully created!\n";
//NtClose()
if (!NT_SUCCESS(CloseHandle(key)))
cout << "Error closing created key handle\n";
}
if (!NT_SUCCESS(CloseHandle(baseKey)))
cout << "Error closing base key handle\n";
}
else {
if (NT_ERROR(opStatus))
cout << "Error opening the base key (" << opStatus << ")\n";
}
}
else {
cout << "Could not fetch the functions from the DLL\n";
}
FreeLibrary(dllHandle);
}
else {
cout << "Could not access Ntdll.dll\n";
}
提前致谢。
解决方案
这就是使用 NTAPI 函数在注册表中创建新键的方式。
#include <winternl.h>
typedef NTSTATUS(*LPCREATEKEY) (PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG, PUNICODE_STRING, ULONG, PULONG);
HINSTANCE dll = nullptr;
LPCREATEKEY ntCreateKey = nullptr;
HANDLE createdKey = nullptr;
UNICODE_STRING newKeyName;
OBJECT_ATTRIBUTES attributes;
WCHAR name[] = L"\\REGISTRY\\MACHINE\\SOFTWARE\\NewKeyName";
NTSTATUS opStatus = NULL;
ULONG keyDispositionValue = NULL;
dll = LoadLibrary(L"Ntdll.dll");
if (nullptr != dll) {
ntCreateKey = (LPCREATEKEY) GetProcAddress(dll, "NtCreateKey");
if (nullptr != ntCreateKey) {
newKeyName.Buffer = name;
newKeyName.Length = sizeof(name) - sizeof(WCHAR);
newKeyName.MaximumLength = wcslen(name);
InitializeObjectAttributes(&attributes, &newKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
opStatus = ntCreateKey(&createdKey, KEY_ALL_ACCESS, &attributes, 0, NULL, REG_OPTION_NON_VOLATILE, &keyDispositionValue);
if (NT_SUCCESS(opStatus)) {
if (keyDispositionValue == REG_CREATED_NEW_KEY)
cout << "New key created!\n";
else if (keyDispositionValue == REG_OPENED_EXISTING_KEY)
cout << "Key already exists!\n";
if (!CloseHandle(createdKey))
cout << "Error closing handle!";
}
else {
printf("%X", opStatus);
}
}
else {
cout << "Could not fetch NtCreateKey() from Ntdll.dll\n";
}
FreeLibrary(dll);
}
else {
cout << "Could not access Ntdll.dll\n";
}
推荐阅读
- android - Android:更改菜单项中的图标颜色
- ruby-on-rails - 如何在我的本地服务器 30000 上运行它?rails s 不工作
- powerbi - 过去 12 个月的 SUM 未在 powerBI 中正确计算
- java - 如何在 kotlin 中编写条件循环
- excel - Excel - 如何对连接的子字符串求和
- git - 当我没有进行任何更改时,Git (Gitkraken) 自发地显示更改的文件
- regex - 正则表达式匹配由空格转义双引号/单引号分割?
- javascript - 如何防止 window.pageYOffset 在渲染时重置?
- javascript - 如何从asp.net中的动态按钮调用javascript?
- python - 创建注意力模型时出错:数据基数不明确