|
Lines 1-709
Link Here
|
| 1 |
/* $Id: mod_evasive.c,v 1.3 2005/10/08 19:17:14 jonz Exp $ */ |
|
|
| 2 |
|
| 3 |
/* |
| 4 |
mod_evasive for Apache 1.3 |
| 5 |
Copyright (c) by Jonathan A. Zdziarski |
| 6 |
|
| 7 |
LICENSE |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU General Public License |
| 11 |
as published by the Free Software Foundation; version 2 |
| 12 |
of the License. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 |
|
| 23 |
*/ |
| 24 |
|
| 25 |
#include <sys/types.h> |
| 26 |
#include <sys/socket.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <netinet/in.h> |
| 29 |
#include <arpa/inet.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <sys/types.h> |
| 33 |
#include <time.h> |
| 34 |
#include <syslog.h> |
| 35 |
#include <errno.h> |
| 36 |
|
| 37 |
#include "httpd.h" |
| 38 |
#include "http_core.h" |
| 39 |
#include "http_config.h" |
| 40 |
#include "http_log.h" |
| 41 |
#include "http_request.h" |
| 42 |
|
| 43 |
module MODULE_VAR_EXPORT evasive_module; |
| 44 |
|
| 45 |
/* BEGIN DoS Evasive Maneuvers Definitions */ |
| 46 |
|
| 47 |
#define MAILER "/bin/mail -t %s" |
| 48 |
#define LOG( A, ... ) { openlog("mod_evasive", LOG_PID, LOG_DAEMON); syslog( A, __VA_ARGS__ ); closelog(); } |
| 49 |
|
| 50 |
#define DEFAULT_HASH_TBL_SIZE 3079ul // Default hash table size |
| 51 |
#define DEFAULT_PAGE_COUNT 2 // Default max page hit count/interval |
| 52 |
#define DEFAULT_SITE_COUNT 50 // Default max site hit count/interval |
| 53 |
#define DEFAULT_PAGE_INTERVAL 1 // Default 1 second page interval |
| 54 |
#define DEFAULT_SITE_INTERVAL 1 // Default 1 second site interval |
| 55 |
#define DEFAULT_BLOCKING_PERIOD 10 // Default block time (Seconds) |
| 56 |
#define DEFAULT_LOG_DIR "/tmp" |
| 57 |
|
| 58 |
/* END DoS Evasive Maneuvers Definitions */ |
| 59 |
|
| 60 |
/* BEGIN NTT (Named Timestamp Tree) Headers */ |
| 61 |
|
| 62 |
enum { ntt_num_primes = 28 }; |
| 63 |
|
| 64 |
/* ntt root tree */ |
| 65 |
struct ntt { |
| 66 |
long size; |
| 67 |
long items; |
| 68 |
struct ntt_node **tbl; |
| 69 |
}; |
| 70 |
|
| 71 |
/* ntt node (entry in the ntt root tree) */ |
| 72 |
struct ntt_node { |
| 73 |
char *key; |
| 74 |
time_t timestamp; |
| 75 |
long count; |
| 76 |
struct ntt_node *next; |
| 77 |
}; |
| 78 |
|
| 79 |
/* ntt cursor */ |
| 80 |
struct ntt_c { |
| 81 |
long iter_index; |
| 82 |
struct ntt_node *iter_next; |
| 83 |
}; |
| 84 |
|
| 85 |
struct ntt *ntt_create(long size); |
| 86 |
struct ntt_node *ntt_find(struct ntt *ntt, const char *key); |
| 87 |
struct ntt_node *ntt_insert(struct ntt *ntt, const char *key, time_t timestamp); |
| 88 |
struct ntt_node *c_ntt_first(struct ntt *ntt, struct ntt_c *c); |
| 89 |
struct ntt_node *c_ntt_next(struct ntt *ntt, struct ntt_c *c); |
| 90 |
int ntt_destroy(struct ntt *ntt); |
| 91 |
int ntt_delete(struct ntt *ntt, const char *key); |
| 92 |
long ntt_hashcode(struct ntt *ntt, const char *key); |
| 93 |
|
| 94 |
/* END NTT (Named Timestamp Tree) Headers */ |
| 95 |
|
| 96 |
|
| 97 |
/* BEGIN DoS Evasive Maneuvers Globals */ |
| 98 |
|
| 99 |
struct ntt *hit_list; // Our dynamic hash table |
| 100 |
struct ntt *white_list = NULL; // White list table |
| 101 |
|
| 102 |
static unsigned long hash_table_size = DEFAULT_HASH_TBL_SIZE; |
| 103 |
static int page_count = DEFAULT_PAGE_COUNT; |
| 104 |
static int page_interval = DEFAULT_PAGE_INTERVAL; |
| 105 |
static int site_count = DEFAULT_SITE_COUNT; |
| 106 |
static int site_interval = DEFAULT_SITE_INTERVAL; |
| 107 |
static int blocking_period = DEFAULT_BLOCKING_PERIOD; |
| 108 |
static char *log_dir = NULL; |
| 109 |
static char *email_notify = NULL; |
| 110 |
static char *sys_command = NULL; |
| 111 |
int is_whitelisted(const char *ip); |
| 112 |
static const char *whitelist(cmd_parms *cmd, void *mconfig, char *ip); |
| 113 |
|
| 114 |
/* END DoS Evasive Maneuvers Globals */ |
| 115 |
|
| 116 |
static void evasive_child_init(server_rec *s, pool *p) |
| 117 |
{ |
| 118 |
hit_list = ntt_create(hash_table_size); |
| 119 |
} |
| 120 |
|
| 121 |
static int check_access(request_rec *r) |
| 122 |
{ |
| 123 |
int ret = OK; |
| 124 |
|
| 125 |
/* BEGIN Evasive Maneuvers Code */ |
| 126 |
|
| 127 |
if (r->prev == NULL && r->main == NULL && hit_list != NULL) { |
| 128 |
unsigned long address = r->connection->remote_addr.sin_addr.s_addr; |
| 129 |
char *text_add = inet_ntoa(r->connection->remote_addr.sin_addr); |
| 130 |
char hash_key[2048]; |
| 131 |
struct ntt_node *n; |
| 132 |
time_t t = time(NULL); |
| 133 |
|
| 134 |
/* Check whitelist */ |
| 135 |
|
| 136 |
if (is_whitelisted(text_add)) |
| 137 |
return OK; |
| 138 |
|
| 139 |
/* First see if the IP itself is on "hold" */ |
| 140 |
snprintf(hash_key, 2048, "%ld", address); |
| 141 |
n = ntt_find(hit_list, hash_key); |
| 142 |
|
| 143 |
if (n != NULL && t-n->timestamp<blocking_period) { |
| 144 |
|
| 145 |
/* If the IP is on "hold", make it wait longer in 403 land */ |
| 146 |
ret = FORBIDDEN; |
| 147 |
n->timestamp = time(NULL); |
| 148 |
|
| 149 |
/* Not on hold, check hit stats */ |
| 150 |
} else { |
| 151 |
|
| 152 |
/* Has URI been hit too much? */ |
| 153 |
snprintf(hash_key, 2048, "%ld_%s", address, r->uri); |
| 154 |
n = ntt_find(hit_list, hash_key); |
| 155 |
if (n != NULL) { |
| 156 |
|
| 157 |
/* If URI is being hit too much, add to "hold" list and 403 */ |
| 158 |
if (t-n->timestamp<page_interval && n->count>=page_count) { |
| 159 |
ret = FORBIDDEN; |
| 160 |
snprintf(hash_key, 2048, "%ld", address); |
| 161 |
ntt_insert(hit_list, hash_key, time(NULL)); |
| 162 |
} else { |
| 163 |
|
| 164 |
/* Reset our hit count list as necessary */ |
| 165 |
if (t-n->timestamp>=page_interval) { |
| 166 |
n->count=0; |
| 167 |
} |
| 168 |
} |
| 169 |
n->timestamp = t; |
| 170 |
n->count++; |
| 171 |
} else { |
| 172 |
ntt_insert(hit_list, hash_key, t); |
| 173 |
} |
| 174 |
|
| 175 |
/* Has site been hit too much? */ |
| 176 |
snprintf(hash_key, 2048, "%ld_SITE", address); |
| 177 |
n = ntt_find(hit_list, hash_key); |
| 178 |
if (n != NULL) { |
| 179 |
|
| 180 |
/* If site is being hit too much, add to "hold" list and 403 */ |
| 181 |
if (t-n->timestamp<site_interval && n->count>=site_count) { |
| 182 |
ret = FORBIDDEN; |
| 183 |
snprintf(hash_key, 2048, "%ld", address); |
| 184 |
ntt_insert(hit_list, hash_key, time(NULL)); |
| 185 |
} else { |
| 186 |
|
| 187 |
/* Reset our hit count list as necessary */ |
| 188 |
if (t-n->timestamp>=site_interval) { |
| 189 |
n->count=0; |
| 190 |
} |
| 191 |
} |
| 192 |
n->timestamp = t; |
| 193 |
n->count++; |
| 194 |
} else { |
| 195 |
ntt_insert(hit_list, hash_key, t); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* Perform email notification and system functions */ |
| 200 |
if (ret == FORBIDDEN) { |
| 201 |
char filename[1024]; |
| 202 |
struct stat s; |
| 203 |
FILE *file; |
| 204 |
|
| 205 |
snprintf(filename, sizeof(filename), "%s/dos-%s", log_dir != NULL ? log_dir : DEFAULT_LOG_DIR, text_add); |
| 206 |
if (stat(filename, &s)) { |
| 207 |
file = fopen(filename, "w"); |
| 208 |
if (file != NULL) { |
| 209 |
fprintf(file, "%ld\n", getpid()); |
| 210 |
fclose(file); |
| 211 |
|
| 212 |
LOG(LOG_ALERT, "Blacklisting address %s: possible attack.", text_add) |
| 213 |
if (email_notify != NULL) { |
| 214 |
snprintf(filename, sizeof(filename), MAILER, email_notify); |
| 215 |
file = popen(filename, "w"); |
| 216 |
if (file != NULL) { |
| 217 |
fprintf(file, "To: %s\n", email_notify); |
| 218 |
fprintf(file, "Subject: HTTP BLACKLIST %s\n\n", text_add); |
| 219 |
fprintf(file, "mod_evasive HTTP Blacklisted %s\n", text_add); |
| 220 |
pclose(file); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (sys_command != NULL) { |
| 225 |
snprintf(filename, sizeof(filename), sys_command, text_add); |
| 226 |
system(filename); |
| 227 |
} |
| 228 |
|
| 229 |
} else { |
| 230 |
LOG(LOG_ALERT, "Couldn't open logfile %s: %s",filename, strerror(errno)); |
| 231 |
} |
| 232 |
|
| 233 |
} /* if (temp file does not exist) */ |
| 234 |
|
| 235 |
} /* if (ret == FORBIDDEN) */ |
| 236 |
|
| 237 |
} /* if (r->prev == NULL && r->main == NULL && hit_list != NULL) */ |
| 238 |
|
| 239 |
/* END Evasive Maneuvers Code */ |
| 240 |
|
| 241 |
if (ret == FORBIDDEN |
| 242 |
&& (ap_satisfies(r) != SATISFY_ANY || !ap_some_auth_required(r))) { |
| 243 |
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, |
| 244 |
"client denied by server configuration: %s", |
| 245 |
r->filename); |
| 246 |
} |
| 247 |
|
| 248 |
return ret; |
| 249 |
} |
| 250 |
|
| 251 |
static void evasive_child_exit(server_rec *s, pool *p) |
| 252 |
{ |
| 253 |
ntt_destroy(hit_list); |
| 254 |
free(email_notify); |
| 255 |
free(sys_command); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/* BEGIN NTT (Named Timestamp Tree) Functions */ |
| 260 |
|
| 261 |
static unsigned long ntt_prime_list[ntt_num_primes] = |
| 262 |
{ |
| 263 |
53ul, 97ul, 193ul, 389ul, 769ul, |
| 264 |
1543ul, 3079ul, 6151ul, 12289ul, 24593ul, |
| 265 |
49157ul, 98317ul, 196613ul, 393241ul, 786433ul, |
| 266 |
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, |
| 267 |
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, |
| 268 |
1610612741ul, 3221225473ul, 4294967291ul |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
/* Find the numeric position in the hash table based on key and modulus */ |
| 273 |
|
| 274 |
long ntt_hashcode(struct ntt *ntt, const char *key) { |
| 275 |
unsigned long val = 0; |
| 276 |
for (; *key; ++key) val = 5 * val + *key; |
| 277 |
return(val % ntt->size); |
| 278 |
} |
| 279 |
|
| 280 |
/* Creates a single node in the tree */ |
| 281 |
|
| 282 |
struct ntt_node *ntt_node_create(const char *key) { |
| 283 |
char *node_key; |
| 284 |
struct ntt_node* node; |
| 285 |
|
| 286 |
node = (struct ntt_node *) malloc(sizeof(struct ntt_node)); |
| 287 |
if (node == NULL) { |
| 288 |
return NULL; |
| 289 |
} |
| 290 |
if ((node_key = strdup(key)) == NULL) { |
| 291 |
free(node); |
| 292 |
return NULL; |
| 293 |
} |
| 294 |
node->key = node_key; |
| 295 |
node->timestamp = time(NULL); |
| 296 |
node->next = NULL; |
| 297 |
return(node); |
| 298 |
} |
| 299 |
|
| 300 |
/* Tree initializer */ |
| 301 |
|
| 302 |
struct ntt *ntt_create(long size) { |
| 303 |
long i = 0; |
| 304 |
struct ntt *ntt = (struct ntt *) malloc(sizeof(struct ntt)); |
| 305 |
|
| 306 |
if (ntt == NULL) |
| 307 |
return NULL; |
| 308 |
while (ntt_prime_list[i] < size) { i++; } |
| 309 |
ntt->size = ntt_prime_list[i]; |
| 310 |
ntt->items = 0; |
| 311 |
ntt->tbl = (struct ntt_node **) calloc(ntt->size, sizeof(struct ntt_node *)); |
| 312 |
if (ntt->tbl == NULL) { |
| 313 |
free(ntt); |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
return(ntt); |
| 317 |
} |
| 318 |
|
| 319 |
/* Find an object in the tree */ |
| 320 |
|
| 321 |
struct ntt_node *ntt_find(struct ntt *ntt, const char *key) { |
| 322 |
long hash_code; |
| 323 |
struct ntt_node *node; |
| 324 |
|
| 325 |
if (ntt == NULL) return NULL; |
| 326 |
|
| 327 |
hash_code = ntt_hashcode(ntt, key); |
| 328 |
node = ntt->tbl[hash_code]; |
| 329 |
|
| 330 |
while (node) { |
| 331 |
if (!strcmp(key, node->key)) { |
| 332 |
return(node); |
| 333 |
} |
| 334 |
node = node->next; |
| 335 |
} |
| 336 |
return((struct ntt_node *)NULL); |
| 337 |
} |
| 338 |
|
| 339 |
/* Insert a node into the tree */ |
| 340 |
|
| 341 |
struct ntt_node *ntt_insert(struct ntt *ntt, const char *key, time_t timestamp) { |
| 342 |
long hash_code; |
| 343 |
struct ntt_node *parent; |
| 344 |
struct ntt_node *node; |
| 345 |
struct ntt_node *new_node = NULL; |
| 346 |
|
| 347 |
if (ntt == NULL) return NULL; |
| 348 |
|
| 349 |
hash_code = ntt_hashcode(ntt, key); |
| 350 |
parent = NULL; |
| 351 |
node = ntt->tbl[hash_code]; |
| 352 |
|
| 353 |
while (node != NULL) { |
| 354 |
if (strcmp(key, node->key) == 0) { |
| 355 |
new_node = node; |
| 356 |
node = NULL; |
| 357 |
} |
| 358 |
|
| 359 |
if (new_node == NULL) { |
| 360 |
parent = node; |
| 361 |
node = node->next; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (new_node != NULL) { |
| 366 |
new_node->timestamp = timestamp; |
| 367 |
new_node->count = 0; |
| 368 |
return new_node; |
| 369 |
} |
| 370 |
|
| 371 |
/* Create a new node */ |
| 372 |
new_node = ntt_node_create(key); |
| 373 |
new_node->timestamp = timestamp; |
| 374 |
new_node->timestamp = 0; |
| 375 |
|
| 376 |
ntt->items++; |
| 377 |
|
| 378 |
/* Insert */ |
| 379 |
if (parent) { /* Existing parent */ |
| 380 |
parent->next = new_node; |
| 381 |
return new_node; /* Return the locked node */ |
| 382 |
} |
| 383 |
|
| 384 |
/* No existing parent; add directly to hash table */ |
| 385 |
ntt->tbl[hash_code] = new_node; |
| 386 |
return new_node; |
| 387 |
} |
| 388 |
|
| 389 |
/* Tree destructor */ |
| 390 |
|
| 391 |
int ntt_destroy(struct ntt *ntt) { |
| 392 |
struct ntt_node *node, *next; |
| 393 |
struct ntt_c c; |
| 394 |
|
| 395 |
if (ntt == NULL) return -1; |
| 396 |
|
| 397 |
node = c_ntt_first(ntt, &c); |
| 398 |
while(node != NULL) { |
| 399 |
next = c_ntt_next(ntt, &c); |
| 400 |
ntt_delete(ntt, node->key); |
| 401 |
node = next; |
| 402 |
} |
| 403 |
|
| 404 |
free(ntt->tbl); |
| 405 |
free(ntt); |
| 406 |
ntt = (struct ntt *) NULL; |
| 407 |
|
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
/* Delete a single node in the tree */ |
| 412 |
|
| 413 |
int ntt_delete(struct ntt *ntt, const char *key) { |
| 414 |
long hash_code; |
| 415 |
struct ntt_node *parent = NULL; |
| 416 |
struct ntt_node *node; |
| 417 |
struct ntt_node *del_node = NULL; |
| 418 |
|
| 419 |
if (ntt == NULL) return -1; |
| 420 |
|
| 421 |
hash_code = ntt_hashcode(ntt, key); |
| 422 |
node = ntt->tbl[hash_code]; |
| 423 |
|
| 424 |
while (node != NULL) { |
| 425 |
if (strcmp(key, node->key) == 0) { |
| 426 |
del_node = node; |
| 427 |
node = NULL; |
| 428 |
} |
| 429 |
|
| 430 |
if (del_node == NULL) { |
| 431 |
parent = node; |
| 432 |
node = node->next; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if (del_node != NULL) { |
| 437 |
|
| 438 |
if (parent) { |
| 439 |
parent->next = del_node->next; |
| 440 |
} else { |
| 441 |
ntt->tbl[hash_code] = del_node->next; |
| 442 |
} |
| 443 |
|
| 444 |
free(del_node->key); |
| 445 |
free(del_node); |
| 446 |
ntt->items--; |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
return -5; |
| 452 |
} |
| 453 |
|
| 454 |
/* Point cursor to first item in tree */ |
| 455 |
|
| 456 |
struct ntt_node *c_ntt_first(struct ntt *ntt, struct ntt_c *c) { |
| 457 |
|
| 458 |
c->iter_index = 0; |
| 459 |
c->iter_next = (struct ntt_node *)NULL; |
| 460 |
return(c_ntt_next(ntt, c)); |
| 461 |
} |
| 462 |
|
| 463 |
/* Point cursor to next iteration in tree */ |
| 464 |
|
| 465 |
struct ntt_node *c_ntt_next(struct ntt *ntt, struct ntt_c *c) { |
| 466 |
long index; |
| 467 |
struct ntt_node *node = c->iter_next; |
| 468 |
|
| 469 |
if (ntt == NULL) return NULL; |
| 470 |
|
| 471 |
if (node) { |
| 472 |
if (node != NULL) { |
| 473 |
c->iter_next = node->next; |
| 474 |
return (node); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if (! node) { |
| 479 |
while (c->iter_index < ntt->size) { |
| 480 |
index = c->iter_index++; |
| 481 |
|
| 482 |
if (ntt->tbl[index]) { |
| 483 |
c->iter_next = ntt->tbl[index]->next; |
| 484 |
return(ntt->tbl[index]); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
return((struct ntt_node *)NULL); |
| 489 |
} |
| 490 |
|
| 491 |
/* END NTT (Named Pointer Tree) Functions */ |
| 492 |
|
| 493 |
/* BEGIN Configuration Functions */ |
| 494 |
|
| 495 |
static const char * |
| 496 |
get_hash_tbl_size(cmd_parms *cmd, void *dconfig, char *value) { |
| 497 |
long n = strtol(value, NULL, 0); |
| 498 |
|
| 499 |
if (n<=0) |
| 500 |
hash_table_size = DEFAULT_HASH_TBL_SIZE; |
| 501 |
else |
| 502 |
hash_table_size = n; |
| 503 |
|
| 504 |
return NULL; |
| 505 |
} |
| 506 |
|
| 507 |
static const char * |
| 508 |
get_page_count(cmd_parms *cmd, void *dconfig, char *value) { |
| 509 |
long n = strtol(value, NULL, 0); |
| 510 |
if (n<=0) |
| 511 |
page_count = DEFAULT_PAGE_COUNT; |
| 512 |
else |
| 513 |
page_count = n; |
| 514 |
|
| 515 |
return NULL; |
| 516 |
} |
| 517 |
|
| 518 |
static const char * |
| 519 |
get_site_count(cmd_parms *cmd, void *dconfig, char *value) { |
| 520 |
long n = strtol(value, NULL, 0); |
| 521 |
if (n<=0) |
| 522 |
site_count = DEFAULT_SITE_COUNT; |
| 523 |
else |
| 524 |
site_count = n; |
| 525 |
|
| 526 |
return NULL; |
| 527 |
} |
| 528 |
|
| 529 |
static const char * |
| 530 |
get_page_interval(cmd_parms *cmd, void *dconfig, char *value) { |
| 531 |
long n = strtol(value, NULL, 0); |
| 532 |
if (n<=0) |
| 533 |
page_interval = DEFAULT_PAGE_INTERVAL; |
| 534 |
else |
| 535 |
page_interval = n; |
| 536 |
|
| 537 |
return NULL; |
| 538 |
} |
| 539 |
|
| 540 |
static const char * |
| 541 |
get_site_interval(cmd_parms *cmd, void *dconfig, char *value) { |
| 542 |
long n = strtol(value, NULL, 0); |
| 543 |
if (n<=0) |
| 544 |
site_interval = DEFAULT_SITE_INTERVAL; |
| 545 |
else |
| 546 |
site_interval = n; |
| 547 |
|
| 548 |
return NULL; |
| 549 |
} |
| 550 |
|
| 551 |
static const char * |
| 552 |
get_blocking_period(cmd_parms *cmd, void *dconfig, char *value) { |
| 553 |
long n = strtol(value, NULL, 0); |
| 554 |
if (n<=0) |
| 555 |
blocking_period = DEFAULT_BLOCKING_PERIOD; |
| 556 |
else |
| 557 |
blocking_period = n; |
| 558 |
|
| 559 |
return NULL; |
| 560 |
} |
| 561 |
|
| 562 |
static const char * |
| 563 |
get_log_dir(cmd_parms *cmd, void *dconfig, char *value) { |
| 564 |
if (value != NULL && value[0] != 0) { |
| 565 |
if (log_dir != NULL) |
| 566 |
free(log_dir); |
| 567 |
log_dir = strdup(value); |
| 568 |
} |
| 569 |
|
| 570 |
return NULL; |
| 571 |
} |
| 572 |
|
| 573 |
static const char * |
| 574 |
get_email_notify(cmd_parms *cmd, void *dconfig, char *value) { |
| 575 |
if (value != NULL && value[0] != 0) { |
| 576 |
if (email_notify != NULL) |
| 577 |
free(email_notify); |
| 578 |
email_notify = strdup(value); |
| 579 |
} |
| 580 |
|
| 581 |
return NULL; |
| 582 |
} |
| 583 |
|
| 584 |
static const char * |
| 585 |
get_sys_command(cmd_parms *cmd, void *dconfig, char *value) { |
| 586 |
if (value != NULL && value[0] != 0) { |
| 587 |
if (sys_command != NULL) |
| 588 |
free(sys_command); |
| 589 |
sys_command = strdup(value); |
| 590 |
} |
| 591 |
|
| 592 |
return NULL; |
| 593 |
} |
| 594 |
|
| 595 |
static const char *whitelist(cmd_parms *cmd, void *mconfig, char *ip) { |
| 596 |
char entry[128]; |
| 597 |
|
| 598 |
if (white_list == NULL) |
| 599 |
white_list = ntt_create(53ul); |
| 600 |
snprintf(entry, sizeof(entry), "%s", ip); |
| 601 |
ntt_insert(white_list, entry, time(NULL)); |
| 602 |
|
| 603 |
return NULL; |
| 604 |
} |
| 605 |
|
| 606 |
/* END Configuration Functions */ |
| 607 |
|
| 608 |
int is_whitelisted(const char *ip) { |
| 609 |
char hashkey[128]; |
| 610 |
char octet[4][4]; |
| 611 |
char *dip; |
| 612 |
char *oct; |
| 613 |
int i = 0; |
| 614 |
|
| 615 |
memset(octet, 0, 16); |
| 616 |
dip = strdup(ip); |
| 617 |
if (dip == NULL) |
| 618 |
return 0; |
| 619 |
|
| 620 |
oct = strtok(dip, "."); |
| 621 |
while(oct != NULL && i<4) { |
| 622 |
if (strlen(oct)<=3) |
| 623 |
strcpy(octet[i], oct); |
| 624 |
i++; |
| 625 |
oct = strtok(NULL, "."); |
| 626 |
} |
| 627 |
free(dip); |
| 628 |
|
| 629 |
/* Exact Match */ |
| 630 |
snprintf(hashkey, sizeof(hashkey), "%s", ip); |
| 631 |
if (ntt_find(white_list, hashkey)!=NULL) |
| 632 |
return 1; |
| 633 |
|
| 634 |
/* IPv4 Wildcards */ |
| 635 |
snprintf(hashkey, sizeof(hashkey), "%s.*.*.*", octet[0]); |
| 636 |
if (ntt_find(white_list, hashkey)!=NULL) |
| 637 |
return 1; |
| 638 |
|
| 639 |
snprintf(hashkey, sizeof(hashkey), "%s.%s.*.*", |
| 640 |
octet[0], octet[1]); |
| 641 |
if (ntt_find(white_list, hashkey)!=NULL) |
| 642 |
return 1; |
| 643 |
|
| 644 |
snprintf(hashkey, sizeof(hashkey), "%s.%s.%s.*", |
| 645 |
octet[0], octet[1], octet[2]); |
| 646 |
if (ntt_find(white_list, hashkey)!=NULL) |
| 647 |
return 1; |
| 648 |
|
| 649 |
/* No match */ |
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
static command_rec command_table[] = { |
| 654 |
|
| 655 |
{ "DOSWhitelist", whitelist, NULL, RSRC_CONF, ITERATE, |
| 656 |
"Whitelist an IP or Wildcard. "}, |
| 657 |
|
| 658 |
{ "DOSHashTableSize", get_hash_tbl_size, NULL, RSRC_CONF, TAKE1, |
| 659 |
"Set size of hash table. " }, |
| 660 |
|
| 661 |
{ "DOSPageCount", get_page_count, NULL, RSRC_CONF, TAKE1, |
| 662 |
"Set maximum page hit count per interval. " }, |
| 663 |
|
| 664 |
{ "DOSSiteCount", get_site_count, NULL, RSRC_CONF, TAKE1, |
| 665 |
"Set maximum site hit count per interval. " }, |
| 666 |
|
| 667 |
{ "DOSPageInterval", get_page_interval, NULL, RSRC_CONF, TAKE1, |
| 668 |
"Set page interval. " }, |
| 669 |
|
| 670 |
{ "DOSSiteInterval", get_site_interval, NULL, RSRC_CONF, TAKE1, |
| 671 |
"Set site interval. " }, |
| 672 |
|
| 673 |
{ "DOSLogDir", get_log_dir, NULL, RSRC_CONF, TAKE1, |
| 674 |
"Set log dir. "}, |
| 675 |
|
| 676 |
{ "DOSEmailNotify", get_email_notify, NULL, RSRC_CONF, TAKE1, |
| 677 |
"Set email notification. "}, |
| 678 |
|
| 679 |
{ "DOSSystemCommand", get_sys_command, NULL, RSRC_CONF, TAKE1, |
| 680 |
"Set system command. "}, |
| 681 |
|
| 682 |
{ "DOSBlockingPeriod", get_blocking_period, NULL, RSRC_CONF, TAKE1, |
| 683 |
"Set blocking period for detected DoS IPs. "}, |
| 684 |
|
| 685 |
{ NULL } |
| 686 |
}; |
| 687 |
|
| 688 |
module MODULE_VAR_EXPORT evasive_module = { |
| 689 |
STANDARD_MODULE_STUFF, |
| 690 |
NULL, /* initializer */ |
| 691 |
NULL, /* dir config creator */ |
| 692 |
NULL, /* dir config merger */ |
| 693 |
NULL, /* server config creator */ |
| 694 |
NULL, /* server config merger */ |
| 695 |
command_table, /* command table */ |
| 696 |
NULL, /* handlers */ |
| 697 |
NULL, /* filename translation */ |
| 698 |
NULL, /* check_user_id */ |
| 699 |
NULL, /* check auth */ |
| 700 |
check_access, /* check access */ |
| 701 |
NULL, /* type_checker */ |
| 702 |
NULL, /* fixups */ |
| 703 |
NULL, /* logger */ |
| 704 |
NULL, /* header parser */ |
| 705 |
evasive_child_init, /* child_init */ |
| 706 |
evasive_child_exit, /* child_exit */ |
| 707 |
NULL /* post read-request */ |
| 708 |
}; |
| 709 |
|