Get the complete process path name from the driver

Source: Internet
Author: User

Original address: http://www.osronline.com/article.cfm? Id = 472

 

 

 

 

 

Over the years developers have needed or wanted to know the name of the image executing in a given process. Traditionally, this was often done using
Psgetprocessimagefile name, Which returns the contents of a field in
EprocessStructure Used by the Windows OS to maintain per-process state information.

As we can see from the information in the local debugger session (see
Figure 1) The process image file is little more than a field within the eprocess structure. notice that the eprocess address is in EBP + 8, making it the first-and only-parameter to this function.

Unfortunately, there are some issues with this approach:

  • Though well-known, this function is uninitialized ented.
  • More seriously, the information contained in this field is severely limited. It contains only the first 16 (ASCII) characters of the image file name.

 

It is actually the second issue that often creates problems for programmers because the name of the image file means essential nothing. for example, we 've seen kernel-mode drivers in the past that validate the name of their service by checking
This field. The most egregious case we 've seen is when the service was called svchost.exe, which is a common name that is often spoofed.

The proposal
We suggest a different model for acquiring this information, as shown in
Figure 2.

 

The function itself is fairly straight-forward. It does rely on use of a single unencrypted ented function (Zwqueryinformationprocess), But note that its counterpart (Ntqueryinformationprocess) Is already ented.
We need to use the ZW variant in order to use a kernel memory buffer.

The key element is that Windows has always stored the full path name to the executable image in order to provide this information in
AuditingSubsystem. This API exploits that existing stored path name.

OtherProcess names
This function has been implemented to extract the process name forCurrent
Process. However, you can use one of the two methods listed below to obtain the process name for a different process:

1. If you have a handle for the process, you can use that value instead of
Ntcurrentprocess ()Macro. (Note that in our experience, we usually have a process
ObjectAnd not a processHandle-The two areNotInterchangeable ).

2. If you have an eprocess address, you can useKestackattachprocess/keunstackdetachprocessTo attach to the process. This technique is rather heavy-weight, so it may be a good idea to cache the information if you need
Perform this operation regularly.

When using the second technique, it is important to note that the name that is returned is a cached name. This cache is
NotUpdated if the name of the original file changes after the name is first cached. in other words, if the executable image is renamed, which is typically allowed for a running executable, the name returned will be the name of the original file.

This issue is not unique. we have also observed that some file systems return the original name even after a Rename (e.g ., the CIFS client implementation does this on Windows XP ). thus, it may require additional processing such as through
A file system filter driver to protect against similar events. For example, you may encounter a security product that relies on knowing the specific name of the image.Alternatives?
There are other options that a driver cocould also pursue such as registering for Process Creation/teardown events (Pssetcreateprocesspolicyroutine) Or image loading (Pssetloadimagenotifyroutine).

 

PssetcreateprocesspolicyroutineHas limitations on the number of drivers that can register using this API. since there is a fixed size table in the Windows OS, it is possible for this call to fail. when this occurs, a driver
Needs to ensure it can handle such a failure. pssetloadimagenotifyroutine has the same limitation (fixed size table), but is called for all image loads, not just the original process image. therefore, it should des drivers, DLLs, executables, etc.

Summary
The bottom line is that all of these approaches provide a useful name because they include the full path name. This is vastly superior to using the short ASCII eye-catching name that is stored in
EprocessStructure. A word of caution-if you decide to use the debug level name, use it for nothing more than debugging since it is not reliable and cannot be relied on for any sort of security check.

We chose to use the proposed technique because it works in all circumstances and does not rely upon a registration that might potentially fail. in your own driver you might implement both this mechanisms and a cache-based mechanisms tied
The Process Creation logic.

 

 

The article provides two methods for obtaining other processes, but they are not very convenient. In my driver, I get the process path in the callback function of pssetcreateprocesspolicyroutine, the obtained process information is PID, so I changed the above Code as follows:

