Advertisement
Guest User

ListFiles in kernel mode

a guest
Jan 24th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1.  
  2. #include <ntifs.h>
  3.  
  4. #define TAG_POOLTEST 'P'
  5.  
  6. NTSTATUS ListDir() {
  7.  
  8.     //WCHAR Buffer[8192];
  9.     PVOID Ptr;
  10.     SIZE_T poolSize;
  11.     UNICODE_STRING DirectoryName;
  12.     OBJECT_ATTRIBUTES DirectoryAttributes;
  13.     NTSTATUS Status;
  14.     HANDLE DirectoryHandle;
  15.     IO_STATUS_BLOCK Iosb;
  16.     PFILE_BOTH_DIR_INFORMATION DirInformation;
  17.  
  18.     poolSize = 8192;
  19.  
  20.     Ptr = ExAllocatePoolWithTag(PagedPool, poolSize, TAG_POOLTEST);
  21.  
  22.     if (!Ptr) return STATUS_INSUFFICIENT_RESOURCES;
  23.  
  24.     RtlInitUnicodeString(&DirectoryName, L"\\??\\C:\\Windows");
  25.  
  26.         InitializeObjectAttributes(&DirectoryAttributes,
  27.             &DirectoryName,
  28.             OBJ_CASE_INSENSITIVE,
  29.             0,          // absolute open, no relative directory handle
  30.             0);         // no security descriptor necessary
  31.  
  32.     Status = ZwCreateFile(&DirectoryHandle,
  33.         (FILE_LIST_DIRECTORY | SYNCHRONIZE),
  34.         &DirectoryAttributes,
  35.         &Iosb,
  36.         0,
  37.         0,
  38.         FILE_SHARE_VALID_FLAGS, // FULL sharing
  39.         FILE_OPEN,          // MUST already exist
  40.         (FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE),   // MUST be a directory
  41.         0,
  42.         0);
  43.  
  44.     if (!NT_SUCCESS(Status)) {
  45.         DbgPrint("%u   Unable to open %S, error = 0x%x\n", DirectoryName.Length / sizeof(WCHAR), DirectoryName.Buffer, Status);
  46.         return Status;
  47.     }
  48.  
  49.     //
  50.     // We pass NO NAME which is the same as *.*
  51.     //
  52.     Status = ZwQueryDirectoryFile(DirectoryHandle,
  53.         NULL,
  54.         NULL,
  55.         // No APC routine
  56.         NULL,
  57.         // No APC context
  58.         &Iosb,
  59.         Ptr,
  60.         poolSize,
  61.         FileBothDirectoryInformation,
  62.         TRUE,
  63.         NULL,
  64.         FALSE);
  65.  
  66.     if (!NT_SUCCESS(Status)) {
  67.         DbgPrint("Unable to query directory contents, error 0x%x\n", Status);
  68.         return Status;
  69.     }
  70.  
  71.     DirInformation = (PFILE_BOTH_DIR_INFORMATION)poolSize;
  72.  
  73.     // Loop over all files
  74.     for (;;) {
  75.         //
  76.         // Dump the full name of the file.  We could dump the other information
  77.         // here as well, but we'll keep the example shorter instead.
  78.         //
  79.         DbgPrint("%u   %ws\n", DirInformation->FileNameLength / sizeof(WCHAR), &DirInformation->FileName[0]);
  80.  
  81.         //
  82.         // If there is no offset in the entry, the buffer has been exhausted.
  83.         //
  84.         if (DirInformation->NextEntryOffset == 0) {
  85.             // Re-fill buffer
  86.             Status = ZwQueryDirectoryFile(DirectoryHandle,
  87.                 NULL,
  88.                 NULL,
  89.                 // No APC routine
  90.                 NULL,
  91.                 // No APC context
  92.                 &Iosb,
  93.                 Ptr,
  94.                 poolSize,
  95.                 FileBothDirectoryInformation,
  96.                 FALSE,
  97.                 NULL,
  98.                 FALSE);
  99.  
  100.             if (!NT_SUCCESS(Status)) {
  101.                 if (Status == STATUS_NO_MORE_FILES) break;
  102.                 DbgPrint("Unable to query directory contents, error 0x%x\n", Status);
  103.                 return Status;
  104.             }
  105.  
  106.             DirInformation = (PFILE_BOTH_DIR_INFORMATION)poolSize;
  107.             continue;
  108.         }
  109.         //
  110.         // Advance to the next entry.
  111.         //
  112.         DirInformation = (PFILE_BOTH_DIR_INFORMATION)(((PUCHAR)DirInformation) + DirInformation->NextEntryOffset);
  113.  
  114.     }
  115.  
  116.     /*NtClose*/ZwClose(DirectoryHandle);
  117.     ExFreePoolWithTag(Ptr, TAG_POOLTEST);
  118.     return Status;
  119. }
  120.  
  121. VOID /*NTAPI*/ DriverUnload(IN PDRIVER_OBJECT DriverObject) {
  122.     DbgPrint("DriverUnload()!\\n");
  123.     return;
  124. }
  125.  
  126. /*__declspec (dllexport)*/ NTSTATUS /*NTAPI*/ DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath) {
  127.     NTSTATUS NtStatus = STATUS_SUCCESS;
  128.     pDriverObject->DriverUnload = /*(PDRIVER_UNLOAD)*/DriverUnload;
  129.     DbgPrint("DriverEntry()!\\n");
  130.     ListDir();
  131.     return NtStatus;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement