-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSteamPipeEmu.dpr
580 lines (481 loc) · 15.1 KB
/
SteamPipeEmu.dpr
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
library SteamPipeEmu;
{$I Default.inc}
uses
Winapi.Windows,
Winapi.WinSock,
Winapi.TlHelp32,
System.SysUtils,
System.Math,
Steam.Callback in 'Source\Public\Steam.Callback.pas',
Steam.API in 'Source\Public\Steam.API.pas',
Emu.SteamPipe in 'Source\Emu\Emu.SteamPipe.pas',
Emu.GCDetour in 'Source\Emu\Emu.GCDetour.pas',
Xander.Memoria in 'Source\Xander\Xander.Memoria.pas',
Xander.ThisWrap in 'Source\Xander\Xander.ThisWrap.pas',
Xander.DllCallback in 'Source\Xander\Xander.DllCallback.pas',
Xander.Exception in 'Source\Xander\Xander.Exception.pas',
Xander.LdrMonitor in 'Source\Xander\Xander.LdrMonitor.pas',
Xander.Helpers in 'Source\Xander\Xander.Helpers.pas',
Xander.Console in 'Source\Xander\Xander.Console.pas',
Xander.Buffer in 'Source\Xander\Xander.Buffer.pas',
Xander.MasterServer in 'Source\Xander\Xander.MasterServer.pas',
Xander.MiniUDP in 'Source\Xander\Xander.MiniUDP.pas';
{$R *.res}
// (char const *, int, enum EVProfBugdetGroup, bool, int, void *)
procedure F(_EAX, _EDX: Integer; This: Pointer; P: Pointer; I: Integer; B: Boolean; E: Integer; I2: Integer; S: PAnsiChar); register;
const
BannedLogList: array of PAnsiChar =
[
'IClientUser::BLoggedOn',
'CSteamMatchMaking::RunFrame',
'CServerList::RunFrame',
'CServerSocket::RunFrame',
'IClientHTTP::CreateHTTPRequest',
'IClientFriends::GetMediumFriendAvatar',
'IClientUtils::GetImageSize',
'IClientFriends::GetSmallFriendAvatar',
'IClientUtils::GetImageSize',
'CUtlString::SetValue',
'IClientFriends::SetRichPresence',
'IClientUser::GetSteamID'
];
BannedSubStrsList: array of PAnsiChar =
[
'UDP',
'HTTP',
'CJob',
'FreeDeletedPackets',
'CNet::',
'CWorkThreadPool::',
'CTCPConnection::',
'CScheduledFunctionMgr::',
'GetNextOutgoingPacket',
'CIPCServer::',
'RunFrame'
];
var
Str: PAnsiChar;
begin
if S = nil then
Exit;
// if StrPos(S, 'BeginAuthSession') <> nil then
// begin
// MessageBox(HWND_DESKTOP, 'BeginAuthSession', '', MB_SYSTEMMODAL);
// asm int 3 end;
// end;
//
for Str in BannedLogList do
begin
if StrComp(S, Str) = 0 then
Exit;
end;
//
// for Str in BannedSubStrsList do
// begin
// if StrPos(S, Str) <> nil then
// Exit;
// end;
TConsole.Important('[EnterScope] %s', [S]);
end;
procedure EnableDebugOutput;
var
P: PLongint;
begin
with TModule.CreateModule('tier0_s.dll') do
begin
HookExport('?EnterScope@CVProfile@@QAE_NPBDHW4EVProfBugdetGroup@@_NHPAX@Z', @F);
CreatePattern(P).GetProcedure('g_VProfProfilesRunningCount');
if P <> nil then
begin
P^ := 1;
TConsole.Success('VProf enabled');
end
else
begin
TConsole.Error('VProf failed');
end;
end;
end;
(*
void(*Msg)(const char *pMsg, ...);
void(*Warning)(const char *pMsg, ...);
void(*Warning_SpewCallStack)(int iMaxCallStackLength, const char *pMsg, ...);
void(*DevMsg)(int level, const char *pMsg, ...);
void(*DevWarning)(int level, const char *pMsg, ...);
void(*ConColorMsg)(const Color &clr, const char *pMsg, ...);
void(*ConMsg)(const char *pMsg, ...);
*)
//procedure Msg(Fmt: PAnsiChar); cdecl;
//const
// TriggerString: PAnsiChar = 'Server using ''%s'' lobbies, requiring pw %s, lobby id %llx'#$A;
//asm
// mov eax, [Fmt]
// mov edx, [TriggerString]
// call StrComp
//
// test al, al
// jnz @A
// // Clear 'dc.m_chLobbyType' string
// mov byte ptr [edi + 1009], 0
//
// // clear 'char context[ 256 ]' string
// lea eax, [esp + 156]
// mov byte ptr [eax], 0
//@A:
//end;
//function sprintf(Buffer: PAnsiChar; Fmt: PAnsiChar): Integer; cdecl varargs; external 'msvcrt' name 'sprintf';
function vsprintf(Buffer: PAnsiChar; Fmt: PAnsiChar; Args: Pointer): Integer; cdecl varargs; external 'msvcrt' name 'vsprintf';
procedure Msg(Fmt: PAnsiChar); cdecl;
var
Buffer: array[0..2047] of AnsiChar;
VAList: Pointer;
begin
TConsole.Write('[Msg] %s', [Fmt], CYAN, [pfNoLineBreak]);
if StrComp(Fmt, 'Server is running a newer version, client version %d, server version %d'#10) = 0 then
begin
VAList := @PPointer(@Fmt)[1];
vsprintf(Buffer, Fmt, VAList);
MessageBoxA(HWND_DESKTOP, Buffer, '', MB_ICONWARNING or MB_SYSTEMMODAL);
Exit;
end;
if StrComp(Fmt, 'Server is running an older version, client version %d, server version %d'#10) = 0 then
begin
VAList := @PPointer(@Fmt)[1];
vsprintf(Buffer, Fmt, VAList);
MessageBoxA(HWND_DESKTOP, Buffer, '', MB_ICONWARNING or MB_SYSTEMMODAL);
Exit;
end;
end;
procedure Warning(Fmt: PAnsiChar); cdecl;
begin
TConsole.Write('[Warning] %s', [Fmt], CYAN, [pfNoLineBreak]);
end;
procedure DevMsg(Level: Integer; Fmt: PAnsiChar); cdecl;
begin
TConsole.Write('[DevMsg] %s', [Fmt], CYAN, [pfNoLineBreak]);
end;
procedure DevWarning(Level: Integer; Fmt: PAnsiChar); cdecl;
begin
TConsole.Write('[DevWarning] %s', [Fmt], CYAN, [pfNoLineBreak]);
end;
procedure ConMsg(Fmt: PAnsiChar); cdecl;
begin
TConsole.Write('[ConMsg] %s', [Fmt], CYAN, [pfNoLineBreak]);
end;
procedure DetourTier0Msgs;
begin
{$IFDEF DEBUG}
with TModule.CreateModule('tier0.dll') do
begin
HookExport('Msg', @Msg);
//HookExport('Warning', @Warning);
//HookExport('DevMsg', @DevMsg);
//HookExport('DevWarning', @DevWarning);
//HookExport('ConMsg', @ConMsg);
end;
{$ENDIF}
end;
type
// results from BeginAuthSession
EBeginAuthSessionResult =
(
k_EBeginAuthSessionResultOK = 0, // Ticket is valid for this game and this steamID.
k_EBeginAuthSessionResultInvalidTicket = 1, // Ticket is not valid.
k_EBeginAuthSessionResultDuplicateRequest = 2, // A ticket has already been submitted for this steamID
k_EBeginAuthSessionResultInvalidVersion = 3, // Ticket is from an incompatible interface version
k_EBeginAuthSessionResultGameMismatch = 4, // Ticket is not for this game
k_EBeginAuthSessionResultExpiredTicket = 5 // Ticket has expired
);
function CGameServer_BeginAuthSession(Data: Pointer; Size: Integer; SteamID: TSteamID): Integer; stdcall;
begin
Result := Ord(k_EBeginAuthSessionResultOK);
end;
procedure DetourCGameServer;
var
P: Pointer;
begin
with TModule.CreateModule('steamclient.dll') do
begin
with CreatePattern(P) do
begin
FindPattern([$55, $8B, $EC, $83, $EC, $54, $53, $81, $65, $F0, $FF, $FF, $0F, $FF]);
FindReference(P);
end;
end;
if P <> nil then
TMemory.WritePrimitive<Pointer>(P, @CGameServer_BeginAuthSession);
end;
procedure RemoveServerQueryLimit;
var
P: Pointer;
begin
with TModule.CreateModule('steamclient.dll') do
begin
with CreatePattern(P) do
begin
FindPattern([$D9, $86, $90, $02, $00, $00]);
FindUInt8($77);
end;
if P <> nil then
TMemory.Fill(P, $90, 2)
{$IFDEF DEBUG}
else
TConsole.Error('RemoveServerQueryLimit: Failed to perform patch #1.')
{$ENDIF}
;
with CreatePattern(P) do
begin
FindPattern([$0F, $2F, $86, $AC, $02, $00, $00]);
FindUInt8($76);
end;
if P <> nil then
TMemory.WritePrimitive<Byte>(P, $EB)
{$IFDEF DEBUG}
else
TConsole.Error('RemoveServerQueryLimit: Failed to perform patch #2.');
{$ENDIF}
;
end;
end;
var
orgrecvfrom: function(s: TSocket; var Buf; len, flags: Integer; var from: TSockAddr; var fromlen: Integer): Integer; stdcall;
type
TServerInfo = record
GSSecure: Boolean;
Password: Boolean;
Friends: Boolean;
IsValve: Boolean;
Encrypted: Boolean;
LobbyType: AnsiString;
Context: AnsiString;
Challenge: Integer;
AuthProtocol: Integer;
GameProtocol: Integer;
SteamKeySize: Word;
GSSteamID: Int64;
LobbyID: Int64;
end;
function hkrecvfrom(s: TSocket; var Buf; len, flags: Integer; var from: TSockAddr; var fromlen: Integer): Integer; stdcall;
var
MsgIn: IReadBuffer;
MsgOut: IWriteBuffer;
Info: TServerInfo;
begin
Result := orgrecvfrom(s, Buf, len, flags, from, fromlen);
if Result <> SOCKET_ERROR then
begin
MsgIn := TReadAndWriteBuffer.CreateRead(@Buf, Result);
MsgOut := TReadAndWriteBuffer.CreateWrite;
if MsgIn.ReadInt32 <> -1 then
Exit;
if AnsiChar(MsgIn.ReadInt8) <> 'A' then // S2C_CHALLENGE
Exit;
FillChar(Info, SizeOf(Info), 0);
Info.Challenge := MsgIn.ReadInt32;
Info.AuthProtocol := MsgIn.ReadInt32;
Info.SteamKeySize := MsgIn.ReadUInt16;
Info.GSSteamID := MsgIn.ReadInt64;
Info.GSSecure := MsgIn.ReadInt8 <> 0;
Info.Context := MsgIn.ReadLStr;
Info.GameProtocol := MsgIn.ReadInt32;
Info.LobbyType := MsgIn.ReadLStr;
Info.Password := MsgIn.ReadInt8 <> 0;
Info.LobbyID := MsgIn.ReadInt64;
Info.Friends := MsgIn.ReadInt8 <> 0;
Info.IsValve := MsgIn.ReadInt8 <> 0;
Info.Encrypted := MsgIn.ReadInt8 <> 0;
if (Info.AuthProtocol <> 3) or (Info.SteamKeySize <> 0) then
begin
{$IFDEF DEBUG}
TConsole.Error('[RecvFrom] Only Steam connections with empty Steam key allowed, but got protocol %d with key size %d.',
[Info.AuthProtocol, Info.SteamKeySize]);
{$ENDIF}
Exit;
end;
if not MsgIn.Eof then
begin
{$IFDEF DEBUG}
TConsole.Error('[RecvFrom] Message should be finished, but got some data instead.');
{$ENDIF}
Exit;
end;
// if Info.LobbyType <> '' then
// begin
// {$IFDEF DEBUG}
// TConsole.Important('[RecvFrom] Received bad LobbyType (%s), spoofing...', [Info.LobbyType]);
// {$ENDIF}
//
// Info.LobbyType := '';
// end;
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// CSGO_GAME_UI_STATE_MAINMENU
// if Info.LobbyID <> 0 then
// begin
// {$IFDEF DEBUG}
// TConsole.Important('[WinSock] Received bad LobbyID (%d), spoofing...', [Info.LobbyID]);
// {$ENDIF}
//
// Info.LobbyID := 0;
// end;
Info.Context := 'connect-granted';
MsgOut.WriteInt32(-1);
MsgOut.WriteInt8(Ord('A'));
MsgOut.WriteUInt32(Info.Challenge);
MsgOut.WriteUInt32(Info.AuthProtocol);
MsgOut.WriteUInt16(Info.SteamKeySize);
MsgOut.WriteUInt64(Info.GSSteamID);
MsgOut.WriteUInt8(Ord(Info.GSSecure));
MsgOut.WriteLStr(Info.Context);
MsgOut.WriteUInt32(Info.GameProtocol);
MsgOut.WriteLStr(Info.LobbyType);
MsgOut.WriteUInt8(Ord(Info.Password));
MsgOut.WriteInt64(Info.LobbyID);
MsgOut.WriteUInt8(Ord(Info.Friends));
MsgOut.WriteUInt8(Ord(Info.IsValve));
MsgOut.WriteUInt8(Ord(Info.Encrypted));
Move(MsgOut.Data^, Buf, MsgOut.Size);
Result := MsgOut.Size;
end;
end;
var
orgSteamAPI_RegisterCallback: procedure(Callback: Pointer; CallbackIndex: Integer); cdecl;
orgSteamAPI_RegisterCallResult: procedure(Callback: Pointer; APICall: UInt64); cdecl;
orgSteam_BGetCallback: function(SteamPipe: HSteamPipe; CallbackMsg: PCallbackMsg): Boolean; cdecl;
procedure SteamAPI_RegisterCallback(Callback: Pointer; CallbackIndex: Integer); cdecl;
begin
TConsole.Write('[SteamAPI_RegisterCallback] From: %s; Callback: %.08X; CallbackIndex: %d',
[TMemory.BeautifyPointer(ReturnAddress), Integer(Callback), CallbackIndex], DARKBLUE);
orgSteamAPI_RegisterCallback(Callback, CallbackIndex);
end;
procedure SteamAPI_RegisterCallResult(Callback: Pointer; APICall: UInt64); cdecl;
begin
TConsole.Write('[SteamAPI_RegisterCallResult] From: %s; Callback: %.08X; APICall: %d',
[TMemory.BeautifyPointer(ReturnAddress), Integer(Callback), APICall], DARKBLUE);
orgSteamAPI_RegisterCallResult(Callback, APICall);
end;
function Steam_BGetCallback(SteamPipe: HSteamPipe; CallbackMsg: PCallbackMsg): Boolean; cdecl;
begin
Result := orgSteam_BGetCallback(SteamPipe, CallbackMsg);
end;
procedure DetourSteamAPICallbacks;
begin
with TModule.CreateModule('steam_api.dll') do
begin
@orgSteamAPI_RegisterCallback := HookExport('SteamAPI_RegisterCallback', @SteamAPI_RegisterCallback);
@orgSteamAPI_RegisterCallResult := HookExport('SteamAPI_RegisterCallResult', @SteamAPI_RegisterCallResult);
end;
end;
procedure DetourChallengeMessages;
begin
@orgrecvfrom := TModule.CreateModule('wsock32.dll').HookExport('recvfrom', @hkrecvfrom);
end;
procedure DetourSteamClientCallbacks;
begin
with TModule.CreateModule('steamclient.dll') do
begin
@orgSteam_BGetCallback := HookExport('Steam_BGetCallback', @Steam_BGetCallback);
end;
end;
procedure DllCallback(const Name: string; Base: Pointer); register;
var
GenName: string;
Desired: Boolean;
begin
Desired := False;
GenName := ExtractFileName(Name);
GenName := LowerCase(GenName);
try
if SameStr('steam_api.dll', GenName) then
begin
Desired := True;
//DetourSteamAPICallbacks;
Exit;
end;
if SameStr('steamclient.dll', GenName) then
begin
Desired := True;
DetourCGameServer;
DetourSteamClientCallbacks;
RemoveServerQueryLimit;
//DetourChallengeMessages;
DetourTier0Msgs;
EnableDebugOutput;
Exit;
end;
finally
if Desired then
TConsole.Write('[DllCallback] Name: %s; Handle: $%s', [GenName, IntToHex(Integer(Base), 8)], DARKGREEN);
end;
end;
var
SteamPipeServer: TSteamPipeServer;
function SteamPipeReadThread(Parameter: Pointer): Integer;
begin
repeat
SteamPipeServer.Frame;
until False;
Exit(1);
end;
function ProcessExists(const FileName: string; out ProcessId: Integer): Boolean; overload;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
UpperName: string;
UpperProcName: string;
begin
if PPointer(@ProcessId) <> nil then
ProcessId := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if FSnapshotHandle = INVALID_HANDLE_VALUE then
Exit(False);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
try
if not ContinueLoop then
Exit(False);
UpperName := UpperCase(FileName);
repeat
UpperProcName := UpperCase(ExtractFileName(FProcessEntry32.szExeFile));
if Pos(UpperName, UpperProcName) > 0 then
begin
if PPointer(@ProcessId) <> nil then
ProcessId := FProcessEntry32.th32ProcessID;
Exit(True);
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
until not ContinueLoop;
finally
CloseHandle(FSnapshotHandle);
end;
Exit(False);
end;
procedure Init;
begin
// TODO: Replace ProcessExists with file mapping check
if ProcessExists('steam.exe', PInteger(nil)^) then
begin
MessageBox(HWND_DESKTOP, 'XanderEmu does not work while Steam is running. Please, terminate Steam and try again.', 'XanderEmu - Error', MB_ICONWARNING or MB_SYSTEMMODAL);
Halt;
end;
{$IFDEF DEBUG}
MessageBox(HWND_DESKTOP, 'Are you ready?', '', MB_SYSTEMMODAL);
AllocConsole;
TConsole.Init(True);
{$ENDIF}
SteamPipeServer := TSteamPipeServer.Create;
BeginThread(nil, 0, SteamPipeReadThread, nil, 0, PCardinal(nil)^);
Xander.DllCallback.Setup(DllCallback);
end;
// TODO: Add validation - was the original steamclient.dll loaded
// Current versions of MacOS Steam libraries can be taken from http://osw.didrole.com/src/
begin
Init;
end.