deviface.c

来自「winNT技术操作系统,国外开放的原代码和LIUX一样」· C语言 代码 · 共 546 行 · 第 1/2 页

C
546
字号
            DPRINT("ZwOpenKey() Failed. (0x%X)\n", Status);
            ExFreePool(fip);
            if (SymLinkList != NULL)
               ExFreePool(SymLinkList);
            RtlFreeUnicodeString(&SubKeyName);
            RtlFreeUnicodeString(&BaseKeyName);
            ZwClose(InterfaceKey);
            return Status;
         }

         Status = ZwQueryKey(
            SubKey,
            KeyFullInformation,
            NULL,
            0,
            &Size);

         if (Status != STATUS_BUFFER_TOO_SMALL)
         {
            DPRINT("ZwQueryKey() Failed. (0x%X)\n", Status);
            ExFreePool(fip);
            RtlFreeUnicodeString(&BaseKeyName);
            RtlFreeUnicodeString(&SubKeyName);
            ZwClose(SubKey);
            ZwClose(InterfaceKey);
            return Status;
         }

         bfip = (PKEY_FULL_INFORMATION)ExAllocatePool(NonPagedPool, Size);
         ASSERT(bfip != NULL);

         Status = ZwQueryKey(
            SubKey,
            KeyFullInformation,
            bfip,
            Size,
            &Size);
  
         if (!NT_SUCCESS(Status))
         {
            DPRINT("ZwQueryKey() Failed. (0x%X)\n", Status);
            ExFreePool(fip);
            RtlFreeUnicodeString(&SubKeyName);
            RtlFreeUnicodeString(&BaseKeyName);
            ZwClose(SubKey);
            ZwClose(InterfaceKey);
            return Status;
         }
         
         for(j = 0; j < bfip->SubKeys; j++)
         {
            Status = ZwEnumerateKey(
               SubKey,
               j,
               KeyBasicInformation,
               NULL,
               0,
               &Size);

            if (Status == STATUS_NO_MORE_ENTRIES)
               continue;

            if (Status != STATUS_BUFFER_TOO_SMALL)
            {
               DPRINT("ZwEnumerateKey() Failed.(0x%X)\n", Status);
               ExFreePool(bfip);
               ExFreePool(fip);
               if (SymLinkList != NULL)
                  ExFreePool(SymLinkList);
               RtlFreeUnicodeString(&SubKeyName);
               RtlFreeUnicodeString(&BaseKeyName);
               ZwClose(SubKey);
               ZwClose(InterfaceKey);
               return Status;
            }

            bip = (PKEY_BASIC_INFORMATION)ExAllocatePool(NonPagedPool, Size);
            ASSERT(bip != NULL);

            Status = ZwEnumerateKey(
               SubKey,
               j,
               KeyBasicInformation,
               bip,
               Size,
               &Size);

            if (!NT_SUCCESS(Status))
            {
               DPRINT("ZwEnumerateKey() Failed.(0x%X)\n", Status);
               ExFreePool(fip);
               ExFreePool(bfip);
               ExFreePool(bip);
               if (SymLinkList != NULL)
                  ExFreePool(SymLinkList);
               RtlFreeUnicodeString(&SubKeyName);
               RtlFreeUnicodeString(&BaseKeyName);
               ZwClose(SubKey);
               ZwClose(InterfaceKey);
               return Status;
            }
         
            if (!wcsncmp(bip->Name, L"Control", bip->NameLength))
            {
               continue;
            }
            
            SymbolicLinkKeyName.Length = 0;
            SymbolicLinkKeyName.MaximumLength = SubKeyName.Length + bip->NameLength + sizeof(WCHAR);
            SymbolicLinkKeyName.Buffer = ExAllocatePool(NonPagedPool, SymbolicLinkKeyName.MaximumLength);
            ASSERT(SymbolicLinkKeyName.Buffer != NULL);
            TempString.Length = TempString.MaximumLength = bip->NameLength;
            TempString.Buffer = bip->Name;
            RtlCopyUnicodeString(&SymbolicLinkKeyName, &SubKeyName);
            RtlAppendUnicodeToString(&SymbolicLinkKeyName, L"\\");
            RtlAppendUnicodeStringToString(&SymbolicLinkKeyName, &TempString);

            ControlKeyName.Length = 0;
            ControlKeyName.MaximumLength = SymbolicLinkKeyName.Length + Control.Length + sizeof(WCHAR);
            ControlKeyName.Buffer = ExAllocatePool(NonPagedPool, ControlKeyName.MaximumLength);
            ASSERT(ControlKeyName.Buffer != NULL);
            RtlCopyUnicodeString(&ControlKeyName, &SymbolicLinkKeyName);
            RtlAppendUnicodeStringToString(&ControlKeyName, &Control);
   
            ExFreePool(bip);

            InitializeObjectAttributes(
               &ObjectAttributes,
               &SymbolicLinkKeyName,
               OBJ_CASE_INSENSITIVE,
               NULL,
               NULL);

            Status = ZwOpenKey(
               &SymbolicLinkKey,
               KEY_READ,
               &ObjectAttributes);

            if (!NT_SUCCESS(Status))
            {
               DPRINT("ZwOpenKey() Failed. (0x%X)\n", Status);
               ExFreePool(fip);
               ExFreePool(bfip);
               if (SymLinkList != NULL)
                  ExFreePool(SymLinkList);
               RtlFreeUnicodeString(&SymbolicLinkKeyName);
               RtlFreeUnicodeString(&SubKeyName);
               RtlFreeUnicodeString(&BaseKeyName);
               ZwClose(SubKey);
               ZwClose(InterfaceKey);
               return Status;
            }

            Status = ZwQueryValueKey(
               SymbolicLinkKey, 
               &SymbolicLink, 
               KeyValuePartialInformation,
               NULL,
               0,
               &Size);
               
            if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
               continue;
      
            if (Status != STATUS_BUFFER_TOO_SMALL)
            {
               DPRINT("ZwQueryValueKey() Failed.(0x%X)\n", Status);
               ExFreePool(fip);
               ExFreePool(bfip);
               if (SymLinkList != NULL)
                  ExFreePool(SymLinkList);
               RtlFreeUnicodeString(&SymbolicLinkKeyName);
               RtlFreeUnicodeString(&SubKeyName);
               RtlFreeUnicodeString(&BaseKeyName);
               ZwClose(SymbolicLinkKey);
               ZwClose(SubKey);
               ZwClose(InterfaceKey);
               return Status;
            }

            vpip = (PKEY_VALUE_PARTIAL_INFORMATION)ExAllocatePool(NonPagedPool, Size);
            ASSERT(vpip != NULL);

            Status = ZwQueryValueKey(
               SymbolicLinkKey,
               &SymbolicLink,
               KeyValuePartialInformation,
               vpip,
               Size,
               &Size);

            if (!NT_SUCCESS(Status))
            {
               DPRINT("ZwQueryValueKey() Failed.(0x%X)\n", Status);
               ExFreePool(fip);
               ExFreePool(bfip);
               ExFreePool(vpip);
               if (SymLinkList != NULL)
                  ExFreePool(SymLinkList);
               RtlFreeUnicodeString(&SymbolicLinkKeyName);
               RtlFreeUnicodeString(&SubKeyName);
               RtlFreeUnicodeString(&BaseKeyName);
               ZwClose(SymbolicLinkKey);
               ZwClose(SubKey);
               ZwClose(InterfaceKey);
               return Status;
            }

            Status = RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE, ControlKeyName.Buffer);

            if (NT_SUCCESS(Status))
            {         
               /* Put the name in the string here */
               if (SymLinkList == NULL)
               {
                  SymLinkListSize = vpip->DataLength;
                  SymLinkList = ExAllocatePool(NonPagedPool, SymLinkListSize + sizeof(WCHAR));
                  ASSERT(SymLinkList != NULL);
                  RtlCopyMemory(SymLinkList, vpip->Data, vpip->DataLength);
                  SymLinkList[vpip->DataLength / sizeof(WCHAR)] = 0;
                  SymLinkList[1] = '?';
               }
               else
               {
                  PWCHAR OldSymLinkList;
                  ULONG OldSymLinkListSize;
                  PWCHAR SymLinkListPtr;

                  OldSymLinkList = SymLinkList;
                  OldSymLinkListSize = SymLinkListSize;
                  SymLinkListSize += vpip->DataLength;
                  SymLinkList = ExAllocatePool(NonPagedPool, SymLinkListSize + sizeof(WCHAR));
                  ASSERT(SymLinkList != NULL);
                  RtlCopyMemory(SymLinkList, OldSymLinkList, OldSymLinkListSize);
                  ExFreePool(OldSymLinkList);
                  SymLinkListPtr = SymLinkList + (OldSymLinkListSize / sizeof(WCHAR));
                  RtlCopyMemory(SymLinkListPtr, vpip->Data, vpip->DataLength);
                  SymLinkListPtr[vpip->DataLength / sizeof(WCHAR)] = 0;
                  SymLinkListPtr[1] = '?';
               }               
            }

            RtlFreeUnicodeString(&SymbolicLinkKeyName);
            RtlFreeUnicodeString(&ControlKeyName);
            ZwClose(SymbolicLinkKey);
         }

         ExFreePool(vpip);
         RtlFreeUnicodeString(&SubKeyName);
         ZwClose(SubKey);
      }

      if (SymLinkList != NULL)
      {
         SymLinkList[SymLinkListSize / sizeof(WCHAR)] = 0;
      }
      else
      {
         SymLinkList = ExAllocatePool(NonPagedPool, 2 * sizeof(WCHAR));
         SymLinkList[0] = 0;
      }

      *SymbolicLinkList = SymLinkList;
    
      RtlFreeUnicodeString(&BaseKeyName);
      ZwClose(InterfaceKey);
      ExFreePool(bfip);
      ExFreePool(fip);
   }

   return STATUS_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?