Making a Process Unkillable in Windows OS
Contents
In this post, I delve into the interseting aspects of the Windows kernel, specifically focusing on the concept of creating unkillable processes. This is not intended as a tutorial or a definitive guide; rather, it serves as a personal reference and insights that I have gathered during my exploration. I acknowledge that there may be inaccuracies, but I believe that learning from mistakes is a crucial part of the development process.
Note: The experiments described herein were conducted on Windows 10 x64, Version 1709, Build 16299.125.
Understanding the PsTerminateProcess Function
The journey begins with the PsTerminateProcess function, which is a critical component in the Windows kernel responsible for terminating processes.
The wrapper gets the current thread, decrements its KernelApcDisable count,
calls PspTerminateProcess, and leaves the critical region before returning the
result. The relevant lines in the screenshot are:
v2 = KeGetCurrentThread();--v2->KernelApcDisable;v3 = PspTerminateProcess(a1, v2, a2, 1);KeLeaveCriticalRegionThread(v2);return v3;This function subsequently invokes PspTerminateProcess.
In this screenshot, the highlighted function is named PspTerminateAllThreads.
The visible branch runs
v11 = PspTerminateAllThreads(process, v6, v5, v10) when
process->Flags2 & 0x400 is zero.
The PspTerminateProcess function then calls PspTerminateThreads, which iterates through all threads associated with the process and invokes PspTerminateThreadByPointer for each thread.
The loop obtains thread pointers with PspGetPreviousProcessThread. Its
highlighted termination call is PspTerminateThreadByPointer(v10, v5, 0) inside
the if (v11 & 8) branch. At the bottom of the loop,
v10 = PspGetPreviousProcessThread(v7, v10) obtains the next pointer, and
while (v10) continues while that pointer is nonzero.
The Role of KeRequestTerminationThread
The PspTerminateThreadByPointer function subsequently calls KeRequestTerminationThread. This function checks the 15th bit of the 0x74th field of the _KTHREAD structure (*(v2+116) & 0x4000). If this bit is set, it inserts a kernel-mode Asynchronous Procedure Call (APC) into the APC queue of the thread, effectively marking it for termination.
The screenshot shows the caller: after a conditional assignment to
v6->ExitStatus, it calls KeRequestTerminationThread(v6) and returns 0i64.
The bit check inside KeRequestTerminationThread described above is not visible
in this excerpt.
It appears that if a thread is not APC queueable (i.e., the 15th bit of the 0x74 field is not set), it becomes impossible to terminate that thread using this method.
Manipulating the _KTHREAD Structure
The _KTHREAD structure’s 0x74 field is a union, where the 15th bit represents the ApcQueueable flag. This raises an interesting question: what if we were to manipulate this bit and set it to 0?
The marked debugger entry reads +0x074 ApcQueueable : Pos 14, 1 Bit. Position
14 is zero-based: it is the fifteenth bit, corresponding to the 0x4000 mask.
To achieve this, we can utilize tools like WinDbg or develop a custom driver. The driver code is relatively straightforward; it receives thread IDs from userland and disables the APCQueueable flag.
The code shown in the screenshot is transcribed below. The final line clears
bit 14 in the 32-bit value at offset 0x74 from the thread pointer:
PETHREAD eth;NTSTATUS ntstatus = PsLookupThreadByThreadId(info->threadID, ð);if (!NT_SUCCESS(ntstatus)){ KdPrint(("PsLookupThreadByThreadId failed\n"));}DWORD32* a = (DWORD32*)((char*)eth + 0x74);*a &= 0xffffbfff;