Thread: Create suspended process

  1. #1
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006

    Create suspended process


    I need to prevent the call to a WinAPI function in the target process when a certain value is passed as argument. The WinAPI function will be called by the target process in its initialisation state. The idea is to set a diversion route on the WinAPI function before it will be called when the target process initializes. Therefore I create the target process in suspended mode. My problem is that the target process will be suspended before it has loaded its load-time dynamic link libraries which makes it impossible to set the diversion route on the WinAPI function.

    My approach:

    Start target process in suspended mode.
    Allocate memory in target process.
    Write code cave in allocated memory region of target process.
    Redirect WinAPI function in target process to the code cave.
    Resume main thread of target process.
    Restore WinAPI function in target process after initialization.
    Free allocated memory (code cave) in target process.

    The only workaround solution I have found is to call the CreateRemoteThread function before I resume the main thread of the target process. Parsing the address of the GetCurrentProcess function as the lpStartAddress parameter to reduce the CPU usage as much as possible. All the load-time dynamic link libraries will be loaded before the remote thread terminates. Now it becomes possible to set the diversion route. Unfortunately this remains a nasty workaround which I rather avoid.

    The following quote on the CreateRemoteThread documentation page of MSDN surprises me: "The function must exist in the remote process". In this particular case described above it seems to have the opposite effect. Maybe someone can enlighten me on the internal workings of this specific Windows component because it seems I do not fully understand it.

    Thanks for your time,
    Last edited by Ktulu; 04-11-2011 at 05:32 PM.
    This parameter is reserved

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    You might want to look into Detours, which is a Microsoft package meant for exactly this kind of runtime hooking. If for some reason you can't do that, you can create the process suspended, attach to it as if you are a debugger (you can do that by setting DEBUG_PROCESS in dwCreationFlags), set a hardware breakpoint at the executable entry point, then resume the process. It will break once it reaches the entry point, at which time the DLLs are resolved and you can inject your hooks. Twiddle the hardware breakpoint back, and resume execution.

    Another method is to rewrite the executable's import table to import a custom DLL instead of the regular one -- the custom DLL just contains forwarders to the actual DLL except for the one function you are trying to hook.

    But seriously, there are frameworks for doing this and you really should try to use one of those if possible.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create and listen to a process
    By frs in forum Linux Programming
    Replies: 8
    Last Post: 07-16-2010, 11:00 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Create a process that uses cpu % without do anything
    By BianConiglio in forum Windows Programming
    Replies: 8
    Last Post: 05-22-2004, 12:30 PM
  4. How to tell when shell create a process?
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2004, 07:04 AM
  5. how to create a process in c++?
    By cc246 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2003, 07:24 AM