-
Notifications
You must be signed in to change notification settings - Fork 1
/
dllmain.cpp
193 lines (153 loc) · 6.68 KB
/
dllmain.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
typedef struct IUnknown IUnknown;
#include <Windows.h>
#include <dbghelp.h>
#include <psapi.h>
#include <iostream>
#include <TlHelp32.h>
#include "dplay.h"
#include "dplobby.h"
#include "include/MinHook.h"
#include "Hooks.h"
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"libs\\libMinHook.x86.lib")
#pragma comment(lib, "dbghelp.lib")
GUID IID_IDirectPlayLobby3AGuid = { 0x2db72491, 0x652c, 0x11d1, 0xa7, 0xa8, 0x0, 0x0, 0xf8, 0x3, 0xab, 0xfc };
GUID IID_IDirectPlay4AGuid = { 0xab1c531, 0x4745, 0x11d1, 0xa7, 0xa1, 0x0, 0x0, 0xf8, 0x3, 0xab, 0xfc };
// {DF394860-E19E-11D0-805F-444553540000} Worms 2 game
GUID worms2_GUID = { 3745073248, 57758, 4560, 128, 95, 68, 69, 83, 84, 0, 0 };
GUID DP4_IPX_SP = { 1750844416, 40236, 4559, 169, 205, 0, 170, 0, 104, 134, 227 };
IDirectPlay4* lpDP = nullptr;
bool isTextSection(const BYTE* arr, size_t size) {
const char* textSection = ".text";
return size >= strlen(textSection) && memcmp(arr, textSection, strlen(textSection)) == 0;
}
bool isDataSection(const BYTE* arr, size_t size) {
const char* textSection = ".data";
return size >= strlen(textSection) && memcmp(arr, textSection, strlen(textSection)) == 0;
}
void PatchMemory(uintptr_t targetAddress, BYTE jmpOpcode[], int codeSize)
{
// Write the modified instruction to the process
SIZE_T bytesWritten;
WriteProcessMemory(GetCurrentProcess(), reinterpret_cast<LPVOID>(targetAddress), jmpOpcode, codeSize, &bytesWritten);
}
void PatchMemory(uintptr_t targetAddress, void* value, int codeSize)
{
// Write the modified instruction to the process
SIZE_T bytesWritten;
WriteProcessMemory(GetCurrentProcess(), reinterpret_cast<LPVOID>(targetAddress), value, codeSize, &bytesWritten);
}
BOOL FAR PASCAL DPConnect_EnumSessionsCallback(LPCDPSESSIONDESC2 lpThisSD,
LPDWORD lpdwTimeOut,
DWORD dwFlags,
LPVOID lpContext)
{
if (lpThisSD != NULL && lpThisSD->lpszSessionNameA != NULL)
{
//we got a game!
return FALSE;
}
return TRUE;
}
//Function that hooks to the DirectPlayLobbyCreateA method
typedef HRESULT(WINAPI* DirectPlayLobbyCreateAType)(LPGUID, LPDIRECTPLAYLOBBYA*, IUnknown*, LPVOID, DWORD);
DirectPlayLobbyCreateAType pDirectPlayLobbyCreateA = nullptr; //original function pointer after hook
DirectPlayLobbyCreateAType pDirectPlayLobbyCreateATarget; //original function pointer BEFORE hook do not call this!
HRESULT WINAPI detourDirectPlayLobbyCreateA(LPGUID guid, LPDIRECTPLAYLOBBYA* lobby, IUnknown* unk, LPVOID ptr, DWORD word) {
auto returnVal = pDirectPlayLobbyCreateA(guid, lobby, unk, ptr, word);
if (SUCCEEDED(returnVal) && *lobby != nullptr) {
IDirectPlayLobby3A* lpDP2 = nullptr;
HRESULT queryResult = (*lobby)->QueryInterface(IID_IDirectPlayLobby3AGuid, (LPVOID*)&lpDP2);
if (SUCCEEDED(queryResult) && lpDP2 != nullptr) {
//Successfully obtained the IDirectPlayLobby3A interface
//Create a buffer to hold the connection settings
DWORD dwSize = 0;
//Get the size of the connection settings
queryResult = lpDP2->GetConnectionSettings(NULL, NULL, &dwSize); // Pass NULL to get the required size
if (queryResult == DPERR_BUFFERTOOSMALL) {
//Allocate memory for the connection settings
auto* pConnectionSettings = (DPLCONNECTION*)malloc(dwSize);
//Now retrieve the connection settings
queryResult = lpDP2->GetConnectionSettings(NULL, pConnectionSettings, &dwSize);
if (SUCCEEDED(queryResult)) {
// Successfully got the connection settings
if (pConnectionSettings->dwFlags == 1 && pConnectionSettings->guidSP == DP4_IPX_SP)
{
//Get directplay object
LPDIRECTPLAY* directPlay = (LPDIRECTPLAY*)malloc(sizeof(LPDIRECTPLAY));
HRESULT result = DirectPlayCreate(&DP4_IPX_SP, directPlay, NULL);
if (SUCCEEDED(result)) {
//Get interface
result = (*directPlay)->QueryInterface(IID_IDirectPlay4AGuid, (LPVOID*)&lpDP);
if (SUCCEEDED(result))
{
//Check for sessions
DPSESSIONDESC2 dpsd;
ZeroMemory(&dpsd, sizeof(dpsd));
dpsd.dwSize = sizeof(dpsd);
dpsd.guidApplication = worms2_GUID;
HRESULT hr = lpDP->EnumSessions(&dpsd, 0, DPConnect_EnumSessionsCallback,
NULL, DPENUMSESSIONS_ALL);
std::cout << "wkDNet: Obtained DirectPlay object";
lpDP->Release();
lpDP = NULL;
}
(*directPlay)->Release();
free(directPlay);
}
}
std::cout << "wkDNet: GetConnectionSettings success";
}
else {
std::cerr << "wkDNet: Failed to get connection settings: " << std::hex << queryResult << std::endl;
}
free(pConnectionSettings);
}
else {
std::cerr << "wkDNet: Failed to get connection settings size: " << std::hex << queryResult << std::endl;
}
lpDP2->Release();
}
}
return returnVal;
}
void shutdown() {
MH_Uninitialize();
}
bool Initialized = false;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
MH_STATUS status = MH_Initialize();
if (status != MH_OK)
{
std::string sStatus = MH_StatusToString(status);
shutdown();
return 0;
}
if (MH_CreateHookApiEx(L"dplayx", "DirectPlayLobbyCreateA", &detourDirectPlayLobbyCreateA, reinterpret_cast<void**>(&pDirectPlayLobbyCreateA), reinterpret_cast<void**>(&pDirectPlayLobbyCreateATarget)) != MH_OK) {
shutdown();
return 1;
}
if (MH_EnableHook(reinterpret_cast<void**>(pDirectPlayLobbyCreateATarget)) != MH_OK) {
shutdown();
return 1;
}
Initialized = true;
}
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if (Initialized && lpReserved)
shutdown();
break;
}
return TRUE;
}