How to use ZwQueryInformationProcess to get ProcessImageFileName in a kernel driver?

后端 未结 3 556
逝去的感伤
逝去的感伤 2021-02-14 07:49

I\'m writing a simple kernel driver for my application (think of a very simple anti-malware application.)

I\'ve hooked ZwOpenFile() and used PsGetCurr

3条回答
  •  野的像风
    2021-02-14 08:44

    The MSDN docs for this API indicate that

    When the ProcessInformationClass parameter is ProcessImageFileName, the buffer pointed to by the ProcessInformation parameter should be large enough to hold a UNICODE_STRING structure as well as the string itself. The string stored in the Buffer member is the name of the image file.file.

    With this in mind, I suggest you try modifying your buffer structure like this:

    WCHAR strBuffer[(sizeof(UNICODE_STRING) / sizeof(WCHAR)) + 260];
    UNICODE_STRING str;
    str = (UNICODE_STRING*)&strBuffer;
    
    //initialize
    str.Buffer = &strBuffer[sizeof(UNICODE_STRING) / sizeof(WCHAR)];
    str.Length = 0x0;
    str.MaximumLength = 260 * sizeof(WCHAR);
    
    //note that the seconds arg (27) is ProcessImageFileName
    ZwQueryInformationProcess(proc, 27, &strBuffer, sizeof(strBuffer), NULL);
    

    Additionally, your code needs to check and handle the error case described in the docs here. This may be why you missed the BSOD trigger case.

    If the buffer is too small, the function fails with the STATUS_INFO_LENGTH_MISMATCH error code and the ReturnLength parameter is set to the required buffer size.

提交回复
热议问题