-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdllmain.cpp
More file actions
434 lines (393 loc) · 18.1 KB
/
dllmain.cpp
File metadata and controls
434 lines (393 loc) · 18.1 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
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
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <psapi.h>
#include <iostream>
#include <tlhelp32.h>
#include <cstdlib>
#include <tchar.h>
#include <thread>
#include <vector>
#include <string>
#include <fstream>
#include <GL/gl.h>
#include <wincodec.h> // For WIC
#pragma comment(lib, "windowscodecs.lib") // Link against the WIC library
bool isFocusInstalled = false; // Global flag
// create global hWND variable.
HWND focusHWND = NULL;
HWND bringWindowToTopHWND = NULL;
HWND setWindowFocusHWND = NULL;
HWND setWindowFocushWndInsertAfter = NULL;
int setWindowFocusX = 0;
int setWindowFocusY = 0;
int setWindowFocuscx = 0;
int setWindowFocuscy = 0;
UINT setWindowFocusuFlags = 0;
BYTE originalBytesForGetForeground[5] = { 0 };
BYTE originalBytesForShowWindow[5] = { 0 };
BYTE originalBytesForSetWindowPos[5] = { 0 };
BYTE orginalBytesForSetFocus[5] = { 0 };
BYTE originalBytesForEmptyClipboard[5] = { 0 };
BYTE originalBytesForSetClipboardData[5] = { 0 };
BYTE originalBytesForTerminateProcess[5] = { 0 };
BYTE originalBytesForExitProcess[5] = { 0 };
// My custom functions
HWND WINAPI MyGetForegroundWindow();
BOOL WINAPI MyShowWindow(HWND hWnd);
BOOL WINAPI MySetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
HWND WINAPI MySetFocus(HWND hWnd);
HWND WINAPI MyGetWindow(HWND hWnd, UINT uCmd);
int WINAPI MyGetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount);
BOOL WINAPI MyK32EnumProcesses(DWORD * pProcessIds, DWORD cb, DWORD * pBytesReturned);
HANDLE WINAPI MyOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
BOOL WINAPI MyTerminateProcess(HANDLE hProcess, UINT uExitCode);
VOID WINAPI MyExitProcess(UINT uExitCode);
BOOL WINAPI MyEmptyClipboard();
HANDLE WINAPI MySetClipboardData(UINT uFormat, HANDLE hMem);
// Implement MySetClipboardData which does nothing
HANDLE WINAPI MySetClipboardData(UINT uFormat, HANDLE hMem) {
// This custom function does nothing
std::cout << "SetClipboardData hook called, but not setting clipboard data." << std::endl;
return NULL; // Indicate failure or that the data was not set
}
BOOL WINAPI MyEmptyClipboard() {
// This custom function pretends to clear the clipboard but does nothing
std::cout << "EmptyClipboard hook called, but not clearing the clipboard." << std::endl;
return TRUE; // Pretend success
}
HANDLE WINAPI MyOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) {
std::cout << "OpenProcess hook called, but not opening process." << std::endl;
return NULL;
}
BOOL WINAPI MyTerminateProcess(HANDLE hProcess, UINT uExitCode) {
std::cout << "TerminateProcess hook called, but not terminating process." << std::endl;
return TRUE; // Simulate success
}
VOID WINAPI MyExitProcess(UINT uExitCode) {
std::cout << "ExitProcess hook called, but not exiting process." << std::endl;
}
BOOL WINAPI MyK32EnumProcesses(DWORD * pProcessIds, DWORD cb, DWORD * pBytesReturned) {
// This custom function behaves as if no processes are running
std::cout << "K32EnumProcesses hook called, but pretending no processes exist." << std::endl;
if (pBytesReturned != NULL) {
*pBytesReturned = 0; // Indicate no processes were written to the buffer
}
return TRUE; // Indicate the function succeeded
}
int WINAPI MyGetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount) {
// This custom function behaves as if no window title is retrieved
std::cout << "GetWindowTextW hook called, but not returning actual window text." << std::endl;
if (nMaxCount > 0) {
lpString[0] = L'\0'; // Return an empty string
}
return 0; // Indicate that no characters were copied to the buffer
}
BOOL WINAPI MySetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags) {
setWindowFocusHWND = hWnd;
setWindowFocushWndInsertAfter = hWndInsertAfter;
setWindowFocusX = X;
setWindowFocusY = Y;
setWindowFocuscx = cx;
setWindowFocuscy = cy;
setWindowFocusuFlags = uFlags;
// This custom function does nothing
std::cout << "SetWindowPos hook called, but not changing window position." << std::endl;
return TRUE; // Pretend success
}
BOOL WINAPI MyShowWindow(HWND hWnd) {
bringWindowToTopHWND = hWnd;
// This custom function does nothing
std::cout << "ShowWindow hook called, but not bringing window to top." << std::endl;
return TRUE; // Pretend success
}
HWND WINAPI MyGetWindow(HWND hWnd, UINT uCmd) {
// This custom function behaves as if there are no windows to return
std::cout << "GetWindow hook called, but pretending no related window." << std::endl;
return NULL; // Indicate no window found
}
void InstallHook() {
std::cout << "Installing hooks..." << std::endl;
DWORD oldProtect;
// Hook EmptyClipboard
HMODULE hUser32 = GetModuleHandle(L"user32.dll");
if (hUser32) {
void* targetEmptyClipboard = GetProcAddress(hUser32, "EmptyClipboard");
if (targetEmptyClipboard) {
DWORD jumpEmptyClipboard = (DWORD)MyEmptyClipboard - (DWORD)targetEmptyClipboard - 5;
memcpy(originalBytesForEmptyClipboard, targetEmptyClipboard, sizeof(originalBytesForEmptyClipboard));
if (VirtualProtect(targetEmptyClipboard, sizeof(originalBytesForEmptyClipboard), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetEmptyClipboard) = 0xE9;
*((DWORD*)((BYTE*)targetEmptyClipboard + 1)) = jumpEmptyClipboard;
VirtualProtect(targetEmptyClipboard, sizeof(originalBytesForEmptyClipboard), oldProtect, &oldProtect);
}
}
}
// Hook GetForegroundWindow
if (hUser32) {
void* targetGetForegroundWindow = GetProcAddress(hUser32, "GetForegroundWindow");
if (targetGetForegroundWindow) {
DWORD jumpGetForeground = (DWORD)MyGetForegroundWindow - (DWORD)targetGetForegroundWindow - 5;
memcpy(originalBytesForGetForeground, targetGetForegroundWindow, sizeof(originalBytesForGetForeground));
if (VirtualProtect(targetGetForegroundWindow, sizeof(originalBytesForGetForeground), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetGetForegroundWindow) = 0xE9;
*((DWORD*)((BYTE*)targetGetForegroundWindow + 1)) = jumpGetForeground;
VirtualProtect(targetGetForegroundWindow, sizeof(originalBytesForGetForeground), oldProtect, &oldProtect);
}
}
}
// Hook TerminateProcess
HMODULE hKernel32 = GetModuleHandle(L"kernel32.dll");
if (hKernel32) {
void* targetTerminateProcess = GetProcAddress(hKernel32, "TerminateProcess");
if (targetTerminateProcess) {
DWORD jumpTerminateProcess = ((DWORD)MyTerminateProcess - (DWORD)targetTerminateProcess - 5);
memcpy(originalBytesForTerminateProcess, targetTerminateProcess, sizeof(originalBytesForTerminateProcess));
if (VirtualProtect(targetTerminateProcess, sizeof(originalBytesForTerminateProcess), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetTerminateProcess) = 0xE9;
*((DWORD*)((BYTE*)targetTerminateProcess + 1)) = jumpTerminateProcess;
VirtualProtect(targetTerminateProcess, sizeof(originalBytesForTerminateProcess), oldProtect, &oldProtect);
}
}
}
// Hook ExitProcess
if (hKernel32) {
void* targetExitProcess = GetProcAddress(hKernel32, "ExitProcess");
if (targetExitProcess) {
DWORD jumpExitProcess = ((DWORD)MyExitProcess - (DWORD)targetExitProcess - 5);
memcpy(originalBytesForExitProcess, targetExitProcess, sizeof(originalBytesForExitProcess));
if (VirtualProtect(targetExitProcess, sizeof(originalBytesForExitProcess), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetExitProcess) = 0xE9;
*((DWORD*)((BYTE*)targetExitProcess + 1)) = jumpExitProcess;
VirtualProtect(targetExitProcess, sizeof(originalBytesForExitProcess), oldProtect, &oldProtect);
}
}
}
// create a message box to show the dll is loaded
MessageBox(NULL, L"Injected :)", L"UndownUnlock", MB_OK);
}
void InstallFocus() {
if (isFocusInstalled) {
return;
}
isFocusInstalled = true;
std::cout << "Installing focus..." << std::endl;
DWORD oldProtect;
// Hook BringWindowToTop
HMODULE hUser32 = GetModuleHandle(L"user32.dll");
if (hUser32) {
void* targetShowWindow = GetProcAddress(hUser32, "BringWindowToTop");
if (targetShowWindow) {
DWORD jumpBringWindowToTop = (DWORD)MyShowWindow - (DWORD)targetShowWindow - 5;
memcpy(originalBytesForShowWindow, targetShowWindow, sizeof(originalBytesForShowWindow));
if (VirtualProtect(targetShowWindow, sizeof(originalBytesForShowWindow), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetShowWindow) = 0xE9;
*((DWORD*)((BYTE*)targetShowWindow + 1)) = jumpBringWindowToTop;
VirtualProtect(targetShowWindow, sizeof(originalBytesForShowWindow), oldProtect, &oldProtect);
}
}
}
// Hook SetWindowPos
if (hUser32) {
void* targetSetWindowPos = GetProcAddress(hUser32, "SetWindowPos");
if (targetSetWindowPos) {
DWORD jumpSetWindowPos = (DWORD)MySetWindowPos - (DWORD)targetSetWindowPos - 5;
memcpy(originalBytesForSetWindowPos, targetSetWindowPos, sizeof(originalBytesForSetWindowPos));
if (VirtualProtect(targetSetWindowPos, sizeof(originalBytesForSetWindowPos), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetSetWindowPos) = 0xE9;
*((DWORD*)((BYTE*)targetSetWindowPos + 1)) = jumpSetWindowPos;
VirtualProtect(targetSetWindowPos, sizeof(originalBytesForSetWindowPos), oldProtect, &oldProtect);
}
}
}
// Hook SetFocus
if (hUser32) {
void* targetSetFocus = GetProcAddress(hUser32, "SetFocus");
if (targetSetFocus) {
DWORD jumpSetFocus = (DWORD)MySetFocus - (DWORD)targetSetFocus - 5;
memcpy(orginalBytesForSetFocus, targetSetFocus, sizeof(orginalBytesForSetFocus));
if (VirtualProtect(targetSetFocus, sizeof(orginalBytesForSetFocus), PAGE_EXECUTE_READWRITE, &oldProtect)) {
*((BYTE*)targetSetFocus) = 0xE9;
*((DWORD*)((BYTE*)targetSetFocus + 1)) = jumpSetFocus;
VirtualProtect(targetSetFocus, sizeof(orginalBytesForSetFocus), oldProtect, &oldProtect);
}
}
}
}
void UninstallFocus() {
if (!isFocusInstalled) {
return;
}
isFocusInstalled = false;
std::cout << "Uninstalling focus..." << std::endl;
DWORD oldProtect;
// Unhook BringWindowToTop
HMODULE hUser32 = GetModuleHandle(L"user32.dll");
if (hUser32) {
void* targetShowWindow = GetProcAddress(hUser32, "BringWindowToTop");
if (targetShowWindow) {
if (VirtualProtect(targetShowWindow, sizeof(originalBytesForShowWindow), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetShowWindow, originalBytesForShowWindow, sizeof(originalBytesForShowWindow));
VirtualProtect(targetShowWindow, sizeof(originalBytesForShowWindow), oldProtect, &oldProtect);
}
}
}
// Unhook SetWindowPos
if (hUser32) {
void* targetSetWindowPos = GetProcAddress(hUser32, "SetWindowPos");
if (targetSetWindowPos) {
if (VirtualProtect(targetSetWindowPos, sizeof(originalBytesForSetWindowPos), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetSetWindowPos, originalBytesForSetWindowPos, sizeof(originalBytesForSetWindowPos));
VirtualProtect(targetSetWindowPos, sizeof(originalBytesForSetWindowPos), oldProtect, &oldProtect);
}
}
}
// Unhook SetFocus
if (hUser32) {
void* targetSetFocus = GetProcAddress(hUser32, "SetFocus");
if (targetSetFocus) {
if (VirtualProtect(targetSetFocus, sizeof(orginalBytesForSetFocus), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetSetFocus, orginalBytesForSetFocus, sizeof(orginalBytesForSetFocus));
VirtualProtect(targetSetFocus, sizeof(orginalBytesForSetFocus), oldProtect, &oldProtect);
}
}
}
}
void UninstallHook() {
DWORD oldProtect;
// Unhook SetClipboardData
HMODULE hUser32 = GetModuleHandle(L"user32.dll");
if (hUser32) {
void* targetSetClipboardData = GetProcAddress(hUser32, "SetClipboardData");
if (targetSetClipboardData) {
if (VirtualProtect(targetSetClipboardData, sizeof(originalBytesForSetClipboardData), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetSetClipboardData, originalBytesForSetClipboardData, sizeof(originalBytesForSetClipboardData));
VirtualProtect(targetSetClipboardData, sizeof(originalBytesForSetClipboardData), oldProtect, &oldProtect);
}
}
}
// Unhook EmptyClipboard
if (hUser32) {
void* targetEmptyClipboard = GetProcAddress(hUser32, "EmptyClipboard");
if (targetEmptyClipboard) {
if (VirtualProtect(targetEmptyClipboard, sizeof(originalBytesForEmptyClipboard), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetEmptyClipboard, originalBytesForEmptyClipboard, sizeof(originalBytesForEmptyClipboard));
VirtualProtect(targetEmptyClipboard, sizeof(originalBytesForEmptyClipboard), oldProtect, &oldProtect);
}
}
}
// Unhook GetForegroundWindow
if (hUser32) {
void* targetGetForegroundWindow = GetProcAddress(hUser32, "GetForegroundWindow");
if (targetGetForegroundWindow) {
if (VirtualProtect(targetGetForegroundWindow, sizeof(originalBytesForGetForeground), PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(targetGetForegroundWindow, originalBytesForGetForeground, sizeof(originalBytesForGetForeground));
VirtualProtect(targetGetForegroundWindow, sizeof(originalBytesForGetForeground), oldProtect, &oldProtect);
}
}
}
}
// Helper function to determine if the given window is the main window of the current process
BOOL IsMainWindow(HWND handle) {
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
// Callback function for EnumWindows
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam) {
DWORD processID = 0;
GetWindowThreadProcessId(handle, &processID);
if (GetCurrentProcessId() == processID && IsMainWindow(handle)) {
// Stop enumeration if a main window is found, and return its handle
*reinterpret_cast<HWND*>(lParam) = handle;
return FALSE;
}
return TRUE;
}
// Function to find the main window of the current process
HWND FindMainWindow() {
HWND mainWindow = NULL;
EnumWindows(EnumWindowsCallback, reinterpret_cast<LPARAM>(&mainWindow));
return mainWindow;
}
// Your hook function implementation
HWND WINAPI MyGetForegroundWindow() {
HWND hWnd = FindMainWindow();
if (hWnd != NULL) {
std::cout << "Returning the main window of the current application." << std::endl;
return hWnd;
}
std::cout << "Main window not found, returning NULL." << std::endl;
return NULL;
}
HWND WINAPI MySetFocus(HWND _hWnd) {
focusHWND = _hWnd;
HWND hWnd = FindMainWindow(); // Find the main window of the current process
if (hWnd != NULL) {
std::cout << "Returning the main window of the current application due to '[' key press." << std::endl;
return hWnd; // Return the main window handle if found
}
else {
std::cout << "Main window not found, returning NULL." << std::endl;
return NULL; // If main window is not found, return NULL
}
}
// Keyboard hook handle
HHOOK hKeyboardHook;
// Keyboard hook callback
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
switch (p->vkCode) {
// VK_UP is the virtual key code for the Up arrow key
case VK_UP:
//CaptureOpenGLScreen();
InstallFocus();
std::cout << "Up arrow key pressed, installing focus hook." << std::endl;
break;
// VK_DOWN is the virtual key code for the Down arrow key
case VK_DOWN:
UninstallFocus();
// call the set focus with the focusHWND
if (focusHWND != NULL) {
SetFocus(focusHWND);
}
if (bringWindowToTopHWND != NULL) {
BringWindowToTop(bringWindowToTopHWND);
}
if (setWindowFocusHWND != NULL) {
SetWindowPos(setWindowFocusHWND, setWindowFocushWndInsertAfter, setWindowFocusX, setWindowFocusY, setWindowFocuscx, setWindowFocuscy, setWindowFocusuFlags);
}
std::cout << "Down arrow key pressed, uninstalling focus hook." << std::endl;
break;
}
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
// Place this in a suitable location in your existing code.
void SetupKeyboardHook() {
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, nullptr, 0);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hModule);
// Start the hook setup in a new thread to keep DllMain non-blocking
InstallHook(); // Sets up your custom hooks
std::thread([]() {
SetupKeyboardHook(); // Sets up the keyboard hook
}).detach();
break;
case DLL_PROCESS_DETACH:
UninstallHook(); // Uninstall your custom hooks
if (hKeyboardHook != nullptr) {
UnhookWindowsHookEx(hKeyboardHook); // Uninstall the keyboard hook
}
break;
}
return TRUE;
}