Injecting a DLL into Another Process's Address Space
Posted
by Zoltan Csizmadia
on September 21st, 2000
To use the CreateRemoteThread() you have to follow these steps:
- Allocate a page of memory in target for the code, via VirtualAllocEx()
- Allocate a page of memory in target for the parameters, via VirtualAllocEx()
- Write the name of the DLL (and other parameters) into the target memory (#2), via WriteProcessMemory()
- Write the code into the target memory (#1), via WriteProcessMemory()
- Call CreateRemoteThread(), passing it the address of the function (#2) and the allocated parameter memory (#2)
- Wait for finishing the remote thread
- Read back the return values from the target memory
- Free the memories with VirtualFreeEx() (#1, #2)
Before you want to allocate memory in the target address space you have to have and enable the SeDebugPrivilege.
The attached example:
Usage: LOADDLL [/L] [/U] processID dllPath [functionName]
/L Loads the module
/U Unloads the module
processID Process ID
dllPath Path for the module
functionName Called function. Mustn't have parameters
Examples:
Loads and then unloads the module for process #728 LOADDLL /L /U 728 your.dll Loads, calls the fnTest and unloads the module for process #728 LOADDLL /L /U 728 your.dll fnTest Call the fnTest function. The module has to be loaded to the process LOADDLL 728 your.dll fnTest Unload the "your.dll" from process #728 LOADDLL /U 728 your.dll Breaks the remote process LOADDLL 728 kernel32.dll DebugBreak

Comments
Working in VistaX64?
Posted by GalaticDan on 09/30/2009 06:27amI had to change the flags on OpenProcess(...) to PROCESS_ALL_ACCESS otherwise CreateRemoteThread fails with 5 (access denied). Not sure which extra flags are required so all sounds ok?
ReplyCan this method be used to copy a Bitmap in another process?
Posted by Legacy on 11/17/2003 12:00amOriginally posted by: Jim White
I have a handle to a bitmap in another process. If it was in my process I could get a copy via GetObject(hBitmap), but this doesn't work across processes (I thought GDI objects WERE global, but clearly I'm wrong).
Can this injection technqiue solve my problem?
Any advice/comments would be gratefully received!!
ReplyWindows XP
Posted by Legacy on 08/28/2003 12:00amOriginally posted by: Mangos
I need this example to run on Windows XP. What is needed to change?
ReplyExcellent sample
Posted by Legacy on 05/19/2003 12:00amOriginally posted by: Real Programmer
Thanks for an excellent foundation for this sort of thing! This saved me lots of time.
I implemented another reader's suggestion to call GetLastError instead of "? 0 : 1", but let's face it, you usually know what the error is ;)
The code did not work in debug mode in my project, as taking the address of a function yieled the address of a jmp table entry instead of the function itself. I'm not sure what option this is, but it should probably be mentioned alongside /GZ.
(I took a much more brute force approach... if RemoteThread() is called with NULL, it returns the current eip. By comparing this result with the address of the function, I can tell if I have to do function import fixup or not.)
I also rigged mine to work by exe name instead of process id, but that belongs in a psapi sample :)
Thanks again
Reply
Retrive a control's object From other application
Posted by Legacy on 03/10/2002 12:00amOriginally posted by: Shanu
I want to retrive a control's object( to retrive it's properties) which is lying in other application . I have hooked this application' process by a dll .
I am getting all the properties of the control if the application(In which control resides) is writeen in VC++,
But on VB based application I am not getting the poperties.
Why is it so?
Is there any way possible.
Regards,
ReplyShailesh
Fine but How To acess objects in a process
Posted by Legacy on 03/08/2002 12:00amOriginally posted by: James
How To acess objects in a process
I want to retrive a control's object to retrive it's properties, which is lying in other application .What I managed to do is hooking up this application' process by a dll .Now I could send messages to this contol ,but How to get the acess the contol's object to retrive it's properties directly.
Regards
ReplyJames
Injecting code into system processes
Posted by Legacy on 12/04/2001 12:00amOriginally posted by: Shawn Van Ness
ReplyInjecting a DLL In Windows9x
Posted by Legacy on 10/21/2001 12:00amOriginally posted by: HellSpawn
Does this source code only work for systems running Windows NT/2000? If so would you have any other code snippets that I could use in order to inject a DLL into a process running on a Windows 9x box? Thanks
Replyooops!!!i have to do some thing more
Posted by Legacy on 05/28/2001 12:00amOriginally posted by: kuchnaheen
Good Work!!!!
in normal thread creation we never allocate memory in process for thered, why do we need that for remote creation....(i have also done the same thing....not as cleanly as u did but have it done....now i am improving my code...thanx to u)to me the reason is the requirement of createremotethread()(at least i have done coz of the function requirements)....but what if this created thread outs another thread....i mean if injected dll creates another thread by normal createthread() function.....in which process memory would the new thread will execute....the process that injects the dll or the target process(to me the target process)....if target process than what are the possibilities of crash as no memeory is allocated for the new thread....now i am trying to do that....till now with no problems.....my thread is working nicely....but it will be cool...if some one like u can assure me the thing
thank u very much for the code....n more thanx in advance if u can guide me a little further on this thread issue
cheers
kuchnaheen
ReplySense of that?
Posted by Legacy on 04/23/2001 12:00amOriginally posted by: Christoph Platz
Hi there!
I already read about that in one of Jeffrey Richters books (MS Press). It sounds cool but I haven't understand for what the hell this could be used....!
Can anyone explain? I really dont know why my app should load a DLL into the address space of another process :)
Thank for any hints !
ReplyChristoph
Loading, Please Wait ...