forked from wolfsoft/poc-apache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_poc.c
120 lines (103 loc) · 3.8 KB
/
mod_poc.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
#include <http_core.h>
#include <httpd.h>
#include <http_config.h>
#include <apr_optional.h>
#include <ap_release.h>
#include <http_log.h>
#include <http_protocol.h>
#include <http_request.h>
// holder of prefetch http body data
typedef struct {
apr_status_t cached_ret;
apr_bucket_brigade *cached_brigade;
} poc_ctx_t;
// input filter helper for fetch and cache post body
static apr_status_t input_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes) {
poc_ctx_t *ctx = (poc_ctx_t*)f->ctx;
if (ctx && ctx->cached_brigade) {
APR_BRIGADE_CONCAT(bb, ctx->cached_brigade);
apr_brigade_cleanup(ctx->cached_brigade);
ctx->cached_brigade = NULL;
return ctx->cached_ret;
}
return ap_get_brigade(f->next, bb, mode, block, readbytes);
}
// main handler
static int handler(request_rec* r) {
// skip processing subrequests
if ((r->main != NULL) || (r->prev != NULL)) {
if (!strcmp(r->handler, CGI_MAGIC_TYPE) || !strcmp(r->handler, "cgi-script")) {
if (r->prev != NULL && r->prev->input_filters != NULL && r->prev->input_filters->frec != NULL && r->prev->input_filters->frec->name != NULL && !strcmp(r->prev->input_filters->frec->name, "poc_in"))
ap_add_input_filter("poc_IN", r->prev->input_filters->ctx, r, r->connection);
}
return DECLINED;
}
// initialize input filter context
ap_filter_t *filter = ap_add_input_filter("poc_IN", NULL, r, r->connection);
poc_ctx_t *ctx = (poc_ctx_t*)filter->ctx;
if (!ctx) {
filter->ctx = ctx = (poc_ctx_t*)apr_pcalloc(r->pool, sizeof(poc_ctx_t));
ctx->cached_brigade = apr_brigade_create(filter->c->pool, filter->c->bucket_alloc);
}
// lets read the whole request body
apr_bucket_brigade *bb = apr_brigade_create(filter->c->pool, filter->c->bucket_alloc);
int fetch_more = 1;
int found = 0;
while (fetch_more) {
ctx->cached_ret = ap_get_brigade(filter->next, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN);
if (ctx->cached_ret != APR_SUCCESS) break;
apr_bucket *b = APR_BRIGADE_FIRST(bb);
APR_BRIGADE_CONCAT(ctx->cached_brigade, bb);
for (; b != APR_BRIGADE_SENTINEL(ctx->cached_brigade); b = APR_BUCKET_NEXT(b)) {
if (!fetch_more) break;
if (APR_BUCKET_IS_EOS(b)) {
fetch_more = 0;
break;
}
if (APR_BUCKET_IS_METADATA(b)) continue;
const char *buf;
apr_size_t nbytes;
if (apr_bucket_read(b, &buf, &nbytes, APR_BLOCK_READ) != APR_SUCCESS) continue;
if (!nbytes) continue;
if (memchr(buf, 'Z', nbytes)) {
found = 1;
}
if (APR_BUCKET_NEXT(b) && APR_BUCKET_IS_EOS(APR_BUCKET_NEXT(b))) {
fetch_more = 0;
}
} // for
} // while (fetch_more)
if (found) {
static const char* page = "<html><body><h1>mod_poc handler</h1><p>The letter 'Z' was found!</p><p><a href=\"javascript:history.back();\">Go back</a></p></body></html>";
ap_set_content_type(r, "text/html");
ap_rwrite(page, strlen(page), r);
return OK;
}
return DECLINED;
}
// entry point
static void register_hooks(apr_pool_t* pool) {
ap_register_input_filter("poc_IN", input_filter, NULL, AP_FTYPE_RESOURCE);
// be sure that our module is first in the chain
static const char *const handlers_afterme_list[] = {
"mod_php.c",
"mod_cgi.c",
"mod_cgid.c",
"mod_fcgid.c",
"mod_actions.c",
NULL
};
ap_hook_handler(handler, NULL, handlers_afterme_list, APR_HOOK_REALLY_FIRST);
}
#ifdef APLOG_USE_MODULE
APLOG_USE_MODULE(poc);
#endif
module AP_MODULE_DECLARE_DATA poc_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
register_hooks /* register hooks */
};