Kayaker
February 21st, 2004, 00:22
Hi All,
I wasn't particularly planning on releasing this, but it is a useful enough utility and example of driver programming. It demonstrates all the information you can retrieve using ZwQuerySystemInformation with the Information Classes of SystemBasicInformation, SystemProcessorInformation, SystemProcessesAndThreadsInformation and SystemModuleInformation.
In terms of more advanced driver programming, it gives an example of mapping kernel memory to user mode using MDL's, as described in the included OSR file 'Sharing Memory Between Drivers and Applications'.
The main reason I'm presenting it is in response to a question about detecting "invisible" processes, or possibly detecting if your code has been "injected". There is a bit of information that ZwQuerySystemInformation discloses that might be of some use. In particular, the SystemProcessesAndThreadsInformation class retrieves a few interesting bits of info for each running process, the ProcessId, and more importantly the InheritedFromProcessId, as well as the complete SYSTEM_THREADS listing.
Why is this important? A little playing with this utility will show that most User processes are started from the everpresent Windows Explorer process, and an applications' InheritedFromProcessId value reflects this. In other words, the ProcessId of Explorer = the InheritedFromProcessId of normally started applications. This appears to be true of applications started from the Desktop, StartMenu or Taskbar.
Now, can the InheritedFromProcessId tell the protectionist anything about the 'hijacking' loader? In terms of an attached Win32 debugger (i.e. OllyDbg), the answer is Yes, InheritedFromProcessId directly indicates the PID of the debugger. In terms of an injected dll, not necessarily, other than it exists, because in many cases of dll injection the "loader" has exited and only the injected dll remains, so the InheritedFromProcessId may be invalid or arbitrary.
So how to tell if your process is running an injected dll? By the number of threads running - if your program has only 1 main thread, but SystemProcessesAndThreadsInformation indicates 2, then you've got a stowaway and the CLIENT_ID structure will give you the ThreadID of it.
What you do then is your choice
I haven't explored all the possibilities of this in terms of protection or otherwise, nor tested with every known injection technique etc., this is just observations from a little playing with my own information gathering utility for personal use. Perhaps this will be of some use or interest to others, full source, tested on Win2K.
Regards,
Kayaker
I wasn't particularly planning on releasing this, but it is a useful enough utility and example of driver programming. It demonstrates all the information you can retrieve using ZwQuerySystemInformation with the Information Classes of SystemBasicInformation, SystemProcessorInformation, SystemProcessesAndThreadsInformation and SystemModuleInformation.
In terms of more advanced driver programming, it gives an example of mapping kernel memory to user mode using MDL's, as described in the included OSR file 'Sharing Memory Between Drivers and Applications'.
The main reason I'm presenting it is in response to a question about detecting "invisible" processes, or possibly detecting if your code has been "injected". There is a bit of information that ZwQuerySystemInformation discloses that might be of some use. In particular, the SystemProcessesAndThreadsInformation class retrieves a few interesting bits of info for each running process, the ProcessId, and more importantly the InheritedFromProcessId, as well as the complete SYSTEM_THREADS listing.
Why is this important? A little playing with this utility will show that most User processes are started from the everpresent Windows Explorer process, and an applications' InheritedFromProcessId value reflects this. In other words, the ProcessId of Explorer = the InheritedFromProcessId of normally started applications. This appears to be true of applications started from the Desktop, StartMenu or Taskbar.
Now, can the InheritedFromProcessId tell the protectionist anything about the 'hijacking' loader? In terms of an attached Win32 debugger (i.e. OllyDbg), the answer is Yes, InheritedFromProcessId directly indicates the PID of the debugger. In terms of an injected dll, not necessarily, other than it exists, because in many cases of dll injection the "loader" has exited and only the injected dll remains, so the InheritedFromProcessId may be invalid or arbitrary.
So how to tell if your process is running an injected dll? By the number of threads running - if your program has only 1 main thread, but SystemProcessesAndThreadsInformation indicates 2, then you've got a stowaway and the CLIENT_ID structure will give you the ThreadID of it.
What you do then is your choice

I haven't explored all the possibilities of this in terms of protection or otherwise, nor tested with every known injection technique etc., this is just observations from a little playing with my own information gathering utility for personal use. Perhaps this will be of some use or interest to others, full source, tested on Win2K.
Regards,
Kayaker