NTSTATUS SetRegistryValueKey()
{
HANDLE KeyHandle = NULL;
NTSTATUS Status = STATUS_SUCCESS;
//UNICODE_STRING KeyPath = G_KeyPath;//RTL_CONSTANT_STRING(L"\\Registry\\User\\S-1-5-21-4212467076-4147758605-2199231723-1000\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");//S-1-5-21-361059460-2165877035-28880545-500
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING SourceKeyName = RTL_CONSTANT_STRING(L"TmpURL");
UNICODE_STRING TargetKeyName = RTL_CONSTANT_STRING(L"AutoConfigURL");
PKEY_VALUE_PARTIAL_INFORMATION AcKeyInfo = NULL;
KEY_VALUE_PARTIAL_INFORMATION KeyInfo;
ULONG Length = 0;
//初始化OBJECT_ATTRIBUTES结构体
InitializeObjectAttributes(
&ObjectAttributes,
&Globals_KEYPATH,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
//打开注册表键
Status = ZwOpenKey(&KeyHandle, /*KEY_READ | KEY_WRITE*/KEY_ALL_ACCESS, &ObjectAttributes);
if (!NT_SUCCESS(Status))
{
DbgPrint("打开注册表键失败!! -- %#X\n", Status);
return Status;
}
//第一次读取注册表键值探测数据大小
Status = ZwQueryValueKey(
KeyHandle,
&SourceKeyName,
KeyValuePartialInformation,
&KeyInfo,
sizeof(KEY_VALUE_PARTIAL_INFORMATION),
&Length);
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW && Status != STATUS_BUFFER_TOO_SMALL)
{
DbgPrint("读取 AutoConfigURL 键值失败!! - %#X\n", Status);
ZwClose(KeyHandle);
return Status;
}
//根据上面探测的注册表键值大小动态分配内存
AcKeyInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ExAllocatePoolWithTag(NonPagedPool, Length, "tag2");
if (NULL == AcKeyInfo)
{
DbgPrint("在分配保存键值的内存空间时失败!!");
ZwClose(KeyHandle);
Status = STATUS_INSUFFICIENT_RESOURCES;
return Status;
}
//再次读取注册表键值
Status = ZwQueryValueKey(
KeyHandle,
&SourceKeyName,
KeyValuePartialInformation,
AcKeyInfo,
Length,
&Length);
if (!NT_SUCCESS(Status))
{
DbgPrint("读取 AutoConfigURL 键值失败!! - %#X\n", Status);
ZwClose(KeyHandle);
ExFreePool(AcKeyInfo);
return Status;
}
//写入注册表值
Status = ZwSetValueKey(
KeyHandle,
&TargetKeyName,
0,
AcKeyInfo->Type,
AcKeyInfo->Data,
AcKeyInfo->DataLength);
if (!NT_SUCCESS(Status))
{
DbgPrint("写入目标键值失败!! - %#X\n", Status);
ZwClose(KeyHandle);
ExFreePool(AcKeyInfo);
return Status;
}
DbgPrint("AutoConfigURL 已写入新值!!\n");
//释放内存与句柄
ZwClose(KeyHandle);
ExFreePool(AcKeyInfo);
return Status;
}