-
Notifications
You must be signed in to change notification settings - Fork 4
/
dllmain.c
546 lines (502 loc) · 13.5 KB
/
dllmain.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#pragma comment(lib, "ProcessHacker.lib")
#define ID_VIEW_HIDEPROCESSESFROMOTHERUSERS 40232
#define ID_PROCESS_CHECK 101
#define COLUMN_ID 101
typedef enum _CURRENT_THREAD_STAGE
{
ThreadStageNone,
ThreadStageSleep,
ThreadStageWininet,
ThreadStageSMB,
ThreadStageTCP,
} CURRENT_THREAD_STAGE;
typedef struct _CS_ITEM
{
union
{
struct
{
BOOL isCS : 1;
BOOL IsAdvapi32 : 1;
BOOL IsDnsapi : 1;
BOOL IsIphlpapi : 1;
BOOL IsSecur32 : 1;
BOOL IsWininet : 1;
BOOL IsWs2_32 : 1;
};
};
HANDLE CurrentThreadId;
PVOID BaseAddressOfCS;
SIZE_T SizeOfCS;
PPH_PROCESS_ITEM ProcessItem;
PPH_SYMBOL_PROVIDER symbolProvider;
CURRENT_THREAD_STAGE CurrentThreadStage;
PPH_STRING Text;
} CS_ITEM, * PCS_ITEM;
PPH_PLUGIN PluginInstance;
HWND ProcessTreeNewHandle;
static PH_CALLBACK_REGISTRATION PluginMenuItemCallbackRegistration;
static PH_CALLBACK_REGISTRATION MainMenuInitializingCallbackRegistration;
static PH_CALLBACK_REGISTRATION ProcessTreeNewInitializingCallbackRegistration;
static PH_CALLBACK_REGISTRATION TreeNewMessageCallbackRegistration;
static PPH_TN_FILTER_ENTRY FilterEntry = NULL;
static VOID NTAPI MainMenuInitializingCallback(
_In_opt_ PVOID Parameter,
_In_opt_ PVOID Context
)
{
PPH_PLUGIN_MENU_INFORMATION menuInfo = (PPH_PLUGIN_MENU_INFORMATION)Parameter;
PPH_EMENU_ITEM menuItem;
ULONG insertIndex;
// Check this menu is the View menu
if (!menuInfo || menuInfo->u.MainMenu.SubMenuIndex != PH_MENU_ITEM_LOCATION_VIEW)
return;
menuItem = PhFindEMenuItem(menuInfo->Menu, 0, NULL, ID_VIEW_HIDEPROCESSESFROMOTHERUSERS);
if (!menuItem) {
return;
}
insertIndex = PhIndexOfEMenuItem(menuInfo->Menu, menuItem);
menuItem = PhPluginCreateEMenuItem(PluginInstance, 0, ID_PROCESS_CHECK, L"Show CobaltStrike processes", NULL);
PhInsertEMenuItem(menuInfo->Menu, menuItem, insertIndex);
if (FilterEntry)
menuItem->Flags |= PH_EMENU_CHECKED;
}
static BOOLEAN NTAPI EnumGenericModulesCallback(
_In_ PPH_MODULE_INFO Module,
_In_opt_ PVOID Context
)
{
PCS_ITEM cs = Context;
if (
PhEqualStringRef2(&Module->Name->sr, L"Wininet.dll", TRUE) ||
PhStartsWithStringRef2(&Module->Name->sr, L"Kernel", TRUE) ||
PhEqualStringRef2(&Module->Name->sr, L"Ntdll.dll", TRUE) ||
PhEqualStringRef2(&Module->Name->sr, L"Ws2_32.dll", TRUE)
) {
PhLoadModuleSymbolProvider(cs->symbolProvider, Module->FileName->Buffer,
(ULONG64)Module->BaseAddress, Module->Size);
}
return TRUE;
}
static BOOLEAN NTAPI EnumGenericModulesCallbackDLL(
_In_ PPH_MODULE_INFO Module,
_In_opt_ PVOID Context
)
{
PCS_ITEM cs = Context;
if (PhEqualStringRef2(&Module->Name->sr, L"Advapi32.dll", TRUE)) {
cs->IsAdvapi32 = 1;
}
else if (PhEqualStringRef2(&Module->Name->sr, L"Dnsapi.dll", TRUE)) {
cs->IsDnsapi = 1;
}
else if (PhEqualStringRef2(&Module->Name->sr, L"Iphlpapi.dll", TRUE)) {
cs->IsIphlpapi = 1;
}
else if (PhEqualStringRef2(&Module->Name->sr, L"Secur32.dll", TRUE)) {
cs->IsSecur32 = 1;
}
else if (PhEqualStringRef2(&Module->Name->sr, L"Wininet.dll", TRUE)) {
cs->IsWininet = 1;
}
else if (PhEqualStringRef2(&Module->Name->sr, L"Ws2_32.dll", TRUE)) {
cs->IsWs2_32 = 1;
}
return TRUE;
}
static VOID DetectAddress(PCS_ITEM cs, PVOID addr) {
//检查内存是否为MEM_PRIVATE、PAGE_READWRITE
MEMORY_BASIC_INFORMATION basicInfo;
if (NtQueryVirtualMemory(
cs->symbolProvider->ProcessHandle,
addr,
MemoryBasicInformation,
&basicInfo,
sizeof(MEMORY_BASIC_INFORMATION),
NULL
) >= 0) {
//判断原始属性可写,最终属性可执行
if (
(basicInfo.Type & MEM_PRIVATE) &&
(basicInfo.Protect & (PAGE_EXECUTE_READWRITE| PAGE_EXECUTE_READ)) &&
(basicInfo.AllocationProtect & (PAGE_EXECUTE_READWRITE| PAGE_READWRITE))
) {
cs->BaseAddressOfCS = basicInfo.AllocationBase;
cs->SizeOfCS = basicInfo.RegionSize;
cs->isCS = TRUE;
PWCHAR type = L"";
switch (cs->CurrentThreadStage)
{
case ThreadStageSleep:
type = L"Sleep";
break;
case ThreadStageWininet:
type = L"Wininet";
break;
case ThreadStageSMB:
type = L"SMB";
break;
case ThreadStageTCP:
type = L"TCP";
break;
}
cs->Text = PhFormatString(
L"Pid:%u Tid:%u Type:%s Mem:%p(%llx) Call:%p",
HandleToUlong(cs->ProcessItem->ProcessId),
HandleToUlong(cs->CurrentThreadId),
type,
cs->BaseAddressOfCS,
cs->SizeOfCS,
addr
);
}
}
}
PH_STRINGREF FrameSleep1 = PH_STRINGREF_INIT(L"!Sleep+0x");
PH_STRINGREF FrameSleep2 = PH_STRINGREF_INIT(L"!SleepEx+0x");
PH_STRINGREF FrameWininet = PH_STRINGREF_INIT(L"Wininet.dll!");
PH_STRINGREF FrameSMB1 = PH_STRINGREF_INIT(L"ReadFile+0x");
PH_STRINGREF FrameSMB2 = PH_STRINGREF_INIT(L"FsControlFile+0x");
PH_STRINGREF FrameTCP1 = PH_STRINGREF_INIT(L"ws2_32.dll!accept+0x");
PH_STRINGREF FrameTCP2 = PH_STRINGREF_INIT(L"ws2_32.dll!recv+0x");
static BOOLEAN NTAPI PhpWalkThreadStackCallback(
_In_ PPH_THREAD_STACK_FRAME StackFrame,
_In_opt_ PVOID Context
)
{
BOOLEAN ret = TRUE;
PCS_ITEM cs = Context;
PPH_STRING symbol = PhGetSymbolFromAddress(
cs->symbolProvider,
(ULONG64)StackFrame->PcAddress,
NULL,
NULL,
NULL,
NULL
);
#ifdef DEBUG
DbgPrint("%S p:%d t:%d %S\n", cs->ProcessItem->ProcessName->Buffer, cs->ProcessItem->ProcessId, cs->CurrentThreadId, symbol->Buffer);
#endif // DEBUG
//之前栈包含sleep或wininet调用,检查当前栈是否可写
switch (cs->CurrentThreadStage)
{
case ThreadStageNone: {
if (
(PhFindStringInStringRef(&symbol->sr, &FrameSleep1, TRUE) != -1) ||
(PhFindStringInStringRef(&symbol->sr, &FrameSleep2, TRUE) != -1))
{
cs->CurrentThreadStage = ThreadStageSleep;
}
else if (PhFindStringInStringRef(&symbol->sr, &FrameWininet, TRUE) != -1) {
cs->CurrentThreadStage = ThreadStageWininet;
}
else if (
(PhFindStringInStringRef(&symbol->sr, &FrameSMB1, TRUE) != -1) ||
(PhFindStringInStringRef(&symbol->sr, &FrameSMB2, TRUE) != -1))
{
cs->CurrentThreadStage = ThreadStageSMB;
}
else if (
(PhFindStringInStringRef(&symbol->sr, &FrameTCP1, TRUE) != -1) ||
(PhFindStringInStringRef(&symbol->sr, &FrameTCP2, TRUE) != -1))
{
cs->CurrentThreadStage = ThreadStageTCP;
}
break;
}
case ThreadStageSleep: {
if (
(PhFindStringInStringRef(&symbol->sr, &FrameSleep1, TRUE) == -1) &&
(PhFindStringInStringRef(&symbol->sr, &FrameSleep2, TRUE) == -1))
{
ret = FALSE;
//检查当前栈是否可写
DetectAddress(cs, StackFrame->PcAddress);
}
break;
}
case ThreadStageWininet: {
if (PhFindStringInStringRef(&symbol->sr, &FrameWininet, TRUE) == -1) {
ret = FALSE;
//检查当前栈是否可写
DetectAddress(cs, StackFrame->PcAddress);
}
break;
}
case ThreadStageSMB: {
if (
(PhFindStringInStringRef(&symbol->sr, &FrameSMB1, TRUE) == -1) &&
(PhFindStringInStringRef(&symbol->sr, &FrameSMB2, TRUE) == -1))
{
ret = FALSE;
//检查当前栈是否可写
DetectAddress(cs, StackFrame->PcAddress);
}
break;
}
case ThreadStageTCP: {
if (
(PhFindStringInStringRef(&symbol->sr, &FrameTCP1, TRUE) == -1) &&
(PhFindStringInStringRef(&symbol->sr, &FrameTCP2, TRUE) == -1))
{
ret = FALSE;
//检查当前栈是否可写
DetectAddress(cs, StackFrame->PcAddress);
}
break;
}
}
PhDereferenceObject(symbol);
return ret;
}
BOOL GetSymbolProvider(PCS_ITEM cs) {
if (cs->symbolProvider) {
return TRUE;
}
cs->symbolProvider = PhCreateSymbolProvider(cs->ProcessItem->ProcessId);
PhLoadSymbolProviderOptions(cs->symbolProvider);
if (!cs->symbolProvider->IsRealHandle)
{
//进程无法打开
goto CLEAN;
}
PhEnumGenericModules(
cs->ProcessItem->ProcessId,
cs->symbolProvider->ProcessHandle,
0,
EnumGenericModulesCallbackDLL,
cs
);
//有CS必然加载的模块
if (cs->IsAdvapi32 && cs->IsIphlpapi && cs->IsWs2_32) {
//加载模块的符号
PhEnumGenericModules(
cs->ProcessItem->ProcessId,
cs->symbolProvider->ProcessHandle,
0,
EnumGenericModulesCallback,
cs
);
return TRUE;
}
CLEAN:
PhDereferenceObject(cs->symbolProvider);
cs->symbolProvider = NULL;
return FALSE;
}
VOID Detect(PCS_ITEM cs) {
PVOID processes;
cs->isCS = FALSE;
if (cs->ProcessItem->IsSuspended) {
return;
}
if (!NT_SUCCESS(PhEnumProcesses(&processes))) {
return;
}
PSYSTEM_PROCESS_INFORMATION process = PhFindProcessInformation(processes, cs->ProcessItem->ProcessId);
if (!process) {
//没找到进程
goto CLEAN;
}
if (!GetSymbolProvider(cs)) {
goto CLEAN;
}
PSYSTEM_THREAD_INFORMATION threads = process->Threads;
ULONG numberOfThreads = process->NumberOfThreads;
NTSTATUS status;
HANDLE threadHandle;
CLIENT_ID clientId;
for (ULONG i = 0; i < numberOfThreads; i++) {
if (!NT_SUCCESS(status = PhOpenThread(
&threadHandle,
ThreadQueryAccess | THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME,
threads[i].ClientId.UniqueThread
)))
{
//线程无法打开,继续下个循环
continue;
}
clientId.UniqueProcess = cs->ProcessItem->ProcessId;
clientId.UniqueThread = threads[i].ClientId.UniqueThread;
//回溯堆栈
cs->CurrentThreadId = clientId.UniqueThread;
cs->CurrentThreadStage = ThreadStageNone;
status = PhWalkThreadStack(
threadHandle,
cs->symbolProvider->ProcessHandle,
&clientId,
cs->symbolProvider,
PH_WALK_I386_STACK | PH_WALK_AMD64_STACK,
PhpWalkThreadStackCallback,
cs
);
NtClose(threadHandle);
if (cs->isCS) {
break;
}
}
CLEAN:
PhFree(processes);
}
static BOOLEAN ProcessTreeFilter(
_In_ PPH_TREENEW_NODE Node,
_In_opt_ PVOID Context
)
{
PPH_PROCESS_NODE node = (PPH_PROCESS_NODE)Node;
PCS_ITEM cs = PhPluginGetObjectExtension(PluginInstance, node->ProcessItem, EmProcessItemType);
//PhEqualStringRef2(&cs->ProcessItem->ProcessName->sr,L"artifact.exe",TRUE)
if (!cs->isCS) {
Detect(cs);
}
return cs->isCS;
}
static VOID NTAPI MenuItemCallback(
_In_opt_ PVOID Parameter,
_In_opt_ PVOID Context
)
{
PPH_PLUGIN_MENU_ITEM menuItem = (PPH_PLUGIN_MENU_ITEM)Parameter;
if (!menuItem || menuItem->Id != ID_PROCESS_CHECK)
{
return;
}
if (!FilterEntry) {
FilterEntry = PhAddTreeNewFilter(PhGetFilterSupportProcessTreeList(), ProcessTreeFilter, NULL);
}
else {
PhRemoveTreeNewFilter(PhGetFilterSupportProcessTreeList(), FilterEntry);
FilterEntry = NULL;
}
PhApplyTreeNewFilters(PhGetFilterSupportProcessTreeList());
}
static VOID NTAPI ItemCreateCallback(
_In_ PVOID Object,
_In_ PH_EM_OBJECT_TYPE ObjectType,
_In_ PVOID Extension
)
{
PCS_ITEM cs = Extension;
memset(cs, 0, sizeof(CS_ITEM));
cs->ProcessItem = Object;
}
VOID NTAPI ItemDeleteCallback(
_In_ PVOID Object,
_In_ PH_EM_OBJECT_TYPE ObjectType,
_In_ PVOID Extension
)
{
PCS_ITEM cs = Extension;
if (cs->symbolProvider) {
PhDereferenceObject(cs->symbolProvider);
}
if (cs->Text) {
PhDereferenceObject(cs->Text);
}
}
static LONG NTAPI ProcessSortFunction(
_In_ PVOID Node1,
_In_ PVOID Node2,
_In_ ULONG SubId,
_In_ PVOID Context
)
{
PPH_PROCESS_NODE node1 = Node1;
PPH_PROCESS_NODE node2 = Node2;
PCS_ITEM cs1 = PhPluginGetObjectExtension(PluginInstance, node1->ProcessItem, EmProcessItemType);
PCS_ITEM cs2 = PhPluginGetObjectExtension(PluginInstance, node2->ProcessItem, EmProcessItemType);
return PhCompareStringWithNull(cs1->Text, cs2->Text, TRUE);
}
VOID ProcessTreeNewInitializingCallback(
_In_opt_ PVOID Parameter,
_In_opt_ PVOID Context
)
{
PPH_PLUGIN_TREENEW_INFORMATION info = Parameter;
PH_TREENEW_COLUMN column;
ProcessTreeNewHandle = info->TreeNewHandle;
memset(&column, 0, sizeof(PH_TREENEW_COLUMN));
column.Text = L"CobaltStrike";
column.Width = 120;
column.Alignment = PH_ALIGN_LEFT;
PhPluginAddTreeNewColumn(PluginInstance, info->CmData, &column, COLUMN_ID, NULL, ProcessSortFunction);
}
VOID TreeNewMessageCallback(
_In_opt_ PVOID Parameter,
_In_opt_ PVOID Context
)
{
PPH_PLUGIN_TREENEW_MESSAGE message = Parameter;
if (message->Message != TreeNewGetCellText || message->TreeNewHandle != ProcessTreeNewHandle) {
return;
}
PPH_TREENEW_GET_CELL_TEXT getCellText = message->Parameter1;
if (message->TreeNewHandle == ProcessTreeNewHandle)
{
PPH_PROCESS_NODE node;
node = (PPH_PROCESS_NODE)getCellText->Node;
switch (message->SubId)
{
case COLUMN_ID:
{
PCS_ITEM cs;
cs = PhPluginGetObjectExtension(PluginInstance, node->ProcessItem, EmProcessItemType);
getCellText->Text = PhGetStringRef(cs->Text);
}
break;
}
}
}
BOOL APIENTRY DllMain(HMODULE Instance,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (ul_reason_for_call != DLL_PROCESS_ATTACH) {
return TRUE;
}
PPH_PLUGIN_INFORMATION info;
// Register your plugin with a unique name, otherwise it will fail.
PluginInstance = PhRegisterPlugin(L"ProcessHacker.CobaltStrikeDetect", Instance, &info);
if (!PluginInstance)
return FALSE;
info->DisplayName = L"CobaltStrike Detect";
info->Author = L"guage";
info->Description = L"Plugin for checking CobaltStrike's process";
info->Url = L"https://guage.cool/";
info->HasOptions = FALSE;
PhRegisterCallback(
PhGetGeneralCallback(GeneralCallbackMainMenuInitializing),
MainMenuInitializingCallback,
NULL,
&MainMenuInitializingCallbackRegistration
);
PhRegisterCallback(
PhGetPluginCallback(PluginInstance, PluginCallbackMenuItem),
MenuItemCallback,
NULL,
&PluginMenuItemCallbackRegistration
);
PhRegisterCallback(
PhGetGeneralCallback(GeneralCallbackProcessTreeNewInitializing),
ProcessTreeNewInitializingCallback,
NULL,
&ProcessTreeNewInitializingCallbackRegistration
);
PhRegisterCallback(
PhGetPluginCallback(PluginInstance, PluginCallbackTreeNewMessage),
TreeNewMessageCallback,
NULL,
&TreeNewMessageCallbackRegistration
);
PhPluginSetObjectExtension(
PluginInstance,
EmProcessItemType,
sizeof(CS_ITEM),
ItemCreateCallback,
ItemDeleteCallback
);
return TRUE;
}