龙马谷

 找回密码
 立即注册

QQ登录

只需一步,快速开始

龙马谷VIP会员办理客服QQ:82926983(如果临时会话没有收到回复,请先加QQ好友再发。)
1 [已完结] GG修改器新手入门与实战教程 31课 2 [已完结] GG修改器美化修改教程 6课 3 [已完结] GG修改器Lua脚本新手入门教程 12课
4 [已完结] 触动精灵脚本新手入门必学教程 22课 5 [已完结] 手游自动化脚本入门实战教程 9课 6 [已完结] C++射击游戏方框骨骼透视与自瞄教程 27课
7 [已完结] C++零基础UE4逆向开发FPS透视自瞄教程 29课 8 [已完结] C++零基础大漠模拟器手游自动化辅助教程 22课
以下是天马阁VIP教程,本站与天马阁合作,赞助VIP可以获得天马阁对应VIP会员,名额有限! 点击进入天马阁论坛
1 [已完结] x64CE与x64dbg入门基础教程 7课 2 [已完结] x64汇编语言基础教程 16课 3 [已完结] x64辅助入门基础教程 9课
4 [已完结] C++x64内存辅助实战技术教程 149课 5 [已完结] C++x64内存检测与过检测技术教程 10课 6 [已完结] C+x64二叉树分析遍历与LUA自动登陆教程 19课
7 [已完结] C++BT功能原理与x64实战教程 29课 8 [已完结] C+FPS框透视与自瞄x64实现原理及防护思路
查看: 1696|回复: 0

获取进程完整路径名附源码

[复制链接]

13

主题

8

回帖

25

积分

编程入门

Rank: 1

龙马币
44

文章里对于如何获取其他进程,提供了两种方法,但是都不太方便,
在我的驱动里,我是在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. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

龙马谷| C/C++辅助教程| 安卓逆向安全| 论坛导航| 免责申明|Archiver|
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表龙马谷立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2018-2021 All Right Reserved.
在线客服
快速回复 返回顶部 返回列表