-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbot_query_hook.cpp
538 lines (464 loc) · 10.9 KB
/
bot_query_hook.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
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
//
// JK_Botti - be more human!
//
// bot_query_hook.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#include <malloc.h>
#include <stdlib.h>
#include <memory.h>
#include <extdll.h>
#include <dllapi.h>
#include "bot_query_hook.h"
//
static bool msg_get_string(const unsigned char * &msg, size_t &len, char * name, int nlen)
{
int i = 0;
while(*msg != 0)
{
if(i < nlen)
name[i++]=*msg;
msg++;
if(--len == 0)
return(false);
}
if(i < nlen)
name[i]=0;
msg++;
if(--len == 0)
return(false);
return(true);
}
// find bot connection times and replace
ssize_t PASCAL handle_player_reply(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
/*
(int32) Header
(char) Character 'D'
--
(byte) Player Count
(byte) Player Number
(string) Player Name
(int32) Player Score
(float32) Player Time
(byte) Player Number
(string) Player Name
(int32) Player Score
(float32) Player Time
*/
unsigned char * newmsg = (unsigned char *)alloca(length);
memcpy(newmsg, message, length);
size_t len = length - 5;
const unsigned char * msg = (const unsigned char*)message + 5;
int i, pcount;
size_t offset;
char pname[64];
//get player count
pcount = *msg++;
if(--len == 0)
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
//parse player slots
for(i = 0; i < pcount; i++)
{
// skip player number
msg++;
if(--len == 0)
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
memset(pname, 0, sizeof(pname));
// detect bot by name
if(!msg_get_string(msg, len, pname, sizeof(pname)))
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
// skip score
if(len <= 4)
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
msg+=4;
len-=4;
// check that there is enough bytes left
if(len < 4)
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
offset = (size_t)msg - (size_t)message;
BotReplaceConnectionTime(pname, (float*)&newmsg[offset]);
msg+=4;
len-=4;
if(len == 0)
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
}
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
}
// find number of bots and replace with zero
ssize_t PASCAL handle_goldsrc_server_info_reply(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
/*
Header byte Always equal to 'm' (0x6D.)
Address string IP address and port of the server.
Name string Name of the server.
Map string Map the server has currently loaded.
Folder string Name of the folder containing the game files.
Game string Full name of the game.
Players byte Number of players on the server.
Max. Players byte Maximum number of players the server reports it can hold.
Protocol byte Protocol version used by the server.
Server type byte Indicates the type of server:
'D' for dedicated server
'L' for non-dedicated server
'P' for a HLTV server
Environment byte Indicates the operating system of the server:
'L' for Linux
'W' for Windows
Visibility byte Indicates whether the server requires a password:
0 for public
1 for private
Mod byte Indicates whether the game is a mod:
0 for Half-Life
1 for Half-Life mod
These fields are only present in the response if "Mod" is 1:
Data Type Comment
Link string URL to mod website.
Download Link string URL to download the mod.
NULL byte NULL byte (0x00)
Version long Version of mod installed on server.
Size long Space (in bytes) the mod takes up.
Type byte Indicates the type of mod:
0 for single and multiplayer mod
1 for multiplayer only mod
DLL byte Indicates whether mod uses its own DLL:
0 if it uses the Half-Life DLL
1 if it uses its own DLL
VAC byte Specifies whether the server uses VAC:
0 for unsecured
1 for secured
Bots byte Number of bots on the server.
*/
unsigned char * newmsg = (unsigned char *)alloca(length);
memcpy(newmsg, message, length);
ssize_t len = length - 5;
unsigned char * msg = (unsigned char*)newmsg + 5;
bool is_mod;
// skip IP address and port
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip server name
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip map name
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip game folder name (valve, gearbox, etc)
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip game name (Half-life, severian's mod, might be modified by metamod-plugins)
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// Next we have bytes: players, max.players, protocol, server type,
// environment, visibility, 'mod'.
// players
msg++;
len--;
if (len <= 0)
goto out;
// max players
if (*msg != gpGlobals->maxClients)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// protocol
msg++;
len--;
if (len <= 0)
goto out;
// server type, dedicated or listen
if (*msg != 'D' && *msg != 'd' && *msg != 'L' && *msg != 'l')
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// environment, linux or windows
if (*msg != 'W' && *msg != 'W' && *msg != 'L' && *msg != 'l')
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// visibility, 0 or 1
if (*msg != 0 && *msg != 1)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// is mod?
is_mod = *msg;
if (is_mod != 0 && is_mod != 1)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// if is mod, we have extra fields...
if (is_mod) {
// skip, mod homepage url
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip, mod download url
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// NULL
if (*msg != 0)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// long, version
msg+=4;
len-=4;
if (len <= 0)
goto out;
// long, size
msg+=4;
len-=4;
if (len <= 0)
goto out;
// type, single&multi player == 0, multi only == 1
if (*msg != 0 && *msg != 1)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// dll, 0 or 1
if (*msg != 0 && *msg != 1)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
}
// VAC, 0 or 1
if (*msg != 0 && *msg != 1)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// TODO: verify
// found bot count, replace with zero
*msg = 0;
out:
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
}
// find number of bots and replace with zero
ssize_t PASCAL handle_source_server_info_reply(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
/*
Header byte Always equal to 'I' (0x49.)
Protocol byte Protocol version used by the server.
Name string Name of the server.
Map string Map the server has currently loaded.
Folder string Name of the folder containing the game files.
Game string Full name of the game.
ID short Steam Application ID of game.
Players byte Number of players on the server.
Max. Players byte Maximum number of players the server reports it can hold.
Bots byte Number of bots on the server.
Server type byte Indicates the type of server:
'D' for a dedicated server
'L' for a non-dedicated server
'P' for a SourceTV server
Environment byte Indicates the operating system of the server:
'L' for Linux
'W' for Windows
Visibility byte Indicates whether the server requires a password:
0 for public
1 for private
VAC byte Specifies whether the server uses VAC:
0 for unsecured
1 for secured
....Continues....
*/
unsigned char * newmsg = (unsigned char *)alloca(length);
memcpy(newmsg, message, length);
ssize_t len = length - 5;
unsigned char * msg = (unsigned char*)newmsg + 5;
// protocol
msg++;
len--;
if (len <= 0)
goto out;
// skip server name
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip map name
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip game folder name (valve, gearbox, etc)
// TODO: verify
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// skip game name (Half-life, severian's mod, might be modified by metamod-plugins)
while (len > 0) {
if (*msg == 0) {
msg++;
len--;
break;
}
msg++;
len--;
}
if (len <= 0)
goto out; /* something is not right here */
// steam id, short
msg++;
len--;
if (len <= 0)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// players
msg++;
len--;
if (len <= 0)
goto out;
// max players
if (*msg != gpGlobals->maxClients)
goto out;
msg++;
len--;
if (len <= 0)
goto out;
// TODO: verify
// found bot count, replace with zero
*msg = 0;
out:
return(call_original_sendto(socket, newmsg, length, flags, dest_addr, dest_len));
}
//
ssize_t PASCAL sendto_hook(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
const unsigned char * orig_buf = (unsigned char*)message;
if (!gpGlobals)
goto out;
if (bot_conntimes < 1)
goto out;
if(length > 5 && orig_buf[0] == 0xff && orig_buf[1] == 0xff && orig_buf[2] == 0xff && orig_buf[3] == 0xff)
{
// check if this is server info reply packet (ÿÿÿÿm)
// old GoldSrc format
if (orig_buf[4] == 'm') {
return handle_goldsrc_server_info_reply(socket, message, length, flags, dest_addr, dest_len);
}
// check if this is server info reply packet (ÿÿÿÿI)
// new Source format (GoldSrc engine has switched to use this)
if (orig_buf[4] == 'I') {
return handle_source_server_info_reply(socket, message, length, flags, dest_addr, dest_len);
}
// check if this is player reply packet (ÿÿÿÿD)
if(orig_buf[4] == 'D') {
return(handle_player_reply(socket, message, length, flags, dest_addr, dest_len));
}
}
out:
return(call_original_sendto(socket, message, length, flags, dest_addr, dest_len));
}