#include <ntddk.h>
#pragma warning( disable : 4100)
#define MEM_TAG 'MyTg'
VOID
MyRegDemo()
{
OBJECT_ATTRIBUTES my_reg_attribute = {0};
UNICODE_STRING objectName;
RtlInitUnicodeString(&objectName, L
"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
);
HANDLE
my_reg;
UNICODE_STRING my_key_name;
RtlInitUnicodeString(&my_key_name, L
"my_key"
);
KEY_VALUE_PARTIAL_INFORMATION key_infor;
PKEY_VALUE_PARTIAL_INFORMATION ac_key_infor = NULL;
ULONG
ac_length;
NTSTATUS status;
UNICODE_STRING my_key_value = RTL_CONSTANT_STRING(L
"A_String"
);
my_key_value.Length = my_key_value.MaximumLength = 0x12;
InitializeObjectAttributes(
&my_reg_attribute,
&objectName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL
);
status = ZwOpenKey(&my_reg, KEY_ALL_ACCESS, &my_reg_attribute);
if
(!NT_SUCCESS(status)) {
DbgPrint(
"ZwOpenKey failed: %08X\n"
, status);
return
;
}
status = ZwSetValueKey(
my_reg,
&my_key_name,
0,
REG_SZ,
my_key_value.Buffer,
my_key_value.Length
);
if
(!NT_SUCCESS(status)) {
DbgPrint(
"ZwSetValueKey failed: %08X\n"
, status);
ZwClose(my_reg);
return
;
}
status = ZwQueryValueKey(
my_reg,
&my_key_name,
KeyValuePartialInformation,
&key_infor,
sizeof
(KEY_VALUE_PARTIAL_INFORMATION),
&ac_length
);
if
(status != STATUS_BUFFER_OVERFLOW && status != STATUS_BUFFER_TOO_SMALL) {
DbgPrint(
"ZwQueryValueKey failed: %08X\n"
, status);
ZwClose(my_reg);
return
;
}
ac_key_infor = (PKEY_VALUE_PARTIAL_INFORMATION)ExAllocatePoolWithTag(NonPagedPool, ac_length, MEM_TAG);
if
(ac_key_infor == NULL) {
DbgPrint(
"ExAllocatePoolWithTag failed\n"
);
ZwClose(my_reg);
return
;
}
status = ZwQueryValueKey(
my_reg,
&my_key_name,
KeyValuePartialInformation,
ac_key_infor,
ac_length,
&ac_length
);
if
(!NT_SUCCESS(status)) {
DbgPrint(
"ZwQueryValueKey failed: %08X\n"
, status);
ExFreePoolWithTag(ac_key_infor, MEM_TAG);
ZwClose(my_reg);
return
;
}
UNICODE_STRING print_key_value;
print_key_value.Length = print_key_value.MaximumLength = (
USHORT
)ac_key_infor->DataLength;
print_key_value.Buffer = (
PWSTR
)ExAllocatePoolWithTag(NonPagedPool, print_key_value.Length, MEM_TAG);
if
(print_key_value.Buffer == NULL) {
DbgPrint(
"ExAllocatePoolWithTag for print_key_value failed\n"
);
ExFreePoolWithTag(ac_key_infor, MEM_TAG);
ZwClose(my_reg);
return
;
}
RtlCopyMemory(print_key_value.Buffer, ac_key_infor->Data, ac_key_infor->DataLength);
print_key_value.Buffer[print_key_value.Length /
sizeof
(
WCHAR
) - 1] = L
'\0'
;
DbgPrint(
"%wZ\n"
, &print_key_value);
ExFreePoolWithTag(print_key_value.Buffer, MEM_TAG);
ExFreePoolWithTag(ac_key_infor, MEM_TAG);
ZwClose(my_reg);
}
VOID
DriverUnload(PDRIVER_OBJECT driver)
{
DbgPrint(
"Driver is unloading...\n"
);
}
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
MyRegDemo();
driver->DriverUnload = DriverUnload;
return
STATUS_SUCCESS;
}