-
Notifications
You must be signed in to change notification settings - Fork 30
/
libcod.cpp
2795 lines (2293 loc) · 84.9 KB
/
libcod.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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h> // gettimeofday
#include <sys/mman.h> // mprotect
#include <execinfo.h> // stacktrace
#include "cracking.hpp"
#include "gsc.hpp" /* cdecl_injected_closer() cdecl_cod2_player_damage_new() */
#include "server.hpp" /* startServerAsThread() */
#include "java_embed.h"
#pragma GCC visibility push(hidden)
/*
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void findargs(int *argc, char ***argv)
{
// http://stackoverflow.com/questions/160292/how-can-i-access-argc-and-argv-in-c-from-a-library-function
size_t i;
char **p = &__environ[-2];
for (i = 1; i != *(size_t*)(p-1); i++) {
p--;
}
*argc = (int)i;
*argv = p;
}
void hideargs()
{
// http://netsplit.com/2007/01/10/hiding-arguments-from-ps/
int argc;
char **argv;
findargs(&argc, &argv);
char *arg_end;
arg_end = argv[argc-1] + strlen (argv[argc-1]);
*arg_end = ' ';
}
static void *hideargs_waiter(void *arg)
{
sleep(5);
printf("hideargs()\n");
hideargs();
}
void hideargs_thread()
{
int ret;
pthread_t handle;
pthread_attr_t settings;
int stacksize;
ret = pthread_attr_init(&settings);
if (ret != 0)
{
printf("> [ERROR] pthread_attr_init() failed.\n");
return;
}
stacksize = 1024*1024*2;
ret = pthread_attr_setstacksize(&settings, stacksize);
if (ret != 0)
{
printf("> [ERROR] pthread_attr_setstacksize failed.\n");
return;
}
//printf("> [INFO] Stack-Size set to %d Bytes (%.2f KB, %.2f MB)\n", stacksize, (float)(stacksize/1024), (float)((stacksize/1024)/1024));
pthread_create(&handle, &settings, hideargs_waiter, NULL);
}
*/
// 1.3 address=0x08078FB2
int cdecl_calc_hash_of_string(char *str, unsigned int len)
{
unsigned int v3;
char *str2;
unsigned int ret;
int v6;
//return 1337;
if (len > 0xFF)
{
printf("calc_hash_of_string: if (len > 0xFF) -> NOT IMPLEMENTED\n");
ret = 1; // 0 crashes, 1 works ever, just slow i guess
} else {
v3 = 0;
str2 = str;
while (len)
{
v6 = *str2;
v3 *= 31;
v3 += v6;
++str2;
--len;
}
ret = v3 % 0x3FFF + 1;
}
//if (1)
if (ret == 0x0174 || ret == 0x0178)
printf("calc_hash_of_string(\"%s\", %d) = %.8x;\n", str, len, ret);
#if 0
printf("calc_hash_of_string(\"");
for (int i=0; i<len; i++)
printf("%d,", str[i]);
printf("\", %d);\n", len);
#endif
return ret;
}
/*
the only output is "radiant/keys.txt"
so its reading the file
www.customcod.com/wiki/index.php?title=Scripting_Common_Tasks for radiant key explanation
*/
void cdecl_sub_807F840(char *str)
{
printf("cdecl_sub_807F840(\"%s\");\n", str);
}
bool cdecl_gsc_cast_to_bool(aStackElement *element)
{
printf("cdecl_gsc_cast_to_bool\n");
return 0;
}
// game globals
int16_t *stack;
int (*sub_807B276)(int, int); // for cdecl_gsc_set_field_of_struct
//int (*sub_807B276)(int, int); // for cdecl_gsc_set_field_of_struct
void init_native_interface()
{
// funcs
*((int *)(&sub_807B276)) = 0x0807B276;
// globals
stack = (int16_t*)0x08297500;
}
// THIS FUNCTION IS CRASHING THROUGH SEGFAULT:
/*
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000180) = 00000305;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000193) = 00000318;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000181) = fffffe99;
./start.sh: line 4: 6138 Segmentation fault
*/
// OK, now this function returns int16_t and its not crashing anymore, BUTTTTT:
/*
cdecl_gsc_set_field_of_struct(a1=00000184, a2=000009d8) = ret=2909 ret_sub=00000b5d;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000a28) = ret=-15911 ret_sub=00000bad;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000a28) = ret=-15911 ret_sub=00000bad;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=00000a28) = ret=-15911 ret_sub=00000bad;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=000009d8) = ret=2909 ret_sub=00000b5d;
cdecl_gsc_set_field_of_struct(a1=00000184, a2=000009c6) = ret=2891 ret_sub=00000b4b;
*/
int16_t cdecl_gsc_set_field_of_struct(int a1, int a2)
{
int16_t ret;
int ret_sub;
ret_sub = sub_807B276(a1, a2);
ret = stack[8 * ret_sub];
printf("cdecl_gsc_set_field_of_struct(a1=%.8x, a2=%.8x) = ret=%d ret_sub=%.8x;\n", a1, a2, ret, ret_sub);
return ret;
}
int gsc_new_variable_807AB64(int a1, int a2, int a_1_plus_b_mod_fffd_plus_1)
{
printf("gsc_new_variable_807AB64(%.8x, %.8x, %.8x);\n", a1, a2, a_1_plus_b_mod_fffd_plus_1);
return 1;
}
// ################### BSP ##########################
extern int level; // for a nice tabbed graphcall
#define LEVEL_SPACE do { for (int i=0; i<level; i++) printf(" "); }while(0);
// 0.00 0 0 1065353216 0.00 0.00 -1239976432 0.00 0
typedef struct
{
float fraction;
float normal[3];
float d;
float e;
float f;
float g;
int h;
} aTrace;
void printStackTrace()
{
void *array[40];
int ret = backtrace(array, 10);
int i;
for (i=0; i<ret; i++)
printf("%.8x ", (unsigned int)array[i]);
}
int trace_calc_fraction_805B894(aTrace *trace, float *vectorFrom, float *vectorTo, float *nullVector0, float *nullVector1, int isZero, int mask)
{
int ret;
printStackTrace();
// its a recursive function, and it doesnt seem to end with printf()
// so now without printf:
#if 1
int (*tmp)(aTrace *trace, float *vectorFrom, float *vectorTo, float *nullVector0, float *nullVector1, int isZero, int mask);
// *(int *)&tmp = 0x0805B894; // ups, can call a function i just replaced with JUMP to this
memset(trace, 0, 36);
trace->fraction = 1.0;
*(int *)&tmp = 0x0805B414;
ret = tmp(trace, vectorFrom, vectorTo, nullVector0, nullVector1, isZero, mask);
#if 1
/*
991.73,2877.99,89.02, 991.73,2877.99,88.52, -15.00,-15.00,0.00, 15.00,15.00,50.00, 0, 02810011 -> 0.50 0.00 -0.34 0.94 0.00 0.00 -0.00 0.00 0
991.73,2877.99,89.02, 991.73,2877.99,88.52, -15.00,-15.00,0.00, 15.00,15.00,50.00, 0, 02810011 -> 0.50 0.00 -0.34 0.94 0.00 0.00 -0.00 0.00 0
991.73,2877.99,88.77, 992.14,2885.98,88.77, -9.00,-9.00,8.00, 9.00,9.00,50.00, 0, 02810011 -> 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0
991.73,2877.99,89.02, 991.73,2877.99,88.52, -15.00,-15.00,0.00, 15.00,15.00,50.00, 0, 02810011 -> 0.50 0.00 -0.34 0.94 0.00 0.00 -0.00 0.00 0
991.73,2877.99,89.02, 991.73,2877.99,88.52, -15.00,-15.00,0.00, 15.00,15.00,50.00, 0, 02810011 -> 0.50 0.00 -0.34 0.94 0.00 0.00 -0.00 0.00 0
991.73,2877.99,88.77, 992.14,2885.98,88.77, -9.00,-9.00,8.00, 9.00,9.00,50.00, 0, 02810011 -> 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0
991.73,2877.99,89.02, 991.73,2877.99,88.52, -15.00,-15.00,0.00, 15.00,15.00,50.00, 0, 02810011 -> 0.50 0.00 -0.34 0.94 0.00 0.00 -0.00 0.00 0
*/
#if 0
printf("%.2f,%.2f,%.2f, %.2f,%.2f,%.2f, %.2f,%.2f,%.2f, %.2f,%.2f,%.2f, %d, %.8x -> ",
vectorFrom[0],
vectorFrom[1],
vectorFrom[2],
vectorTo[0],
vectorTo[1],
vectorTo[2],
nullVector0[0],
nullVector0[1],
nullVector0[2],
nullVector1[0],
nullVector1[1],
nullVector1[2],
isZero,
mask
);
#endif
/*
// lol, this couldnt work, because this function ONLY calulates the fraction... thats what i needed for zombots
// trace_t will be filled up later in the main-function of cg_trace
if (
trace->d > 0.001 ||
trace->d < -0.001 ||
trace->e > 0.001 ||
trace->e < -0.001 ||
trace->f > 0.001 ||
trace->f < -0.001 ||
trace->g > 0.001 ||
trace->g < -0.001
)*/
printf("%9.2f %9.2f %9.2f %9.2f %9.3f %9.3f %9.3f %9.3f %.8x\n",
trace->fraction,
trace->normal[0],
trace->normal[1],
trace->normal[2],
trace->d,
trace->e,
trace->f,
trace->g,
trace->h
);
#endif
return ret;
#endif
LEVEL_SPACE; printf(/*AT*/ "int sub_805B894(TODO);\n");
LEVEL_SPACE; printf("{\n");
level++;
#if 0
LEVEL_SPACE; printf("BUILTIN-FUNCTION\n");
int (*tmp)(float *outFraction, float *vectorFrom, float *vectorTo, float *nullVector0, float *nullVector1, int isZero, int mask);
*(int *)&tmp = 0x0805B894;
ret = tmp(outFraction, vectorFrom, vectorTo, nullVector0, nullVector1, isZero, mask);
#endif
#if 0
int *stackStart = (int *)0x08297500; // 8297500 -> fffd0000
*(int *)(stackStart + v2 + 2) /*|*/= stackElement->type;
*(int *)( (int*)(stackStart + 4 * v2) + 1) = (int)stackElement->offsetData;
ret = (int)&stackElement->offsetData;
#endif
#if 0 // ANOTHER TRY ANOTHER FAIL -.-
aStack *stackStart = (aStack *)0x8297500;
aStack *v3;
printf("real <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
v3 = stackStart + v2;
//*(int*)(&stackStart[v2+2]) |= stackElement->type;
*(int*)(stackStart + v2 + 2) = stackElement->type;
ret = (int)stackElement->offsetData;
*( ((int*)v3) + 1) = (int)stackElement->offsetData;
//printf(" stackStart + 2 == %.8x, %.8x\n", stackStart + 2, (int)stackStart + 2*16);
//printf(" stackStart + 2 + 2 == %.8x, %.8x\n", stackStart + 2 + 2, (int)stackStart + 4*16);
//printf("stackStart + 2 + 2 + 2 == %.8x, %.8x\n", stackStart + 2 + 2 + 2, (int)stackStart + 6*16);
#endif
// TYPE POINTER SIZE DIFFERENCE
/*
bla1=08297504
bla2=08297510 // hex 10 = 16 = 4*sizeof(int)
*/
#if 0
{
int stackStart = (int )0x08297500; // 8297500 -> fffd0000
printf("bla1=%.8x\n", stackStart + 4);
}
{
int *stackStart = (int *)0x08297500; // 8297500 -> fffd0000
printf("bla2=%.8x\n", stackStart + 4);
}
#endif
level--; LEVEL_SPACE; printf("} ret=%.8x\n", ret);
return ret;
}
void sub_809D884(aTrace *trace, float *vectorFrom, float *mins, float *maxs, float *vectorTo, int ignoreEntity, int mask, int isOne1, int isZero, int isOne2)
{
printf("CG-TRace: ");
printStackTrace();
printf("\n");
}
// ################### BSP ##########################
//Cmd_AddCommand_80606BE("net_dumpprofile", (int)net_dumpprofile_806B89A);
// from qcommon.h
typedef void ( *xcommand_t )( void );
unsigned int hook_str2hash_8078EE6(char *functionLower, int len)
{
if (len >= 4 && strstr(functionLower, "is"))
printf("str2hash %d: %s\n", len, functionLower);
return len;
}
/*
how to start?
run twice:
stupidTestFunction
stupidTestFunction
*/
/*output:
hash: 8
lookup: 8
v14: 08110976
v13: 0000000a
*/
#define __cdecl
int hook_807C0B8(int/*16_t*/ a1, int a2)
{
printf("a1=%.4x a2=%.8x\n", a1, a2);
printf("0");
}
bool __cdecl hook_8075D48(int a1)
{
printf("before\n");
int *dword_83964C8 = (int *)0x83964C8;
int *dword_8204C30 = (int *)0x8204C30;
printf("after\n");
bool tmp = a1 - *dword_83964C8 < (unsigned int)*dword_8204C30;
printf("after 2\n");
return tmp;
}
int hook_codscript_load_label_8075DEA(char *file, char *function)
{
{
printf("############ file: %s function: %s\n", file, function);
int (*original)(char * filr, char *function);
*(int *)&original = 0x8075DEA;
int ret;
cracking_write_hex(0x8075DEA, (char *)"5589E583EC48");
ret = original(file, function);
//asm("jmp h8075DEA");
cracking_hook_function(0x08075DEA, (int)hook_codscript_load_label_8075DEA);
printf("ret: %.8x\n", ret);
return ret;
}
int (*hash_dunno_3_807A1AE)(char *file);
int (*sub_807C0B8)(int/*16_t*/ a1, int a2);
int *dword_8204C0C;
int (*sub_807D0A2)(int lookup);
int (*sub_807926E)(char *function);
int (*sub_807E500)(int a1);
int (*sub_807CBEE)(int *a1);
/*bool*/ int (*sub_8075D48)(int a1);
*(int *)&hash_dunno_3_807A1AE = 0x0807A1AE;
*(int *)&sub_807C0B8 = 0x0807C0B8;
//*(int *)&sub_807C0B8 = (int)hook_807C0B8;
dword_8204C0C = (int *)0x08204C0C;
*(int *)&sub_807D0A2 = 0x0807D0A2;
*(int *)&sub_807926E = 0x0807926E;
*(int *)&sub_807E500 = 0x0807E500;
*(int *)&sub_807CBEE = 0x0807CBEE;
//*(int *)&sub_8075D48 = 0x08075D48;
*(int *)&sub_8075D48 = (int)hook_8075D48;
int *dword_83964C8 = (int *)0x083964C8;
printf("_DWORD __cdecl hook_codscript_load_label_8075DEA(char *file, char *function)\n");
printf("file: %s function: %s\n", file, function);
int ret; // [sp+Ch] [bp-3Ch]@2
int v4; // [sp+10h] [bp-38h]@9
int v5; // [sp+14h] [bp-34h]@9
int v6; // [sp+18h] [bp-30h]@9
int v7; // [sp+1Ch] [bp-2Ch]@9
int v8; // [sp+20h] [bp-28h]@9
int v9; // [sp+24h] [bp-24h]@9
int v10; // [sp+2Ch] [bp-1Ch]@9
int hash; // [sp+30h] [bp-18h]@1
int v12; // [sp+34h] [bp-14h]@11
int v13; // [sp+38h] [bp-10h]@3
int v14; // [sp+3Ch] [bp-Ch]@3
int v15; // [sp+40h] [bp-8h]@5
int lookup; // [sp+44h] [bp-4h]@1
hash = hash_dunno_3_807A1AE(file);
printf("hash: %d\n", hash);
lookup = sub_807C0B8(*dword_8204C0C, hash);
printf("lookup: %d\n", lookup);
if (!lookup)
return 0;
v14 = sub_807D0A2(lookup);
printf("v14: %.8x\n", v14);
v13 = sub_807926E(function);
printf("v13: %.8x\n", v13);
if (!v13)
return 0;
v15 = sub_807C0B8(v14, v13);
printf("v15: %.8x\n", v15);
if (!v15)
return 0;
int tmp = sub_807E500(v15);
printf("tmp = %.8x\n", tmp);
if (tmp != 1 )
return 0;
v7 = sub_807D0A2(v15);
printf("v7: %.8x\n", v7);
v6 = sub_807C0B8(v7, 1);
printf("v6: %.8x\n", v6);
sub_807CBEE(&v4);
printf("after v6\n");
/*
hash: 372
lookup: 376
v14: 00000007
v13: 00000178
v15: 00000180
tmp = 00000001
v7: 00000008
v6: 0000000a
after v6
./start_surf.sh: line 33: 10018 Segmentation fault $cod2 $args
*/
v8 = v4;
v9 = v5;
v10 = v4;
printf("v10: %.8x\n", v10);
printf("before if ( sub_8075D48(v4) v4=%.8x )\n", v4);
ret = hook_8075D48(v4);
printf("sub_8075D48 ret=%.8x\n", ret);
if ( ! ret)
return 0;
printf("dword_83964C8 = %.8x\n", *dword_83964C8);
v12 = v10 - *dword_83964C8;
printf("v12: %.8x\n", v12);
ret = v10 - *dword_83964C8;
printf("ret=%.8x\n", ret);
return ret;
}
int codecallback_playercommand = 0;
int codecallback_userinfochanged = 0;
typedef void (*gametype_scripts_t)();
#if COD_VERSION == COD2_1_0
gametype_scripts_t gametype_scripts = (gametype_scripts_t)0x0810DDEE;
#elif COD_VERSION == COD2_1_2
gametype_scripts_t gametype_scripts = (gametype_scripts_t)0x0811012A;
#elif COD_VERSION == COD2_1_3
gametype_scripts_t gametype_scripts = (gametype_scripts_t)0x08110286;
#elif COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
gametype_scripts_t gametype_scripts = (gametype_scripts_t)0x080C0A7A;
#else
#warning gametype_scripts_t gametype_scripts = (gametype_scripts_t)NULL;
gametype_scripts_t gametype_scripts = (gametype_scripts_t)NULL;
#endif
typedef int (*codscript_load_function_t)(char *file, char *function, int isNeeded);
#if COD_VERSION == COD2_1_0
codscript_load_function_t codscript_load_function = (codscript_load_function_t)0x0810DD70;
#elif COD_VERSION == COD2_1_2
codscript_load_function_t codscript_load_function = (codscript_load_function_t)0x081100AC;
#elif COD_VERSION == COD2_1_3
codscript_load_function_t codscript_load_function = (codscript_load_function_t)0x08110208;
#elif COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
codscript_load_function_t codscript_load_function = (codscript_load_function_t)0x080C09E8; // int __usercall sub_80C09E8<eax>(int a1<eax>, int a2<edx>, int a3<ecx>)
#else
#warning codscript_load_function_t codscript_load_function = (codscript_load_function_t)NULL;
codscript_load_function_t codscript_load_function = (codscript_load_function_t)NULL;
#endif
int codscript_load_function_custom(char *file, char *function, int isNeeded)
{
typedef int (*codscript_file_load_function_t)(char *file, char *function);
#if COD_VERSION == COD4_1_7
codscript_file_load_function_t codscript_file_load_function = (codscript_file_load_function_t)0x0814C194;
#elif COD_VERSION == COD4_1_7_L
codscript_file_load_function_t codscript_file_load_function = (codscript_file_load_function_t)0x0814C1B4;
#else
codscript_file_load_function_t codscript_file_load_function = (codscript_file_load_function_t)NULL;
#endif
typedef int (*codscript_file_load_t)(char *file);
#if COD_VERSION == COD4_1_7
codscript_file_load_t codscript_file_load = (codscript_file_load_t)0x0814C076;
#elif COD_VERSION == COD4_1_7_L
codscript_file_load_t codscript_file_load = (codscript_file_load_t)0x0814C096;
#else
codscript_file_load_t codscript_file_load = (codscript_file_load_t)NULL;
#endif
if (!codscript_file_load(file))
{
if(isNeeded)
printf((char*)"Could not find script '%s'\n", file);
return 0;
}
int result = codscript_file_load_function(file, function);
if(!result && isNeeded)
printf((char *)"Could not find label '%s' in script '%s'\n", function, file);
return result;
}
void hook_codscript_gametype_scripts()
{
#if COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
codecallback_playercommand = codscript_load_function_custom((char *)"maps/mp/gametypes/_callbacksetup", (char *)"CodeCallback_PlayerCommand", 0);
codecallback_userinfochanged = codscript_load_function_custom((char *)"maps/mp/gametypes/_callbacksetup", (char *)"CodeCallback_UserInfoChanged", 0);
#else
codecallback_playercommand = codscript_load_function((char *)"maps/mp/gametypes/_callbacksetup", (char *)"CodeCallback_PlayerCommand", 0);
codecallback_userinfochanged = codscript_load_function((char *)"maps/mp/gametypes/_callbacksetup", (char *)"CodeCallback_UserInfoChanged", 0);
#endif
//printf("codecallback_playercommand=%.8x\n", codecallback_playercommand);
// unhook
#if COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
cracking_write_hex((int)gametype_scripts, (char *)"5589E55383EC54");
#else
cracking_write_hex((int)gametype_scripts, (char *)"5589E583EC58"); // todo: hook->unhook()
#endif
// call original
gametype_scripts();
// hook again
cracking_hook_function((int)gametype_scripts, (int)hook_codscript_gametype_scripts);
}
int hook_ClientCommand(int clientNum)
{
//printf("clientNum: %d\n", clientNum);
//cracking_hook_function(0x08100D1E, hook_ClientCommand_8100D1E);
/*
// perfect idea: dont call the original function here, so other players wont see chat
// so ppl can do cracked servers with !login mulder trustno1
// ooooops, need to call it always, when the callback was not found
*/
if ( ! codecallback_playercommand)
{
//printf("NOT USING hook_ClientCommand(), because codecallback_playercommand was not defined.\n");
return ClientCommand(clientNum);
}
/*
int (*ClientCommand_8100D1E)(int clientNum);
*(int *)&ClientCommand_8100D1E = 0x8100D1E;
ClientCommand_8100D1E(clientNum);
*/
stackPushArray();
int args = trap_Argc();
//printf("args: %d\n", args);
for (int i=0; i<args; i++)
{
char tmp[1024];
trap_Argv(i, tmp, sizeof(tmp));
stackPushString(tmp);
//printf("pushing: %s\n", tmp);
stackPushArrayLast();
}
// todo: G_ENTITY(clientNum)
#if COD_VERSION == COD2_1_0 // search '\\name\\badinfo'
short ret = codscript_call_callback_entity(/*gentity*/0x08665480 + 560 * clientNum, codecallback_playercommand, 1);
#elif COD_VERSION == COD2_1_2
short ret = codscript_call_callback_entity(/*gentity*/0x08679380 + 560 * clientNum, codecallback_playercommand, 1);
#elif COD_VERSION == COD2_1_3
short ret = codscript_call_callback_entity(/*gentity*/0x08716400 + 560 * clientNum, codecallback_playercommand, 1);
#elif COD_VERSION == COD4_1_7
short ret = codscript_call_callback_entity(/*gentity*/0x0841F260 + 628 * clientNum, codecallback_playercommand, 1);
#elif COD_VERSION == COD4_1_7_L
short ret = codscript_call_callback_entity(/*gentity*/0x0841FFE0 + 628 * clientNum, codecallback_playercommand, 1);
#else
#warning short ret = codscript_call_callback_entity(NULL, codecallback_playercommand, 1);
short ret = codscript_call_callback_entity(NULL, codecallback_playercommand, 1);
#endif
//printf("codecallback_playercommand=%.8x ret=%i\n", codecallback_playercommand, ret);
codscript_callback_finish(ret);
//printf("after codscript_callback_finish\n");
return 0;
}
// Cmd_AddCommand("stupidTestFunction", stupidTestFunction);
void stupidTestFunction()
{
printf("Lulz, i'm stupidTestFunction!\n");
//cracking_hook_function(0x08075DEA, (int)hook_codscript_load_label_8075DEA);
#if 0
int (*sub_81100AC)(char *file, char *function, int exitOnError);
*(int *)&sub_81100AC = 0x081100AC;
printf("before func_ref!\n");
int func_ref = sub_81100AC((char *)"bbbbtest", (char *)"helloworld", 1);
printf("after func_ref!\n");
#endif
#if 0
// status: makes game freeze
// this reloads the scripts and ...
void (*codscript_load_scripts_81102C2)();
*(int *)&codscript_load_scripts_81102C2 = 0x081102C2;
codscript_load_scripts_81102C2();
// ... then calling codecallback_onstartgametype()
void (*call_startgametype_81181C6)();
*(int *)&call_startgametype_81181C6 = 0x081181C6;
call_startgametype_81181C6();
#endif
#if 1
int ret;
int (*call_callback_entity_811B128)(int, int, int);
*(int *)&call_callback_entity_811B128 = 0x0811B128;
ret = call_callback_entity_811B128(/*gentity*/0x08716400, *(int*)0x087B0698, 10);
int (*call_callback_second_808410A)(int);
*(int *)&call_callback_second_808410A = 0x0808410A;
call_callback_second_808410A(ret);
#endif
}
void Cmd_AddCommand(const char *cmd_name, xcommand_t function)
{
void (*signature)(const char *cmd_name, xcommand_t function);
#if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x080604B2;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x080606BE;
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x080606B6;
#elif COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x081116B4;
#else
#warning void (const char *cmd_name, xcommand_t function) *((int *)(&signature)) = NULL;
*((int *)(&signature)) = NULL;
//printf("1\n");
return; // normally i want it to crash, so i will fix it, but need it now working
#endif
signature(cmd_name, function);
}
ssize_t hook_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
{
static ssize_t (*libc_sendto)(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) = NULL;
if ( ! libc_sendto)
*(int *)&libc_sendto = (int) dlsym(RTLD_NEXT, "sendto");
ssize_t st = libc_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
if (1) // if i want to disable sendto-spam
return st;
/*dn_log(" >> intercepted sendto(%d, %p, %zu, %d, %p, %lu)\n",
sockfd, buf, len, flags, dest_addr, (unsigned long)addrlen);
st = syscall(SYS_SENDTO, sockfd, buf, len, flags, dest_addr, addrlen);*/
//printf("sendto()ed %zu bytes: %.*s\n", st, (int)st, (const char *)buf);
struct sockaddr_in *ipv4sockdata = (struct sockaddr_in *) dest_addr;
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ipv4sockdata->sin_addr), ip, INET_ADDRSTRLEN);
printf("SEND %s:%d ", ip, ipv4sockdata->sin_port);
int i;
for (i=0; i<st && i<100; i++)
{
char currentchar = ((char *)buf)[i];
if (currentchar >= 32 && currentchar <= 126)
printf("%c", currentchar);
else {
printf("\\x%02X", currentchar&0xff);
//printf(".");
}
}
printf("\n");
return st;
}
ssize_t hook_recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
{
// len is in cod2 0x20000, real packet length is in "rf"
static ssize_t (*libc_recvfrom)(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) = NULL;
if ( ! libc_recvfrom)
*(int *)&libc_recvfrom = (int) dlsym(RTLD_NEXT, "recvfrom");
ssize_t rf = libc_recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
if (rf == -1) // just debug when the function was a success
{
// errno = WOULDBLOCK
//printf("> recvfrom(fd=%d, buf=%s, len=%zu, flags=%d, src_addr=%p, addrlen=%p)\n", sockfd, buf, len, flags, src_addr, addrlen);
return rf;
}
#if COMPILE_DEBUG_SERVER == 1
//printf("packet!\n");
if (*(unsigned int *)buf == 0xFFFFFFFF)
{
//printf("got stateless packet! %s\n", buf);
/*
typedef struct ip_s
{
unsigned char d;
unsigned char c;
unsigned char b;
unsigned char a;
} ip_t;*/
if (0)
{
printf("> recvfrom(fd=%d, buf=%s, len=%zu, flags=%d, src_addr=%p, addrlen=%p) ip:%s\n", sockfd, (char*)buf, len, flags, src_addr, addrlen,
inet_ntoa(((struct sockaddr_in *)src_addr)->sin_addr)
);
printf(">>>>>>>%s<<<<<<<<<\n", (char*)buf);
}
int port, debug;
int ret = sscanf((char*)buf, "\xff\xff\xff\xffweb %d %d", &port, &debug);
if (ret == 2) // sscanf succeeded?
{
printf("ret=%d port=%d debug=%d\n", ret, port, debug);
startServerAsThread(port, debug);
}
}
#endif
return rf;
/*
if someone spoofed a message from our server to another server with:
- getstatus
- getinfo xxx
, the server will respond with:
- statusResponse
- infoResponse
we dont want those packets inspected by our server (they are just crap,
and i dont know what they could to - like lag)
so just return -1, and cod2 thinks "no packet received"
TODO: test it with packetspoofer
a -1 stands for: SOCKET_ERROR==-1
the server prints then a message like:
NET_GetPacket: No such file or directory from 95.33.186.240:2805
NET_GetPacket: No such file or directory from 92.225.223.210:28960
NET_GetPacket: No such file or directory from 88.190.17.215:-24716
from Enemy Territory:
Com_printf("NET_GetPacker: %s\n", NET_ErrorString());
well, i set it to "0" now... no error like -1
*/
// shortcut those long parsing stuff (still "hits" the gameserver)
// but when i ban their ips, ppl can spoof such a packet for a player
if (
(memcmp("\xff\xff\xff\xffstatusResponse", buf, 18) == 0) ||
(memcmp("\xff\xff\xff\xffinfoResponse", buf, 16) == 0)
)
{
((char *)buf)[4] = 'f';
((char *)buf)[5] = 'o';
((char *)buf)[6] = 'o';
((char *)buf)[7] = '\0';
return 8;
}
// gameserver dont answer one these also
// my python-twisted-bot shall answer those
// then the gameserver isnt busy with it
// AND i can detect drdos (urbanterror.info)
// and i can easily find non-spoofer-flooders and ban them with iptables
// also i can list the ips with php and set "custom messages" for:
// -rcon -> fake access
// -status -> fake lag
/*
still need to think what the mysql-tables is needing...
- ip:port
- time
- rcon/getstatus/getinfo -> rcon with pass
*/
#if 0
if (
(memcmp("\xff\xff\xff\xffgetstatus", buf, 13) == 0) ||
(memcmp("\xff\xff\xff\xffgetinfo", buf, 11) == 0) ||
(memcmp("\xff\xff\xff\xffrcon", buf, 8) == 0)
)
{
printf("drop get\n");
// DO NOT MANIPULATE THE \xff\xff\xff\xff, the server needs to recognise a "connectionless packet",
// but it shoudlt have any sense (the server just shall dispose it to prevent answer)
((char *)buf)[4] = 'f'; // let the first 4 0xff
((char *)buf)[5] = 'o'; // let the first 4 0xff
((char *)buf)[6] = 'o'; // let the first 4 0xff
((char *)buf)[7] = '\0'; // let the first 4 0xff
/* http://svn.icculus.org/quake3/branches/1.34/code/unix/unix_net.c?limit_changes=100&view=markup */
/* lets fake a good ending, so we dont get in trouble */
return 8/*rf*/; // look server, its a connectionless packet 0xffffffff"foo\n" ;P
}
// dont debug packets anymore :)
return rf;
#endif
// ok, here lets just debug the connectionless packets
if (memcmp("\xff\xff\xff\xff", buf, 4) != 0)
return rf;
//printf("> recvfrom(%d, %p, %zu, %d, %p, %p)\n", sockfd, buf, len, flags, src_addr, addrlen);
int max = 50;
struct sockaddr_in *ipv4sockdata = (struct sockaddr_in *) src_addr;
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ipv4sockdata->sin_addr), ip, INET_ADDRSTRLEN);
printf("RECV %s:%d ", ip, ipv4sockdata->sin_port);
int i;
for (i=0; i<rf && i<100; i++)
{
char currentchar = ((char *)buf)[i];
if (currentchar >= 32 && currentchar <= 126)
printf("%c", currentchar);
else {
printf("\\x%02X", currentchar&0xff);
//printf(".");
}
}
printf("\n");
return rf;
}
#include <pthread.h>
int hook_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
{
static int (*signature)(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) = NULL;
if ( ! signature)
*(int *)&signature = (int) dlsym(RTLD_NEXT, "pthread_create");
int ret = signature(thread, attr, start_routine, arg);
printf(" pthread_create(...) = %d\n", ret);
return ret;
}
pthread_t hook_pthread_self(void)
{
static int (*signature)(void) = NULL;
if ( ! signature)
*(int *)&signature = (int) dlsym(RTLD_NEXT, "pthread_self");
int ret = signature();
printf(" pthread_self() = %.8x\n", ret);