The phenomenon goes like this.
I'm trying to implement dll injection. My application create process in 'suspended' state, (Using CreateProcess with CREATE_SUSPENDED), wait for an important dll to be loaded(kernel32.dll of course), and then executes injection. The suspended process is resumed using ResumeThread after the injection.
I'm using suspended process creation in the hope that dll is injected prior to any other execution except dll load. But while executing this, I found something interesting.
I've simply tested this injection using notepad.exe and it worked just fine. But when I've duplicated the executable as notepad2.exe, my injection waits for infinite time. Kernel32.dll never loads up until the main thread of the process is resumed.
It seems there's some sort of 'authenticated' executable that modules are loaded beforehand (or maybe some security solutions installed in my computer that already uses dll injection might cause this different execution).
Does Anyone knows if there's any sort of thing that modules are loaded in different manner? (Frankly, I still don't understand the life cycle of windows process...)
ntdll!LdrpInitializeProcess
, which initializes the execution environment (e.g. language support, the heap, thread-local storage, theKnownDlls
directory), loads kernel32.dll and gets the address ofBaseThreadInitThunk
, does static DLL imports, breaks for an attached debugger, and runs the init routines. Then execution jumps tontdll!RtlUserThreadStart
, which callskernel32!BaseThreadInitThunk
, which calls the EXE's entry point such asWinMainCRTStartup
. – Eryk Sunntdll!LdrInitializeThunk
has been fairly consistent over the years. I always see the initialization steps and DLL load order in the following debug session:cdb -xe cpr -c "bp ntdll!LdrInitializeThunk; g" notepad
. That said, it could be thatNtCreateUserProcess
is sometimes pre-mapping DLLs other than ntdll.dll. – Eryk Sun