内核驱动程序中获取当前用户进程的进程名的方法

   

    驱动程序的加载函数DriverEntry是运行在System进程中的.通过PsGetCurrentProcess可以获取System进程的内核EPROCESS结构的地址,然后从该地址开始寻找"System"字符串.找到后,便是EPROCESS的进程名存放的偏移处.得到进程名在EPROCESS结构的偏移后,以后的进程调用驱动的时候,就可以直接在该偏移处获取当前进程名.代码如下:

Code
BOOL GetProcessName( PCHAR theName )
{
    PEPROCESS       curproc;
    
char            *nameptr;
    ULONG           i;
    KIRQL           oldirql;

    
if( gProcessNameOffset )
    
{
        curproc 
= PsGetCurrentProcess();
        nameptr   
= (PCHAR) curproc + ProcNameOffset;
        strncpy( theName, nameptr, NT_PROCNAMELEN );
        theName[NT_PROCNAMELEN] 
= 0/* NULL at end */
        
return TRUE;
    }

    
return FALSE;
}


regmon中使用的就是这种方法。

  

原文地址:https://www.cnblogs.com/dflower/p/1425298.html