C ++ code
  1. Ntstatus
  2. Getprocessimagepath (
  3. In DWORD dwprocessid,
  4. Out punicode_string processimagepath
  5. )
  6. {
  7. Ntstatus status;
  8. Handle hprocess;
  9. Peprocess;
  10. Ulong returnedlength;
  11. Ulong bufferlength;
  12. Pvoid buffer;
  13. Punicode_string imagename;
  14. Paged_code (); // This eliminates the possibility of the idle thread/process
  15. If (null = zwqueryinformationprocess ){
  16. Unicode_string routinename;
  17. Rtlinitunicodestring (& routinename, l "zwqueryinformationprocess ");
  18. Zwqueryinformationprocess =
  19. (Query_info_process) mmgetsystemroutineaddress (& routinename );
  20. If (null = zwqueryinformationprocess ){
  21. Dbuplint ("cannot resolve zwqueryinformationprocess/N ");
  22. }
  23. }
  24. Status = pslookupprocessbyprocessid (handle) dwprocessid, & peprocess );
  25. If (! Nt_success (Status ))
    Return status;
  26. Status = obopenobjectbypointer (peprocess, // object
  27. Obj_kernel_handle, // handleattributes
  28. Null, // passedaccessstate optional
  29. Generic_read, // desiredaccess
  30. * Psprocesstype, // objecttype
  31. Kernelmode, // accessmode
  32. & Hprocess );
  33. If (! Nt_success (Status ))
    Return status;
  34. //
  35. // Step one-get the size we need
  36. //
  37. Status = zwqueryinformationprocess (hprocess,
  38. Processimagefilename,
  39. Null, // Buffer
  40. 0, // buffer size
  41. & Returnedlength );
  42. If (status_info_length_mismatch! = Status ){
  43. Return status;
  44. }
  45. //
  46. // Is the passed-in buffer going to be big enough for us?
  47. // This function returns a single contguous buffer model...
  48. //
  49. Bufferlength = returnedlength-sizeof (unicode_string );
  50. If (processimagepath-> maximumlength <bufferlength ){
  51. Processimagepath-> length = (ushort) bufferlength;
  52. Return status_buffer_overflow;
  53. }
  54. //
  55. // If we get here, the buffer is going to be big enough for us, so
  56. // Let's allocate some storage.
  57. //
  58. Buffer = exallocatepoolwithtag (pagedpool, returnedlength, 'ipgd ');
  59. If (null = buffer ){
  60. Return status_insufficient_resources;
  61. }
  62. //
  63. // Now lets go get the data
  64. //
  65. Status = zwqueryinformationprocess (hprocess,
  66. Processimagefilename,
  67. Buffer,
  68. Returnedlength,
  69. & Returnedlength );
  70. If (nt_success (Status )){
  71. //
  72. // Ah, we got what we needed
  73. //
  74. Imagename = (punicode_string) buffer;
  75. Rtlcopyunicodestring (processimagepath, imagename );
  76. }
  77. Zwclose (hprocess );
  78. //
  79. // Free our Buffer
  80. //
  81. Exfreepool (buffer );
  82. //
  83. // And tell the caller what happened.
  84. //
  85. Return status;
  86. }

Typedef ntstatus (* query_info_process )(
_ In handle processhandle,
_ In processinfoclass processinformationclass,
_ Out_bcount (processinformationlength) pvoid processinformation,
_ In ulong processinformationlength,
_ Out_opt Pulong returnlength
);

Query_info_process zwqueryinformationprocess;

Ntstatus getprocessimagename (punicode_string processimagename)
{
Ntstatus status;
Ulong returnedlength;
Ulong bufferlength;
Pvoid buffer;
Punicode_string imagename;

Paged_code (); // This eliminates the possibility of the idle thread/process

If (null = zwqueryinformationprocess ){

Unicode_string routinename;

Rtlinitunicodestring (& routinename, l "zwqueryinformationprocess ");

Zwqueryinformationprocess =
(Query_info_process) mmgetsystemroutineaddress (& routinename );

If (null = zwqueryinformationprocess ){
Dbuplint ("cannot resolve zwqueryinformationprocess/N ");
}
}
//
// Step one-get the size we need
//
Status = zwqueryinformationprocess (ntcurrentprocess (),
Processimagefilename,
Null, // Buffer
0, // buffer size
& Returnedlength );

If (status_info_length_mismatch! = Status ){

Return status;

}

//
// Is the passed-in buffer going to be big enough for us?
// This function returns a single contguous buffer model...
//
Bufferlength = returnedlength-sizeof (unicode_string );

If (processimagename-> maximumlength <bufferlength ){

Processimagename-> length = (ushort) bufferlength;

Return status_buffer_overflow;

}

//
// If we get here, the buffer is going to be big enough for us, so
// Let's allocate some storage.
//
Buffer = exallocatepoolwithtag (pagedpool, returnedlength, 'ipgd ');

If (null = buffer ){

Return status_insufficient_resources;

}

//
// Now lets go get the data
//
Status = zwqueryinformationprocess (ntcurrentprocess (),
Processimagefilename,
Buffer,
Returnedlength,
& Returnedlength );

If (nt_success (Status )){
//
// Ah, we got what we needed
//
Imagename = (punicode_string) buffer;

Rtlcopyunicodestring (processimagename, imagename );

}

//
// Free our Buffer
//
Exfreepool (buffer );

//
// And tell the caller what happened.
//
Return status;

}

Figure 2-a new proposal

Connected to Windows XP 2600x86 compatible target, ptr64 false
Symbol search path is: SRV * C:/symbols/websymbols * http://msdl.microsoft.com/download/symbols
Executable search path is:
**************************************** ***************************************
Warning: Local kernel debugging requires booting with/debug to work optimally.
**************************************** ***************************************
Windows XP kernel version 2600 (Service Pack 2) MP (2 procs) Free x86 compatible
Product: WINNT, Suite: terminalserver singleuserts
Built by: 2600. xpsp_sp2_gdr.050301-1519
Kernel base = 0x804d7000 psloadedmodulelist = 0x805624a0
Debug session time: Mon Aug 7 13:29:53. 486 2006 (GMT-4)
System uptime: 2 days 11:08:38. 140
Lkd>. Reload
Connected to Windows XP 2600x86 compatible target, ptr64 false
Loading kernel symbols
........................................ ......................................
Loading user symbols
........................................ ......................................
Loading unloaded module list
....................... * ** Error: Symbol file cocould not be found. defaulted to export symbols for C
Lkd> u nt! Psgetprocessimagefilename
NT! Psgetprocessimagefilename:
8050a14a 8bff mov EDI, EDI
8050a14c 55 push EBP
8050a14d 8bec mov EBP, ESP
8050a14f 8bda-8 mov eax, dword ptr [EBP + 8]
8050a152 0574010000 add eax, 174 h
8050a157 5d pop EBP
8050a158 c20400 RET 4
8050a15b 8bce mov ECx, ESI

Figure 1-local debug session of psgetprocessimagefilename

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.