看流星社区

 找回密码
 注册账号
查看: 2584|回复: 0

驱动中获取进程完整路径名

[复制链接]

该用户从未签到

发表于 2013-4-12 09:42:56 | 显示全部楼层 |阅读模式
作者:未知
文章里对于如何获取其他进程,提供了两种方法,但是都不太方便,在我的驱动里,我是在PsSetCreateProcessNotifyRoutine的回调函数里获取进程路径,能得到的进程信息是PID,于是动手改了下上面的代码,如下:

C++代码
  1. NTSTATUS     
  2.     GetProcessImagePath(   
  3.         IN  DWORD   dwProcessId,   
  4.         OUT PUNICODE_STRING ProcessImagePath   
  5.     )   
  6. {   
  7.     NTSTATUS Status;   
  8.     HANDLE hProcess;   
  9.     PEPROCESS pEprocess;   
  10.     ULONG returnedLength;   
  11.     ULONG bufferLength;   
  12.     PVOID buffer;   
  13.     PUNICODE_STRING imageName;   
  14.       
  15.     PAGED_CODE(); // this eliminates the possibility of the IDLE Thread/Process   
  16.    
  17.     if (NULL == ZwQueryInformationProcess) {   
  18.    
  19.         UNICODE_STRING routineName;   
  20.    
  21.         RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");   
  22.    
  23.         ZwQueryInformationProcess =   
  24.                (QUERY_INFO_PROCESS) MmGetSystemRoutineAddress(&routineName);   
  25.    
  26.         if (NULL == ZwQueryInformationProcess) {   
  27.             DbgPrint("Cannot resolve ZwQueryInformationProcess\n");   
  28.         }   
  29.     }   
  30.         
  31.     Status = PsLookupProcessByProcessId((HANDLE)dwProcessId, &pEprocess);   
  32.     if (!NT_SUCCESS(Status))
  33.         return Status;
  34.         
  35.     Status = ObOpenObjectByPointer(pEprocess,          // Object   
  36.                                    OBJ_KERNEL_HANDLE,  // HandleAttributes   
  37.                                    NULL,               // PassedAccessState OPTIONAL   
  38.                                    GENERIC_READ,       // DesiredAccess   
  39.                                    *PsProcessType,     // ObjectType   
  40.                                    KernelMode,         // AccessMode   
  41.                                    &hProcess);   
  42.     if (!NT_SUCCESS(Status))
  43.         return Status;  
  44.         
  45.         
  46.     //   
  47.     // Step one - get the size we need   
  48.     //   
  49.     Status = ZwQueryInformationProcess( hProcess,   
  50.                                         ProcessImageFileName,   
  51.                                         NULL, // buffer   
  52.                                         0, // buffer size   
  53.                                         &returnedLength);   
  54.         
  55.    
  56.     if (STATUS_INFO_LENGTH_MISMATCH != Status) {   
  57.    
  58.         return Status;   
  59.    
  60.     }   
  61.    
  62.     //   
  63.     // Is the passed-in buffer going to be big enough for us?     
  64.     // This function returns a single contguous buffer model...   
  65.     //   
  66.     bufferLength = returnedLength - sizeof(UNICODE_STRING);   
  67.       
  68.     if (ProcessImagePath->MaximumLength < bufferLength) {   
  69.    
  70.         ProcessImagePath->Length = (USHORT) bufferLength;   
  71.    
  72.         return STATUS_BUFFER_OVERFLOW;   
  73.            
  74.     }   
  75.    
  76.     //   
  77.     // If we get here, the buffer IS going to be big enough for us, so   
  78.     // let's allocate some storage.   
  79.     //   
  80.     buffer = ExAllocatePoolWithTag(PagedPool, returnedLength, 'ipgD');   
  81.    
  82.     if (NULL == buffer) {   
  83.    
  84.         return STATUS_INSUFFICIENT_RESOURCES;   
  85.            
  86.     }   
  87.    
  88.     //   
  89.     // Now lets go get the data   
  90.     //   
  91.     Status = ZwQueryInformationProcess( hProcess,   
  92.                                         ProcessImageFileName,   
  93.                                         buffer,   
  94.                                         returnedLength,   
  95.                                         &returnedLength);   
  96.    
  97.     if (NT_SUCCESS(Status)) {   
  98.         //   
  99.         // Ah, we got what we needed   
  100.         //   
  101.         imageName = (PUNICODE_STRING) buffer;   
  102.    
  103.         RtlCopyUnicodeString(ProcessImagePath, imageName);   
  104.            
  105.     }   
  106.         
  107.     ZwClose(hProcess);   
  108.    
  109.     //   
  110.     // free our buffer   
  111.     //   
  112.     ExFreePool(buffer);   
  113.    
  114.     //   
  115.     // And tell the caller what happened.   
  116.     //      
  117.     return Status;   
  118.       
  119. }   

  120. typedef NTSTATUS (*QUERY_INFO_PROCESS) (
  121.     __in HANDLE ProcessHandle,
  122.     __in PROCESSINFOCLASS ProcessInformationClass,
  123.     __out_bcount(ProcessInformationLength) PVOID ProcessInformation,
  124.     __in ULONG ProcessInformationLength,
  125.     __out_opt PULONG ReturnLength
  126.     );
  127. QUERY_INFO_PROCESS ZwQueryInformationProcess;
  128. NTSTATUS GetProcessImageName(PUNICODE_STRING ProcessImageName)
  129. {
  130.     NTSTATUS status;
  131.     ULONG returnedLength;
  132.     ULONG bufferLength;
  133.     PVOID buffer;
  134.     PUNICODE_STRING imageName;
  135.    
  136.     PAGED_CODE(); // this eliminates the possibility of the IDLE Thread/Process
  137.     if (NULL == ZwQueryInformationProcess) {
  138.         UNICODE_STRING routineName;
  139.         RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");
  140.         ZwQueryInformationProcess =
  141.                (QUERY_INFO_PROCESS) MmGetSystemRoutineAddress(&routineName);
  142.         if (NULL == ZwQueryInformationProcess) {
  143.             DbgPrint("Cannot resolve ZwQueryInformationProcess\n");
  144.         }
  145.     }
  146.     //
  147.     // Step one - get the size we need
  148.     //
  149.     status = ZwQueryInformationProcess( NtCurrentProcess(),
  150.                                         ProcessImageFileName,
  151.                                         NULL, // buffer
  152.                                         0, // buffer size
  153.                                         &returnedLength);
  154.     if (STATUS_INFO_LENGTH_MISMATCH != status) {
  155.         return status;
  156.     }
  157.     //
  158.     // Is the passed-in buffer going to be big enough for us?  
  159.     // This function returns a single contguous buffer model...
  160.     //
  161.     bufferLength = returnedLength - sizeof(UNICODE_STRING);
  162.    
  163.     if (ProcessImageName->MaximumLength < bufferLength) {
  164.         ProcessImageName->Length = (USHORT) bufferLength;
  165.         return STATUS_BUFFER_OVERFLOW;
  166.         
  167.     }
  168.     //
  169.     // If we get here, the buffer IS going to be big enough for us, so
  170.     // let's allocate some storage.
  171.     //
  172.     buffer = ExAllocatePoolWithTag(PagedPool, returnedLength, 'ipgD');
  173.     if (NULL == buffer) {
  174.         return STATUS_INSUFFICIENT_RESOURCES;
  175.         
  176.     }
  177.     //
  178.     // Now lets go get the data
  179.     //
  180.     status = ZwQueryInformationProcess( NtCurrentProcess(),
  181.                                         ProcessImageFileName,
  182.                                         buffer,
  183.                                         returnedLength,
  184.                                         &returnedLength);
  185.     if (NT_SUCCESS(status)) {
  186.         //
  187.         // Ah, we got what we needed
  188.         //
  189.         imageName = (PUNICODE_STRING) buffer;
  190.         RtlCopyUnicodeString(ProcessImageName, imageName);
  191.         
  192.     }
  193.     //
  194.     // free our buffer
  195.     //
  196.     ExFreePool(buffer);
  197.     //
  198.     // And tell the caller what happened.
  199.     //   
  200.     return status;
  201.    
  202. }
复制代码
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-29 17:32

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表