forked from 1401199262/AntiScreenCapture-r0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHideWindow.cpp
304 lines (239 loc) · 7.27 KB
/
HideWindow.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
#include "headers.h"
#include "HideWindow.h"
struct tag_thread_info
{
PETHREAD owning_thread;
};
struct tag_wnd
{
char pad_0[0x10];
tag_thread_info* thread_info;
};
UNICODE_STRING _ToUnicode(const char* str)
{
UNICODE_STRING ret;
ANSI_STRING ansi_str;
RtlInitAnsiString(&ansi_str, str);
RtlAnsiStringToUnicodeString(&ret, &ansi_str, TRUE);
return ret;
}
UNICODE_STRING _ToUnicode(const wchar_t* str)
{
UNICODE_STRING ret;
RtlInitUnicodeString(&ret, str);
return ret;
}
PVOID GetKernelBase(const char* szModuleName,PULONG pImageSize)
{
typedef struct _SYSTEM_MODULE_ENTRY
{
HANDLE Section;
PVOID MappedBase;
PVOID ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} SYSTEM_MODULE_ENTRY, * PSYSTEM_MODULE_ENTRY;
#pragma warning(disable:4200)
typedef struct _SYSTEM_MODULE_INFORMATION
{
ULONG Count;
SYSTEM_MODULE_ENTRY Module[0];
} SYSTEM_MODULE_INFORMATION, * PSYSTEM_MODULE_INFORMATION;
PVOID pModuleBase = NULL;
PSYSTEM_MODULE_INFORMATION pSystemInfoBuffer = NULL;
ULONG SystemInfoBufferSize = 0;
NTSTATUS status = ZwQuerySystemInformation(SystemModuleInformation,
&SystemInfoBufferSize,
0,
&SystemInfoBufferSize);
if (!SystemInfoBufferSize)
{
return NULL;
}
pSystemInfoBuffer = (PSYSTEM_MODULE_INFORMATION)ExAllocatePoolWithTag(NonPagedPool, SystemInfoBufferSize * 2,'1gaT');
if (!pSystemInfoBuffer)
{
return NULL;
}
memset(pSystemInfoBuffer, 0, SystemInfoBufferSize * 2);
status = ZwQuerySystemInformation(SystemModuleInformation,
pSystemInfoBuffer,
SystemInfoBufferSize * 2,
&SystemInfoBufferSize);
if (NT_SUCCESS(status))
{
for (int ModuleCount = 0; ModuleCount < pSystemInfoBuffer->Count; ModuleCount++)
{
char* ModuleFileName = (char*)pSystemInfoBuffer->Module[ModuleCount].FullPathName;
int l = strlen(ModuleFileName);
for (int i = l; i != 0; i--)
{
if (ModuleFileName[i] == '\\')
{
ModuleFileName = ModuleFileName + i + 1;
break;
}
}
if (_stricmp(szModuleName, ModuleFileName) == 0)
{
pModuleBase = pSystemInfoBuffer->Module[ModuleCount].ImageBase;
if (pImageSize)
*pImageSize = pSystemInfoBuffer->Module[ModuleCount].ImageSize;
break;
}
}
}
ExFreePoolWithTag(pSystemInfoBuffer,'1gaT');
return pModuleBase;
}
BOOLEAN GetNtUserSetWindowDisplayAffinity(PULONG64 Addr)
{
ULONG kernelSize;
ULONG_PTR win32kfullBase = (ULONG_PTR)GetKernelBase("win32kfull.sys", &kernelSize);
if (win32kfullBase == 0 || kernelSize == 0)
{
DbgPrint("[-] win32kfull.sys not geted \n");
return FALSE;
}
DbgPrint("[+] win32kfull.sys=%llx \n", win32kfullBase);
ULONG64 NtUserSetWindowDisplayAffinity = 0;
NtUserSetWindowDisplayAffinity = (ULONG64)RtlFindExportedRoutineByName((PVOID)win32kfullBase, "NtUserSetWindowDisplayAffinity");
*Addr = NtUserSetWindowDisplayAffinity;
return TRUE;
}
BOOLEAN GetZwUserSetWindowDisplayAffinity(PULONG64 Addr)
{
ULONG64 NtUserSetWindowDisplayAffinity = 0;
if (GetNtUserSetWindowDisplayAffinity(&NtUserSetWindowDisplayAffinity) && NtUserSetWindowDisplayAffinity)
{
//DbgBreakPoint();
DbgPrint("[+] NtUserSetWindowDisplayAffinity=%llx\n", NtUserSetWindowDisplayAffinity);
for (int SearchCount = 0; SearchCount < 0x100; SearchCount++)
{
//mov edx,esi |mov rcx,rdi |call ZwUserxxx
if (*(PULONG)(NtUserSetWindowDisplayAffinity + SearchCount) == 0x8B48D68B
&& *(PUSHORT)(NtUserSetWindowDisplayAffinity + SearchCount + 4) == 0xE8CF)
{
*Addr = NtUserSetWindowDisplayAffinity + SearchCount + 10 + *(PLONG)(NtUserSetWindowDisplayAffinity + SearchCount + 6);
return TRUE;
}
}
}
DbgPrint("[-] NtUserSetWindowDisplayAffinity not found \n");
return FALSE;
}
BOOLEAN GetChangeWindowTreeProtection(PULONG64 Addr)
{
ULONG64 ZwUserSetWindowDisplayAffinity = 0;
if (GetZwUserSetWindowDisplayAffinity(&ZwUserSetWindowDisplayAffinity) && ZwUserSetWindowDisplayAffinity)
{
DbgPrint("[+] ZwUserSetWindowDisplayAffinity=%llx\n", ZwUserSetWindowDisplayAffinity);
for (int SearchCount = 0; SearchCount < 0x200; SearchCount++)
{
//mov edx,xxx |mov rcx,rbx |call ChangeWindowTreeProtection
if (*(PUCHAR)(ZwUserSetWindowDisplayAffinity + SearchCount) == 0x8B
&& *(PULONG)(ZwUserSetWindowDisplayAffinity + SearchCount + 2) == 0xE8CB8B48)
{
*Addr = ZwUserSetWindowDisplayAffinity + SearchCount + 10 + *(PLONG)(ZwUserSetWindowDisplayAffinity + SearchCount + 6);
return TRUE;
}
}
}
DbgPrint("[-] ZwUserSetWindowDisplayAffinity not found \n");
return FALSE;
}
LONGLONG (NTAPI* ChangeWindowTreePro)(struct tag_wnd*, unsigned int) = 0;
tag_wnd* (*validate_hwnd)(HANDLE) = 0;
NTSTATUS init_function()
{
auto status = STATUS_SUCCESS;
PEPROCESS pepro = 0;
auto pid = GetProcessIdByProcessImageName("winlogon.exe");
if (pid)
{
if (NT_SUCCESS(PsLookupProcessByProcessId(pid, &pepro)))
{
KAPC_STATE apc = { 0 };
KeStackAttachProcess(pepro, &apc);
ULONG kernelSize;
auto win32kbase = (ULONG_PTR)GetKernelBase("win32kbase.sys", &kernelSize);
if (win32kbase == 0 || kernelSize == 0)
{
DbgPrint("[-] win32kbase.sys not geted\n");
status = STATUS_UNSUCCESSFUL;
goto cleanup;
}
if (GetChangeWindowTreeProtection((PULONG64)&ChangeWindowTreePro) && ChangeWindowTreePro)
{
DbgPrint("[+] ChangeWindowTreePro=%llx\n", ChangeWindowTreePro);
}
else
{
DbgPrint("[-] ChangeWindowTreePro not found\n");
status = STATUS_UNSUCCESSFUL;
goto cleanup;
}
validate_hwnd = (tag_wnd * (*)(HANDLE))RtlFindExportedRoutineByName((PVOID)win32kbase, "ValidateHwnd");
if (validate_hwnd)
{
DbgPrint("[+] validate_hwnd=%llx\n", validate_hwnd);
}
else
{
DbgPrint("[-] validate_hwnd not found\n");
status = STATUS_UNSUCCESSFUL;
goto cleanup;
}
status = STATUS_SUCCESS;
cleanup:
KeUnstackDetachProcess(&apc);
ObDereferenceObject(pepro);
return status;
}
}
DbgPrint("[-] winlogon.exe not found\n");
status = STATUS_UNSUCCESSFUL;
return status;
}
#define WDA_NONE 0x00000000
#define WDA_MONITOR 0x00000001
#define WDA_EXCLUDEFROMCAPTURE 0x00000011
LONGLONG ChangeWindowTreeProtection(HANDLE hwnd, ULONG ulAffinity)
{
auto window_instance = validate_hwnd(hwnd);
if (!window_instance)
{
DbgPrint("[!] invalid HWND\n");
return 0;
}
return ChangeWindowTreePro(window_instance, ulAffinity );
}
HANDLE GetProcessIdByProcessImageName(const char* process_name) {
ULONG buffer_size = 0;
ZwQuerySystemInformation(SystemProcessInformation, NULL, NULL, &buffer_size);
auto buffer = ExAllocatePoolWithTag(NonPagedPool, buffer_size, 'mder');
if (!buffer) {
return INVALID_HANDLE_VALUE;
}
HANDLE pid = INVALID_HANDLE_VALUE;
auto process_name_unicode = _ToUnicode(process_name);
auto process_info = (PSYSTEM_PROCESS_INFORMATION)buffer;
if (NT_SUCCESS(ZwQuerySystemInformation(SystemProcessInformation, process_info, buffer_size, NULL))) {
while (process_info->NextEntryOffset) {
//DbgPrint("Image: %ws\n", process_info->ImageName.Buffer);
if (!RtlCompareUnicodeString(&process_name_unicode, &process_info->ImageName, true))
{
pid = process_info->UniqueProcessId;
break;
}
process_info = (PSYSTEM_PROCESS_INFORMATION)((BYTE*)process_info + process_info->NextEntryOffset);
}
}
ExFreePoolWithTag(buffer, 'mder');
return pid;
}