-
Notifications
You must be signed in to change notification settings - Fork 15
/
hook_window.c
375 lines (322 loc) · 9.89 KB
/
hook_window.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
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2015 Cuckoo Sandbox Developers, Optiv, Inc. ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "ntapi.h"
#include "hooking.h"
#include "misc.h"
#include "pipe.h"
#include "log.h"
typedef DWORD (WINAPI * __GetWindowThreadProcessId)(
__in HWND hWnd,
__out_opt LPDWORD lpdwProcessId
);
__GetWindowThreadProcessId _GetWindowThreadProcessId;
DWORD WINAPI our_GetWindowThreadProcessId(
__in HWND hWnd,
__out_opt LPDWORD lpdwProcessId
) {
lasterror_t lasterror;
DWORD ret;
get_lasterrors(&lasterror);
if (!_GetWindowThreadProcessId) {
_GetWindowThreadProcessId = (__GetWindowThreadProcessId)GetProcAddress(LoadLibraryA("user32"), "GetWindowThreadProcessId");
}
ret = _GetWindowThreadProcessId(hWnd, lpdwProcessId);
set_lasterrors(&lasterror);
return ret;
}
typedef DWORD(WINAPI * __GetClassNameA)(
_In_ HWND hWnd,
_Out_ LPTSTR lpClassName,
_In_ int nMaxCount
);
__GetClassNameA _GetClassNameA;
DWORD WINAPI our_GetClassNameA(
_In_ HWND hWnd,
_Out_ LPSTR lpClassName,
_In_ int nMaxCount
) {
lasterror_t lasterror;
DWORD ret;
get_lasterrors(&lasterror);
if (!_GetClassNameA) {
_GetClassNameA = (__GetClassNameA)GetProcAddress(LoadLibraryA("user32"), "GetClassNameA");
}
ret = _GetClassNameA(hWnd, lpClassName, nMaxCount);
set_lasterrors(&lasterror);
return ret;
}
HOOKDEF(HWND, WINAPI, FindWindowA,
__in_opt LPCTSTR lpClassName,
__in_opt LPCTSTR lpWindowName
) {
// The atom must be in the low-order word of lpClassName;
// the high-order word must be zero (from MSDN documentation.)
HWND ret = Old_FindWindowA(lpClassName, lpWindowName);
if(((DWORD_PTR) lpClassName & 0xffff) == (DWORD_PTR) lpClassName) {
LOQ_nonnull("windows", "is", "ClassName", lpClassName, "WindowName", lpWindowName);
}
else {
LOQ_nonnull("windows", "ss", "ClassName", lpClassName, "WindowName", lpWindowName);
}
return ret;
}
HOOKDEF(HWND, WINAPI, FindWindowW,
__in_opt LPWSTR lpClassName,
__in_opt LPWSTR lpWindowName
) {
HWND ret = Old_FindWindowW(lpClassName, lpWindowName);
if(((DWORD_PTR) lpClassName & 0xffff) == (DWORD_PTR) lpClassName) {
LOQ_nonnull("windows", "iu", "ClassName", lpClassName, "WindowName", lpWindowName);
}
else {
LOQ_nonnull("windows", "uu", "ClassName", lpClassName, "WindowName", lpWindowName);
}
return ret;
}
HOOKDEF(HWND, WINAPI, FindWindowExA,
__in_opt HWND hwndParent,
__in_opt HWND hwndChildAfter,
__in_opt LPCTSTR lpszClass,
__in_opt LPCTSTR lpszWindow
) {
HWND ret = Old_FindWindowExA(hwndParent, hwndChildAfter, lpszClass,
lpszWindow);
// lpszClass can be one of the predefined window controls.. which lay in
// the 0..ffff range
if(((DWORD_PTR) lpszClass & 0xffff) == (DWORD_PTR) lpszClass) {
LOQ_nonnull("windows", "is", "ClassName", lpszClass, "WindowName", lpszWindow);
}
else {
LOQ_nonnull("windows", "ss", "ClassName", lpszClass, "WindowName", lpszWindow);
}
return ret;
}
HOOKDEF(HWND, WINAPI, FindWindowExW,
__in_opt HWND hwndParent,
__in_opt HWND hwndChildAfter,
__in_opt LPWSTR lpszClass,
__in_opt LPWSTR lpszWindow
) {
HWND ret = Old_FindWindowExW(hwndParent, hwndChildAfter, lpszClass,
lpszWindow);
// lpszClass can be one of the predefined window controls.. which lay in
// the 0..ffff range
if(((DWORD_PTR) lpszClass & 0xffff) == (DWORD_PTR) lpszClass) {
LOQ_nonnull("windows", "iu", "ClassName", lpszClass, "WindowName", lpszWindow);
}
else {
LOQ_nonnull("windows", "uu", "ClassName", lpszClass, "WindowName", lpszWindow);
}
return ret;
}
HOOKDEF(BOOL, WINAPI, SendNotifyMessageA,
_In_ HWND hWnd,
_In_ UINT Msg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
BOOL ret;
/*
DWORD pid;
lasterror_t lasterror;
get_lasterrors(&lasterror);
GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId())
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
set_lasterrors(&lasterror);
*/
ret = Old_SendNotifyMessageA(hWnd, Msg, wParam, lParam);
LOQ_bool("windows", "ph", "WindowHandle", hWnd, "Message", Msg);
return ret;
}
HOOKDEF(BOOL, WINAPI, SendNotifyMessageW,
_In_ HWND hWnd,
_In_ UINT Msg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
BOOL ret;
/*
DWORD pid;
lasterror_t lasterror;
get_lasterrors(&lasterror);
GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId())
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
set_lasterrors(&lasterror);
*/
ret = Old_SendNotifyMessageW(hWnd, Msg, wParam, lParam);
LOQ_bool("windows", "ph", "WindowHandle", hWnd, "Message", Msg);
return ret;
}
HOOKDEF(LONG, WINAPI, SetWindowLongA,
_In_ HWND hWnd,
_In_ int nIndex,
_In_ LONG dwNewLong
) {
DWORD pid;
lasterror_t lasterror;
LONG ret;
BOOL isbad = FALSE;
get_lasterrors(&lasterror);
our_GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId()) {
char classname[1024];
our_GetClassNameA(hWnd, classname, sizeof(classname));
if (!stricmp(classname, "Shell_TrayWnd") && nIndex == 0) {
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
isbad = TRUE;
}
}
set_lasterrors(&lasterror);
ret = Old_SetWindowLongA(hWnd, nIndex, dwNewLong);
if (isbad)
LOQ_nonzero("windows", "pip", "WindowHandle", hWnd, "Index", nIndex, "NewLong", dwNewLong);
return ret;
}
HOOKDEF(LONG_PTR, WINAPI, SetWindowLongPtrA,
_In_ HWND hWnd,
_In_ int nIndex,
_In_ LONG_PTR dwNewLong
) {
DWORD pid;
lasterror_t lasterror;
LONG_PTR ret;
BOOL isbad = FALSE;
get_lasterrors(&lasterror);
our_GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId()) {
char classname[1024];
our_GetClassNameA(hWnd, classname, sizeof(classname));
if (!stricmp(classname, "Shell_TrayWnd") && nIndex == 0) {
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
isbad = TRUE;
}
}
set_lasterrors(&lasterror);
ret = Old_SetWindowLongPtrA(hWnd, nIndex, dwNewLong);
if (isbad)
LOQ_nonzero("windows", "pip", "WindowHandle", hWnd, "Index", nIndex, "NewLong", dwNewLong);
return ret;
}
HOOKDEF(LONG, WINAPI, SetWindowLongW,
_In_ HWND hWnd,
_In_ int nIndex,
_In_ LONG dwNewLong
) {
DWORD pid;
lasterror_t lasterror;
LONG ret;
BOOL isbad = FALSE;
get_lasterrors(&lasterror);
our_GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId()) {
char classname[1024];
our_GetClassNameA(hWnd, classname, sizeof(classname));
if (!stricmp(classname, "Shell_TrayWnd") && nIndex == 0) {
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
isbad = TRUE;
}
}
set_lasterrors(&lasterror);
ret = Old_SetWindowLongW(hWnd, nIndex, dwNewLong);
if (isbad)
LOQ_nonzero("windows", "pip", "WindowHandle", hWnd, "Index", nIndex, "NewLong", dwNewLong);
return ret;
}
HOOKDEF(LONG_PTR, WINAPI, SetWindowLongPtrW,
_In_ HWND hWnd,
_In_ int nIndex,
_In_ LONG_PTR dwNewLong
) {
DWORD pid;
lasterror_t lasterror;
LONG_PTR ret;
BOOL isbad = FALSE;
get_lasterrors(&lasterror);
our_GetWindowThreadProcessId(hWnd, &pid);
if (pid != GetCurrentProcessId()) {
char classname[1024];
our_GetClassNameA(hWnd, classname, sizeof(classname));
if (!stricmp(classname, "Shell_TrayWnd") && nIndex == 0) {
pipe("PROCESS:%d:%d", is_suspended(pid, 0), pid);
isbad = TRUE;
}
}
set_lasterrors(&lasterror);
ret = Old_SetWindowLongPtrW(hWnd, nIndex, dwNewLong);
if (isbad)
LOQ_nonzero("windows", "pip", "WindowHandle", hWnd, "Index", nIndex, "NewLong", dwNewLong);
return ret;
}
HOOKDEF(BOOL, WINAPI, EnumWindows,
_In_ WNDENUMPROC lpEnumFunc,
_In_ LPARAM lParam
) {
BOOL ret = Old_EnumWindows(lpEnumFunc, lParam);
LOQ_bool("windows", "");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, CreateWindowExA,
__in DWORD dwExStyle,
__in_opt LPCSTR lpClassName,
__in_opt LPCSTR lpWindowName,
__in DWORD dwStyle,
__in int x,
__in int y,
__in int nWidth,
__in int nHeight,
__in_opt HWND hWndParent,
__in_opt HMENU hMenu,
__in_opt HINSTANCE hInstance,
__in_opt LPVOID lpParam
) {
HWND ret = (HWND)1;
// lpClassName can be one of the predefined window controls.. which lay in
// the 0..ffff range
if (((DWORD_PTR)lpClassName & 0xffff) == (DWORD_PTR)lpClassName) {
LOQ_nonnull("windows", "isiiiih", "ClassName", lpClassName, "WindowName", lpWindowName, "x", x, "y", y, "Width", nWidth, "Height", nHeight, "Style", dwStyle);
}
else {
LOQ_nonnull("windows", "ssiiiih", "ClassName", lpClassName, "WindowName", lpWindowName, "x", x, "y", y, "Width", nWidth, "Height", nHeight, "Style", dwStyle);
}
return 0;
}
HOOKDEF_NOTAIL(WINAPI, CreateWindowExW,
__in DWORD dwExStyle,
__in_opt LPWSTR lpClassName,
__in_opt LPWSTR lpWindowName,
__in DWORD dwStyle,
__in int x,
__in int y,
__in int nWidth,
__in int nHeight,
__in_opt HWND hWndParent,
__in_opt HMENU hMenu,
__in_opt HINSTANCE hInstance,
__in_opt LPVOID lpParam
) {
HWND ret = (HWND)1;
// lpClassName can be one of the predefined window controls.. which lay in
// the 0..ffff range
if (((DWORD_PTR)lpClassName & 0xffff) == (DWORD_PTR)lpClassName) {
LOQ_nonnull("windows", "iuiiiih", "ClassName", lpClassName, "WindowName", lpWindowName, "x", x, "y", y, "Width", nWidth, "Height", nHeight, "Style", dwStyle);
}
else {
LOQ_nonnull("windows", "uuiiiih", "ClassName", lpClassName, "WindowName", lpWindowName, "x", x, "y", y, "Width", nWidth, "Height", nHeight, "Style", dwStyle);
}
return 0;
}