-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFSLAN.cpp
408 lines (344 loc) · 13.7 KB
/
NFSLAN.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
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
// NFS LAN server launcher for Most Wanted (2005) and Underground 2
// by Xan/Tenjoin
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <regex>
#include <filesystem>
#include <signal.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iphlpapi.h>
#include "injector/injector.hpp"
#include "injector/assembly.hpp"
#include "injector/hooking/Hooking.Patterns.h"
#include "Network.h"
#include <thread>
bool (*StartServer)(char* ServerName, int32_t ForceNameNFSMW, void* Callback, void* CallbackParam);
bool (*IsServerRunning)();
void (*StopServer)();
bool bDisablePatching = false;
uintptr_t who_func = 0x1000AAD0;
uintptr_t packet_buffer = 0x10058A5C;
std::map<uint32_t, uint32_t> RedirIPs;
std::vector<uint32_t> LocalUsers;
uint32_t lobbyClientDestAddr = 0;
// requires that the client is using the LanIP plugin!
void LocalChallengeClient(uint32_t addr)
{
constexpr DWORD ChallengeTimeOut = 1000; // 1sec timeout
uint32_t query = 0x6A093EC9; // strhash of "LOCAL?" 6A093EC9
uint32_t response = 0;
char strIP[32];
sprintf(strIP, "%u.%u.%u.%u", addr >> 24 & 0xFF, addr >> 16 & 0xFF, addr >> 8 & 0xFF, addr & 0xFF);
std::cout << "NFSLAN: challenging addr " << strIP << '\n';
try
{
UDPSocket Socket;
Socket.SetTimeout(ChallengeTimeOut);
Socket.SendTo(strIP, 9901, (char*)&query, sizeof(uint32_t));
Socket.RecvFrom((char*)&response, sizeof(uint32_t));
if (response == 0x8DB682D1) // strhash of "YESIMLOCAL" 8DB682D1
{
sprintf(strIP, "%u.%u.%u.%u", addr >> 24 & 0xFF, addr >> 16 & 0xFF, addr >> 8 & 0xFF, addr & 0xFF);
std::cout << "NFSLAN: addr " << strIP << " is local!\n";
LocalUsers.push_back(addr);
}
}
catch (std::exception& ex)
{
std::cerr << "ERROR: " << ex.what() << '\n';
return;
}
}
uintptr_t lobbyAddrFunc = 0x100025E0;
void hkLobbyAddr(uintptr_t a0, uintptr_t a1, uintptr_t a2, uint32_t addr)
{
uint32_t setaddr = addr;
if (addr != lobbyClientDestAddr)
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), lobbyClientDestAddr) != LocalUsers.cend())
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), addr) != LocalUsers.cend())
{
setaddr = addr;
}
else if (RedirIPs.find(addr) != RedirIPs.end())
setaddr = RedirIPs.at(addr);
}
else if (RedirIPs.find(addr) != RedirIPs.end())
setaddr = RedirIPs.at(addr);
}
else if (RedirIPs.find(addr) != RedirIPs.end())
setaddr = RedirIPs.at(addr);
//printf("Setting local: Addr: %X Dest: %X\n", setaddr, lobbyClientDestAddr);
//printf("Addr: %X Dest: %X\n", setaddr, lobbyClientDestAddr);
return reinterpret_cast<void(*)(uintptr_t, uintptr_t, uintptr_t, uint32_t)>(lobbyAddrFunc)(a0, a1, a2, setaddr);
}
// server patches for MW server (server.dll in MW and Carbon (yes really, Carbon's is slightly different but it's there))
void PatchServerMW(uintptr_t base)
{
// base is usually 10000000 but it's better safe than sorry
hook::details::set_process_base(base);
// 1001DC8B - 1001DC93
uintptr_t loc_1001DC8B = reinterpret_cast<uintptr_t>(hook::pattern("83 C4 08 50 E8 ? ? ? ? 8B 4E 7C 83 C4 08 6A 00 8B D8 68").get_first(0)) + 4;
// 1001DCAD - 1001DCBA
uintptr_t loc_1001DCAD = loc_1001DC8B + 0x22;
// 10006ABE
uintptr_t loc_10006ABE = reinterpret_cast<uintptr_t>(hook::pattern("C7 46 14 FE FF FF FF C6 01 00").get_first(0));
// 10007294
uintptr_t loc_10007294 = reinterpret_cast<uintptr_t>(hook::pattern("8B 4E 5C 8B 56 60 8B E8 8B 45 18 50").get_first(0));
// 1001BEDF
uintptr_t loc_1001BEDF = reinterpret_cast<uintptr_t>(hook::pattern("8B 82 38 0A 00 00 8D 88 70 03 00 00 51 8B 8A 6C 0D 00 00").get_first(0));
// 1000AB32
uintptr_t loc_1000AB32 = reinterpret_cast<uintptr_t>(hook::pattern("8B 86 D0 02 00 00 8D 8E D0 00 00 00 51 8B").get_first(0));
// 1000AB03
uintptr_t loc_1000AB03 = reinterpret_cast<uintptr_t>(hook::pattern("8B 8E D4 02 00 00 52 8B 96 18 03 00 00").get_first(0));
// 100099EF
uintptr_t loc_100099EF = reinterpret_cast<uintptr_t>(hook::pattern("8B 86 38 0A 00 00 85 C0 BB ? ? ? ? 74 13 50 E8").get_first(0));
// 10026514
uintptr_t loc_10026514 = reinterpret_cast<uintptr_t>(hook::pattern("55 8D 4C 24 14 51 57 53 E8 ? ? ? ? 56 8D 54 24 24 68 ? ? ? ? 52 E8 ? ? ? ? 8B 44 24 50").get_first(0)) + 8;
// 1001363D
uintptr_t loc_1001363D = reinterpret_cast<uintptr_t>(hook::pattern("C7 45 5C F4 FF 00 00 89 75 50 89 75 54 89 75 58").get_first(0)) + 7;
// 10013C95
uintptr_t loc_10013C95 = reinterpret_cast<uintptr_t>(hook::pattern("51 89 7D 50 89 7D 54 89 7D 58 8B 4B 10").get_first(0)) + 1;
struct PatchAddr1
{
void operator()(injector::reg_pack& regs)
{
regs.eax = *(uint32_t*)(regs.esi + 0x14);
regs.ecx = *(uint32_t*)(regs.esi + 0x7C);
}
}; injector::MakeInline<PatchAddr1>(loc_1001DC8B, loc_1001DC8B + 8);
struct PatchPort1
{
void operator()(injector::reg_pack& regs)
{
regs.ecx = *(uint8_t*)(regs.esp + 0x1E);
regs.edi = *(uint16_t*)(regs.esi + 0x18);
}
}; injector::MakeInline<PatchPort1>(loc_1001DCAD, loc_1001DCAD + 0xD);
// disable IP address invalidation at this point
injector::MakeNOP(loc_10006ABE, 7);
struct PatchAddrAndPort2
{
void operator()(injector::reg_pack& regs)
{
*(uint32_t*)(regs.eax + 0x18) = *(uint32_t*)(regs.esi + 0x14);
//*(uint16_t*)(regs.eax + 0xC) = *(uint16_t*)(regs.esi + 0x18);
*(int32_t*)(regs.esi + 0x14) = -5;
regs.ecx = *(uint32_t*)(regs.esi + 0x5C);
regs.edx = *(uint32_t*)(regs.esi + 0x60);
}
}; injector::MakeInline<PatchAddrAndPort2>(loc_10007294, loc_10007294 + 6);
struct CatchLocalSKU
{
void operator()(injector::reg_pack& regs)
{
regs.eax = *(uint32_t*)(regs.edx + 0xA38);
char* strSKU = (char*)(regs.eax + 0x80);
if (std::regex_match(strSKU, std::regex("^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$")))
{
uint32_t connIP = *(uint32_t*)(regs.ebx + 0x14);
uint32_t incomingIP = 0;
uint8_t p1, p2, p3, p4;
sscanf(strSKU, "%hhu.%hhu.%hhu.%hhu", &p1, &p2, &p3, &p4);
incomingIP = p1 << 24 | p2 << 16 | p3 << 8 | p4;
if (incomingIP != connIP)
{
RedirIPs.insert(std::pair(connIP, incomingIP));
// challenge the connIP to see if it's local to the server, and if it is, add it to its own list
std::thread(LocalChallengeClient, connIP).detach();
}
}
}
}; injector::MakeInline<CatchLocalSKU>(loc_1001BEDF, loc_1001BEDF + 6);
struct TestLocalSKU
{
void operator()(injector::reg_pack& regs)
{
uint32_t connIP = *(uint32_t*)(regs.esi + 0x2D0);
uint32_t destIP = *(uint32_t*)(regs.edi + 0x14);
if (connIP != destIP)
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), destIP) != LocalUsers.cend())
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), connIP) != LocalUsers.cend())
{
regs.eax = connIP;
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.eax = RedirIPs.at(connIP);
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.eax = RedirIPs.at(connIP);
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.eax = RedirIPs.at(connIP);
//printf("A: Setting local: Addr: %X Dest: %X\n", regs.eax, destIP);
}
}; injector::MakeInline<TestLocalSKU>(loc_1000AB32, loc_1000AB32 + 6);
struct TestLocalSKU_LA
{
void operator()(injector::reg_pack& regs)
{
uint32_t connIP = *(uint32_t*)(regs.esi + 0x2D4);
uint32_t destIP = *(uint32_t*)(regs.edi + 0x14);
if (connIP != destIP)
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), destIP) != LocalUsers.cend())
{
if (std::find(LocalUsers.cbegin(), LocalUsers.cend(), connIP) != LocalUsers.cend())
{
regs.ecx = connIP;
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.ecx = RedirIPs.at(connIP);
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.ecx = RedirIPs.at(connIP);
}
else if (RedirIPs.find(connIP) != RedirIPs.end())
regs.ecx = RedirIPs.at(connIP);
//printf("LA: Setting local: Addr: %X Dest: %X\n", regs.ecx, destIP);
}
}; injector::MakeInline<TestLocalSKU_LA>(loc_1000AB03, loc_1000AB03 + 6);
struct CatchLocalSKU_TERM
{
void operator()(injector::reg_pack& regs)
{
uint32_t a1 = *(uint32_t*)(regs.esp + 0x410);
uint32_t connIP = *(uint32_t*)(a1 + 0x14);
LocalUsers.erase(std::remove(LocalUsers.begin(), LocalUsers.end(), connIP), LocalUsers.end());
RedirIPs.erase(connIP);
regs.eax = *(uint32_t*)(regs.esi + 0xA38);
}
}; injector::MakeInline<CatchLocalSKU_TERM>(loc_100099EF, loc_100099EF + 6);
struct CatchDestAddr1
{
void operator()(injector::reg_pack& regs)
{
lobbyClientDestAddr = *(uint32_t*)(regs.ebp + 0x14);
*(uint32_t*)(regs.ebp + 0x50) = 0;
*(uint32_t*)(regs.ebp + 0x54) = 0;
}
}; injector::MakeInline<CatchDestAddr1>(loc_1001363D, loc_1001363D + 6);
struct CatchDestAddr2
{
void operator()(injector::reg_pack& regs)
{
lobbyClientDestAddr = *(uint32_t*)(regs.ebp + 0x14);
*(uint32_t*)(regs.ebp + 0x50) = 0;
*(uint32_t*)(regs.ebp + 0x54) = 0;
}
}; injector::MakeInline<CatchDestAddr2>(loc_10013C95, loc_10013C95 + 6);
lobbyAddrFunc = reinterpret_cast<uintptr_t>(injector::MakeCALL(loc_10026514, hkLobbyAddr).get_raw<void>());
injector::MakeCALL(loc_10026514 + 0x21, hkLobbyAddr);
}
void PatchServerUG2(uintptr_t base)
{
std::cout << "NFSLAN: Server patching for NFS Underground 2 not yet implemented.\n";
}
bool bIsUnderground2Server(uintptr_t base)
{
// base is usually 10000000 but it's better safe than sorry
hook::details::set_process_base(base);
// 100013FB in MW, 100013EC in UG2
uintptr_t defServerNamePtr = reinterpret_cast<uintptr_t>(hook::pattern("6A 03 68 66 76 64 61 53").get_first(0)) + 0x12;
char* defServerName = *(char**)defServerNamePtr;
if ((strstr(defServerName, "Underground 2") == nullptr) && (defServerName != nullptr))
return false;
return true;
}
void SigInterruptHandler(int signum)
{
if (IsServerRunning())
{
std::cout << "NFSLAN: Stopping server...\n";
StopServer();
}
else
{
std::cout << "NFSLAN: WARNING - server was NOT running during interrupt!\n";
}
exit(signum);
}
int main(int argc, char* argv[])
{
std::cout << "NFS LAN Server Launcher\n";
if (argc < 2)
{
std::cout << "USAGE: NFSLAN servername [-n]\n" << "-n = no server patching\n";
return -1;
}
if (argc >= 3)
{
if (argv[2][0] == '-' && argv[2][1] == 'n')
{
bDisablePatching = true;
}
}
if (!std::filesystem::exists("server.dll"))
{
std::cerr << "ERROR: server.dll not found! Please place the server.dll from the game in this executable's path!\n";
return -1;
}
HMODULE serverdll = LoadLibraryA("server");
if (!serverdll)
{
std::cerr << "ERROR: server.dll failed to load!\n";
return -1;
}
StartServer = (bool(*)(char*, int32_t, void*, void*))GetProcAddress(serverdll, "StartServer");
if (!StartServer)
{
std::cerr << "ERROR: could not find function StartServer inside server.dll!\n";
return -1;
}
IsServerRunning = (bool(*)())GetProcAddress(serverdll, "IsServerRunning");
if (!IsServerRunning)
{
std::cerr << "ERROR: could not find function IsServerRunning inside server.dll!\n";
return -1;
}
StopServer = (void(*)())GetProcAddress(serverdll, "StopServer");
if (!StopServer)
{
std::cerr << "ERROR: could not find function StopServer inside server.dll!\n";
return -1;
}
if (!bDisablePatching)
{
std::cout << "NFSLAN: Patching the server to work on any network...\n";
if (bIsUnderground2Server((uintptr_t)serverdll))
PatchServerUG2((uintptr_t)serverdll);
else
PatchServerMW((uintptr_t)serverdll);
}
signal(SIGINT, SigInterruptHandler);
signal(SIGTERM, SigInterruptHandler);
if (!StartServer(argv[1], 0, nullptr, nullptr))
{
std::cerr << "ERROR: could not launch server! StartServer returned false!\n";
return -1;
}
if (!IsServerRunning())
{
std::cerr << "ERROR: could not launch server! StartServer returned true but IsServerRunning returned false!\n";
return -1;
}
std::cout << "NFSLAN: Server started. To stop gracefully, send CTRL+C to the console\n";
while (IsServerRunning()) { Sleep(1); }
if (IsServerRunning())
{
std::cout << "NFSLAN: Stopping server...\n";
StopServer();
}
else
{
std::cout << "NFSLAN: Server not running anymore, exiting...\n";
}
return 0;
}