forked from ldebug/BetaShield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiddenProcessScan.cpp
More file actions
216 lines (166 loc) · 6.37 KB
/
Copy pathHiddenProcessScan.cpp
File metadata and controls
216 lines (166 loc) · 6.37 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
#include "ProjectMain.h"
#include "Main.h"
#include "Scan.h"
#include "DynamicWinapi.h"
#include "Functions.h"
#include "CLog.h"
#include "VersionHelpers.h"
struct ProcInfo {
HWND hWnd;
DWORD dwProcId;
CHAR szTitle[255];
CHAR szClass[255];
};
void CScan::CheckHiddenProcesses()
{
HWND hWnd;
DWORD dwProcId;
ProcInfo mProc[255];
int mIdList[255];
int nCount = 0;
int nPID = 0;
int i = 0;
hWnd = FindWindowA(0, 0);
while (hWnd > 0)
{
if (GetParent(hWnd) == 0)
{
GetWindowThreadProcessId(hWnd, &dwProcId);
if (!OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcId))
{
mProc[nCount].hWnd = hWnd;
mProc[nCount].dwProcId = dwProcId;
GetWindowTextA(hWnd, mProc[nCount].szTitle, 255);
GetClassNameA(hWnd, mProc[nCount].szClass, 255);
nCount++;
for (i = 0; i < nPID; i++)
if (dwProcId == mIdList[i])
break;
if (i == nPID)
mIdList[nPID++] = dwProcId;
}
}
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
}
if (nCount <= 0)
return;
for (i = 0; i < nCount; i++)
{
if (IsWindow(mProc[i].hWnd) == FALSE)
continue;
if (IsWindowVisible(mProc[i].hWnd) == FALSE)
continue;
if (mProc[i].dwProcId != GetCurrentProcessId())
{
CHAR __closestart[] = { 'H', 'i', 'd', 'd', 'e', 'n', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ':', ' ', '%', 's', '|', '%', 's', '(', '%', 'u', ')', ' ', 't', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'i', 'n', 'g', '!', 0x0 }; // Hidden process: %s|%s(%u) terminating!
CHAR __notclosed[] = { 'H', 'i', 'd', 'd', 'e', 'n', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 'c', 'a', 'n', ' ', 'n', 'o', 't', ' ', 't', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'e', 'd', '!', 0x0 }; // Hidden process can not terminated!
LPLog->ErrorLog(0, __closestart,
mProc[i].szClass, mProc[i].szTitle, mProc[i].dwProcId);
BetaFunctionTable->WinStationTerminateProcess(NULL, mProc[i].dwProcId, DBG_TERMINATE_PROCESS);
BetaFunctionTable->EndTask(mProc[i].hWnd, FALSE, TRUE);
BetaFunctionTable->Sleep(100);
if (LPFunctions->ProcessIsItAlive(mProc[i].dwProcId))
LPFunctions->CloseProcess(__notclosed, false, "");
}
}
}
#if 0
#define DUPLICATE_SAME_ATTRIBUTES 0x00000004
BOOL IsProcessFound(DWORD dwProcessId, PSYSTEM_PROCESS_INFORMATION pInfos)
{
PSYSTEM_PROCESS_INFORMATION pCurrent = pInfos;
while (TRUE) {
if ((DWORD)pCurrent->UniqueProcessId == dwProcessId)
return TRUE;
if (pCurrent->NextEntryOffset == 0)
break;
pCurrent = (PSYSTEM_PROCESS_INFORMATION)((DWORD_PTR)pCurrent + pCurrent->NextEntryOffset);
}
return FALSE;
}
void CScan::CheckHiddenProcesses()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Check hidden process event has been started");
#endif
DWORD dwLen = sizeof(SYSTEM_PROCESS_INFORMATION);
PSYSTEM_PROCESS_INFORMATION pProcessInfos = (PSYSTEM_PROCESS_INFORMATION)malloc(dwLen);
while (pProcessInfos) {
NTSTATUS status = BetaFunctionTable->NtQuerySystemInformation(SystemProcessInformation, pProcessInfos, dwLen, &dwLen);
if (NT_SUCCESS(status))
break;
else if (status != STATUS_INFO_LENGTH_MISMATCH) {
free(pProcessInfos);
return;
}
free(pProcessInfos);
pProcessInfos = (PSYSTEM_PROCESS_INFORMATION)malloc(dwLen);
}
if (!pProcessInfos)
return;
dwLen = sizeof(SYSTEM_HANDLE_INFORMATION);
PSYSTEM_HANDLE_INFORMATION pHandleInfos = (PSYSTEM_HANDLE_INFORMATION)malloc(dwLen);
while (pHandleInfos) {
NTSTATUS status = BetaFunctionTable->NtQuerySystemInformation(SystemHandleInformation, pHandleInfos, dwLen, &dwLen);
if (NT_SUCCESS(status))
break;
else if (status != STATUS_INFO_LENGTH_MISMATCH) {
free(pHandleInfos);
return;
}
free(pHandleInfos);
pHandleInfos = (PSYSTEM_HANDLE_INFORMATION)malloc(dwLen);
}
if (!pHandleInfos)
return;
POBJECT_TYPE_INFORMATION pType = (POBJECT_TYPE_INFORMATION)malloc(4096);
if (!pType) {
free(pHandleInfos);
free(pProcessInfos);
return;
}
for (ULONG i = 0; i < pHandleInfos->HandleCount; i++) {
DWORD dwOwner = pHandleInfos->Handles[i].ProcessId;
HANDLE hHandle = (HANDLE)pHandleInfos->Handles[i].Handle;
HANDLE hOwner = BetaFunctionTable->OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwOwner);
if (hOwner == NULL)
continue;
// we duplicate the handle so we can query it
HANDLE hHandleLocal = NULL;
NTSTATUS status = BetaFunctionTable->NtDuplicateObject(hOwner, hHandle, GetCurrentProcess(), &hHandleLocal, 0, 0, DUPLICATE_SAME_ACCESS | DUPLICATE_SAME_ATTRIBUTES);
if (!NT_SUCCESS(status))
continue;
status = BetaFunctionTable->NtQueryObject(hHandleLocal, ObjectTypeInformation, pType, 4096, NULL);
if (!NT_SUCCESS(status))
continue;
WCHAR __process[] = { L'P', L'r', L'o', L'c', L'e', L's', L's', L'\0' }; // Process
if (pType->TypeName.Buffer && wcscmp(pType->TypeName.Buffer, __process) == 0)
{
DWORD dwProcessId = LPFunctions->GetProcessIdNative(hHandleLocal);
if (!IsProcessFound(dwProcessId, pProcessInfos))
{
std::string szProcessName = LPFunctions->GetProcessFullName(hHandleLocal);
std::string szRealName = LPFunctions->DosDevicePath2LogicalPath(szProcessName.c_str());
HWND hwTargetWindow = LPFunctions->FindWindowFromProcessId(dwProcessId);
//CHAR __conhostexe[] = { 'c', 'o', 'n', 'h', 'o', 's', 't', '.', 'e', 'x', 'e', 0x0 }; // conhost.exe
//if (LPData->GetGameCode() == TEST_CONSOLE && strstr(szRealName.c_str(), __conhostexe))
// continue;
CHAR __closestart[] = { 'H', 'i', 'd', 'd', 'e', 'n', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ':', ' ', '%', 's', '(', '%', 'u', ')', ' ', 't', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'i', 'n', 'g', '!', 0x0 }; // Hidden process: %s(%u) terminating!
//CHAR __notclosed[] = { 'H', 'i', 'd', 'd', 'e', 'n', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 'c', 'a', 'n', ' ', 'n', 'o', 't', ' ', 't', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'e', 'd', '!', 0x0 }; // Hidden process can not terminated!
LPLog->ErrorLog(0, __closestart, szRealName.empty() ? "None" : szRealName.c_str(), dwProcessId);
//BetaFunctionTable->WinStationTerminateProcess(NULL, dwProcessId, DBG_TERMINATE_PROCESS);
//BetaFunctionTable->EndTask(hwTargetWindow, FALSE, TRUE);
//BetaFunctionTable->NtTerminateProcess(hHandleLocal, 0);
//BetaFunctionTable->Sleep(100);
//if (LPFunctions->ProcessIsItAlive(dwProcessId))
// LPFunctions->CloseProcess(__notclosed, false, "");
}
}
BetaFunctionTable->CloseHandle(hOwner);
}
free(pType);
free(pHandleInfos);
free(pProcessInfos);
LPLog->AddLog(0, "Check hidden process event completed!");
}
#endif