注册表打开

  1. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  2. DriverObject->DriverUnload = Unload;
  3. OBJECT_ATTRIBUTES Attr = { 0 };
  4. HANDLE hKey = NULL;
  5. ULONG ulDisposition = 0;
  6. NTSTATUS nStatus = STATUS_SUCCESS;
  7. UNREFERENCED_PARAMETER(DriverObject);
  8. //使用宏初始化objectattr 结构体
  9. InitializeObjectAttributes(
  10. &Attr,
  11. RegisteryPath,
  12. OBJ_KERNEL_HANDLE|OBJ_CASE_INSENSITIVE,
  13. NULL,
  14. NULL);
  15. nStatus = ZwCreateKey(
  16. &hKey,//注册表handle
  17. KEY_WRITE,//访问权限KEY_ALL_ACCESS,KEY_SET_VALUE
  18. &Attr,//对象属性
  19. 0,//可以设置为null
  20. NULL,
  21. REG_OPTION_NON_VOLATILE,//重启后不保留
  22. &ulDisposition);//返回结果 REG_CREATED_NEW_KEY(创建了新的)或REG_OPENED_EXISTING_KEY
  23. if (hKey!=NULL)
  24. {
  25. ZwClose(hKey);//关闭handle
  26. hKey = NULL;
  27. }
  28. return STATUS_SUCCESS;
  29. }

注册表修改

ZwSetValueKey 设置驱动为自启动

  1. if (hKey!=NULL)
  2. {
  3. UNICODE_STRING usValueName = { 0 };
  4. ULONG ulNewStartValue = 2;
  5. RtlInitUnicodeString(&usValueName, L"Start");
  6. nStatus = ZwSetValueKey(
  7. hKey,
  8. &usValueName,
  9. 0,
  10. REG_DWORD,
  11. (PVOID)&ulNewStartValue,
  12. sizeof(ulNewStartValue));
  13. ZwClose(hKey);
  14. hKey = NULL;
  15. }

注册表读取

ZwQueryValueKey