Generally when you are kernel debugging a machine, you may be in an arbitrary process’s context. It is difficult to using the stepping commands and wait till the process of your interest starts. If you want to attach to a process or activate the debugger only when the process starts, there’s an easier way. Watch for PspInsertProcess, and set a breakpoint on this function.
This function will get called after initialization of EPROCESS datastructure. This is basically inserting the process to the Global Kernel Process list. The first parameter will give you the EPROCESS pointer and the second last parameter will give you the Cmdline for the process launch.
Now, the debugger will get activated for every new process that I launched on the debuggee. To set your break point on a specific process name, you can create a small script with the following lines:
Create a file called Debugger.txt which contains:
Now, Set your breakpoint such that this script gets exected at each breakpoint. Use this syntax:
bp nt!NtCreateUserProcess "$$<C:\\Temp\\Debugger.txt"
Essentially - we’ve set a conditional breakpoint which runs the lines in the Debugger.txt script. This script checks the first parameter of the PspInsertProcess function (which is in RCX) and then gets the 448th offset. The first parameter of this function is an EPROCESS object and the 448th offset of an EPROCESS contain the image name of the process. We do a pattern match to see if this image name matches our desired process name (in this case miexec.exe). If it matches, then the debugger breaks, else it continues execution.
Once the debugger breaks, you can get to the first thread by using this:
And that’s the first thread in the process.