-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_ip_count.c
461 lines (405 loc) · 14.4 KB
/
mod_ip_count.c
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
/* $Id: mod_ip_count.c,v 1.2 2008/11/22 09:10:07 proger Exp $ */
/*
* Copyright 2004 Ian Holsman,
* Copyright 2008 Ivan Fitenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define APR_WANT_STRFUNC
#include <apr_want.h>
#include <apr_strings.h>
#include <apr_time.h>
#include <apr_network_io.h>
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "apr_memcache.h"
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "mod_ip_count.h"
#define DEFAULT_SERVER "127.0.0.1:11211"
#define DEFURI "/"
#define NOWHITELIST "255.255.255.255"
module AP_MODULE_DECLARE_DATA ip_count_module;
typedef struct {
char *memcache_servers;
char *used_uris;
unsigned int memcache_max_requests;
unsigned int httpretcode;
apr_short_interval_time_t memcache_max_time;
apr_short_interval_time_t memcache_block_time;
apr_short_interval_time_t rq_latency;
apr_memcache_t *mc;
char *whitelisted_ip;
/* TODO: add config settings for these... */
/* ... or just get this TODO the hell out of here.*/
apr_uint32_t min_sockets;
apr_uint32_t smax_sockets;
apr_uint32_t max_sockets;
apr_uint32_t ttl_sockets;
} ip_count_config_rec;
typedef struct {
int size; /* container for MaxTime setting */
int nelts; /* request counter */
int posn; /* positon of current request */
int lastseenposn; /* position of oldest request */
int pidaras; /* blacklist token */
apr_time_t times[]; /* storage for request times*/
} user_details;
static const char *set_servers(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->memcache_servers = apr_pstrdup( cmd->pool, arg1);
return NULL;
}
static const char *set_used_uris(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->used_uris = apr_pstrdup( cmd->pool, arg1);
return NULL;
}
static const char *set_max_time(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->memcache_max_time = apr_time_make( atoi( arg1) ,0);
return NULL;
}
static const char *set_block_time(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->memcache_block_time = apr_time_make( atoi( arg1) ,0);
return NULL;
}
static const char *set_max_requests(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->memcache_max_requests = atoi( arg1) - 1;
return NULL;
}
static const char *set_rq_latency(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->rq_latency = atoi( arg1);
return NULL;
}
static const char *set_httpretcode(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->httpretcode = atoi( arg1);
return NULL;
}
static const char *set_whitelisted_ip(cmd_parms *cmd, void *config, const char *arg1 )
{
ip_count_config_rec *cfg;
cfg = ap_get_module_config(cmd->server->module_config, &ip_count_module );
cfg->whitelisted_ip = apr_pstrdup( cmd->pool, arg1);
return NULL;
}
static void *create_ip_count_server_config(apr_pool_t *p, server_rec *s)
{
ip_count_config_rec *conf = apr_palloc(p, sizeof(*conf));
conf->memcache_servers = DEFAULT_SERVER;
conf->used_uris = DEFURI;
conf->memcache_max_requests = 50;
conf->memcache_max_time = apr_time_make(120,0);
conf->memcache_block_time = apr_time_make(120,0);
conf->httpretcode = 403;
conf->min_sockets=2;
conf->smax_sockets = 5;
conf->max_sockets = 10;
conf->ttl_sockets= 600;
conf->mc = NULL;
conf->whitelisted_ip= NOWHITELIST;
conf->rq_latency = 0;
return conf;
}
static void ip_count_child_init(apr_pool_t *p, server_rec *s)
{
char *serverstring;
char *serverport;
char *last;
int nservers=0;
apr_status_t rv;
apr_memcache_server_t *serverrec;
ip_count_config_rec *conf = ap_get_module_config(s->module_config,
&ip_count_module);
/* Find all the servers in the first run to get a total count */
serverstring = apr_pstrdup(p, conf->memcache_servers);
serverport = apr_strtok(serverstring, " ", &last);
while (serverport) {
nservers++;
serverport = apr_strtok(NULL," ", &last);
}
rv = apr_memcache_create(p, nservers, 0, &conf->mc);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"apr_memcache_create:unable to initialize memcached");
}
serverstring = apr_pstrdup(p, conf->memcache_servers);
serverport = apr_strtok(serverstring, " ", &last);
while (serverport) {
char *server;
char *scope;
apr_port_t port;
rv = apr_parse_addr_port( &server, &scope, &port, serverport, p );
if ( rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"apr_memcache_server_create: unable to parse %s (%s)", serverport, conf->memcache_servers );
}
if (server == NULL ) {
server = DEFAULT_SERVER;
}
rv = apr_memcache_server_create(p,
server,
port,
conf->min_sockets,
conf->smax_sockets,
conf->max_sockets,
conf->ttl_sockets,
&serverrec);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"apr_memcache_server_create:%s %d", server, port);
return DECLINED;
}
rv = apr_memcache_add_server(conf->mc, serverrec);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"apr_memcache_add_server:");
}
serverport = apr_strtok(NULL," ", &last);
}
}
/* Checking ID */
static int ip_count_check_auth(request_rec *r)
{
ip_count_config_rec *conf = ap_get_module_config(r->server->module_config,
&ip_count_module);
char *reason = NULL;
apr_status_t rv;
user_details *det;
int i;
int t_var;
apr_time_t oldest_time;
request_rec *rr;
char datetimestr[APR_RFC822_DATE_LEN];
apr_time_t *last_seen;
char *key;
apr_memcache_t *mc;
int new=0;
apr_size_t len;
apr_uint32_t flags;
char *result;
char *uri_samples;
char* req_uris;
char* all_whitelisted_ip;
char* this_whitelisted_ip;
char* whitelisted_netmask;
apr_ipsubnet_t* this_subnet;
char *last;
int pattern_matched = 0;
if (r->main) {
return DECLINED;
}
#ifdef WITH_APACHE24
key = r->connection->client_ip;
#else
key = r->connection->remote_ip;
#endif
/* Use this for debugging
* key = r->args;
*/
if (!key) {
key = "none";
}
mc = conf->mc;
rv = apr_memcache_getp( mc, r->pool, key, &result, &len, &flags );
if (rv != APR_SUCCESS && rv != APR_NOTFOUND) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r,
"Getting cache key for IP %s", key);
return DECLINED;
}
if (rv == APR_NOTFOUND) {
det = apr_palloc( r->pool, sizeof(user_details) + sizeof(apr_time_t)* conf->memcache_max_requests );
det->size = conf->memcache_max_requests;
det->nelts = 0;
det->pidaras = 0;
det->posn = 0;
det->lastseenposn = 0;
new = 1;
} else {
det = (user_details*)result;
}
/*for the configured uris... */
req_uris = apr_pstrdup(r->pool, conf->used_uris);
uri_samples = apr_strtok(req_uris, " ",&last);
do {
if (strstr(r->uri, uri_samples)) {
pattern_matched = 1;
break;
}
uri_samples = apr_strtok(NULL, " ",&last);
} while (uri_samples);
if (!pattern_matched) {
return DECLINED;
}
/*Process the whitelist, skip the requests mathcing the set criteria*/
all_whitelisted_ip = apr_pstrdup(r->pool, conf->whitelisted_ip);
this_whitelisted_ip = apr_strtok(all_whitelisted_ip, " ",&last);
do {
if (!strncmp(this_whitelisted_ip,"env=",4)){
if (apr_table_get(r->subprocess_env, (this_whitelisted_ip+4))) {
return DECLINED;
}
}
if ((whitelisted_netmask = strchr(this_whitelisted_ip, '/'))) {
*whitelisted_netmask++ = '\0';
apr_ipsubnet_create(&this_subnet,this_whitelisted_ip,
whitelisted_netmask,r->pool);
} else {
apr_ipsubnet_create(&this_subnet,this_whitelisted_ip,NULL,r->pool);
}
#ifdef WITH_APACHE24
if (apr_ipsubnet_test(this_subnet,r->connection->client_addr)) {
#else
if (apr_ipsubnet_test(this_subnet,r->connection->remote_addr)) {
#endif
return DECLINED;
}
this_whitelisted_ip = apr_strtok(NULL, " ",&last);
} while (this_whitelisted_ip);
/* Pass the next request if comes fast enough to be considered the 'dup'*/
if (conf->rq_latency) {
if ((r->request_time - det->times[ det->posn ]) <= conf->rq_latency) {
return DECLINED;
}
}
if (det->pidaras) {
oldest_time = r->request_time - conf->memcache_block_time;
} else {
oldest_time = r->request_time - conf->memcache_max_time;
}
/* remove the requests with expired time */
last_seen = det->times;
while ( det->nelts > 0 && last_seen[ det->lastseenposn ] < oldest_time) {
det->nelts--;
det->times[ det->lastseenposn ] = -1;
det->lastseenposn = ( det->lastseenposn + 1 ) % det->size;
}
/* ...check if we have gone too far... */
if ( det->nelts >= det->size || det->nelts >= conf->memcache_max_requests) {
apr_rfc822_date(datetimestr, det->times[det->lastseenposn] );
/* ...if so, mark for blacklist... */
if (!det->pidaras) {
det->pidaras=1;
if (conf->memcache_block_time < conf->memcache_max_time) {
rv = apr_memcache_set( mc,
key,
(char*)det,
sizeof(user_details) + (sizeof(apr_time_t)* det->size),
apr_time_sec(conf->memcache_max_time)+600,
0);
} else {
rv = apr_memcache_set( mc,
key,
(char*)det,
sizeof(user_details) + (sizeof(apr_time_t)* det->size),
apr_time_sec(conf->memcache_block_time)+600,
0);
}
if (rv) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "Setting cache key for IP %s failed", key);
}
}
/* ... and send the predefined response. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"access to %s blocked for IP %s requests started at %s", r->uri, key, datetimestr);
return conf->httpretcode;
}
/* now add this one */
det->nelts++;
det->posn = ( det->posn + 1 ) % det->size;
det->times[det->posn ] = r->request_time;
/* Debug reporting for each hit
* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
* "Setting cache key for IP %s OK: seen %d in the last %d seconds", key, det->nelts, apr_time_sec(conf->memcache_max_time));
*/
/* keep the record around for a bit longer than the max time */
if (conf->memcache_block_time < conf->memcache_max_time){
rv = apr_memcache_set( mc,
key,
(char*)det,
sizeof(user_details) + (sizeof(apr_time_t)* det->size),
apr_time_sec(conf->memcache_max_time)+600,
0);
} else {
rv = apr_memcache_set( mc,
key,
(char*)det,
sizeof(user_details) + (sizeof(apr_time_t)* det->size),
apr_time_sec(conf->memcache_block_time)+600,
0);
}
if (rv) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"Setting cache key for IP %s failed", key);
}
return DECLINED;
}
static const command_rec ip_count_cmds[] =
{
AP_INIT_TAKE1("MemCacheServers", set_servers, NULL, RSRC_CONF,
"a list of servers running the memcached server (host:port)"),
AP_INIT_TAKE1("UriList", set_used_uris, NULL, RSRC_CONF,
"a list of patterns to match for the uris to operate on"),
AP_INIT_TAKE1("MemCacheMaxRequests", set_max_requests, NULL, RSRC_CONF,
"Max number of requests before failing"),
AP_INIT_TAKE1("MemCacheMaxTime", set_max_time, NULL, RSRC_CONF,
"Time period in which the requests have to come (seconds)"),
AP_INIT_TAKE1("MemCacheBlockTime", set_block_time, NULL, RSRC_CONF,
"Additional blocking time (seconds)"),
AP_INIT_TAKE1("Latency", set_rq_latency, NULL, RSRC_CONF,
"time between requests to consider them a single hit (microseconds)"),
AP_INIT_TAKE1("HttpResponse", set_httpretcode, NULL, RSRC_CONF,
"An HTTP code to return in response to a blocked request"),
AP_INIT_TAKE1("MemCacheAllow", set_whitelisted_ip, NULL, RSRC_CONF,
"IP adresses NOT to perform checks on"),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_child_init(ip_count_child_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_fixups(ip_count_check_auth, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA ip_count_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_ip_count_server_config,/* server config */
NULL, /* merge server config */
ip_count_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};