问题描述
#include<Ntifs.h>
#include <ntddk.h>
#include <WinDef.h>
void SampleUnload(_In_ PDRIVER_OBJECT DriverObject) {
UNREFERENCED_ParaMETER(DriverObject);
DbgPrint("Sample driver Unload called\n");
}
extern "C"
NTSTATUS
DriverEntry(_In_ PDRIVER_OBJECT DriverObject,_In_ PUNICODE_STRING RegistryPath) {
UNREFERENCED_ParaMETER(RegistryPath);
DriverObject->DriverUnload = SampleUnload;
DbgPrint("Sample driver Load called\n");
PEPROCESS EP = NULL;
if (PsLookupProcessByProcessId(::PsGetCurrentProcessId(),&EP) == STATUS_INVALID_ParaMETER) {
DbgPrint("Can't get the eprocess");
}
else {
DbgPrint("Its working");
}
LPBYTE pUpi = ((LPBYTE)EP) + 0x440;
PVOID UniqueProcessId = *((PVOID*)pUpi);
DbgPrint("Test Test Test!");
DbgPrint((CHAR*)UniqueProcessId);
return STATUS_SUCCESS;
}
大家好, 我试图打印出驱动程序的 pid 作为练习。 当我启动驱动程序时,他正在工作,但在 30 行中,他不打印任何内容,而在其他所有行中,他都在打印! 我想使用 EPROCESS 打印出进程的 pid。
有人可以帮我吗?
解决方法
但在第 30 行他不打印任何东西
你试着说
DbgPrint((CHAR*)UniqueProcessId);
不打印任何东西。
DbgPrint
接受指向格式 string 的指针以在第一个参数中打印。但 (CHAR*)UniqueProcessId
不是字符串,即使您将其转换为 (CHAR*)
。如果 UniqueProcessId
有效值 - 它很小,通常小于 0x10000
,并且对该位置的内存访问(DbgPrint
将尝试读取此“字符串”)必须导致异常/蓝屏。但是因为使用来自 EPROCESS
的硬编码偏移量 (0x440) 总是错误的 - 您读取的不是处理 UniqueProcessId 而是一些随机数据,在您的情况下意外指向有效记忆。打印的有效代码必须像
DbgPrint("UniqueProcessId=%p\n",UniqueProcessId);
还有你在这一行之前的所有代码,没有意义并且包含严重错误