-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate_system_thread.c
54 lines (43 loc) · 1.09 KB
/
create_system_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
HANDLE hThread;
SHORT thread_status;
VOID StopSysthread()
{
PKTHREAD oThread;
if (hThread != NULL && thread_status == 1)
{
if (InterlockedCompareExchange16(&thread_status, 2, 1) == 1)
{
if (NT_SUCCESS(ObReferenceObjectByHandle(hThread, 0, 0, KernelMode, &oThread, 0)))
{
KeWaitForSingleObject(oThread, Executive, KernelMode, FALSE, NULL);
ObDereferenceObject(oThread);
}
}
}
if (hThread != NULL)
{
ZwClose(hThread);
hThread = NULL;
}
}
NTSTATUS StartSysThread()
{
NTSTATUS status;
OBJECT_ATTRIBUTES oAttr;
thread_status = 0;
hThread = NULL;
InitializeObjectAttributes(&oAttr, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
status = PsCreateSystemThread(&hThread, THREAD_ALL_ACCESS, &oAttr, NULL, NULL, (PKSTART_ROUTINE)&threadProc, NULL);
return status;
}
NTSTATUS threadProc()
{
if (InterlockedCompareExchange16(&thread_status, 1, 0) == 2)
PsTerminateSystemThread(STATUS_SUCCESS);
// do something
// place this anywhere you want
if (thread_status == 2)
PsTerminateSystemThread(STATUS_SUCCESS);
// do something
PsTerminateSystemThread(STATUS_SUCCESS);
}