-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenctypex_decoder.c
More file actions
688 lines (580 loc) · 20.5 KB
/
enctypex_decoder.c
File metadata and controls
688 lines (580 loc) · 20.5 KB
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#ifdef _WIN32
//#define snprintf _snprintf
#define lfind _lfind
#endif // _WIN32
/*
GS enctypeX servers list decoder/encoder 0.1.3b
by Luigi Auriemma
e-mail: aluigi@autistici.org
web: aluigi.org
This is the algorithm used by ANY new and old game which contacts the Gamespy master server.
It has been written for being used in gslist so there are no explanations or comments here,
if you want to understand something take a look to gslist.c
Copyright 2008-2012 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl-2.0.txt
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
typedef struct {
unsigned char encxkey[261]; // static key
int offset; // everything decrypted till now (total)
int start; // where starts the buffer (so how much big is the header), this is the only one you need to zero
} enctypex_data_t;
int enctypex_func5(unsigned char *encxkey, int cnt, unsigned char *id, int idlen, int *n1, int *n2) {
int i,
tmp,
mask = 1;
if(!cnt) return(0);
if(cnt > 1) {
do {
mask = (mask << 1) + 1;
} while(mask < cnt);
}
i = 0;
do {
*n1 = encxkey[*n1 & 0xff] + id[*n2];
(*n2)++;
if(*n2 >= idlen) {
*n2 = 0;
*n1 += idlen;
}
tmp = *n1 & mask;
if(++i > 11) tmp %= cnt;
} while(tmp > cnt);
return(tmp);
}
void enctypex_func4(unsigned char *encxkey, unsigned char *id, int idlen) {
int i,
n1 = 0,
n2 = 0;
unsigned char t1,
t2;
if(idlen < 1) return;
for(i = 0; i < 256; i++) encxkey[i] = i;
for(i = 255; i >= 0; i--) {
t1 = enctypex_func5(encxkey, i, id, idlen, &n1, &n2);
t2 = encxkey[i];
encxkey[i] = encxkey[t1];
encxkey[t1] = t2;
}
encxkey[256] = encxkey[1];
encxkey[257] = encxkey[3];
encxkey[258] = encxkey[5];
encxkey[259] = encxkey[7];
encxkey[260] = encxkey[n1 & 0xff];
}
int enctypex_func7(unsigned char *encxkey, unsigned char d) {
unsigned char a,
b,
c;
a = encxkey[256];
b = encxkey[257];
c = encxkey[a];
encxkey[256] = a + 1;
encxkey[257] = b + c;
a = encxkey[260];
b = encxkey[257];
b = encxkey[b];
c = encxkey[a];
encxkey[a] = b;
a = encxkey[259];
b = encxkey[257];
a = encxkey[a];
encxkey[b] = a;
a = encxkey[256];
b = encxkey[259];
a = encxkey[a];
encxkey[b] = a;
a = encxkey[256];
encxkey[a] = c;
b = encxkey[258];
a = encxkey[c];
c = encxkey[259];
b += a;
encxkey[258] = b;
a = b;
c = encxkey[c];
b = encxkey[257];
b = encxkey[b];
a = encxkey[a];
c += b;
b = encxkey[260];
b = encxkey[b];
c += b;
b = encxkey[c];
c = encxkey[256];
c = encxkey[c];
a += c;
c = encxkey[b];
b = encxkey[a];
encxkey[260] = d;
c ^= b ^ d;
encxkey[259] = c;
return(c);
}
int enctypex_func7e(unsigned char *encxkey, unsigned char d) {
unsigned char a,
b,
c;
a = encxkey[256];
b = encxkey[257];
c = encxkey[a];
encxkey[256] = a + 1;
encxkey[257] = b + c;
a = encxkey[260];
b = encxkey[257];
b = encxkey[b];
c = encxkey[a];
encxkey[a] = b;
a = encxkey[259];
b = encxkey[257];
a = encxkey[a];
encxkey[b] = a;
a = encxkey[256];
b = encxkey[259];
a = encxkey[a];
encxkey[b] = a;
a = encxkey[256];
encxkey[a] = c;
b = encxkey[258];
a = encxkey[c];
c = encxkey[259];
b += a;
encxkey[258] = b;
a = b;
c = encxkey[c];
b = encxkey[257];
b = encxkey[b];
a = encxkey[a];
c += b;
b = encxkey[260];
b = encxkey[b];
c += b;
b = encxkey[c];
c = encxkey[256];
c = encxkey[c];
a += c;
c = encxkey[b];
b = encxkey[a];
c ^= b ^ d;
encxkey[260] = c; // encrypt
encxkey[259] = d; // encrypt
return(c);
}
int enctypex_func6(unsigned char *encxkey, unsigned char *data, int len) {
int i;
for(i = 0; i < len; i++) {
data[i] = enctypex_func7(encxkey, data[i]);
}
return(len);
}
int enctypex_func6e(unsigned char *encxkey, unsigned char *data, int len) {
int i;
for(i = 0; i < len; i++) {
data[i] = enctypex_func7e(encxkey, data[i]);
}
return(len);
}
void enctypex_funcx(unsigned char *encxkey, unsigned char *key, unsigned char *encxvalidate, unsigned char *data, int datalen) {
int i,
keylen;
keylen = strlen(key);
for(i = 0; i < datalen; i++) {
encxvalidate[(key[i % keylen] * i) & 7] ^= encxvalidate[i & 7] ^ data[i];
}
enctypex_func4(encxkey, encxvalidate, 8);
}
static int enctypex_data_cleaner_level = 2; // 0 = do nothing
// 1 = colors
// 2 = colors + strange chars
// 3 = colors + strange chars + sql
int enctypex_data_cleaner(unsigned char *dst, unsigned char *src, int max) {
static const unsigned char strange_chars[] = {
' ','E',' ',',','f',',','.','t',' ','^','%','S','<','E',' ','Z',
' ',' ','`','`','"','"','.','-','-','~','`','S','>','e',' ','Z',
'Y','Y','i','c','e','o','Y','I','S','`','c','a','<','-','-','E',
'-','`','+','2','3','`','u','P','-',',','1','`','>','%','%','%',
'?','A','A','A','A','A','A','A','C','E','E','E','E','I','I','I',
'I','D','N','O','O','O','O','O','x','0','U','U','U','U','Y','D',
'B','a','a','a','a','a','a','e','c','e','e','e','e','i','i','i',
'i','o','n','o','o','o','o','o','+','o','u','u','u','u','y','b',
'y' };
unsigned char c,
*p;
if(!dst) return(0);
if(dst != src) dst[0] = 0; // the only change in 0.1.3a
if(!src) return(0);
if(max < 0) max = strlen(src);
for(p = dst; (c = *src) && (max > 0); src++, max--) {
if(c == '\\') { // avoids the backslash delimiter
*p++ = '/';
continue;
}
if(enctypex_data_cleaner_level >= 1) {
if(c == '^') { // Quake 3 colors
//if(src[1] == 'x') { // ^x112233 (I don't remember the game which used this format)
//src += 7;
//max -= 7;
//} else
if(isdigit(src[1]) || islower(src[1])) { // ^0-^9, ^a-^z... a good compromise
src++;
max--;
} else {
*p++ = c;
}
continue;
}
if(c == 0x1b) { // Unreal colors
src += 3;
max -= 3;
continue;
}
if(c < ' ') { // other colors
continue;
}
}
if(enctypex_data_cleaner_level >= 2) {
if(c >= 0x7f) c = strange_chars[c - 0x7f];
}
if(enctypex_data_cleaner_level >= 3) {
switch(c) { // html/SQL injection (paranoid mode)
case '\'':
case '\"':
case '&':
case '^':
case '?':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case '-':
case ';':
case '~':
case '|':
case '$':
case '!':
case '<':
case '>':
case '*':
case '%':
case ',': c = '.'; break;
default: break;
}
}
if((c == '\r') || (c == '\n')) { // no new line
continue;
}
*p++ = c;
}
*p = 0;
return(p - dst);
}
// function not related to the algorithm, I have created it only for a quick handling of the received data
// very quick explanation:
// - if you use out it will be considered as an output buffer where placing all the IP and ports of the servers in the classical format: 4 bytes for IP and 2 for the port
// - if you don't use out the function will return a non-zero value if you have received all the data from the master server
// - if you use infobuff it will be considered as an output buffer where placing all the informations of one server at time in the format "IP:port \parameter1\value1\...\parameterN\valueN"
// - infobuff_size is used to avoid to write more data than how much supported by infobuff
// - infobuff_offset instead is used for quickly handling the next servers for infobuff because, as just said, the function handles only one server at time
// infobuff_offset is just the offset of the server to handle in our enctypex buffer, the function returns the offset to the next one or a value zero or -1 if there are no other hosts
// data and out can't be the same buffer because in some games like AA the gamespy master server returns
// 5 bytes for each IP/port and so there is the risk of overwriting the data to handle, that's why I use
// an output buffer which is at least "(datalen / 5) * 6" bytes long
int enctypex_decoder_convert_to_ipport(unsigned char *data, int datalen, unsigned char *out, unsigned char *infobuff, int infobuff_size, int infobuff_offset) {
#define enctypex_infobuff_check(X) \
if(infobuff) { \
if((int)(infobuff_size - infobuff_len) <= (int)(X)) { \
infobuff_size = 0; \
} else
typedef struct {
unsigned char type;
unsigned char *name;
} par_t;
int i,
len,
pars = 0, // pars and vals are used for making the function
vals = 0, // thread-safe when infobuff is not used
infobuff_len = 0;
unsigned char tmpip[6],
port[2],
t,
*p,
*o,
*l;
static const int use_parval = 1; // par and val are required, so this bool is useless
static unsigned char // this function is not thread-safe if you use it for retrieving the extra data (infobuff)
parz = 0,
valz = 0,
**val = NULL;
static par_t *par = NULL; // par[255] and *val[255] was good too
if(!data) return(0);
if(datalen < 6) return(0); // covers the 6 bytes of IP:port
o = out;
p = data;
l = data + datalen;
p += 4; // your IP
port[0] = *p++; // the most used port
port[1] = *p++;
if((port[0] == 0xff) && (port[1] == 0xff)) {
return(-1); // error message from the server
}
if(infobuff && infobuff_offset) { // restore the data
p = data + infobuff_offset;
} else {
if(p < l) {
pars = *p++;
if(use_parval) { // save the static data
parz = pars;
par = realloc(par, sizeof(par_t) * parz);
}
for(i = 0; (i < pars) && (p < l); i++) {
t = *p++;
if(use_parval) {
par[i].type = t;
par[i].name = p;
}
p += strlen(p) + 1;
}
}
if(p < l) {
vals = *p++;
if(use_parval) { // save the static data
valz = vals;
val = realloc(val, sizeof(unsigned char *) * valz);
}
for(i = 0; (i < vals) && (p < l); i++) {
if(use_parval) val[i] = p;
p += strlen(p) + 1;
}
}
}
if(use_parval) {
pars = parz;
vals = valz;
}
if(infobuff && (infobuff_size > 0)) {
infobuff[0] = 0;
}
while(p < l) {
t = *p++;
if(!t && !memcmp(p, "\xff\xff\xff\xff", 4)) {
if(!out) o = out - 1; // so the return is not 0 and means that we have reached the end
break;
}
len = 5;
if(t & 0x02) len = 9;
if(t & 0x08) len += 4;
if(t & 0x10) len += 2;
if(t & 0x20) len += 2;
tmpip[0] = p[0];
tmpip[1] = p[1];
tmpip[2] = p[2];
tmpip[3] = p[3];
if((len < 6) || !(t & 0x10)) {
tmpip[4] = port[0];
tmpip[5] = port[1];
} else {
tmpip[4] = p[4];
tmpip[5] = p[5];
}
if(out) {
memcpy(o, tmpip, 6);
o += 6;
}
enctypex_infobuff_check(22) {
infobuff_len = sprintf(infobuff,
"%u.%u.%u.%u:%hu ",
tmpip[0], tmpip[1], tmpip[2], tmpip[3],
(unsigned short)((tmpip[4] << 8) | tmpip[5]));
}}
p += len - 1; // the value in len is no longer used from this point
if(t & 0x40) {
for(i = 0; (i < pars) && (p < l); i++) {
enctypex_infobuff_check(1 + strlen(par[i].name) + 1) {
infobuff[infobuff_len++] = '\\';
infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, par[i].name, -1);
infobuff[infobuff_len++] = '\\';
infobuff[infobuff_len] = 0;
}}
t = *p++;
if(use_parval) {
if(!par[i].type) { // string
if(t == 0xff) { // inline string
enctypex_infobuff_check(strlen(p)) {
infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, p, -1);
}}
p += strlen(p) + 1;
} else { // fixed string
if(t < vals) {
enctypex_infobuff_check(strlen(val[t])) {
infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, val[t], -1);
}}
}
}
} else { // number (-128 to 127)
enctypex_infobuff_check(5) {
infobuff_len += sprintf(infobuff + infobuff_len, "%d", (signed char)t);
}}
}
}
}
}
if(infobuff) { // do NOT touch par/val, I use realloc
return(p - data);
}
}
if((out == data) && ((o - out) > (p - data))) { // I need to remember this
fprintf(stderr, "\nError: input and output buffer are the same and there is not enough space\n");
exit(1);
}
if(infobuff) { // do NOT touch par/val, I use realloc
parz = 0;
valz = 0;
return(-1);
}
return(o - out);
}
int enctypex_decoder_rand_validate(unsigned char *validate) {
int i,
rnd;
rnd = ~time(NULL);
for(i = 0; i < 8; i++) {
do {
rnd = ((rnd * 0x343FD) + 0x269EC3) & 0x7f;
} while((rnd < 0x21) || (rnd >= 0x7f));
validate[i] = rnd;
}
validate[i] = 0;
return(i);
}
unsigned char *enctypex_init(unsigned char *encxkey, unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
int a,
b;
unsigned char encxvalidate[8];
if(*datalen < 1) return(NULL);
a = (data[0] ^ 0xec) + 2;
if(*datalen < a) return(NULL);
b = data[a - 1] ^ 0xea;
if(*datalen < (a + b)) return(NULL);
memcpy(encxvalidate, validate, 8);
enctypex_funcx(encxkey, key, encxvalidate, data + a, b);
a += b;
if(!enctypex_data) {
data += a;
*datalen -= a; // datalen is untouched in stream mode!!!
} else {
enctypex_data->offset = a;
enctypex_data->start = a;
}
return(data);
}
unsigned char *enctypex_decoder(unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
unsigned char encxkeyb[261],
*encxkey;
encxkey = enctypex_data ? enctypex_data->encxkey : encxkeyb;
if(!enctypex_data || (enctypex_data && !enctypex_data->start)) {
data = enctypex_init(encxkey, key, validate, data, datalen, enctypex_data);
if(!data) return(NULL);
}
if(!enctypex_data) {
enctypex_func6(encxkey, data, *datalen);
return(data);
} else if(enctypex_data && enctypex_data->start) {
enctypex_data->offset += enctypex_func6(encxkey, data + enctypex_data->offset, *datalen - enctypex_data->offset);
return(data + enctypex_data->start);
}
return(NULL);
}
// exactly as above but with enctypex_func6e instead of enctypex_func6
unsigned char *enctypex_encoder(unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
unsigned char encxkeyb[261],
*encxkey;
encxkey = enctypex_data ? enctypex_data->encxkey : encxkeyb;
if(!enctypex_data || (enctypex_data && !enctypex_data->start)) {
data = enctypex_init(encxkey, key, validate, data, datalen, enctypex_data);
if(!data) return(NULL);
}
if(!enctypex_data) {
enctypex_func6e(encxkey, data, *datalen);
return(data);
} else if(enctypex_data && enctypex_data->start) {
enctypex_data->offset += enctypex_func6e(encxkey, data + enctypex_data->offset, *datalen - enctypex_data->offset);
return(data + enctypex_data->start);
}
return(NULL);
}
unsigned char *enctypex_msname(unsigned char *gamename, unsigned char *retname) {
static unsigned char msname[256];
unsigned i,
c,
server_num;
if(!gamename) return(NULL);
server_num = 0;
for(i = 0; gamename[i]; i++) {
c = tolower(gamename[i]);
server_num = c - (server_num * 0x63306ce7);
}
server_num %= 20;
if(retname) {
snprintf(retname, 256, "%s.ms%d.gamespy.com", gamename, server_num);
return(retname);
}
snprintf(msname, sizeof(msname), "%s.ms%d.gamespy.com", gamename, server_num);
return(msname);
}
int enctypex_wrapper(unsigned char *key, unsigned char *validate, unsigned char *data, int size) {
int i;
unsigned char *p;
if(!key || !validate || !data || (size < 0)) return(0);
p = enctypex_decoder(key, validate, data, &size, NULL);
if(!p) return(-1);
for(i = 0; i < size; i++) {
data[i] = p[i];
}
return(size);
}
// data must be enough big to include the 23 bytes header, remember it: data = realloc(data, size + 23);
int enctypex_quick_encrypt(unsigned char *key, unsigned char *validate, unsigned char *data, int size) {
int i,
rnd,
tmpsize,
keylen,
vallen;
unsigned char tmp[23];
if(!key || !validate || !data || (size < 0)) return(0);
keylen = strlen(key); // only for giving a certain randomness, so useless
vallen = strlen(validate);
rnd = ~time(NULL);
for(i = 0; i < sizeof(tmp); i++) {
rnd = (rnd * 0x343FD) + 0x269EC3;
tmp[i] = rnd ^ key[i % keylen] ^ validate[i % vallen];
}
tmp[0] = 0xeb; // 7
tmp[1] = 0x00;
tmp[2] = 0x00;
tmp[8] = 0xe4; // 14
for(i = size - 1; i >= 0; i--) {
data[sizeof(tmp) + i] = data[i];
}
memcpy(data, tmp, sizeof(tmp));
size += sizeof(tmp);
tmpsize = size;
enctypex_encoder(key, validate, data, &tmpsize, NULL);
return(size);
}