-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill.c
78 lines (71 loc) · 1.62 KB
/
kill.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "kill.h"
// 杀死进程函数
BOOLEAN KillProcess(LONG pid)
{
HANDLE ProcessHandle;
NTSTATUS status;
OBJECT_ATTRIBUTES ObjectAttributes;
CLIENT_ID Cid;
// 初始化ObjectAttributes和Cid
InitializeObjectAttributes(&ObjectAttributes, 0, 0, 0, 0);
Cid.UniqueProcess = (HANDLE)pid;
Cid.UniqueThread = 0;
// 打开进程句柄
status = ZwOpenProcess(&ProcessHandle, PROCESS_ALL_ACCESS, &ObjectAttributes, &Cid);
if (NT_SUCCESS(status))
{
DbgPrint("Open Process %d Successful!\n", pid);
// 结束进程
ZwTerminateProcess(ProcessHandle, status);
// 关闭句柄
ZwClose(ProcessHandle);
return TRUE;
}
DbgPrint("Open Process %d Failed!\n", pid);
return FALSE;
}
BOOLEAN MemKillProcess(LONG pid)
{
PEPROCESS proc = NULL;
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
PKAPC_STATE pApcState = NULL;
PsLookupProcessByProcessId((HANDLE)pid, &proc);
if (proc == 0)
{
return TRUE;
}
pApcState = (PKAPC_STATE)ExAllocatePoolWithTag(NonPagedPool, sizeof(PKAPC_STATE), '1111');
//pApcState = (PKAPC_STATE)ExAllocatePool2(NonPagedPool, sizeof(PKAPC_STATE), '1111');
if (NULL == pApcState)
{
ObDereferenceObject(proc);
return TRUE;
}
__try {
KeStackAttachProcess((PRKPROCESS)proc, pApcState);
//KeAttachProcess(proc);
for (int i = 0x10000; i < 0x20000000; i += PAGE_SIZE)
{
__try
{
memset((PVOID)i, 0, PAGE_SIZE);
}
__except (1)
{
; //内部处理异常
}
}
KeUnstackDetachProcess(pApcState);
//KeDetachProcess();
ObDereferenceObject(proc);
return TRUE;
}
__except (1)
{
DbgPrint("强杀出错\r\n");
KeUnstackDetachProcess(pApcState);
ObDereferenceObject(proc);
return FALSE;
}
return TRUE;
}