-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_sec.c
executable file
·166 lines (137 loc) · 3.94 KB
/
mod_sec.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
#include "mod_sec.h"
#include "body_reader.h"
static ModSecValuePair *process_intervention(request_rec *r, Transaction *trans)
{
ModSecValuePair *msvp = malloc(sizeof(ModSecValuePair));
if (!msvp)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to allocate memory for ModSecValuePair");
return NULL;
}
msvp->message = NULL;
ModSecurityIntervention it;
it.status = 200;
it.url = NULL;
it.log = NULL;
it.disruptive = 0;
msc_intervention(trans, &it);
if (it.log != NULL)
{
const char *xssMsg = "msg \"";
const char *msgStart = strstr(it.log, xssMsg);
if (msgStart)
{
msgStart += strlen(xssMsg);
const char *msgEnd = strchr(msgStart, '"');
if (msgEnd)
{
char *msg = malloc(msgEnd - msgStart + 1);
if (msg)
{
strncpy(msg, msgStart, msgEnd - msgStart);
msg[msgEnd - msgStart] = '\0';
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Detected message: %s", msg);
msvp->message = msg;
}
else
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to allocate memory for message");
}
}
}
free(it.log);
}
if (it.url != NULL)
{
free(it.url);
}
msvp->status = it.status;
return msvp;
}
void add_request_headers_to_transaction(Transaction *transaction, request_rec *r)
{
const apr_array_header_t *arr = apr_table_elts(r->headers_in);
const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
const char *prefixFormData = "multipart/form-data";
for (int i = 0; i < arr->nelts; i++)
{
if (strncmp(elts[i].val, prefixFormData, strlen(prefixFormData)) == 0)
{
msc_add_request_header(transaction, "Content-Type", "application/x-www-form-urlencoded");
}
else
{
msc_add_request_header(transaction, elts[i].key, elts[i].val);
}
}
}
void add_request_body(Transaction *transaction, json_object *json_obj)
{
const char *body = json_object_to_json_string(json_obj);
msc_append_request_body(transaction, body, strlen(body));
}
ModSecValuePair *mod_sec_handler(request_rec *r, json_object *json_obj, const char *rules_path, const char *socket_url)
{
int ret;
const char *error = NULL;
ModSecurity *modsec;
Transaction *transaction = NULL;
RulesSet *rules;
ModSecValuePair *msvp = NULL;
// Initialize ModSecurity
modsec = msc_init();
if (!modsec)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to initialize ModSecurity");
return NULL;
}
// Set ModSecurity connector info
msc_set_connector_info(modsec, "ModSec Connector v0.1");
// Create rules set
rules = msc_create_rules_set();
if (!rules)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to create rules set");
msc_cleanup(modsec);
return NULL;
}
// Load local rules file
if (rules_path) {
ret = msc_rules_add_file(rules, rules_path, &error);
if (ret < 0)
{
msc_cleanup(modsec);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to get file : %s", error);
return NULL;
}
}
// Create new transaction
transaction = msc_new_transaction(modsec, rules, NULL);
if (!transaction)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to create new transaction");
msc_cleanup(modsec);
return NULL;
}
// msc_process_connection(transaction, "127.0.0.1", 8282, "127.0.0.1", r->server->port);
msc_process_connection(transaction, r->hostname, r->useragent_addr->port, r->server->server_hostname, r->server->port);
msc_process_uri(transaction, r->unparsed_uri, r->method, "HTTP/1.1");
add_request_headers_to_transaction(transaction, r);
if (json_object_object_length(json_obj) != 0)
{
add_request_body(transaction, json_obj);
}
msc_process_request_headers(transaction);
msc_process_request_body(transaction);
msc_process_response_headers(transaction, 200, "HTTP 1.3");
msc_process_response_body(transaction);
msc_process_logging(transaction);
// Retrieve intervention details
msvp = process_intervention(r, transaction);
// Cleanup
// free(full_uri);
msc_transaction_cleanup(transaction);
msc_rules_cleanup(rules);
msc_cleanup(modsec);
return msvp;
}