Thread Execution Hijacking
Adversaries may inject malicious code into hijacked processes in order to evade process-based defenses as well as possibly elevate privileges. Thread Execution Hijacking is a method of executing arbitrary code in the address space of a separate live process.
Thread Execution Hijacking is commonly performed by suspending an existing process then unmapping/hollowing its memory, which can then be replaced with malicious code or the path to a DLL. A handle to an existing victim process is first created with native Windows API calls such as OpenThread. At this point the process can be suspended then written to, realigned to the injected code, and resumed via SuspendThread, VirtualAllocEx, WriteProcessMemory, SetThreadContext, then ResumeThread respectively.
This is very similar to Process Hollowing but targets an existing process rather than creating a process in a suspended state.
Running code in the context of another process may allow access to the process's memory, system/network resources, and possibly elevated privileges. Execution via Thread Execution hijacking may also evade detection from security products since the execution is masked under a legitimate process.
Here will use a tool ThreadInject this currently works with only 32bit processes so in this sample I will use a 32-bit process and inject a DLL into the process, then this will pause and restart the thread of the process, once it reaches the proper location it will execute the DLL.
Flow of injection
1) Parse the DLL name and the target process ID from command line.
2) Allocate buffer for the shellcode and DLL name.
3) Copy the shellcode to the buffer.
4) Copy the DLL name to the end of shellcode.
5) Open the target process handle.
6) Allocate memory in the target process.
7) Find a running thread to hijack.
8) Get the context of the target thread.
9) Write the eip register to the shellcode.
10) Write the address of LoadLibrary to the shellcode.
11) Write the shellcode and DLL name to the target process.
12) Hijack a running thread in the target process to execute the shellcode.
13) The hijacked thread executes the shellcode. The shellcode calls the LoadLibrary function to load the DLL.
14) The shellcode returns, and the thread continue to execute its own code.