-
Notifications
You must be signed in to change notification settings - Fork 3
/
drover.dpr
578 lines (492 loc) · 17.5 KB
/
drover.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
library drover;
uses
System.SysUtils,
Winapi.Windows,
DDetours,
PsAPI,
TlHelp32,
WinSock,
WinSock2,
IniFiles,
System.RegularExpressions,
System.NetEncoding,
SocketManager,
Options,
System.IOUtils,
Classes,
DiscordFolders;
var
RealGetFileVersionInfoA: pointer;
RealGetFileVersionInfoByHandle: pointer;
RealGetFileVersionInfoExA: pointer;
RealGetFileVersionInfoExW: pointer;
RealGetFileVersionInfoSizeA: pointer;
RealGetFileVersionInfoSizeExA: pointer;
RealGetFileVersionInfoSizeExW: pointer;
RealGetFileVersionInfoSizeW: pointer;
RealGetFileVersionInfoW: pointer;
RealVerFindFileA: pointer;
RealVerFindFileW: pointer;
RealVerInstallFileA: pointer;
RealVerInstallFileW: pointer;
RealVerLanguageNameA: pointer;
RealVerLanguageNameW: pointer;
RealVerQueryValueA: pointer;
RealVerQueryValueW: pointer;
RealGetEnvironmentVariableW: function(lpName: LPCWSTR; lpBuffer: LPWSTR; nSize: DWORD): DWORD; stdcall;
RealCreateProcessW: function(lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: bool; dwCreationFlags: DWORD;
lpEnvironment: pointer; lpCurrentDirectory: LPCWSTR; const lpStartupInfo: TStartupInfoW;
var lpProcessInformation: TProcessInformation): bool; stdcall;
RealGetCommandLineW: function: LPWSTR; stdcall;
RealSocket: function(af, type_, protocol: integer): TSocket; stdcall;
RealWSASocket: function(af, type_, protocol: integer; lpProtocolInfo: LPWSAPROTOCOL_INFO; g: GROUP; dwFlags: DWORD)
: TSocket; stdcall;
RealWSASend: function(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD; lpNumberOfBytesSent: PDWORD;
dwFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE)
: integer; stdcall;
RealWSASendTo: function(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD; lpNumberOfBytesSent: LPDWORD;
dwFlags: DWORD; const lpTo: TSockAddr; iTolen: integer; lpOverlapped: LPWSAOVERLAPPED;
lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE): integer; stdcall;
RealSend: function(s: TSocket; const buf; len, flags: integer): integer; stdcall;
RealRecv: function(s: TSocket; var buf; len, flags: integer): integer; stdcall;
currentProcessDir: string;
sockManager: TSocketManager;
droverOptions: TDroverOptions;
proxyValue: TProxyValue;
procedure MyGetFileVersionInfoA;
asm
JMP [RealGetFileVersionInfoA]
end;
procedure MyGetFileVersionInfoByHandle;
asm
JMP [RealGetFileVersionInfoByHandle]
end;
procedure MyGetFileVersionInfoExA;
asm
JMP [RealGetFileVersionInfoExA]
end;
procedure MyGetFileVersionInfoExW;
asm
JMP [RealGetFileVersionInfoExW]
end;
procedure MyGetFileVersionInfoSizeA;
asm
JMP [RealGetFileVersionInfoSizeA]
end;
procedure MyGetFileVersionInfoSizeExA;
asm
JMP [RealGetFileVersionInfoSizeExA]
end;
procedure MyGetFileVersionInfoSizeExW;
asm
JMP [RealGetFileVersionInfoSizeExW]
end;
procedure MyGetFileVersionInfoSizeW;
asm
JMP [RealGetFileVersionInfoSizeW]
end;
procedure MyGetFileVersionInfoW;
asm
JMP [RealGetFileVersionInfoW]
end;
procedure MyVerFindFileA;
asm
JMP [RealVerFindFileA]
end;
procedure MyVerFindFileW;
asm
JMP [RealVerFindFileW]
end;
procedure MyVerInstallFileA;
asm
JMP [RealVerInstallFileA]
end;
procedure MyVerInstallFileW;
asm
JMP [RealVerInstallFileW]
end;
procedure MyVerLanguageNameA;
asm
JMP [RealVerLanguageNameA]
end;
procedure MyVerLanguageNameW;
asm
JMP [RealVerLanguageNameW]
end;
procedure MyVerQueryValueA;
asm
JMP [RealVerQueryValueA]
end;
procedure MyVerQueryValueW;
asm
JMP [RealVerQueryValueW]
end;
function MyGetEnvironmentVariableW(lpName: LPCWSTR; lpBuffer: LPWSTR; nSize: DWORD): DWORD; stdcall;
var
s: string;
newValue: string;
begin
if proxyValue.isSpecified then
begin
s := lpName;
if (Pos('http_proxy', s) > 0) or (Pos('HTTP_PROXY', s) > 0) or (Pos('https_proxy', s) > 0) or
(Pos('HTTPS_PROXY', s) > 0) then
begin
newValue := proxyValue.FormatToHttpEnv;
StringToWideChar(newValue, lpBuffer, nSize);
result := Length(newValue);
exit;
end;
end;
result := RealGetEnvironmentVariableW(lpName, lpBuffer, nSize);
end;
procedure FindDiscordDirs(list: TStringList);
var
subdirs: TArray<string>;
s, subdir, baseDir: string;
begin
baseDir := IncludeTrailingPathDelimiter(ExtractFilePath(ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)))));
if TDirectory.Exists(baseDir) then
begin
subdirs := TDirectory.GetDirectories(baseDir, 'app-*', TSearchOption.soTopDirectoryOnly);
for subdir in subdirs do
begin
s := IncludeTrailingPathDelimiter(subdir);
if DirHasDiscordExecutable(s) then
begin
list.Add(s);
end;
end;
end;
end;
procedure CopyFilesToAllDiscordDirs;
var
dirs: TStringList;
dir: string;
srcOptionsPath, srcDllPath, dstOptionsPath, dstDllPath: string;
begin
srcOptionsPath := currentProcessDir + OPTIONS_FILENAME;
srcDllPath := currentProcessDir + DLL_FILENAME;
if not FileExists(srcOptionsPath) or not FileExists(srcDllPath) then
exit;
dirs := TStringList.Create;
try
FindDiscordDirs(dirs);
for dir in dirs do
begin
dstOptionsPath := dir + OPTIONS_FILENAME;
dstDllPath := dir + DLL_FILENAME;
if DirHasDiscordExecutable(dir) and not FileExists(dstOptionsPath) and not FileExists(dstDllPath) then
begin
CopyFile(PChar(srcOptionsPath), PChar(dstOptionsPath), true);
CopyFile(PChar(srcDllPath), PChar(dstDllPath), true);
end;
end;
finally
dirs.Free;
end;
end;
procedure CopyFilesOnCreateProcessIfNeeded(lpApplicationName: LPCWSTR);
var
appName: string;
begin
if lpApplicationName = nil then
exit;
appName := ExtractFileName(lpApplicationName);
if IsDiscordExecutable(appName) or SameText(appName, 'reg.exe') then
CopyFilesToAllDiscordDirs;
end;
function MyCreateProcessW(lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: bool; dwCreationFlags: DWORD;
lpEnvironment: pointer; lpCurrentDirectory: LPCWSTR; const lpStartupInfo: TStartupInfoW;
var lpProcessInformation: TProcessInformation): bool; stdcall;
begin
CopyFilesOnCreateProcessIfNeeded(lpApplicationName);
result := RealCreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
end;
function MyGetCommandLineW: LPWSTR; stdcall;
var
s: string;
begin
s := RealGetCommandLineW;
if proxyValue.isSpecified then
begin
if IsDiscordExecutable(ExtractFileName(ParamStr(0))) then
s := s + ' --proxy-server=' + proxyValue.FormatToChromeProxy;
end;
result := PChar(s);
end;
function MySocket(af, type_, protocol: integer): TSocket; stdcall;
begin
result := RealSocket(af, type_, protocol);
sockManager.Add(result, type_, protocol);
end;
function MyWSASocket(af, type_, protocol: integer; lpProtocolInfo: LPWSAPROTOCOL_INFO; g: GROUP; dwFlags: DWORD)
: TSocket; stdcall;
begin
result := RealWSASocket(af, type_, protocol, lpProtocolInfo, g, dwFlags);
sockManager.Add(result, type_, protocol);
end;
function AddHttpProxyAuthorizationHeader(socketManagerItem: TSocketManagerItem; lpBuffers: LPWSABUF;
dwBufferCount: DWORD; lpNumberOfBytesSent: PDWORD; dwFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED;
lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE): boolean;
var
pck, injectedData, filler: RawByteString;
uaStartPos, uaEndPos, uaLen, fillerLen: integer;
begin
result := false;
if (not proxyValue.isSpecified) or (not proxyValue.isHttp) or (not proxyValue.isAuth) or (not socketManagerItem.isTcp)
then
exit;
if (dwBufferCount <> 1) or (lpBuffers.len < 1) then
exit;
SetLength(pck, lpBuffers.len);
Move(lpBuffers.buf^, pck[1], lpBuffers.len);
if Pos(RawByteString(#13#10 + 'Proxy-Authorization: '), pck) > 0 then
exit;
uaStartPos := Pos(RawByteString('User-Agent:'), pck);
if uaStartPos < 1 then
exit;
uaEndPos := Pos(RawByteString(#13#10), pck, uaStartPos);
if uaEndPos < 1 then
exit;
uaLen := uaEndPos - uaStartPos;
injectedData := 'Proxy-Authorization: Basic ' +
RawByteString(TNetEncoding.Base64.EncodeBytesToString(BytesOf(RawByteString(proxyValue.login + ':' +
proxyValue.password))));
fillerLen := uaLen - Length(injectedData);
if fillerLen < 6 then
exit;
filler := #13#10 + 'X: ' + RawByteString(StringOfChar('X', fillerLen - 5));
injectedData := injectedData + filler;
if Length(injectedData) <> uaLen then
exit;
Move(injectedData[1], pck[uaStartPos], uaLen);
Move(pck[1], lpBuffers.buf^, lpBuffers.len);
result := true;
end;
function MyWSASend(sock: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD; lpNumberOfBytesSent: PDWORD;
dwFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE)
: integer; stdcall;
var
sockManagerItem: TSocketManagerItem;
begin
if sockManager.IsFirstSend(sock, sockManagerItem) then
begin
AddHttpProxyAuthorizationHeader(sockManagerItem, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
lpOverlapped, lpCompletionRoutine);
end;
result := RealWSASend(sock, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpOverlapped,
lpCompletionRoutine);
end;
function MyWSASendTo(sock: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD; lpNumberOfBytesSent: LPDWORD;
dwFlags: DWORD; const lpTo: TSockAddr; iTolen: integer; lpOverlapped: LPWSAOVERLAPPED;
lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE): integer; stdcall;
var
payload: byte;
sockManagerItem: TSocketManagerItem;
begin
if sockManager.IsFirstSend(sock, sockManagerItem) then
begin
if sockManagerItem.isUdp and (lpBuffers.len = 74) then
begin
payload := 0;
sendto(sock, pointer(@payload)^, 1, 0, @lpTo, iTolen);
payload := 1;
sendto(sock, pointer(@payload)^, 1, 0, @lpTo, iTolen);
Sleep(50);
end;
end;
result := RealWSASendTo(sock, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpTo, iTolen, lpOverlapped,
lpCompletionRoutine);
end;
function ConvertHttpToSocks5(socketManagerItem: TSocketManagerItem; const buf; len, flags: integer): boolean;
var
s, targetHost: RawByteString;
targetPort: word;
fdSet: TFDSet;
tv: TTimeVal;
i: integer;
match: TMatch;
sock: TSocket;
begin
result := false;
if (not proxyValue.isSpecified) or (not proxyValue.isSocks5) or (not socketManagerItem.isTcp) then
exit;
i := 8;
if len < i then
exit;
SetLength(s, i);
Move(buf, s[1], i);
if s <> 'CONNECT ' then
exit;
SetLength(s, len);
Move(buf, s[1], len);
match := TRegEx.match(string(s), '\ACONNECT ([a-z\d.-]+):(\d+)', [roIgnoreCase]);
if not match.Success then
exit;
targetHost := RawByteString(match.Groups[1].Value);
targetPort := StrToIntDef(match.Groups[2].Value, 0);
sock := socketManagerItem.sock;
s := #$05#$01#$00;
i := Length(s);
if RealSend(sock, s[1], i, flags) <> i then
exit;
FD_ZERO(fdSet);
_FD_SET(sock, fdSet);
tv.tv_sec := 10;
tv.tv_usec := 0;
if select(0, @fdSet, nil, nil, @tv) < 1 then
exit;
if not FD_ISSET(sock, fdSet) then
exit;
i := 2;
SetLength(s, i);
if RealRecv(sock, s[1], i, 0) <> i then
exit;
if s <> #$05#$00 then
exit;
s := #$05#$01#$00#$03 + RawByteString(AnsiChar(Length(targetHost))) + targetHost +
RawByteString(AnsiChar(Hi(targetPort))) + RawByteString(AnsiChar(Lo(targetPort)));
i := Length(s);
if RealSend(sock, s[1], i, flags) <> i then
exit;
sockManager.SetFakeHttpProxyFlag(sock);
result := true;
end;
function MySend(sock: TSocket; const buf; len, flags: integer): integer; stdcall;
var
sockManagerItem: TSocketManagerItem;
begin
if sockManager.IsFirstSend(sock, sockManagerItem) then
begin
if ConvertHttpToSocks5(sockManagerItem, buf, len, flags) then
exit(len);
end;
result := RealSend(sock, buf, len, flags);
end;
function MyRecv(sock: TSocket; var buf; len, flags: integer): integer; stdcall;
var
s: RawByteString;
i: integer;
begin
result := RealRecv(sock, buf, len, flags);
if (result > 0) and sockManager.ResetFakeHttpProxyFlag(sock) then
begin
if result >= 10 then
begin
// Potential issue: real server data may mix with the SOCKS5 response
SetLength(s, result);
Move(buf, s[1], result);
if Copy(s, 1, 3) = #$05#$00#$00 then
begin
s := 'HTTP/1.1 200 Connection Established' + #13#10 + #13#10;
i := Length(s);
if i <= len then
begin
Move(s[1], buf, i);
exit(i);
end;
end;
end;
end;
end;
function GetSystemFolder: string;
var
s: string;
begin
SetLength(s, MAX_PATH);
GetSystemDirectory(PChar(s), MAX_PATH);
result := IncludeTrailingPathDelimiter(PChar(s));
end;
procedure LoadOriginalVersionDll;
var
hOriginal: THandle;
begin
hOriginal := LoadLibrary(PChar(GetSystemFolder + 'version.dll'));
if hOriginal = 0 then
raise Exception.Create('Error.');
RealGetFileVersionInfoA := GetProcAddress(hOriginal, 'GetFileVersionInfoA');
RealGetFileVersionInfoByHandle := GetProcAddress(hOriginal, 'GetFileVersionInfoByHandle');
RealGetFileVersionInfoExA := GetProcAddress(hOriginal, 'GetFileVersionInfoExA');
RealGetFileVersionInfoExW := GetProcAddress(hOriginal, 'GetFileVersionInfoExW');
RealGetFileVersionInfoSizeA := GetProcAddress(hOriginal, 'GetFileVersionInfoSizeA');
RealGetFileVersionInfoSizeExA := GetProcAddress(hOriginal, 'GetFileVersionInfoSizeExA');
RealGetFileVersionInfoSizeExW := GetProcAddress(hOriginal, 'GetFileVersionInfoSizeExW');
RealGetFileVersionInfoSizeW := GetProcAddress(hOriginal, 'GetFileVersionInfoSizeW');
RealGetFileVersionInfoW := GetProcAddress(hOriginal, 'GetFileVersionInfoW');
RealVerFindFileA := GetProcAddress(hOriginal, 'VerFindFileA');
RealVerFindFileW := GetProcAddress(hOriginal, 'VerFindFileW');
RealVerInstallFileA := GetProcAddress(hOriginal, 'VerInstallFileA');
RealVerInstallFileW := GetProcAddress(hOriginal, 'VerInstallFileW');
RealVerLanguageNameA := GetProcAddress(hOriginal, 'VerLanguageNameA');
RealVerLanguageNameW := GetProcAddress(hOriginal, 'VerLanguageNameW');
RealVerQueryValueA := GetProcAddress(hOriginal, 'VerQueryValueA');
RealVerQueryValueW := GetProcAddress(hOriginal, 'VerQueryValueW');
end;
function IsNekoBoxExists: bool;
var
hSnapshot: THandle;
pe32: TProcessEntry32;
processName: string;
begin
result := false;
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnapshot = INVALID_HANDLE_VALUE then
exit;
try
pe32.dwSize := SizeOf(TProcessEntry32);
if Process32First(hSnapshot, pe32) then
begin
repeat
processName := LowerCase(StrPas(pe32.szExeFile));
if (Pos('nekobox', processName) > 0) or (Pos('nekoray', processName) > 0) then
begin
result := true;
exit;
end;
until not Process32Next(hSnapshot, pe32);
end;
finally
CloseHandle(hSnapshot);
end;
end;
exports
MyGetFileVersionInfoA index 1 name 'GetFileVersionInfoA',
MyGetFileVersionInfoByHandle index 2 name 'GetFileVersionInfoByHandle',
MyGetFileVersionInfoExA index 3 name 'GetFileVersionInfoExA',
MyGetFileVersionInfoExW index 4 name 'GetFileVersionInfoExW',
MyGetFileVersionInfoSizeA index 5 name 'GetFileVersionInfoSizeA',
MyGetFileVersionInfoSizeExA index 6 name 'GetFileVersionInfoSizeExA',
MyGetFileVersionInfoSizeExW index 7 name 'GetFileVersionInfoSizeExW',
MyGetFileVersionInfoSizeW index 8 name 'GetFileVersionInfoSizeW',
MyGetFileVersionInfoW index 9 name 'GetFileVersionInfoW',
MyVerFindFileA index 10 name 'VerFindFileA',
MyVerFindFileW index 11 name 'VerFindFileW',
MyVerInstallFileA index 12 name 'VerInstallFileA',
MyVerInstallFileW index 13 name 'VerInstallFileW',
MyVerLanguageNameA index 14 name 'VerLanguageNameA',
MyVerLanguageNameW index 15 name 'VerLanguageNameW',
MyVerQueryValueA index 16 name 'VerQueryValueA',
MyVerQueryValueW index 17 name 'VerQueryValueW';
begin
currentProcessDir := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
sockManager := TSocketManager.Create;
droverOptions := LoadOptions(currentProcessDir + OPTIONS_FILENAME);
if droverOptions.useNekoboxProxy and IsNekoBoxExists then
proxyValue.ParseFromString(droverOptions.nekoboxProxy)
else
proxyValue.ParseFromString(droverOptions.proxy);
LoadOriginalVersionDll;
RealGetEnvironmentVariableW := InterceptCreate(@GetEnvironmentVariableW, @MyGetEnvironmentVariableW, nil);
RealCreateProcessW := InterceptCreate(@CreateProcessW, @MyCreateProcessW, nil);
RealGetCommandLineW := InterceptCreate(@GetCommandLineW, @MyGetCommandLineW, nil);
RealSocket := InterceptCreate(@socket, @MySocket, nil);
RealWSASocket := InterceptCreate(@WSASocket, @MyWSASocket, nil);
RealWSASend := InterceptCreate(@WSASend, @MyWSASend, nil);
RealWSASendTo := InterceptCreate(@WSASendTo, @MyWSASendTo, nil);
RealSend := InterceptCreate(@send, @MySend, nil);
RealRecv := InterceptCreate(@recv, @MyRecv, nil);
end.