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
- Ntstatus
- Getprocessimagepath (
- In DWORD dwprocessid,
- Out punicode_string processimagepath
- )
- {
- Ntstatus status;
- Handle hprocess;
- Peprocess;
- 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 ");
- }
- }
- Status = pslookupprocessbyprocessid (handle) dwprocessid, & peprocess );
- If (! Nt_success (Status ))
Return status;
- Status = obopenobjectbypointer (peprocess, // object
- Obj_kernel_handle, // handleattributes
- Null, // passedaccessstate optional
- Generic_read, // desiredaccess
- * Psprocesstype, // objecttype
- Kernelmode, // accessmode
- & Hprocess );
- If (! Nt_success (Status ))
Return status;
- //
- // Step one-get the size we need
- //
- Status = zwqueryinformationprocess (hprocess,
- 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 (processimagepath-> maximumlength <bufferlength ){
- Processimagepath-> 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 (hprocess,
- Processimagefilename,
- Buffer,
- Returnedlength,
- & Returnedlength );
- If (nt_success (Status )){
- //
- // Ah, we got what we needed
- //
- Imagename = (punicode_string) buffer;
- Rtlcopyunicodestring (processimagepath, imagename );
- }
- Zwclose (hprocess );
- //
- // Free our Buffer
- //
- Exfreepool (buffer );
- //
- // And tell the caller what happened.
- //
- Return status;
- }
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