-
Notifications
You must be signed in to change notification settings - Fork 443
/
AutoBan.cpp
1543 lines (1322 loc) · 39.7 KB
/
AutoBan.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 "gb-include.h"
#include "AutoBan.h"
#include <limits.h>
#include "SafeBuf.h"
#include "Pages.h"
#include "Loop.h"
#include "sort.h"
#include "Users.h"
AutoBan g_autoBan;
void resetHash(int fd, void *state);
static int32_t *SorterTable;
//static ull_t *SorterTable2;
// #define EXPLICIT_ALLOW 0x8000000000000000ULL
// #define BANNED 0x4000000000000000ULL
// #define COUNT_OVERFLOW 0x0040040000000000ULL
// #define TOP_LONG_MASK 0x7fffffff00000000ULL
// #define DAY_COUNT_MASK 0x0000ffff00000000ULL
AutoBan::AutoBan() {
m_detectKeys = NULL;
m_detectVals = NULL;
m_tableSize = 0;
}
AutoBan::~AutoBan ( ) {
reset();
}
void AutoBan::reset ( ) {
if ( m_detectKeys ) {
mfree ( m_detectKeys, m_tableSize * sizeof(int32_t), "AutoBanK" );
m_detectKeys = NULL;
}
if ( m_detectVals ) {
mfree ( m_detectVals, m_tableSize * sizeof(DetectVal),
"AutoBanV" );
m_detectVals = NULL;
}
m_ht.reset();
}
bool AutoBan::init() {
m_tableSize = AUTOBAN_INITSIZE;
m_numEntries = 0;
m_detectKeys = (int32_t*)mmalloc(m_tableSize * sizeof(int32_t),
"AutoBan");
if(!m_detectKeys) {
return false;
}
m_detectVals = (DetectVal*)mmalloc(m_tableSize *
sizeof(DetectVal),
"AutoBan");
if(!m_detectVals) {
mfree (m_detectKeys, m_tableSize * sizeof(int32_t), "AutoBan");
return false;
}
memset(m_detectKeys, 0, sizeof(int32_t) * m_tableSize);
//now add all banIps and allowIps from gconf.
setCodesFromConf();
if(!restore())
log(LOG_WARN, "init: autoban could not restore dat file.");
setFromConf();
g_loop.registerSleepCallback ( ONE_DAY ,
NULL,
resetHash,0);
return true;
}
bool AutoBan::addIp(int32_t ip, char action) {
int32_t now = getTime();
DetectVal d;
d.m_minuteExpires = now + 60;
d.m_dayExpires = now + ONE_DAY;
d.m_dayCount = 0;
d.m_minuteCount = 0;
d.m_timesBanned = 0;
d.m_flags = action | FROMCONF;
bool retval = addKey(ip, &d);
return retval;
}
bool AutoBan::addKey(int32_t ip, DetectVal* d) {
int32_t tabsize = m_tableSize - 1;
uint32_t i = (uint32_t)ip & tabsize;
do {
if(m_detectKeys[i] == ip) {
m_detectKeys[i] = ip;
m_detectVals[i].m_flags = d->m_flags;
m_detectVals[i].m_minuteCount = d->m_minuteCount;
m_detectVals[i].m_dayCount = d->m_dayCount;
m_detectVals[i].m_minuteExpires = d->m_minuteExpires;
m_detectVals[i].m_dayExpires = d->m_dayExpires;
m_detectVals[i].m_timesBanned = d->m_timesBanned;
break;
}
if(m_detectKeys[i] == 0) {
if(m_numEntries * 1.2 > m_tableSize ) {
//here we grow the table and adjust i to an
//empty slot in the new (bigger) table
if(!growTable()) return false;
int32_t tabsize = m_tableSize - 1;
i = (uint32_t)ip & tabsize;
while(m_detectKeys[i] != 0)
i = (i + 1) & tabsize;
}
m_detectKeys[i] = ip;
m_detectVals[i].m_flags = d->m_flags;
m_detectVals[i].m_minuteCount = d->m_minuteCount;
m_detectVals[i].m_dayCount = d->m_dayCount;
m_detectVals[i].m_minuteExpires = d->m_minuteExpires;
m_detectVals[i].m_dayExpires = d->m_dayExpires;
m_detectVals[i].m_timesBanned = d->m_timesBanned;
m_numEntries++;
break;
}
i = (i + 1) & tabsize;
} while(1);
return true;
}
void AutoBan::removeIp(int32_t ip) {
int32_t tabsize = m_tableSize - 1;
uint32_t i = (uint32_t)ip & tabsize;
do {
if(m_detectKeys[i] == ip) {
m_detectKeys[i] = 0;
i = (i + 1) & tabsize;
while ( m_detectKeys[i] ) {
int32_t key = m_detectKeys[i];
DetectVal *val = &m_detectVals[i];
m_detectKeys[i] = 0;
m_numEntries--;
addKey(key, val);
i = (i + 1) & tabsize;
}
m_numEntries--;
break;
}
if(m_detectKeys[i] == 0) {
break;
}
i = (i + 1) & tabsize;
} while(1);
}
bool AutoBan::growTable() {
int32_t oldTableSize = m_tableSize;
int32_t *oldDetectKeys = m_detectKeys;
DetectVal *oldDetectVals = m_detectVals;
m_tableSize = m_tableSize << 1;
// log(LOG_INFO, "Autoban: Resize %"INT32" to %"INT32"", oldTableSize,
// m_tableSize);
m_detectKeys = (int32_t*)mmalloc(m_tableSize * sizeof(int32_t),
"AutoBanK");
if(!m_detectKeys) {
m_detectKeys = oldDetectKeys;
m_detectVals = oldDetectVals;
m_tableSize = oldTableSize;
return false;
}
m_detectVals = (DetectVal*)mmalloc(m_tableSize *
sizeof(DetectVal),
"AutoBanB");
if(!m_detectVals) {
mfree (m_detectKeys, m_tableSize * sizeof(int32_t),
"AutoBan");
m_detectKeys = oldDetectKeys;
m_detectVals = oldDetectVals;
m_tableSize = oldTableSize;
return false;
}
memset(m_detectKeys, 0, sizeof(int32_t) * m_tableSize);
//now copy them to the new space.
for(int32_t i = 0; i < oldTableSize; i++) {
if(oldDetectKeys[i] == 0) continue;
addKey(oldDetectKeys[i], &oldDetectVals[i]);
}
mfree(oldDetectKeys, oldTableSize * sizeof(int32_t), "AutoBan");
mfree(oldDetectVals, oldTableSize * sizeof(DetectVal), "AutoBan");
return true;
}
void resetHash(int fd, void *state) {
g_autoBan.cleanHouse();
}
//here we forget about people that haven't queried us in a while.
void AutoBan::cleanHouse() {
int32_t now = getTime();
for(int32_t i = 0; i < m_tableSize; i++) {
if(m_detectKeys[i] == 0) continue;
if(m_detectVals[i].m_flags & FROMCONF) continue;
if(m_detectVals[i].m_timesBanned == 0 &&
m_detectVals[i].m_dayExpires < now)
removeIp(m_detectKeys[i]);
}
}
bool AutoBan::setCodesFromConf() {
static bool s_firstTime = true;
m_codeResetTime = getTime();
char *p = g_conf.m_validCodes;
while(*p) {
if(!isspace(*p)) {
int32_t len = 0;
while(p[len] && !isspace(p[len])) len++;
//now p points to a code, with length len.
//log(LOG_WARN, "autoban code is %s %"INT32"", p, len);
int32_t h = hash32(p,len);
CodeVal cv;
int32_t max = len; if ( max > 30 ) max = 30;
strncpy(cv.m_code,p,max);
cv.m_code[max]='\0';
cv.m_ip = 0;
cv.m_count = 0;
cv.m_bytesSent = 0;
cv.m_bytesRead = 0;
// we might be doing an update, so only set this
// count to 0 the first time we are called on startup
if ( s_firstTime )
cv.m_outstanding = 0;
cv.m_maxEver = 0;
cv.m_maxOutstanding = 5000;
//m_numCodes++;
p += len;
// skip spaces or tabs
while ( *p == ' ' || *p == '\t' ) p++;
// do we got a number? that is the max outstanding cnt
if ( is_digit ( *p ) )
cv.m_maxOutstanding = atoi(p);
// ensure no breach
if ( cv.m_maxOutstanding < 10 )
log("gb: client code %s has LOW max "
"outstanding limit of %"INT32"",
cv.m_code,cv.m_maxOutstanding);
// skip the digits, until we hit \r or \n
while ( is_digit ( *p ) ) p++;
// now add it
if ( ! m_ht.addKey ( h , cv ) ) return false;
}
p++;
}
s_firstTime = false;
return true;
}
void AutoBan::setFromConf(){
char* banIps = g_conf.m_banIps;
char *start = banIps;
do {
while(*banIps && !isspace(*banIps)) banIps++;
int32_t ip = atoip(start, banIps - start);
if(ip) {
if(!addIp(ip, DENY)) {
log(LOG_WARN,
"autoban: malloc failed, couldn't add IP.");
}
}
while(*banIps && isspace(*banIps)) banIps++;
start = banIps;
} while(*banIps);
char* allowIps = g_conf.m_allowIps;
start = allowIps;
do {
while(*allowIps && !isspace(*allowIps)) allowIps++;
int32_t ip = atoip(start, allowIps - start);
if(ip) {
if(!addIp(ip, ALLOW)) {
log(LOG_WARN,
"autoban: malloc failed, couldn't add IP.");
}
}
while(*allowIps && isspace(*allowIps)) allowIps++;
start = allowIps;
} while(*allowIps);
}
bool AutoBan::hasCode(char *code, int32_t codeLen, int32_t ip ) {
if(codeLen == 0) return false;
int32_t h = hash32(code,codeLen);
CodeVal *cv = m_ht.getValuePointer ( h );
if ( ! cv )
return log(LOG_INFO, "query: unrecognized code: %s", code);
cv->m_ip = ip;
cv->m_count++;
return true;
}
// returns true if client is over the limit, false otherwise
bool AutoBan::incRequestCount ( int32_t ch , int32_t bytesRead ) {
if ( ! ch ) return false;
CodeVal *cv = m_ht.getValuePointer ( ch );
if ( ! cv ) return false;
// sanity check
if ( bytesRead < 0 ) { char *xx=NULL;*xx=0; }
// inc his count
cv->m_outstanding++;
cv->m_bytesRead += bytesRead;
// the max ever?
if ( cv->m_outstanding > cv->m_maxEver )
cv->m_maxEver = cv->m_outstanding;
// over limit?
return ( cv->m_outstanding > cv->m_maxOutstanding );
}
void AutoBan::decRequestCount ( int32_t ch , int32_t bytesSent ) {
if ( ! ch ) return;
CodeVal *cv = m_ht.getValuePointer ( ch );
if ( ! cv ) return;
// sanity check
if ( bytesSent < 0 ) { char *xx=NULL;*xx=0; }
// dec the count
cv->m_outstanding--;
cv->m_bytesSent += bytesSent;
}
bool AutoBan::hasPerm(int32_t ip,
char *code, int32_t codeLen,
char *uip, int32_t uipLen,
TcpSocket *s,
HttpRequest *r,
SafeBuf* testBuf,
bool justCheck ) {
char *reqStr = r->getRequest();
int32_t reqLen = r->getRequestLen();
int32_t raw = r->getLong("xml", 0);
int32_t isHuman = 0;
if(code && hasCode(code, codeLen, ip )) {
//don't close client's sockets
if(s) s->m_prefLevel++;
//no ip, but valid code, let them through.
if(!uip) return true;
ip = atoip(uip, uipLen);
// log(LOG_WARN, "has uip %s", uip);
if(!ip) return true;
//has code and uip, do the check.
//the front end can administer a turing test
//and tell us to unban them
isHuman = r->getLong("ishuman", 0);
}
// if ip is local and uip is there, use it
if ( uip && r->isLocal() ) {
// it's local, let it through
if( ! uip ) return true;
// get the new ip then
ip = atoip(uip, uipLen);
// log(LOG_WARN, "has uip %s", uip);
if ( !ip ) return true;
//has code and uip, do the check.
}
//now we check the ip block which the ip is in.
uint32_t ipBlock = (uint32_t)ip & 0x0000ffff;
uint32_t i = getSlot((uint32_t)ipBlock);
if((uint32_t)m_detectKeys[i] == ipBlock) {
if(m_detectVals[i].m_flags & ALLOW) {
if ( justCheck ) return true;
m_detectVals[i].m_dayCount++;
if(s) s->m_prefLevel++;
return true;
}
if(m_detectVals[i].m_flags & DENY) {
if ( justCheck ) return false;
m_detectVals[i].m_dayCount++;
return false;
}
}
//now we check the ip group which the ip is in.
uint32_t ipGroup = (uint32_t)ip & 0x00ffffff;
i = getSlot((uint32_t)ipGroup);
if((uint32_t)m_detectKeys[i] == ipGroup) {
if(m_detectVals[i].m_flags & ALLOW) {
if ( justCheck ) return true;
m_detectVals[i].m_dayCount++;
if(s) s->m_prefLevel++;
return true;
}
if(m_detectVals[i].m_flags & DENY) {
if ( justCheck ) return false;
m_detectVals[i].m_dayCount++;
return false;
}
}
i = getSlot((uint32_t)ip);
int32_t now = getTime();
int32_t banTest = r->getLong("bantest",0);
if ( banTest ) {
log("autoban: doing ban test");
goto doTuringTest;
}
if(m_detectKeys[i] == ip) {
if(m_detectVals[i].m_flags & ALLOW) {
// do not inc if just checking, like for a gif file
if ( justCheck ) return true;
//explicitly allowed.
//log(LOG_WARN,"autoban: %"INT32" allowed.", ip);
m_detectVals[i].m_dayCount++;
if(s) s->m_prefLevel++;
return true;
}
if(m_detectVals[i].m_flags & DENY) {
// do not inc if just checking, like for a gif file
if ( justCheck ) return false;
//banned by autoban, or explicitly banned by matt.
int32_t explicitBan = m_detectVals[i].m_flags & FROMCONF;
//log(LOG_WARN,"autoban: %"INT32" rejected.", ip);
if(!explicitBan &&
// MDW yippy project - no! don't unban bots!
//(m_detectVals[i].m_dayExpires < now || isHuman)) {
(isHuman)) {
//they are unbanned for now, I guess.
m_detectVals[i].m_flags &= ~DENY;
m_detectVals[i].m_dayExpires = now + ONE_DAY;
m_detectVals[i].m_minuteExpires = now + 60;
m_detectVals[i].m_dayCount = 1;
m_detectVals[i].m_minuteCount = 1;
log("autoban: auto-unbanning %s",iptoa(ip));
//return true;
goto checkSubstr;
}
m_detectVals[i].m_dayCount++;
if(explicitBan) return false;
if(uip) return false;
goto doTuringTest;
}
// do not inc if just checking, like a gif file
if ( justCheck ) return true;
/*
if( m_detectVals[i].m_minuteCount > 0 &&
// two requests in one second?
now == m_detectVals[i].m_minuteExpires - 60 ) {
m_detectVals[i].m_flags |= DENY;
log("autoban: second-banning %s",iptoa(ip));
int32_t banUntil = now +
(ONE_DAY *
(m_detectVals[i].m_timesBanned + 1));
if(banUntil < 0 ||
m_detectVals[i].m_timesBanned == 255 ) {
m_detectVals[i].m_dayExpires =
0x7fffffff;
}
else {
m_detectVals[i].m_timesBanned++;
m_detectVals[i].m_dayExpires =banUntil;
}
return false;
}
*/
if(m_detectVals[i].m_minuteCount >=
g_conf.m_numFreeQueriesPerMinute) {
if(m_detectVals[i].m_minuteExpires > now) {
//ban 'em, they are a cowbot, so they
//don't get the turing test
m_detectVals[i].m_flags |= DENY;
log("autoban: minute-banning %s",iptoa(ip));
int32_t banUntil = now +
(ONE_DAY *
(m_detectVals[i].m_timesBanned + 1));
if(banUntil < 0 || m_detectVals[i].m_timesBanned == 255 ) {
m_detectVals[i].m_dayExpires = 0x7fffffff;
}
else {
m_detectVals[i].m_timesBanned++;
m_detectVals[i].m_dayExpires = banUntil;
}
return false;
//goto doTuringTest;
}
else {
m_detectVals[i].m_minuteExpires = now + 60;
m_detectVals[i].m_minuteCount = 0;
}
}
if((uint32_t)m_detectVals[i].m_dayCount >=
g_conf.m_numFreeQueriesPerDay) {
if(m_detectVals[i].m_dayExpires > now) {
//ban 'em
log("autoban: day-banning %s",iptoa(ip));
m_detectVals[i].m_flags |= DENY;
if(m_detectVals[i].m_timesBanned != 255)
m_detectVals[i].m_timesBanned++;
m_detectVals[i].m_dayExpires = now +
(ONE_DAY * m_detectVals[i].
m_timesBanned);
if(uip) return false;
goto doTuringTest;
}
else {
m_detectVals[i].m_dayExpires = now + ONE_DAY;
m_detectVals[i].m_dayCount = 0;
}
}
m_detectVals[i].m_minuteCount++;
m_detectVals[i].m_dayCount++;
//return true;
goto checkSubstr;
}
// do not inc if just checking, like for a gif file
if ( justCheck ) return true;
if(m_detectKeys[i] == 0) {
if(m_numEntries * 1.2 > m_tableSize ) {
//here we grow the table and adjust i to an
//empty slot in the new (bigger) table
if(!growTable())
//return true;
goto checkSubstr;
i = getSlot(ip);
}
m_detectKeys[i] = ip;
m_detectVals[i].m_flags = 0;
m_detectVals[i].m_minuteCount = 1;
m_detectVals[i].m_dayCount = 1;
m_detectVals[i].m_minuteExpires = now + 60;
m_detectVals[i].m_dayExpires = now + ONE_DAY;
m_detectVals[i].m_timesBanned = 0;
++m_numEntries;
//log(LOG_WARN,"autoban: %"INT32" adding to empty slot.",
//ip);
//return true;
goto checkSubstr;
}
//we go here if someone is banned and they are trying to search
doTuringTest:
// sanity!
if ( justCheck ) { char *xx=NULL;*xx=0; }
if( raw == 0 ) {
// did we get a good response from the turing test?
if( g_turingTest.isHuman(r)) {
m_detectVals[i].m_flags &= ~DENY;
//log("autoban: turing-unbanning %s",iptoa(ip));
m_detectVals[i].m_dayExpires = now + ONE_DAY;
m_detectVals[i].m_minuteExpires = now + 60;
m_detectVals[i].m_dayCount = 1;
m_detectVals[i].m_minuteCount = 1;
log(LOG_INFO, "autoban: ip %s has unbanned "
"themselves", iptoa(ip));
return true;
}
testBuf->safePrintf("<form method=get>");
int32_t queryLen = 0;
char* query = r->getValue("q" , &queryLen);
int32_t start = r->getLong("s" , 0);
if ( query )
testBuf->safePrintf("<input type=hidden name=\"q\" "
"value=\"%s\">\n", query);
if ( start > 0 )
testBuf->safePrintf("<input type=hidden name=\"s\" "
"value=\"%"INT32"\">\n", start);
int32_t gigabits = r->getLong("gigabits",0);
if ( gigabits )
testBuf->safePrintf("<input type=hidden name=gigabits "
"value=1>\n");
//
// yippy parms
//
char *ifs = r->getString("input-form",NULL);
if ( ifs )
testBuf->safePrintf("<input type=hidden "
"name=\"input-form\" "
"value=\"%s\">\n", ifs );
char *vs = r->getString("v:sources",NULL);
if ( vs )
testBuf->safePrintf("<input type=hidden "
"name=\"v:sources\" "
"value=\"%s\">\n", vs );
char *vp = r->getString("v:project",NULL);
if ( vp )
testBuf->safePrintf("<input type=hidden "
"name=\"v:project\" "
"value=\"%s\">\n", vp );
char *qp = r->getString("query",NULL);
if ( qp )
testBuf->safePrintf("<input type=hidden "
"name=\"query\" "
"value=\"%s\">\n", qp);
if ( banTest )
testBuf->safePrintf("<input type=hidden "
"name=\"bantest\" "
"value=\"1\">\n");
//
// end yippy parms
//
// display the turing test so they can unban themselves
g_turingTest.printTest(testBuf);
testBuf->safePrintf("<br><center><input type=submit "
"value=\"submit\"></center><br>");
testBuf->safePrintf("</form>");
}
return false;
checkSubstr:
// sanity!
if ( justCheck ) { char *xx=NULL;*xx=0; }
// Look for regular expressions that may serve as a signature of
// a botnet attack
char *banRegex = g_conf.m_banRegex;
int32_t banRegexLen = g_conf.m_banRegexLen;
if (!banRegex || !banRegexLen) return true;
// Don't do regex...look for comma-separated lists of substrings
int32_t start = 0;
bool gotMatch = false;
bool missedMatch = false;
for (int32_t i=0;i<= banRegexLen;i++) {
if (i != banRegexLen &&
banRegex[i] && banRegex[i] != '\n' && banRegex[i] != '\r'
&& banRegex[i] != ',')
continue;
char c = banRegex[i];
// NULL terminate
banRegex[i] = '\0';
// search for substr (must be longer than 2 chars
if ( i - start > 2){
if (strnstr2(reqStr, reqLen, &banRegex[start]))
gotMatch = true;
else missedMatch = true;
}
banRegex[i] = c;
start = i+1;
// check the next substr if we're not at the
// end of line or end of buffer
if (c != '\n' && c != '\r' && c != '\0') continue;
// did we get all the substrings?
if (gotMatch && !missedMatch) return false;
// reset for the next set of substrings
gotMatch = false;
missedMatch = false;
}
return true;
}
// //just check, don't update the table.
// bool AutoBan::isBanned(uint32_t ip) {
// int32_t i = getSlot((uint32_t)ip);
// if(m_detectVals[i] & 0x4000000000000000ULL) {
// return true;
// }
// return false;
// }
int32_t AutoBan::getSlot(int32_t ip) {
int32_t tabsize = m_tableSize - 1;
uint32_t i = (uint32_t)ip & tabsize;
do {
if(m_detectKeys[i] == ip) {
return i;
}
if(m_detectKeys[i] == 0) {
return i;
}
i = (i + 1) & tabsize;
} while(1);
return i;
}
//find repeated spaces and trim them down to only
//one space in a row.
void trimWhite(char* beginning) {
char *to = beginning;
char *from;
bool lastIsSpace = false;
while(*to != '\0') {
if(isspace(*to)) {
if(lastIsSpace) {
from = to;
char* begin = to + 1;
while(isspace(*from)) from++;
while(*to) *to++ = *from++;
trimWhite(begin);
return;
}
else lastIsSpace = true;
if(*to == '\r') *to = '\n';
}
else lastIsSpace = false;
to++;
}
}
//same as strstr, but this makes sure that you are on a word
//boundary.
char* findToken(char* body, char* substr, int32_t substrLen) {
char *start = body;
while(body) {
body = strstr(body, substr);
if(body) {
//check the body and the end to make
//sure that what we have is not a substring of
//a larger string
if((body == start ||
isspace(*(body - 1)))
&&
(isspace(*(body + substrLen)) ||
*(body + substrLen) == '\0')) {
break;
}
else body += substrLen;
}
else break;
}
return body;
}
static int ip_cmp ( const void *h1 , const void *h2 ) {
char* tmp1;
char* tmp2;
tmp1 = ((char *)&(SorterTable[*(int32_t*)h1]));
tmp2 = ((char *)&(SorterTable[*(int32_t*)h2]));
return strncmp(tmp1, tmp2, 4);
}
#define BABY_BLUE "e0e0d0"
#define LIGHT_BLUE "d0d0e0"
#define DARK_BLUE "c0c0f0"
#define GREEN "00ff00"
#define RED "ff0000"
#define YELLOW "ffff00"
bool sendPageAutoban ( TcpSocket *s , HttpRequest *r ) {
return g_autoBan.printTable(s,r);
}
bool AutoBan::printTable( TcpSocket *s , HttpRequest *r ) {
SafeBuf sb(512 * 512,"autobbuf");
//read in all of the possible cgi parms off the bat:
//int32_t user = g_pages.getUserType( s , r );
//char *username = g_users.getUsername(r);
//char *pwd = r->getString ("pwd");
char *coll = r->getString ("c");
int32_t banIpsLen;
char *banIps = r->getString ("banIps" , &banIpsLen , NULL);
int32_t allowIpsLen;
char *allowIps = r->getString ("allowIps" , &allowIpsLen , NULL);
int32_t clearLen;
char *clear = r->getString ("clear" , &clearLen , NULL);
bool changed = false;
int32_t validCodesLen;
char *validCodes = r->getString ("validCodes", &validCodesLen, NULL);
int32_t showAllIps = r->getLong("showAllIps", 0);
int32_t showLongView = r->getLong("int32_tview", 0);
// do it all from parm now
//int32_t banRegexLen;
//char *banRegex = r->getString("banRegex", &banRegexLen, NULL);
// char *ss = sb.getBuf();
// char *ssend = sb.getBufEnd();
g_pages.printAdminTop ( &sb, s , r );
//sb.incrementLength(sss - ss);
// MDW: moved to here
int32_t now = getTime();
int32_t days;
int32_t hours;
int32_t minutes;
int32_t secs;
int32_t msecs;
if(r->getLong("resetcodes", 0)) {
setCodesFromConf();
}
sb.safePrintf("\n<br><br><table %s>\n",TABLE_STYLE);
getCalendarFromMs((now - m_codeResetTime) * 1000,
&days,
&hours,
&minutes,
&secs,
&msecs);
sb.safePrintf("<tr><td colspan=18 bgcolor=#%s>"
"<center><b>Code Usage "
"(<a href=\"/admin/"
"autoban?c=%s&resetcodes=1\">reset</a> "
"%"INT32" days %"INT32" hours %"INT32" "
"minutes %"INT32" sec ago)"
"</b></center></td></tr>",
DARK_BLUE,
coll,
days,
hours,
minutes,
secs);
sb.safePrintf("<tr bgcolor=#%s>"
"<td><center><b>Code</b></center></td>"
"<td><center><b>IP</b></center></td>"
"<td><center><b>Query Count</b></center></td>"
"<td><center><b>Bytes Read</b></center></td>"
"<td><center><b>Bytes Sent</b></center></td>"
"<td><center><b>Outstanding Count</b></center></td>"
"<td><center><b>Most Ever Outstanding</b></center></td>"
"<td><center><b>Max Outstanding</b></center></td>"
"</tr>",
LIGHT_BLUE);
for(int32_t i = 0; i < m_ht.getNumSlots(); i++) {
if ( m_ht.getKey ( i ) == 0 ) continue;
CodeVal *cv = m_ht.getValuePointerFromSlot ( i );
if ( ! cv ) continue;
sb.safePrintf("<tr>");
sb.safePrintf("<td>");
sb.copyToken(cv->m_code);//m_codeVals[i].m_code);
sb.safePrintf("</td>");
sb.safePrintf("<td><center>%s</center> </td>",
iptoa(cv->m_ip));
sb.safePrintf("<td><center>%"INT64"</center></td>",
cv->m_count);
sb.safePrintf("<td><center>%"INT64"</center></td>",
cv->m_bytesRead);
sb.safePrintf("<td><center>%"INT64"</center></td>",
cv->m_bytesSent);
sb.safePrintf("<td><center>%"INT32"</center></td>",
cv->m_outstanding);
sb.safePrintf("<td><center>%"INT32"</center></td>",
cv->m_maxEver);
if ( cv->m_maxOutstanding != 50 )
sb.safePrintf("<td><center><b>%"INT32"</b></center></td>",
cv->m_maxOutstanding);
else
sb.safePrintf("<td><center>%"INT32"</center></td>",
cv->m_maxOutstanding);
sb.safePrintf("</tr>");
}
sb.safePrintf ("</table><br><br>\n" );
if(clear && clearLen < 64) {
int32_t ip = atoip(clear, clearLen);
if(ip) {
removeIp(ip);
char *beginning;
char ipbuf[64];//gotta NULL terminate for strstr
gbmemcpy(ipbuf, clear, clearLen);
ipbuf[clearLen] = '\0';
beginning = findToken(g_conf.m_banIps, ipbuf,
clearLen);
if(beginning) {
char *to = beginning;
char *from = beginning + clearLen;
while(*to) *to++ = *from++;
}
beginning = findToken(g_conf.m_allowIps, ipbuf,
clearLen);
if(beginning) {
char *to = beginning;
char *from = beginning + clearLen;
while(*to) *to++ = *from++;
}
changed = true;
}
}
int32_t allowLen;
char *allow = r->getString ( "allow" , &allowLen , NULL );
if(allow && allowLen < 64) {
int32_t ip = atoip(allow, allowLen);
if(ip) {
char *beginning;
char ipbuf[64];//gotta NULL terminate for strstr
gbmemcpy(ipbuf, allow, allowLen);
ipbuf[allowLen] = '\0';
beginning = findToken(g_conf.m_allowIps, ipbuf,
allowLen);
if(!beginning) {
//its not present, so add it.
char *p = g_conf.m_allowIps;
while(*p) p++;
if(p - g_conf.m_allowIps + allowLen + 2
< AUTOBAN_TEXT_SIZE) {
*p++ = '\n';
gbmemcpy(p, ipbuf,allowLen);
*(p + allowLen) = '\0';
}
else {
sb.safePrintf("<font color=red>"
"Not enough stack space "
"to fit allowIps. "
"Increase "
"AUTOBAN_TEXT_SIZE in "
"Conf.h. "
"Had %"INT32" need %"INT32"."
"</font>",
(int32_t)AUTOBAN_TEXT_SIZE,
(int32_t)(p - g_conf.m_allowIps +
allowLen + 2));
goto dontRemove1;
}
}
beginning = findToken(g_conf.m_banIps, ipbuf,
allowLen);
if(beginning) {
//remove it from banned if present.
char *to = beginning;
char *from = beginning + allowLen;
while(*to) *to++ = *from++;
}
changed = true;
}
}
dontRemove1:
int32_t denyLen;