PE Injection: Executing PEs inside Remote Processes
Code Injection
This is a quick lab of a simplified way of injecting an entire portable executabe (PE) into another running process.
Note that in order to inject more complex PEs, additional DLLs in the target process may need to be loaded and Import Address Table fixed and for this, refer to my other lab Reflective DLL Injection.
Overview
In this lab, I wrote a simple C++ executable that self-injects its PE into a target process. This executable contains 2 functions:
main- this is the function that performs the self-injection of the PE image into a specified remote/target process, which is going to benotepad.exein this case;InjectionEntryPoint- this is the function that will get executed by the target process (notepad) once notepads gets injected with our PE.This function will pop a
MessageBoxwith a name of the module the code is currently running from. If injection is successful, it should spit out a path of notepad.exe.
Technique Overview
Inside the current process, that's doing the self-injection of its PE:
Get the image base address
imageBaseParse the PE headers and get its
sizeOfImageAllocate a block of memory (size of PE image retrieved in step 1). Let's call it
localImageCopy the image of the current process into the newly allocated local memory
localImageAllocate a new memory block (size of PE image retrieved in step 1) in a remote process - the target process we want to inject the currently running PE into. Let's call it
targetImageCalculate the delta between memory addresses
targetImageandimageBase, let's call itdeltaImageBaseRelocate/rebase the PE that's stored in
localImagetotargetImage. For more information about image relocations, see my other lab T1093: Process Hollowing and Portable Executable RelocationsWrite the patched PE into the
targetImagememory location usingWriteProcessMemoryCreate remote thread and point it to
InjectionEntryPointfunction inside the PE target process
Walkthrough
Getting sizeOfImage of the current process (local process) that will be injecting itself into a target process and allocating a new memory block in the local process:

In my case, the new memory block got allocated at address 0x000001813acc0000. Let's copy the current process's image in there:

Let's allocate a new block of memory in the target process. In my case it got allocated at 0x000001bfc0c20000:

Calculate the delta between 0x000001bfc0c20000 and 0x000001813acc0000 and perform image base relocations. Once that's done, we can move over our rebased PE from 0x000001813acc0000 to 0x000001bfc0c20000 in the remote process using WriteProcessMemory.
Below shows that our imaged has now been moved to the remote process:

Finally, we can create a remote thread and point it to the InjectionEntryPoint function inside the remote process:

Demo
Below shows how we've injected the PE into the notepad (PID 11068) and executed its function InjectionEntryPoint which printed out the name of a module the code was running from, proving that the PE injection was succesful:

Code
Below is the commented code that performs the PE injection:
References
Last updated