免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

驱动读写注册表

注册表项相当于文件夹,注册表子项子文件夹(类似目录)。

1、创建关闭

ZwCreateKey  

2、打开  

ZwOpenKey  

3、添加、修改、注册表键值

ZwSetValueKey  

4、查询  

ZwQueryValueKey  

来查询注册表项。

1)用ZwQueryValueKey 获取数据结构的长度。

2)分配如此长度的内存。

3)再次调用ZwQueryValueKey 查询。

4)回收内存。

5、枚举子项

用ZwQueryKey与ZwEnumerateKey 来枚举子项。

ZwQueryKey获取某注册表项究竟有多少个子项,而ZwEnumerateKey 针对第几个子项获取该子项的具体信息。

我们发现,凡是调用有长度的函数,一般都是两次调用,第一次调用来获得具体查询(使用该函数)需要的内存需要多大长度,第二次调用时来查询具体信息。


6、枚举子键

通过ZwQueryKey与ZwEnumerateValueKey来枚举子键。

7、删除子项

ZwDeleteKey

只能删除没有子项的项目。

8、其它

DDK中定义了一些运行时函数来简化上面的操作。

RtlCreateRegistryKey  创建注册表

RtlCheckRegistryKey  checks for the existence of a given named key in the registry

RtlWriteRegistryValue  写注册表

