-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmod_security3.c
624 lines (508 loc) · 14.7 KB
/
mod_security3.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
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
#include <stdio.h>
#include "mod_security3.h"
#include "msc_utils.h"
#include "msc_config.h"
/*
*
*/
msc_global *msc_apache;
char err_calloc[] = "ModSecurity: can't allocate memory for logmsg.";
void modsecurity_log_cb(void *log, const void* data)
{
const char *msg;
char *msglog;
unsigned int i, j;
if (log == NULL || data == NULL) {
return;
}
msg = (const char *) data;
request_rec *r = (request_rec *) log;
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
msglog = calloc(sizeof(char), strlen(msg)*2);
if (msglog == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
err_calloc,
r->status);
}
else {
// add % escape to avoid the '%' chars placeholder mark in logmsg
j = 0;
for(i=0; msg[i] != '\0'; i++) {
if (msg[i] == '%') {
msglog[j++] = '%';
}
msglog[j++] = msg[i];
}
msglog[j] = '\0';
ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
msglog,
r->status);
free(msglog);
}
#else
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
msg,
r->status);
#endif
}
int process_intervention (Transaction *t, request_rec *r)
{
ModSecurityIntervention intervention;
intervention.status = N_INTERVENTION_STATUS;
intervention.url = NULL;
intervention.log = NULL;
intervention.disruptive = 0;
int z = msc_intervention(t, &intervention);
if (z == 0)
{
return N_INTERVENTION_STATUS;
}
if (intervention.log == NULL)
{
intervention.log = "(no log message was specified)";
}
if (intervention.status == 301 || intervention.status == 302
||intervention.status == 303 || intervention.status == 307)
{
if (intervention.url != NULL)
{
apr_table_setn(r->headers_out, "Location", intervention.url);
return HTTP_MOVED_TEMPORARILY;
}
}
if (intervention.status != N_INTERVENTION_STATUS)
{
return intervention.status;
}
return N_INTERVENTION_STATUS;
}
/*
* Called only once. Used to initialise the ModSecurity
*
*/
int msc_apache_init(apr_pool_t *mp)
{
msc_apache = apr_pcalloc(mp, sizeof(msc_global));
if (msc_apache == NULL)
{
goto err_no_mem;
}
msc_apache->modsec = msc_init();
msc_set_connector_info(msc_apache->modsec, MSC_APACHE_CONNECTOR);
apr_pool_cleanup_register(mp, NULL, msc_module_cleanup, apr_pool_cleanup_null);
msc_set_log_cb(msc_apache->modsec, modsecurity_log_cb);
return 0;
err_no_mem:
return -1;
}
/*
* Called only once. Used to cleanup ModSecurity
*
*/
int msc_apache_cleanup()
{
msc_cleanup(msc_apache->modsec);
}
/*
* Used to cleanup the module
*
*/
static apr_status_t msc_module_cleanup(void *data)
{
msc_apache_cleanup();
return APR_SUCCESS;
}
/**
* Stores transaction context where it can be found in subsequent
* phases, redirections, or subrequests.
*/
static void store_tx_context(msc_t *msr, request_rec *r)
{
apr_table_setn(r->notes, NOTE_MSR, (void *)msr);
}
static msc_t *create_tx_context(request_rec *r) {
msc_t *msr = NULL;
msc_conf_t *z = NULL;
char *unique_id = NULL;
z = (msc_conf_t *)ap_get_module_config(r->per_dir_config,
&security3_module);
msr = (msc_t *)apr_pcalloc(r->pool, sizeof(msc_t));
if (msr == NULL) {
return NULL;
}
msr->r = r;
unique_id = getenv("UNIQUE_ID");
if (unique_id != NULL && strlen(unique_id) > 0) {
msr->t = msc_new_transaction_with_id(msc_apache->modsec,
(Rules *)z->rules_set, unique_id, (void *)r);
} else {
msr->t = msc_new_transaction(msc_apache->modsec,
(Rules *)z->rules_set, (void *)r);
}
store_tx_context(msr, r);
return msr;
}
/**
* Retrieves a previously stored transaction context by
* looking at the main request, and the previous requests.
*/
static msc_t *retrieve_tx_context(request_rec *r) {
msc_t *msr = NULL;
request_rec *rx = NULL;
/* Look in the current request first. */
msr = (msc_t *)apr_table_get(r->notes, NOTE_MSR);
if (msr != NULL)
{
msr->r = r;
return msr;
}
/* If this is a subrequest then look in the main request. */
if (r->main != NULL)
{
msr = (msc_t *)apr_table_get(r->main->notes, NOTE_MSR);
if (msr != NULL)
{
msr->r = r;
return msr;
}
}
/* If the request was redirected then look in the previous requests. */
rx = r->prev;
while (rx != NULL)
{
msr = (msc_t *)apr_table_get(rx->notes, NOTE_MSR);
if (msr != NULL)
{
msr->r = r;
return msr;
}
rx = rx->prev;
}
return NULL;
}
static int msc_hook_pre_config(apr_pool_t *mp, apr_pool_t *mp_log,
apr_pool_t *mp_temp)
{
void *data = NULL;
const char *key = "modsecurity-pre-config-init-flag";
int first_time = 0;
/* Figure out if we are here for the first time */
apr_pool_userdata_get(&data, key, mp);
if (data == NULL)
{
apr_pool_userdata_set((const void *) 1, key,
apr_pool_cleanup_null, mp);
first_time = 1;
}
if (!first_time)
{
return OK;
}
// Code to run only at the very first call.
int ret = msc_apache_init(mp);
if (ret == -1)
{
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"ModSecurity: Failed to initialise.");
return HTTP_INTERNAL_SERVER_ERROR;
}
return OK;
}
static int msc_hook_post_config(apr_pool_t *mp, apr_pool_t *mp_log,
apr_pool_t *mp_temp, server_rec *s)
{
void *data = NULL;
const char *key = "modsecurity-post-config-init-flag";
int first_time = 0;
/* Figure out if we are here for the first time */
apr_pool_userdata_get(&data, key, s->process->pool);
if (data == NULL)
{
apr_pool_userdata_set((const void *) 1, key,
apr_pool_cleanup_null, s->process->pool);
first_time = 1;
}
if (!first_time)
{
return OK;
}
// Code to run only at the very first call.
ap_log_error(APLOG_MARK, APLOG_NOTICE | APLOG_NOERRNO, 0, s,
"ModSecurity: %s configured.", MSC_APACHE_CONNECTOR);
return OK;
}
static int hook_connection_early(conn_rec *conn)
{
// At this point there isn't a request_rec attached to the request,
// therefore we can't create the config yet, lets wait till next phase.
return DECLINED;
}
/**
* Initial request processing, executed immediatelly after
* Apache receives the request headers. This function wil create
* a transaction context.
*/
static int hook_request_early(request_rec *r) {
msc_t *msr = NULL;
int rc = DECLINED;
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER < 3
const char *client_ip = r->connection->remote_ip;
int client_port = r->connection->remote_addr->port;
#else
const char *client_ip = r->connection->client_ip;
int client_port = r->connection->client_addr->port;
#endif
/* This function needs to run only once per transaction
* (i.e. subrequests and redirects are excluded).
*/
if ((r->main != NULL) || (r->prev != NULL)) {
return DECLINED;
}
/* Initialise transaction context and
* create the initial configuration.
*/
#ifdef REQUEST_EARLY
#error "Request Early is not ready for v3 yet."
msr = create_tx_context(r);
if (msr == NULL)
{
return DECLINED;
}
#endif
#ifndef LATE_CONNECTION_PROCESS
#error "Currently in v3 connection can only be processed late."
msc_process_connection(msr->t, client_ip,
client_port,
r->server->server_hostname,
(int) r->server->port);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
#endif
#ifdef REQUEST_EARLY
it = process_request_headers(r, msr);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
#endif
return rc;
}
/**
* Invoked as the first hook in the handler chain, this function
* executes the second phase of ModSecurity request processing.
*/
static int hook_request_late(request_rec *r)
{
msc_t *msr = NULL;
int it;
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER < 3
const char *client_ip = r->connection->remote_ip;
int client_port = r->connection->remote_addr->port;
#else
const char *client_ip = r->connection->client_ip;
int client_port = r->connection->client_addr->port;
#endif
/* This function needs to run only once per transaction
* (i.e. subrequests and redirects are excluded).
*/
if ((r->main != NULL) || (r->prev != NULL))
{
return DECLINED;
}
/* Find the transaction context and make sure
* we are supposed to proceed.
*/
#ifdef REQUEST_EARLY
msr = retrieve_tx_context(r);
#else
msr = create_tx_context(r);
#endif
if (msr == NULL)
{
/* If we can't find the context that probably means it's
* a subrequest that was not initiated from the outside.
*/
return DECLINED;
}
#ifdef LATE_CONNECTION_PROCESS
msc_process_connection(msr->t, client_ip,
client_port,
r->server->server_hostname,
(int) r->server->port);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
#endif
#ifndef REQUEST_EARLY
it = process_request_headers(r, msr);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
#endif
return DECLINED;
}
/**
* Invoked at the end of each transaction.
*/
static int hook_log_transaction(request_rec *r)
{
const apr_array_header_t *arr = NULL;
request_rec *origr = NULL;
msc_t *msr = NULL;
int it;
msr = retrieve_tx_context(r);
if (msr == NULL)
{
return DECLINED;
}
msc_update_status_code(msr->t, r->status);
msc_process_logging(msr->t);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
return DECLINED;
}
/**
* Invoked right before request processing begins. This is
* when we need to decide if we want to hook into the output
* filter chain.
*/
static void hook_insert_filter(request_rec *r)
{
msc_t *msr = NULL;
/* Find the transaction context first. */
msr = retrieve_tx_context(r);
if (msr == NULL)
{
return;
}
#if 1
/* Add the input filter, but only if we need it to run. */
ap_add_input_filter("MODSECURITY_IN", msr, r, r->connection);
#endif
/* The output filters only need to be added only once per transaction
* (i.e. subrequests and redirects are excluded).
*/
if ((r->main != NULL) || (r->prev != NULL))
{
return;
}
ap_add_output_filter("MODSECURITY_OUT", msr, r, r->connection);
}
static int process_request_headers(request_rec *r, msc_t *msr) {
/* process uri */
{
int it;
int offset = (r->protocol && strlen(r->protocol) > 5 && r->protocol[0] == 'H') ? 5 : 0;
msc_process_uri(msr->t, r->unparsed_uri, r->method, r->protocol + offset);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
}
/* add request headers */
{
const apr_array_header_t *arr = NULL;
const apr_table_entry_t *te = NULL;
int i;
int it;
arr = apr_table_elts(r->headers_in);
te = (apr_table_entry_t *)arr->elts;
for (i = 0; i < arr->nelts; i++)
{
const char *key = te[i].key;
const char *val = te[i].val;
msc_add_request_header(msr->t, key, val);
}
msc_process_request_headers(msr->t);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
return it;
}
}
return N_INTERVENTION_STATUS;
}
static void msc_register_hooks(apr_pool_t *pool)
{
static const char *const postconfig_beforeme_list[] = {
"mod_unique_id.c",
"mod_ssl.c",
NULL
};
static const char *const postconfig_afterme_list[] = {
"mod_fcgid.c",
"mod_cgid.c",
NULL
};
static const char *const postread_beforeme_list[] = {
"mod_rpaf.c",
"mod_rpaf-2.0.c",
"mod_extract_forwarded.c",
"mod_extract_forwarded2.c",
"mod_remoteip.c",
"mod_custom_header.c",
"mod_breach_realip.c",
"mod_breach_trans.c",
"mod_unique_id.c",
NULL
};
static const char *const postread_afterme_list[] = {
"mod_log_forensic.c",
NULL
};
static const char *const transaction_afterme_list[] = {
"mod_log_config.c",
NULL
};
static const char *const fixups_beforeme_list[] = {
"mod_env.c",
NULL
};
/* Module initialization */
ap_hook_pre_config(msc_hook_pre_config, NULL, NULL, APR_HOOK_FIRST);
ap_hook_post_config(msc_hook_post_config, postconfig_beforeme_list,
postconfig_afterme_list, APR_HOOK_REALLY_LAST);
/* Connection processing hooks - only global configuration. */
ap_hook_post_read_request(hook_request_early,
postread_beforeme_list, postread_afterme_list, APR_HOOK_REALLY_FIRST);
/* still, we don't have location configuration yet. */
ap_hook_process_connection(hook_connection_early, NULL, NULL, APR_HOOK_FIRST);
ap_hook_fixups(hook_request_late, fixups_beforeme_list, NULL, APR_HOOK_REALLY_FIRST);
/* Lets add the remaining hooks */
ap_hook_insert_filter(hook_insert_filter, NULL, NULL, APR_HOOK_FIRST);
/* Logging */
/* ap_hook_error_log is called for every error log entry that apache writes.
* may not be necessary in our particular case. Disabling for now.
*
* ap_hook_error_log(hook_error_log, NULL, NULL, APR_HOOK_MIDDLE);
*
*/
ap_hook_log_transaction(hook_log_transaction, NULL, transaction_afterme_list, APR_HOOK_MIDDLE);
/* request body */
ap_register_input_filter("MODSECURITY_IN", input_filter,
NULL, AP_FTYPE_CONTENT_SET);
/* response body */
ap_register_output_filter("MODSECURITY_OUT", output_filter,
NULL, AP_FTYPE_CONTENT_SET - 3);
}
module AP_MODULE_DECLARE_DATA security3_module =
{
STANDARD20_MODULE_STUFF,
msc_hook_create_config_directory, // Per-directory configuration.
msc_hook_merge_config_directory, // Merge handler for per-directory.
NULL, // Per-server conf handler.
NULL, // Merge handler for per-server
// configurations.
module_directives,
msc_register_hooks
};