-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
214 lines (175 loc) · 5.59 KB
/
main.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
#define PSAPI_VERSION 1
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Shlwapi.h>
#include <psapi.h>
#include "dinput8.h"
DirectInput8CreateProc m_pDirectInput8Create;
DllCanUnloadNowProc m_pDllCanUnloadNow;
DllGetClassObjectProc m_pDllGetClassObject;
DllRegisterServerProc m_pDllRegisterServer;
DllUnregisterServerProc m_pDllUnregisterServer;
GetdfDIJoystickProc m_pGetdfDIJoystick;
PCHAR FindPattern(LPCSTR pattern, LPCSTR mask, PCHAR start, DWORD size)
{
DWORD _;
VirtualProtect((PVOID)start, size, PAGE_EXECUTE_READWRITE, &_);
size_t patternLength = strlen(pattern);
for (size_t i = 0; i < size - patternLength; ++i)
{
bool found = true;
for (size_t j = 0; j < patternLength; ++j)
{
if (mask[j] != '?' && pattern[j] != *(start + i + j))
{
found = false;
break;
}
}
if (found)
{
return (start + i);
}
}
return NULL;
}
HMODULE LoadSystemModule(const char* dllName)
{
char path[MAX_PATH];
GetSystemDirectoryA(path, MAX_PATH);
PathAppendA(path, dllName);
return LoadLibraryA(path);
}
void SetDinputFuncs(HMODULE dinputModule)
{
m_pDirectInput8Create = (DirectInput8CreateProc)GetProcAddress(dinputModule, "DirectInput8Create");
m_pDllCanUnloadNow = (DllCanUnloadNowProc)GetProcAddress(dinputModule, "DllCanUnloadNow");
m_pDllGetClassObject = (DllGetClassObjectProc)GetProcAddress(dinputModule, "DllGetClassObject");
m_pDllRegisterServer = (DllRegisterServerProc)GetProcAddress(dinputModule, "DllRegisterServer");
m_pDllUnregisterServer = (DllUnregisterServerProc)GetProcAddress(dinputModule, "DllUnregisterServer");
m_pGetdfDIJoystick = (GetdfDIJoystickProc)GetProcAddress(dinputModule, "GetdfDIJoystick");
}
void ApplyWindowedFix(HMODULE dinputModule)
{
MODULEINFO moduleInfo = { 0 };
if (GetModuleInformation(GetCurrentProcess(), dinputModule, &moduleInfo, sizeof(MODULEINFO)) == FALSE)
{
// If the module information can't be retrieved for whatever reason, don't proceed
return;
}
// 01
// 74 ??
// 8D ?? 08 (start the jump here)
// 50
// FF 15 ?? ?? ?? ??
// 8D 45 ??
// 50
// FF ?? 10
// FF 15 ?? ?? ?? ??
// 8B 45 ??
PCHAR jmpStart = FindPattern(
"\x01\x74?\x8D?\x08\x50\xFF\x15????\x8D\x45?\x50\xFF?\x10\xFF\x15????\x8B\x45?",
"xx?x?xxxx????xx?xx?xxx????xx?",
(PCHAR)moduleInfo.lpBaseOfDll, moduleInfo.SizeOfImage);
if (jmpStart == NULL)
{
// If it can't find the first pattern, don't bother
return;
}
jmpStart += 3; // Set offset after the je
// D1 ??
// ??
// FF 15 ?? ?? ?? ??
// 53 (jump to here, right after the SetCursorPos call)
// FF 15 ?? ?? ?? ??
// 8B ?? 14
PCHAR jmpDest = FindPattern(
"\xD1??\xFF\x15????\x53\xFF\x15????\x8B?\x14",
"x??xx????xxx????x?x",
jmpStart, 100);
if (jmpDest == NULL)
{
// If the second pattern can't be found within 100 bytes after the first pattern, try an alternative pattern
// D1 ??
// ??
// FF 15 ?? ?? ?? ??
// 33 DB (jump to here, right after the SetCursorPos call)
// 53
// FF 15 ?? ?? ?? ??
// 8B ?? 14
jmpDest = FindPattern(
"\xD1??\xFF\x15????\x33\xDB\x53\xFF\x15????\x8B?\x14",
"x??xx????xxxxx????x?x",
jmpStart, 100);
if (jmpDest == NULL)
{
// If the alternative pattern can't be found either, give up
return;
}
}
jmpDest += 9; // Set offset at push ebx
DWORD _;
VirtualProtect((PVOID)jmpStart, sizeof(BYTE) * 2, PAGE_EXECUTE_READWRITE, &_);
*(PBYTE)jmpStart = 0xEB; // jmp instruction
*(PCHAR)(jmpStart + 1) = ((char)(jmpDest - (jmpStart + 2))); // jump to the destination;
// (jmpStart + 2 because the jump distance is counted starting two bytes after the je instruction)
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
static HMODULE dinputModule = NULL;
UNREFERENCED_PARAMETER(hinstDLL);
UNREFERENCED_PARAMETER(lpReserved);
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
dinputModule = LoadSystemModule("dinput8.dll");
if (!dinputModule)
{
// Uhh...
return FALSE;
}
SetDinputFuncs(dinputModule);
ApplyWindowedFix(dinputModule);
break;
case DLL_PROCESS_DETACH:
FreeLibrary(dinputModule);
break;
}
return TRUE;
}
HRESULT WINAPI DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID* ppvOut, LPUNKNOWN punkOuter)
{
if (!m_pDirectInput8Create)
return E_FAIL;
return m_pDirectInput8Create(hinst, dwVersion, riidltf, ppvOut, punkOuter);
}
HRESULT WINAPI DllCanUnloadNow()
{
if (!m_pDllCanUnloadNow)
return E_FAIL;
return m_pDllCanUnloadNow();
}
HRESULT WINAPI DllGetClassObject(IN REFCLSID rclsid, IN REFIID riid, OUT LPVOID FAR* ppv)
{
if (!m_pDllGetClassObject)
return E_FAIL;
return m_pDllGetClassObject(rclsid, riid, ppv);
}
HRESULT WINAPI DllRegisterServer()
{
if (!m_pDllRegisterServer)
return E_FAIL;
return m_pDllRegisterServer();
}
HRESULT WINAPI DllUnregisterServer()
{
if (!m_pDllUnregisterServer)
return E_FAIL;
return m_pDllUnregisterServer();
}
LPCDIDATAFORMAT WINAPI GetdfDIJoystick()
{
if (!m_pGetdfDIJoystick)
return NULL;
return m_pGetdfDIJoystick();
}