RtlDeleteRegistryValue  删除注册表
  1. #include "ntddk.h"

  2. #define PAGEDCODE code_seg("PAGE")
  3. #define LOCKEDCODE code_seg()
  4. #define INITCODE code_seg("INIT")

  5. #define PAGEDDATA data_seg("PAGE")
  6. #define LOCKEDDATA data_seg()
  7. #define INITDATA data_seg("INIT")


  8. NTSTATUS MyEnumerateKey(ULONG SubKeys);


  9. #pragma PAGEDCODE
  10. NTSTATUS MyCreateKey()
  11. {
  12.         NTSTATUS Status;

  13.         HANDLE hRegister;
  14.         OBJECT_ATTRIBUTES ObjAttr;
  15.         UNICODE_STRING usKeyName;
  16.         ULONG Disposition;

  17.         RtlInitUnicodeString(&usKeyName, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Services\\MyKeys");

  18.         InitializeObjectAttributes(&ObjAttr, &usKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);

  19.         Status = ZwCreateKey(&hRegister, KEY_ALL_ACCESS, &ObjAttr, 0, NULL,
  20.                 REG_OPTION_NON_VOLATILE, &Disposition);

  21.         if (!NT_SUCCESS(Status))
  22.         {
  23.                 KdPrint(("Failed Code:%X", Status));
  24.                 return Status;
  25.         }

  26.         if (Disposition == REG_CREATED_NEW_KEY)
  27.         {
  28.                 KdPrint(("create key!\n"));
  29.         }
  30.         else
  31.         {
  32.                 KdPrint(("open key!\n"));
  33.         }


  34.         ZwClose(hRegister);
  35.         return Status;
  36. }


  37. #pragma PAGEDCODE
  38. NTSTATUS MyOpenKey()
  39. {
  40.         NTSTATUS Status;

  41.         HANDLE hRegister;
  42.         OBJECT_ATTRIBUTES ObjAttr;
  43.         UNICODE_STRING usKeyName, usValueKeyName;
  44.         ULONG ValueKey;

  45.         RtlInitUnicodeString(&usKeyName, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Services\\MyKeys");
  46.         RtlInitUnicodeString(&usValueKeyName, L"MyValueKey");

  47.         InitializeObjectAttributes(&ObjAttr, &usKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);

  48.         Status = ZwOpenKey(&hRegister, KEY_ALL_ACCESS, &ObjAttr);

  49.         if (!NT_SUCCESS(Status))
  50.         {
  51.                 KdPrint(("Failed Code:%X", Status));
  52.                 return Status;
  53.         }

  54.         KdPrint(("open key success!\n"));

  55.         ValueKey = 10;
  56.         Status = ZwSetValueKey(hRegister, &usValueKeyName, 0, REG_DWORD, &ValueKey, sizeof(ULONG));

  57.         if (!NT_SUCCESS(Status))
  58.         {
  59.                 KdPrint(("Failed Code:%X", Status));
  60.                 return Status;
  61.         }

  62.         ZwClose(hRegister);
  63.         return Status;
  64. }


  65. #pragma PAGEDCODE
  66. NTSTATUS MyQueryValueKey()
  67. {
  68.         NTSTATUS Status;

  69.         HANDLE hRegister;
  70.         OBJECT_ATTRIBUTES ObjAttr;
  71.         UNICODE_STRING usKeyName, usValueKeyName;
  72.         ULONG ResultLength;
  73.         PKEY_VALUE_BASIC_INFORMATION pKeyValueBasicInfo;

  74.         RtlInitUnicodeString(&usKeyName, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Services\\FltMgr");
  75.         RtlInitUnicodeString(&usValueKeyName, L"Type");

  76.         InitializeObjectAttributes(&ObjAttr, &usKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);

  77.         Status = ZwOpenKey(&hRegister, KEY_ALL_ACCESS, &ObjAttr);

  78.         if (!NT_SUCCESS(Status))
  79.         {
  80.                 KdPrint(("Failed Code:%X", Status));
  81.                 return Status;
  82.         }

  83.         Status = ZwQueryValueKey(hRegister, &usValueKeyName, KeyValueBasicInformation,
  84.                 NULL, 0, &ResultLength);

  85.         if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
  86.         {
  87.                 KdPrint(("Failed Code:%X", Status));
  88.                 return Status;
  89.         }

  90.         pKeyValueBasicInfo = (PKEY_VALUE_BASIC_INFORMATION)ExAllocatePool(PagedPool, ResultLength);

  91.         Status = ZwQueryValueKey(hRegister, &usValueKeyName, KeyValueBasicInformation,
  92.                 pKeyValueBasicInfo, ResultLength, &ResultLength);

  93.         if (!NT_SUCCESS(Status))
  94.         {
  95.                 KdPrint(("Failed Code:%X", Status));
  96.                 return Status;
  97.         }
  98.        
  99.         KdPrint(("Type: %d", pKeyValueBasicInfo->Type));
  100.         KdPrint(("Name: %S", pKeyValueBasicInfo->Name));

  101.         ZwClose(hRegister);
  102.         ExFreePool(pKeyValueBasicInfo);
  103.        
  104.         return Status;
  105. }


  106. #pragma PAGEDCODE
  107. NTSTATUS MyQueryKey()
  108. {
  109.         NTSTATUS Status;

  110.         HANDLE hRegister;
  111.         OBJECT_ATTRIBUTES ObjAttr;
  112.         UNICODE_STRING usKeyName;
  113.         ULONG ResultLength;
  114.         PKEY_FULL_INFORMATION pKeyFullInfo;

  115.         RtlInitUnicodeString(&usKeyName, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Services\\FltMgr");
  116.        

  117.         InitializeObjectAttributes(&ObjAttr, &usKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);

  118.         Status = ZwOpenKey(&hRegister, KEY_ALL_ACCESS, &ObjAttr);

  119.         if (!NT_SUCCESS(Status))
  120.         {
  121.                 KdPrint(("Failed Code:%X", Status));
  122.                 return Status;
  123.         }

  124.         Status = ZwQueryKey(hRegister, KeyFullInformation,
  125.                 NULL, 0, &ResultLength);

  126.         if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
  127.         {
  128.                 KdPrint(("Failed Code:%X", Status));
  129.                 return Status;
  130.         }

  131.         pKeyFullInfo = (PKEY_FULL_INFORMATION)ExAllocatePool(PagedPool, ResultLength);

  132.         Status = ZwQueryKey(hRegister, KeyFullInformation,
  133.                 pKeyFullInfo, ResultLength, &ResultLength);

  134.         if (!NT_SUCCESS(Status))
  135.         {
  136.                 KdPrint(("Failed Code:%X", Status));
  137.                 return Status;
  138.         }

  139.         KdPrint(("Subkey: %d", pKeyFullInfo->SubKeys));

  140.         MyEnumerateKey(pKeyFullInfo->SubKeys);

  141.         ZwClose(hRegister);
  142.         ExFreePool(pKeyFullInfo);

  143.         return Status;
  144. }


  145. #pragma PAGEDCODE
  146. NTSTATUS MyEnumerateKey(ULONG SubKeys)
  147. {
  148.         NTSTATUS Status;

  149.         HANDLE hRegister;
  150.         OBJECT_ATTRIBUTES ObjAttr;
  151.         UNICODE_STRING usKeyName;
  152.         ULONG ResultLength;
  153.         PKEY_BASIC_INFORMATION pKeyBasicInfo;
  154.         ULONG Index;

  155.         RtlInitUnicodeString(&usKeyName, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Services\\FltMgr");


  156.         InitializeObjectAttributes(&ObjAttr, &usKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);

  157.         Status = ZwOpenKey(&hRegister, KEY_ALL_ACCESS, &ObjAttr);

  158.         if (!NT_SUCCESS(Status))
  159.         {
  160.                 KdPrint(("Failed Code:%X", Status));
  161.                 return Status;
  162.         }

  163.         for (Index = 0; Index < SubKeys; Index++)
  164.         {
  165.                 Status = ZwEnumerateKey(hRegister, Index, KeyBasicInformation, NULL, 0, &ResultLength);

  166.                 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
  167.                 {
  168.                         KdPrint(("Failed Code:%X", Status));
  169.                         return Status;
  170.                 }

  171.                 pKeyBasicInfo = (PKEY_BASIC_INFORMATION)ExAllocatePool(PagedPool, ResultLength);

  172.                 Status = ZwEnumerateKey(hRegister, Index, KeyBasicInformation, pKeyBasicInfo, ResultLength, &ResultLength);

  173.                 if (!NT_SUCCESS(Status))
  174.                 {
  175.                         KdPrint(("Failed Code:%X", Status));
  176.                         return Status;
  177.                 }


  178.                 KdPrint(("Name:%S\n", pKeyBasicInfo->Name));

  179.                 ExFreePool(pKeyBasicInfo);

  180.         }

  181.        
  182.         ZwClose(hRegister);
  183.         return Status;

  184. }


  185. #pragma PAGEDCODE
  186. VOID MyDriverUnload(IN PDRIVER_OBJECT pDriverObject)
  187. {

  188.         KdPrint(("DriverEntry unLoading...\n"));

  189. }



  190. #pragma INITCODE
  191. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath)
  192. {
  193.         NTSTATUS status = STATUS_SUCCESS;

  194.         KdPrint(("%wZ\n", RegistryPath));

  195.         MyQueryKey();

  196.         pDriverObject->DriverUnload = MyDriverUnload;
  197.         return status;
  198. }
复制代码

返回列表