-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathEnumRemove.c
More file actions
254 lines (229 loc) · 6.47 KB
/
Copy pathEnumRemove.c
File metadata and controls
254 lines (229 loc) · 6.47 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "EnumRemove.h"
VOID ShowError(PCHAR lpszText, NTSTATUS ntStatus)
{
DbgPrint("%s Error[0x%X]\n", lpszText, ntStatus);
}
// 遍历回调
BOOLEAN EnumNotifyRoutine()
{
ULONG i = 0;
PVOID pPspCreateProcessNotifyRoutineAddress = NULL;
PVOID pNotifyRoutineAddress = NULL;
// 获取 PspCreateProcessNotifyRoutine 数组地址
pPspCreateProcessNotifyRoutineAddress = GetPspCreateProcessNotifyRoutine();
if (NULL == pPspCreateProcessNotifyRoutineAddress)
{
DbgPrint("GetPspCreateProcessNotifyRoutine Error!\n");
return FALSE;
}
DbgPrint("pPspCreateProcessNotifyRoutineAddress=0x%p\n", pPspCreateProcessNotifyRoutineAddress);
// 获取回调地址并解密
#ifdef _WIN64
for (i = 0; i < 64; i++)
{
pNotifyRoutineAddress = *(PVOID *)((PUCHAR)pPspCreateProcessNotifyRoutineAddress + sizeof(PVOID) * i);
pNotifyRoutineAddress = (PVOID)((ULONG64)pNotifyRoutineAddress & 0xfffffffffffffff8);
if (MmIsAddressValid(pNotifyRoutineAddress))
{
pNotifyRoutineAddress = *(PVOID *)pNotifyRoutineAddress;
DbgPrint("[%d]ullNotifyRoutine=0x%p\n", i, pNotifyRoutineAddress);
}
}
#else
for (i = 0; i < 8; i++)
{
pNotifyRoutineAddress = *(PVOID *)((PUCHAR)pPspCreateProcessNotifyRoutineAddress + sizeof(PVOID) * i);
pNotifyRoutineAddress = (PVOID)((ULONG)pNotifyRoutineAddress & 0xfffffff8);
if (MmIsAddressValid(pNotifyRoutineAddress))
{
pNotifyRoutineAddress = *(PVOID *)((PUCHAR)pNotifyRoutineAddress + 4);
DbgPrint("[%d]ullNotifyRoutine=0x%p\n", i, pNotifyRoutineAddress);
}
}
#endif
return TRUE;
}
// 移除回调
NTSTATUS RemoveNotifyRoutine(PVOID pNotifyRoutineAddress)
{
NTSTATUS status = PsSetCreateProcessNotifyRoutine((PCREATE_PROCESS_NOTIFY_ROUTINE)pNotifyRoutineAddress, TRUE);
if (!NT_SUCCESS(status))
{
ShowError("PsSetCreateProcessNotifyRoutine", status);
}
return status;
}
// 获取 PspCreateProcessNotifyRoutine 数组地址
PVOID GetPspCreateProcessNotifyRoutine()
{
PVOID pPspCreateProcessNotifyRoutineAddress = NULL;
RTL_OSVERSIONINFOW osInfo = { 0 };
UCHAR pFirstSpecialData[50] = { 0 };
ULONG ulFirstSpecialDataSize = 0;
UCHAR pSecondSpecialData[50] = { 0 };
ULONG ulSecondSpecialDataSize = 0;
// 获取系统版本信息, 判断系统版本
RtlGetVersion(&osInfo);
if (6 == osInfo.dwMajorVersion)
{
if (1 == osInfo.dwMinorVersion)
{
// Win7
#ifdef _WIN64
// 64 位
// E9
pFirstSpecialData[0] = 0xE9;
ulFirstSpecialDataSize = 1;
// 4C8D35
pSecondSpecialData[0] = 0x4C;
pSecondSpecialData[1] = 0x8D;
pSecondSpecialData[2] = 0x35;
ulSecondSpecialDataSize = 3;
#else
// 32 位
// E8
pFirstSpecialData[0] = 0xE8;
ulFirstSpecialDataSize = 1;
// C7450C
pSecondSpecialData[0] = 0xC7;
pSecondSpecialData[1] = 0x45;
pSecondSpecialData[2] = 0x0C;
ulSecondSpecialDataSize = 3;
#endif
}
else if (2 == osInfo.dwMinorVersion)
{
// Win8
#ifdef _WIN64
// 64 位
#else
// 32 位
#endif
}
else if (3 == osInfo.dwMinorVersion)
{
// Win8.1
#ifdef _WIN64
// 64 位
// E9
pFirstSpecialData[0] = 0xE9;
ulFirstSpecialDataSize = 1;
// 4C8D3D
pSecondSpecialData[0] = 0x4C;
pSecondSpecialData[1] = 0x8D;
pSecondSpecialData[2] = 0x3D;
ulSecondSpecialDataSize = 3;
#else
// 32 位
// E8
pFirstSpecialData[0] = 0xE8;
ulFirstSpecialDataSize = 1;
// B8
pSecondSpecialData[0] = 0xB8;
ulSecondSpecialDataSize = 1;
#endif
}
}
else if (10 == osInfo.dwMajorVersion)
{
// Win10
#ifdef _WIN64
// 64 位
// E9
pFirstSpecialData[0] = 0xE9;
ulFirstSpecialDataSize = 1;
// 4C8D3D
pSecondSpecialData[0] = 0x4C;
pSecondSpecialData[1] = 0x8D;
pSecondSpecialData[2] = 0x3D;
ulSecondSpecialDataSize = 3;
#else
// 32 位
// E8
pFirstSpecialData[0] = 0xE8;
ulFirstSpecialDataSize = 1;
// BB
pSecondSpecialData[0] = 0xBB;
ulSecondSpecialDataSize = 1;
#endif
}
// 根据特征码获取地址
pPspCreateProcessNotifyRoutineAddress = SearchPspCreateProcessNotifyRoutine(pFirstSpecialData, ulFirstSpecialDataSize, pSecondSpecialData, ulSecondSpecialDataSize);
return pPspCreateProcessNotifyRoutineAddress;
}
// 根据特征码获取 PspCreateProcessNotifyRoutine 数组地址
PVOID SearchPspCreateProcessNotifyRoutine(PUCHAR pFirstSpecialData, ULONG ulFirstSpecialDataSize, PUCHAR pSecondSpecialData, ULONG ulSecondSpecialDataSize)
{
UNICODE_STRING ustrFuncName;
PVOID pAddress = NULL;
LONG lOffset = 0;
PVOID pPsSetCteateProcessNotifyRoutine = NULL;
PVOID pPspSetCreateProcessNotifyRoutineAddress = NULL;
PVOID pPspCreateProcessNotifyRoutineAddress = NULL;
// 先获取 PsSetCreateProcessNotifyRoutine 函数地址
RtlInitUnicodeString(&ustrFuncName, L"PsSetCreateProcessNotifyRoutine");
pPsSetCteateProcessNotifyRoutine = MmGetSystemRoutineAddress(&ustrFuncName);
if (NULL == pPsSetCteateProcessNotifyRoutine)
{
ShowError("MmGetSystemRoutineAddress", 0);
return pPspCreateProcessNotifyRoutineAddress;
}
// 然后, 查找 PspSetCreateProcessNotifyRoutine 函数地址
pAddress = SearchMemory(pPsSetCteateProcessNotifyRoutine,
(PVOID)((PUCHAR)pPsSetCteateProcessNotifyRoutine + 0xFF),
pFirstSpecialData, ulFirstSpecialDataSize);
if (NULL == pAddress)
{
ShowError("SearchMemory1", 0);
return pPspCreateProcessNotifyRoutineAddress;
}
// 获取偏移数据, 并计算地址
lOffset = *(PLONG)pAddress;
pPspSetCreateProcessNotifyRoutineAddress = (PVOID)((PUCHAR)pAddress + sizeof(LONG) + lOffset);
// 最后, 查找 PspCreateProcessNotifyRoutine 地址
pAddress = SearchMemory(pPspSetCreateProcessNotifyRoutineAddress,
(PVOID)((PUCHAR)pPspSetCreateProcessNotifyRoutineAddress + 0xFF),
pSecondSpecialData, ulSecondSpecialDataSize);
if (NULL == pAddress)
{
ShowError("SearchMemory2", 0);
return pPspCreateProcessNotifyRoutineAddress;
}
// 获取地址
#ifdef _WIN64
// 64 位先获取偏移, 再计算地址
lOffset = *(PLONG)pAddress;
pPspCreateProcessNotifyRoutineAddress = (PVOID)((PUCHAR)pAddress + sizeof(LONG) + lOffset);
#else
// 32 位直接获取地址
pPspCreateProcessNotifyRoutineAddress = *(PVOID *)pAddress;
#endif
return pPspCreateProcessNotifyRoutineAddress;
}
// 指定内存区域的特征码扫描
PVOID SearchMemory(PVOID pStartAddress, PVOID pEndAddress, PUCHAR pMemoryData, ULONG ulMemoryDataSize)
{
PVOID pAddress = NULL;
PUCHAR i = NULL;
ULONG m = 0;
// 扫描内存
for (i = (PUCHAR)pStartAddress; i < (PUCHAR)pEndAddress; i++)
{
// 判断特征码
for (m = 0; m < ulMemoryDataSize; m++)
{
if (*(PUCHAR)(i + m) != pMemoryData[m])
{
break;
}
}
// 判断是否找到符合特征码的地址
if (m >= ulMemoryDataSize)
{
// 找到特征码位置, 获取紧接着特征码的下一地址
pAddress = (PVOID)(i + ulMemoryDataSize);
break;
}
}
return pAddress;
}