簡體   English   中英

使用 PsLookupProcessByProcessId 獲取進程 PID

[英]Get Process PID With PsLookupProcessByProcessId

#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接受指向格式字符串的指針以在第一個參數中打印。 但是(CHAR*)UniqueProcessId不是字符串,即使您將其轉換為(CHAR*)也是如此。 如果UniqueProcessId有效值 - 它很小,通常小於0x10000 ,並且 memory 訪問此位置( DbgPrint將嘗試讀取此“字符串”)必須導致異常/bsod。 但是因為使用EPROCESS中的硬編碼偏移量( 0x440 )總是錯誤的 - 您讀取的不是處理UniqueProcessId而是一些隨機數據,在您的情況下,這些數據意外指向有效的 memory。 打印的有效代碼必須像

DbgPrint("UniqueProcessId=%p\n",UniqueProcessId);

此行之前的所有代碼也沒有意義並且包含嚴重錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM