#include <Windows.h>
|
#include <winternl.h>
|
#include <cstdio>
|
|
#define FileBothDirectoryInformation ((FILE_INFORMATION_CLASS)3)
|
|
extern "C"
|
NTSTATUS WINAPI NtQueryDirectoryFile(
|
_In_ HANDLE FileHandle,
|
_In_opt_ HANDLE Event,
|
_In_opt_ PIO_APC_ROUTINE ApcRoutine,
|
_In_opt_ PVOID ApcContext,
|
_Out_ PIO_STATUS_BLOCK IoStatusBlock,
|
_Out_ PVOID FileInformation,
|
_In_ ULONG Length,
|
_In_ FILE_INFORMATION_CLASS FileInformationClass,
|
_In_ BOOLEAN ReturnSingleEntry,
|
_In_opt_ PUNICODE_STRING FileName,
|
_In_ BOOLEAN RestartScan
|
);
|
|
VOID PrintHex(PBYTE Data, ULONG dwBytes) {
|
for (ULONG i = 0; i < dwBytes; i += 16) {
|
printf("%.8x: ", i);
|
|
for (ULONG j = 0; j < 16; j++) {
|
if (i + j < dwBytes) {
|
printf("%.2x ", Data[i + j]);
|
}
|
else {
|
printf("?? ");
|
}
|
}
|
|
for (ULONG j = 0; j < 16; j++) {
|
if (i + j < dwBytes && Data[i + j] >= 0x20 && Data[i + j] <= 0x7e) {
|
printf("%c", Data[i + j]);
|
}
|
else {
|
printf(".");
|
}
|
}
|
|
printf("\n");
|
}
|
}
|
|
int main() {
|
// Open the disk device.
|
HANDLE hDir = CreateFile(L"C:\\Windows", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
if (hDir == INVALID_HANDLE_VALUE) {
|
printf("CreateFile failed, %d\n", GetLastError());
|
return 1;
|
}
|
|
// Obtain the output data, assuming that it will fit into 1024 bytes.
|
IO_STATUS_BLOCK iosb;
|
UNICODE_STRING FileName;
|
RtlInitUnicodeString(&FileName, L"explorer.exe");
|
|
BYTE OutputBuffer[1024];
|
RtlZeroMemory(OutputBuffer, sizeof(OutputBuffer));
|
|
NTSTATUS st = NtQueryDirectoryFile(hDir, NULL, NULL, NULL, &iosb, OutputBuffer, sizeof(OutputBuffer), FileBothDirectoryInformation, TRUE, &FileName, FALSE);
|
if (NT_SUCCESS(st)) {
|
PrintHex(OutputBuffer, iosb.Information);
|
} else {
|
printf("NtQueryDirectoryFile failed, %x\n", st);
|
}
|
|
CloseHandle(hDir);
|
|
return 0;
|
}
|