-
Notifications
You must be signed in to change notification settings - Fork 3
/
Enum.cpp
819 lines (709 loc) · 17.6 KB
/
Enum.cpp
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#include "Enum.h"
#include "Common.h"
#include "spdlog/spdlog.h"
#include <Windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <psapi.h>
#include <WtsApi32.h>
#include <tchar.h>
#include <winperf.h>
#include <map>
#include <Pdh.h>
#include <PdhMsg.h>
#include <string>
#include <vector>
#pragma comment(lib,"WtsApi32.lib")
#pragma comment(lib,"Pdh.lib")
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
typedef struct _LSA_UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} LSA_UNICODE_STRING, * PLSA_UNICODE_STRING, UNICODE_STRING, * PUNICODE_STRING;
typedef struct _SYSTEM_PROCESS_INFORMATION {
ULONG NextEntryOffset;
ULONG ThreadCount;
ULONG Reserved1[6];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ProcessName;
long BasePriority;
ULONG ProcessId;
ULONG InheritedFromProcessId;
ULONG HandleCount;
ULONG Reserved2[2];
SIZE_T VmCounters;
IO_COUNTERS IoCounters;
LARGE_INTEGER Threads[1];
} SYSTEM_PROCESS_INFORMATION, * PSYSTEM_PROCESS_INFORMATION;
typedef LONG NTSTATUS;
typedef NTSTATUS(NTAPI* P_ZwQuerySystemInformation)(
IN ULONG SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength
);
extern "C" ULONG _ZwQuerySystemInformation(
IN ULONG SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength);
void Enum_CreateToolhelp32Snapshot()
{
INT ProcCount = 0;
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hProcessSnap)
{
return;
}
PROCESSENTRY32 process = { sizeof(PROCESSENTRY32) };
for (BOOL flag = Process32First(hProcessSnap, &process); flag; flag = Process32Next(hProcessSnap, &process))
{
if (_wcsicmp(process.szExeFile, SELF_PROCNAME_W) == 0)
{
ProcCount++;
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_EnumProcesses()
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
HANDLE hProcess = NULL;
WCHAR FilePath[MAX_PATH] = { 0 };
INT ProcCount = 0;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return;
}
cProcesses = cbNeeded / sizeof(DWORD);
for (UINT i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, aProcesses[i]);
if (NULL != hProcess)
{
memset(FilePath, 0, MAX_PATH);
GetModuleFileNameExW(hProcess, 0, FilePath, MAX_PATH);
if (wcsstr(FilePath, SELF_PROCNAME_W) != 0)
{
ProcCount++;
}
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_WTSEnumerateProcess()
{
INT ProcCount = 0;
DWORD dwCount = 0;
PWTS_PROCESS_INFO pi = { 0 };
if (WTSEnumerateProcesses(NULL,0,1,&pi,&dwCount))
{
for (UINT i = 0; i < dwCount; i++)
{
if (_wcsicmp(pi[i].pProcessName, SELF_PROCNAME_W) == 0)
{
ProcCount++;
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_ZwQuerySystemInformation()
{
HMODULE hNtdll = LoadLibrary(L"ntdll.dll");
if (!hNtdll)
{
return;
}
P_ZwQuerySystemInformation ZwQuerySystemInformation = (P_ZwQuerySystemInformation)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
INT ProcCount = 0;
DWORD len;
NTSTATUS result;
PSYSTEM_PROCESS_INFORMATION spi;
BYTE* pBuf;
result = ZwQuerySystemInformation((UINT)5, NULL, 0, &len);
pBuf = new BYTE[len];
if (result == STATUS_INFO_LENGTH_MISMATCH)
{
result = ZwQuerySystemInformation(5, pBuf, len, &len);
if (result == STATUS_SUCCESS)
{
PSYSTEM_PROCESS_INFORMATION pre = spi = (PSYSTEM_PROCESS_INFORMATION)pBuf;
do
{
if (spi->ProcessName.Buffer)
{
if (_wcsicmp(spi->ProcessName.Buffer, SELF_PROCNAME_W) == 0)
{
ProcCount++;
}
}
pre = spi;
spi = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)spi + spi->NextEntryOffset);
} while (pre->NextEntryOffset != 0);
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_DirectSystemCalls()
{
INT ProcCount = 0;
DWORD len;
NTSTATUS result;
PSYSTEM_PROCESS_INFORMATION spi;
BYTE* pBuf;
result = _ZwQuerySystemInformation((UINT)5, NULL, 0, &len);
pBuf = new BYTE[len];
if (result == STATUS_INFO_LENGTH_MISMATCH)
{
result = _ZwQuerySystemInformation(5, pBuf, len, &len);
if (result == STATUS_SUCCESS)
{
PSYSTEM_PROCESS_INFORMATION pre = spi = (PSYSTEM_PROCESS_INFORMATION)pBuf;
do
{
if (spi->ProcessName.Buffer)
{
if (_wcsicmp(spi->ProcessName.Buffer, SELF_PROCNAME_W) == 0)
{
ProcCount++;
}
}
pre = spi;
spi = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)spi + spi->NextEntryOffset);
} while (pre->NextEntryOffset != 0);
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_OpenProcess()
{
INT ProcCount = 0;
HANDLE hProcess = NULL;
WCHAR FilePath[MAX_PATH] = { 0 };
for (UINT i = 0; i < 100000; i += 4)
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, i);
if (NULL != hProcess)
{
memset(FilePath, 0, MAX_PATH);
GetModuleFileNameExW(hProcess, 0, FilePath, MAX_PATH);
if (wcsstr(FilePath, SELF_PROCNAME_W) != 0)
{
ProcCount++;
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_PerformanceData()
{
INT ProcCount = 0;
BYTE data[0x40000] = { 0 };
DWORD cb = 0x40000, type = 0;;
RegQueryValueExA(HKEY_PERFORMANCE_DATA, "230 232", NULL, &type, data, &cb);
PPERF_DATA_BLOCK ppdb = (PPERF_DATA_BLOCK)data;
PPERF_OBJECT_TYPE ppbt = (PPERF_OBJECT_TYPE)((BYTE*)data + ppdb->HeaderLength);
UINT count_obj = 0;
while (ppbt->ObjectNameTitleIndex != 230)
{
ppbt = (PPERF_OBJECT_TYPE)(ppbt->TotalByteLength + (BYTE*)ppbt);
if (++count_obj >= ppdb->NumObjectTypes)
{
return ;
}
}
PPERF_COUNTER_DEFINITION ppcd = (PPERF_COUNTER_DEFINITION)(ppbt->HeaderLength + (BYTE*)ppbt);
UINT count_counter = 0;
while (ppcd->CounterNameTitleIndex != 784)
{
ppcd = (PPERF_COUNTER_DEFINITION)(ppcd->ByteLength + (BYTE*)ppcd);
if (++count_counter >= ppbt->NumCounters)
{
printf("error, no pid counter found\n");
return ;
}
}
PERF_INSTANCE_DEFINITION* ppid = (PPERF_INSTANCE_DEFINITION)(ppbt->DefinitionLength + (BYTE*)ppbt);
int count_instance = 0;
while (ppid && ppid->ByteLength)
{
if (_wcsicmp((PWCHAR)(ppid->NameOffset + (BYTE*)ppid), L"BypassSbieProcIsolate") == 0)
{
ProcCount++;
}
//wprintf(L"%s %d\n", ppid->NameOffset + (BYTE*)ppid, *(DWORD*)(ppid->ByteLength + (BYTE*)ppid + ppcd->CounterOffset));
ppid = (PERF_INSTANCE_DEFINITION*)(*(DWORD*)(ppid->ByteLength + (BYTE*)ppid) + ppid->ByteLength + (BYTE*)ppid);
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_PerformanceDataHelper()
{
INT ProcCount = 0;
LPTSTR szCounterListBuffer = NULL;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL;
DWORD dwInstanceListSize = 0;
BOOL pass = FALSE;
PDH_STATUS pdhStatus = PdhEnumObjectItems(NULL, NULL, TEXT("Process"),
szCounterListBuffer, &dwCounterListSize, szInstanceListBuffer,
&dwInstanceListSize, PERF_DETAIL_WIZARD, 0);
if (pdhStatus != ERROR_SUCCESS)
{
szCounterListBuffer = (LPTSTR)malloc((dwCounterListSize * sizeof(TCHAR)));
szInstanceListBuffer = (LPTSTR)malloc((dwInstanceListSize * sizeof(TCHAR)));
pdhStatus = PdhEnumObjectItems(NULL, NULL, TEXT("Process"),
szCounterListBuffer, &dwCounterListSize, szInstanceListBuffer,
&dwInstanceListSize, PERF_DETAIL_WIZARD, 0);
if (pdhStatus == ERROR_SUCCESS)
{
pass = TRUE;
LPTSTR pInst = szInstanceListBuffer;
for (; *pInst != 0; pInst += lstrlen(pInst) + 1)
{
if (_wcsicmp(pInst, L"System") && _wcsicmp(pInst, L"Idle") &&
_wcsicmp(pInst, L"_Total"))
{
// GetPIDCounterValue(pInst) :https://blog.51cto.com/u_15127644/4028515
// pInst
if (_wcsicmp(pInst, L"BypassSbieProcIsolate") == 0)
{
ProcCount++;
}
}
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
return ;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
INT* pProcCount = (INT*)lParam;
WCHAR WndText[MAX_PATH] = { 0 };
GetWindowText(hwnd, WndText, MAX_PATH);
//printf("%S \n", WndText);
if (wcsstr(WndText, SELF_PROCNAME_W) != 0)
{
*pProcCount = *pProcCount + 1;
}
return TRUE;
}
void Enum_EnumWindows()
{
INT ProcCount = 0;
EnumWindows(EnumWindowsProc ,(LPARAM)&ProcCount);
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_EnumChildWindows()
{
INT ProcCount = 0;
HWND hdesk = (HWND)0x10010;
if (!hdesk)
{
return;
}
EnumChildWindows(hdesk, EnumWindowsProc, (LPARAM)&ProcCount);
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
BOOL CALLBACK DeskEnum(LPWSTR desk, LPARAM lParam)
{
HDESK hDesk = OpenDesktopW(desk, 0, FALSE, DESKTOP_READOBJECTS | DESKTOP_ENUMERATE);
if (hDesk)
{
HDESK hCurrentDesk = GetThreadDesktop(GetCurrentThreadId());
EnumDesktopWindows(hDesk, &EnumWindowsProc, lParam);
}
return TRUE;
}
BOOL CALLBACK EnumWinStationProc(LPTSTR winsta, LPARAM lParam)
{
HWINSTA current = GetProcessWindowStation();
HWINSTA hWinsta = OpenWindowStationW(winsta, FALSE, WINSTA_ENUMDESKTOPS);
if (hWinsta)
{
EnumDesktopsW(hWinsta, &DeskEnum, lParam);
}
return true;
}
void Enum_EnumDesktopWindows()
{
INT ProcCount = 0;
EnumWindowStationsW(&EnumWinStationProc, (LPARAM)&ProcCount);
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_GetNextWindow()
{
INT ProcCount = 0;
HWND hDsktp = GetDesktopWindow();
HWND hNextWnd = hDsktp;
WCHAR szBuf[MAX_PATH] = { 0 };
hNextWnd = GetWindow(hDsktp, GW_CHILD);
do
{
GetWindowText(hNextWnd, szBuf, MAX_PATH);
if (wcsstr(szBuf, SELF_PROCNAME_W) != 0)
{
ProcCount++;
}
} while (NULL != (hNextWnd = GetWindow(hNextWnd, GW_HWNDNEXT)));
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_EnumThreadWindows()
{
INT ProcCount = 0;
for (int i = 0; i < 100000; i += 4)
{
EnumThreadWindows(i, EnumWindowsProc, (LPARAM)&ProcCount);
}
ProcCount /= 2;
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_IsWindow()
{
// 这个遍历在 sbie 中非常耗时,但最终只能遍历到 Default IME 窗口
return;
INT ProcCount = 0;
WCHAR WndText[MAX_PATH] = { 0 };
for (ULONG_PTR i = 0; i < 0x1000000; i+= 2)
{
if (IsWindow((HWND)i))
{
memset(WndText, 0, MAX_PATH);
GetWindowText((HWND)i, WndText, MAX_PATH);
if (WndText[0] == L'\0')
continue;
if (wcsstr(WndText, SELF_PROCNAME_W) != 0)
{
ProcCount++;
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_FindWindowEx()
{
INT ProcCount = 0;
HWND child = NULL;
WCHAR buf[MAX_PATH];
do {
memset(buf, 0, MAX_PATH);
child = FindWindowEx(NULL, child, NULL, NULL);
GetWindowText(child, buf, MAX_PATH);
if (wcsstr(buf, SELF_PROCNAME_W) != 0)
{
ProcCount++;
}
} while (child);
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_HotKey()
{
INT ProcCount = 0;
for (int i = 0x70; i < 0x87; ++i)
{
if (RegisterHotKey(NULL, 1, MOD_ALT, i))
{
ProcCount = i - 0x70 + 1;
break;
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
HANDLE hMutex;
int ObjRecursion(std::wstring path, INT* ProcCount)
{
#define BUFFER_SIZE 0x1000
#define DIRECTORY_QUERY 0x0001
#define NTSTATUS ULONG
typedef struct _LSA_UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING;
typedef struct _OBJDIR_INFORMATION {
UNICODE_STRING ObjectName;
UNICODE_STRING ObjectTypeName;
BYTE Data[1];
} OBJDIR_INFORMATION, * POBJDIR_INFORMATION;
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length;
HANDLE RootDirectory;
UNICODE_STRING* ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES;
#define InitializeObjectAttributes( p, n, a, r, s ) { \
(p)->Length = sizeof( OBJECT_ATTRIBUTES ); \
(p)->RootDirectory = r; \
(p)->Attributes = a; \
(p)->ObjectName = n; \
(p)->SecurityDescriptor = s; \
(p)->SecurityQualityOfService = NULL; \
}
typedef DWORD(WINAPI* NTQUERYDIRECTORYOBJECT)(HANDLE, OBJDIR_INFORMATION*, DWORD, DWORD, DWORD, DWORD*, DWORD*);
NTQUERYDIRECTORYOBJECT NtQueryDirectoryObject;
typedef DWORD(WINAPI* NTOPENDIRECTORYOBJECT)(HANDLE*, DWORD, OBJECT_ATTRIBUTES*);
NTOPENDIRECTORYOBJECT NtOpenDirectoryObject;
// 创建一个 mutex
std::wstring SectionName = L"lysm_";
SectionName += std::to_wstring(GetCurrentProcessId());
if (!hMutex)
{
hMutex = CreateMutex(NULL, TRUE, SectionName.c_str());
}
HANDLE file_handle;
NTSTATUS status_code;
HMODULE hNtdll;
UNICODE_STRING unicode_str;
OBJECT_ATTRIBUTES path_attributes;
DWORD object_index = 0;
DWORD data_written = 0;
hNtdll = LoadLibrary(L"ntdll.dll");
if (!hNtdll)
return 0;
NtQueryDirectoryObject = (NTQUERYDIRECTORYOBJECT)GetProcAddress(hNtdll, "NtQueryDirectoryObject");
NtOpenDirectoryObject = (NTOPENDIRECTORYOBJECT)GetProcAddress(hNtdll, "NtOpenDirectoryObject");
unicode_str.Length = (USHORT)path.length() * 2;
unicode_str.MaximumLength = (USHORT)path.length() * 2 + 2;
unicode_str.Buffer = (PWSTR)path.c_str();
InitializeObjectAttributes(&path_attributes, &unicode_str, 0, NULL, NULL);
OBJDIR_INFORMATION* object_directory_info = (OBJDIR_INFORMATION*) ::HeapAlloc(GetProcessHeap(), 0, BUFFER_SIZE);
status_code = NtOpenDirectoryObject(&file_handle, DIRECTORY_QUERY, &path_attributes);
if (status_code != 0)
return 0;
status_code = NtQueryDirectoryObject(file_handle,
object_directory_info,
BUFFER_SIZE,
TRUE,
TRUE,
&object_index,
&data_written);
if (status_code != 0)
return 0;
do
{
if (!object_directory_info)
continue;
std::wstring cur_path = object_directory_info->ObjectName.Buffer;
std::wstring cur_type = object_directory_info->ObjectTypeName.Buffer;
std::wstring new_path;
if (path == L"\\")
{
new_path = path + cur_path;
}
else
{
new_path = path + L"\\" + cur_path;
}
if (cur_type == L"Directory") {
ObjRecursion(new_path, ProcCount);
}
if (cur_path.find(L"lysm_") != cur_path.npos)
{
*ProcCount += 1;
}
//printf("[%S] [%S] [%S] \n",
// path.c_str(),
// cur_type.c_str(),
// cur_path.c_str());
} while (NtQueryDirectoryObject(file_handle, object_directory_info,
BUFFER_SIZE, TRUE, FALSE, &object_index,
&data_written) == 0);
return 0;
}
void Enum_NtQueryDirectoryObject()
{
// 需要管理员启动才能遍历出所有内容
INT ProcCount = 0;
ObjRecursion(L"\\", &ProcCount);
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
return ;
}
void Enum_WindowFromPoint()
{
// WindowFromPhysicalPoint、RealChildWindowFromPoint 同理
std::vector<HWND> vecArr;
INT ProcCount = 0;
POINT point = { 0 };
HWND hwnd;
CHAR Title[MAX_PATH] = { 0 };
for (int i = 0; i < 1920; ++i)
{
for (int j = 0; j < 1080; j++)
{
point.x = i;
point.y = j;
hwnd = WindowFromPoint(point);
if (hwnd)
{
auto iter = std::find(vecArr.begin(), vecArr.end(), hwnd);
if (iter != vecArr.end())
{
continue;
}
vecArr.push_back(hwnd);
memset(Title, 0, MAX_PATH);
if (!GetWindowTextA(hwnd, Title, MAX_PATH))
{
continue;
}
if (Title[0] != L'\0')
{
//printf("%llx [%d,%d] %s \n", (ULONG_PTR)hwnd, i, j, Title);
if (strstr(Title, SELF_PROCNAME_A) != 0)
{
ProcCount++;
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid != GetCurrentProcessId())
{
// sbie 对窗口句柄的权限做了限制,调用 CloseWindow 并不能关闭窗口
//CloseWindow(hwnd);
}
}
}
}
}
}
if (ProcCount > 1)
{
spdlog::error("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
else
{
spdlog::info("[{}] ProcCount:{} ", __FUNCTION__, ProcCount);
}
}
void Enum_Test()
{
}