I use ZwQueryInformationProcess to get the image file name of the process. Which I can succesfully. But after some more lines of code, the string is overwritten randomly, specially when I use it to initialize an object.
Status = ZwQueryInformationProcess(ProcessHandle, ProcessImageFileName, NULL, 0, &ReturnLength);
if (Status == STATUS_INFO_LENGTH_MISMATCH) {
ImageFileNameBuffer = ExAllocatePoolZero(NonPagedPool, ReturnLength, 0);
if (ImageFileNameBuffer == NULL) return PkDispatchDenyAccess(Irp);
ImageFileName = *(PUNICODE_STRING)ImageFileNameBuffer;
Status = ZwQueryInformationProcess(ProcessHandle, ProcessImageFileName, &ImageFileName, ReturnLength, NULL); // The buffer is right, the string is right.
// "\Device\Mup\vmware-host\Shared Folders\Debug\Process.exe"
if (!NT_SUCCESS(Status)) {
ZwClose(ProcessHandle);
return PkDispatchDenyAccess(Irp);
}
} else {
ZwClose(ProcessHandle);
return PkDispatchDenyAccess(Irp);
}
ZwClose(ProcessHandle);
InitializeObjectAttributes(&FileAttributes, &ImageFileName, OBJ_KERNEL_HANDLE, NULL, NULL);
// Now things change."\Device\Mup\vmware-host\Shared F0". Just randomly. Or something else. It differs.
Status = ZwOpenFile(&FileHandle, 0, &FileAttributes, &IoBlock, 0, 0); // Cannot open because object name not found
if (!NT_SUCCESS(Status)) return PkDispatchDenyAccess(Irp);
I am not exactly sure what is causing that. But the Length nor MaximumLength of the UNICODE_STRING does not change. The buffer in memory is overwritten. And yes, I tried to manually initialize the object. The result was same.
UPDATE: The object does not matter. It is overwritten after a few lines. I tried to copy the string.
UPDATE 2: I tried to move the initialization and copy the string right after its written by ZwQueryInformationProcess. I got a SYSTEM_SERVICE_EXCEPTION with access violation at RtlCopyMemory.