-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_scrollbug.cpp
More file actions
271 lines (229 loc) · 7.77 KB
/
fix_scrollbug.cpp
File metadata and controls
271 lines (229 loc) · 7.77 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
#define _WIN32_WINNT 0x0400
#define WIN32
#define NT
#define DEBUG 0
#include <windows.h>
#include <stdio.h>
#include "detours.h"
#define PULONG_PTR PVOID
#define PLONG_PTR PVOID
#define ULONG_PTR PVOID
#define ENUMRESNAMEPROCA PVOID
#define ENUMRESNAMEPROCW PVOID
#define ENUMRESLANGPROCA PVOID
#define ENUMRESLANGPROCW PVOID
#define ENUMRESTYPEPROCA PVOID
#define ENUMRESTYPEPROCW PVOID
#define STGOPTIONS PVOID
static HMODULE s_hInst = NULL;
static CHAR s_szDllPath[MAX_PATH];
extern "C" {
HANDLE (WINAPI *
Real_CreateFileW)(LPCWSTR a0,
DWORD a1,
DWORD a2,
LPSECURITY_ATTRIBUTES a3,
DWORD a4,
DWORD a5,
HANDLE a6)
= CreateFileW;
BOOL (WINAPI *
Real_WriteFile)(HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped)
= WriteFile;
BOOL (WINAPI *
Real_FlushFileBuffers)(HANDLE hFile)
= FlushFileBuffers;
BOOL (WINAPI *
Real_CloseHandle)(HANDLE hObject)
= CloseHandle;
BOOL (WINAPI *
Real_WaitNamedPipeW)(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
= WaitNamedPipeW;
BOOL (WINAPI *
Real_SetNamedPipeHandleState)(HANDLE hNamedPipe,
LPDWORD lpMode,
LPDWORD lpMaxCollectionCount,
LPDWORD lpCollectDataTimeout)
= SetNamedPipeHandleState;
DWORD (WINAPI *
Real_GetCurrentProcessId)(VOID)
= GetCurrentProcessId;
VOID (WINAPI *
Real_GetSystemTimeAsFileTime)(LPFILETIME lpSystemTimeAsFileTime)
= GetSystemTimeAsFileTime;
VOID (WINAPI *
Real_InitializeCriticalSection)(LPCRITICAL_SECTION lpSection)
= InitializeCriticalSection;
VOID (WINAPI *
Real_EnterCriticalSection)(LPCRITICAL_SECTION lpSection)
= EnterCriticalSection;
VOID (WINAPI *
Real_LeaveCriticalSection)(LPCRITICAL_SECTION lpSection)
= LeaveCriticalSection;
}
DWORD (WINAPI * Real_GetModuleFileNameW)(HMODULE a0,
LPWSTR a1,
DWORD a2)
= GetModuleFileNameW;
DWORD (WINAPI * Real_GetModuleFileNameA)(HMODULE a0,
LPSTR a1,
DWORD a2)
= GetModuleFileNameA;
BOOL (WINAPI * Real_CreateProcessW)(LPCWSTR a0,
LPWSTR a1,
LPSECURITY_ATTRIBUTES a2,
LPSECURITY_ATTRIBUTES a3,
BOOL a4,
DWORD a5,
LPVOID a6,
LPCWSTR a7,
struct _STARTUPINFOW* a8,
LPPROCESS_INFORMATION a9)
= CreateProcessW;
static SHORT (WINAPI * Real_GetKeyState)(int nVirtKey) = GetKeyState;
SHORT WINAPI Mine_GetKeyState(int nVirtKey)
{
SHORT ret;
ret = Real_GetKeyState(nVirtKey);
#if DEBUG
printf("getkeystate(%d) - detoured - result 0x%04x\r\n", nVirtKey, ret);
#endif
if (ret & ~0x8001) {
#if DEBUG
printf("bad flags are set in GetKeyState(%d): 0x%04x, fixing them for you to save you!!!111111111111111111111\r\n", nVirtKey, ret);
#endif
ret &= 0x8001;
}
return ret;
}
static BOOL (WINAPI *Real_GetKeyboardState)(PBYTE lpKeyState) = GetKeyboardState;
BOOL WINAPI Mine_GetKeyboardState(PBYTE lpKeyState)
{
BOOL ret;
int i;
ret = Real_GetKeyboardState(lpKeyState);
#if DEBUG
printf("GetKeyboardState() - detoured\r\n");
#endif
if (lpKeyState == NULL) {
#if DEBUG
printf("GetKeyboardState() - buffer is nullptr\r\n");
#endif
return ret;
}
if (ret == FALSE) {
#if DEBUG
printf("GetKeyboardState() - failed\r\n");
#endif
return ret;
}
for (i = 255; i >= 0; i--) {
if (lpKeyState[i] & ~0x81) {
#if DEBUG
printf("GetKeyboardState(): bad flag in lpKeyState[%d]: 0x%02x, fixing it for you to save you!!1111111\r\n", i, lpKeyState[i] & 0xff);
#endif
lpKeyState[i] &= 0x81;
}
}
return ret;
}
BOOL WINAPI Mine_CreateProcessW(LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation)
{
BOOL rv = 0;
rv = DetourCreateProcessWithDllExW(lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
lpCurrentDirectory,
lpStartupInfo,
lpProcessInformation,
s_szDllPath,
Real_CreateProcessW);
return rv;
}
#define ATTACH(x) DetourAttach(&(PVOID&)Real_##x,Mine_##x)
LONG AttachDetours(VOID)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
printf("attaching detours [%lld]\n", (long long int) GetCurrentThread());
ATTACH(CreateProcessW);
ATTACH(GetKeyboardState);
ATTACH(GetKeyState);
return DetourTransactionCommit();
}
#define DETACH(x) DetourDetach(&(PVOID&)Real_##x,Mine_##x)
LONG DetachDetours(VOID)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
printf("detaching detours [%lld]\n", (long long int) GetCurrentThread());
DETACH(CreateProcessW);
DETACH(GetKeyboardState);
DETACH(GetKeyState);
return DetourTransactionCommit();
}
BOOL ThreadAttach(HMODULE hDll)
{
(void)hDll;
return TRUE;
}
BOOL ThreadDetach(HMODULE hDll)
{
(void)hDll;
return TRUE;
}
BOOL ProcessAttach(HMODULE hDll)
{
WCHAR wzExePath[MAX_PATH];
s_hInst = hDll;
Real_GetModuleFileNameA(s_hInst, s_szDllPath, ARRAYSIZE(s_szDllPath));
Real_GetModuleFileNameW(NULL, wzExePath, ARRAYSIZE(wzExePath));
LONG error = AttachDetours();
(void) error;
ThreadAttach(hDll);
return TRUE;
}
BOOL ProcessDetach(HMODULE hDll)
{
ThreadDetach(hDll);
LONG error = DetachDetours();
(void) error;
return TRUE;
}
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, PVOID lpReserved)
{
(void)hModule;
(void)lpReserved;
if (DetourIsHelperProcess()) {
return TRUE;
}
switch (dwReason) {
case DLL_PROCESS_ATTACH:
DetourRestoreAfterWith();
return ProcessAttach(hModule);
case DLL_PROCESS_DETACH:
return ProcessDetach(hModule);
case DLL_THREAD_ATTACH:
return ThreadAttach(hModule);
case DLL_THREAD_DETACH:
return ThreadDetach(hModule);
}
return TRUE;
}