-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatchers.c
322 lines (274 loc) · 10.4 KB
/
matchers.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "lrc.h"
#include "logger.h"
#include "matchers.h"
struct matcher_entry *matchers_match(const char *data, int datalen, struct ctx *ctx, u_int proto, u_int src_port, u_int dst_port)
{
struct matcher_entry *matcher;
int ovector[30];
for(matcher = ctx->matchers_list; matcher != NULL; matcher = matcher->next) {
if(matcher->proto != MATCHER_PROTO_ANY && matcher->proto != proto) {
continue;
}
if((matcher->dst_port > 0 && matcher->dst_port != dst_port) || (matcher->src_port > 0 && matcher->src_port != src_port)) {
continue;
}
if(pcre_exec(matcher->match, NULL, data, datalen, 0, 0, ovector, 30) > 0) {
logger(INFO, "Matched pattern for '%s'", matcher->name);
if(matcher->ignore && pcre_exec(matcher->ignore, NULL, data, datalen, 0, 0, ovector, 30) > 0) {
logger(INFO, "Matched ignore for '%s'", matcher->name);
continue;
} else {
return matcher;
}
}
}
return NULL;
}
struct matcher_entry *matchers_get_response(u_char *data, u_int datalen, struct ctx *ctx, u_int type, u_int src_port, u_int dst_port)
{
struct matcher_entry *matcher;
#ifdef HAVE_PYTHON
PyObject *args;
PyObject *value;
Py_ssize_t rdatalen;
char *rdata;
#endif
if(!(matcher = matchers_match((const char *)data, datalen, ctx, type, src_port, dst_port))) {
logger(DBG, "No matchers found for data");
return NULL;
}
#ifdef HAVE_PYTHON
if(matcher->pyfunc) {
logger(DBG, "We have a Python code to construct response");
args = PyTuple_New(2);
PyTuple_SetItem(args,0,PyString_FromStringAndSize((const char *)data, datalen)); // here is data
PyTuple_SetItem(args,1,PyInt_FromSsize_t(datalen));
value = PyObject_CallObject(matcher->pyfunc, args);
if(value == NULL) {
PyErr_Print();
logger(WARN, "Python function returns no data!");
return NULL;
}
rdata = PyString_AsString(value);
rdatalen = PyString_Size(value);
if(rdata != NULL && rdatalen > 0) {
matcher->response_len = (u_int) rdatalen;
if(matcher->response) {
// We already have previous response, free it
free(matcher->response);
}
matcher->response = malloc(matcher->response_len);
memcpy(matcher->response, (u_char *) rdata, rdatalen);
} else {
PyErr_Print();
logger(WARN, "Python cannot convert return string");
return NULL;
}
return matcher;
}
#endif
if(matcher->response) {
logger(DBG, "We have a plain text response");
return matcher;
}
logger(WARN, "There is no response data!");
return NULL;
}
struct matcher_entry *check_block_params(struct matcher_entry *head)
{
if(head->name == NULL) {
printf("You must specify block name\n");
return NULL;
}
if(head->match == NULL) {
printf("Error: block \"%s\" missing match!\n", head->name);
return NULL;
}
#ifdef HAVE_PYTHON
if((head->response == NULL || head->response_len < 1 || head->response_len > MATCHER_MAX_RESPONSE) && head->pyfunc == NULL) {
#else
if((head->response == NULL || head->response_len < 1 || head->response_len > MATCHER_MAX_RESPONSE)) {
#endif
printf("Error: block \"%s\" has missing or malformed response/pymodule!\n", head->name);
return NULL;
}
if(!head->proto) {
printf("Error: block \"%s\" has missing proto\n", head->name);
return NULL;
}
if(head->dst_port >= 65535 || head->dst_port < 0) {
printf("Error: block \"%s\" has incorrect dst port\n", head->name);
return NULL;
}
if(head->src_port >= 65535 || head->src_port < 0) {
printf("Error: block \"%s\" has incorrect src port\n", head->name);
return NULL;
}
return head;
}
struct matcher_entry *parse_matchers_file(char *matcher_file_path)
{
FILE *matcher_file;
char matcher_line[MATCHER_MAX_LEN];
struct matcher_entry *head = NULL;
int line_no = 0;
matcher_file = fopen(matcher_file_path, "r");
if(matcher_file == NULL) {
perror("fopen");
return NULL;
}
while(fgets(matcher_line, MATCHER_MAX_LEN, matcher_file) != NULL) {
char command[64] = {0};
char *argument, *ptr;
const char *errptr;
int arglen, lenread=0;
int c, fd;
#ifdef HAVE_PYTHON
int pyinitialized=0;
#endif
struct stat statbuf;
line_no++;
matcher_line[MATCHER_MAX_LEN - 1] = 0;
sscanf(matcher_line, "%64s", command);
if(command[0] == 0 || command[0] == '#') {
continue;
}
argument = matcher_line + strlen(command);
// skip over any whitespace
while(*argument == 0x20 || *argument == 0x09) {
argument++;
}
arglen = strlen(argument);
// truncate any new-lines etc
for(ptr = argument + arglen -1; ptr > argument ; ptr--) {
if(*ptr == '\n' || *ptr == '\r') {
*ptr = 0;
}
}
// start parsing commands
if(strcmp(command, "begin") == 0) {
struct matcher_entry *tmp = malloc(sizeof(struct matcher_entry));
if(tmp == NULL) {
perror("malloc");
return NULL;
}
// need to zero this struct
bzero(tmp, sizeof(struct matcher_entry));
tmp->next = head;
head = tmp;
strncpy(head->name, argument, sizeof(head->name));
} else {
if(head == NULL) {
printf("Error in matchers file line %u\n", line_no);
return NULL;
}
if(strcmp(command, "match") == 0) {
// the regex to match
head->match = pcre_compile(argument, PCRE_MULTILINE|PCRE_DOTALL, &errptr, &c, NULL);
if(head->match == NULL) {
printf("Error at character %d in pattern: \"%s\" (%s)\n", c, argument, errptr);
return NULL;
}
} else if(strcmp(command, "ignore") == 0) {
// the regex to ignore
head->ignore = pcre_compile(argument, PCRE_MULTILINE|PCRE_DOTALL, &errptr, &c,NULL);
if(head->ignore == NULL) {
printf("Error at character %d in pattern: \"%s\" (%s)\n", c, argument, errptr);
return NULL;
}
} else if(strcmp(command, "option") == 0) {
if(strcmp(argument, "reset") == 0) {
head->options |= MATCHER_OPTION_RESET;
} else {
printf("Unknown option: %s\n", argument);
return NULL;
}
} else if(strcmp(command, "proto") == 0) {
if(strcmp(argument, "tcp") == 0) {
head->proto = MATCHER_PROTO_TCP;
} else if(strcmp(argument, "udp") == 0) {
head->proto = MATCHER_PROTO_UDP;
} else if(strcmp(argument, "any") == 0) {
head->proto = MATCHER_PROTO_ANY;
} else {
printf("Unknown proto: %s\n", argument);
return NULL;
}
} else if(strcmp(command, "dst_port") == 0) {
if(strcmp(argument, "any") == 0) {
head->dst_port = 0;
} else {
head->dst_port = atoi(argument);
}
} else if(strcmp(command, "src_port") == 0) {
if(strcmp(argument, "any") == 0) {
head->src_port = 0;
} else {
head->src_port = atoi(argument);
}
} else if(strcmp(command, "response") == 0) {
// path to the file to load the response from
if((fd = open(argument, O_RDONLY)) < 0) {
printf("Error opening file: %s\n", argument);
perror("open");
return NULL;
}
if(fstat(fd, &statbuf) < 0) {
perror("stat");
return NULL;
}
if(statbuf.st_size > MATCHER_MAX_RESPONSE) {
printf("Error: file %s is too large! (Maximum size is %u)\n", argument, MATCHER_MAX_RESPONSE);
return NULL;
}
head->response = malloc(statbuf.st_size + 1);
if(head->response == NULL) {
perror("malloc");
return NULL;
}
while((c = read(fd, head->response + lenread, statbuf.st_size - lenread)) < statbuf.st_size) {
lenread += c;
printf("read %d bytes\n", lenread);
}
lenread += c;
head->response_len = lenread;
#ifdef HAVE_PYTHON
} else if(strcmp(command, "pymodule") == 0) {
if(!pyinitialized) {
setenv("PYTHONPATH", PYTHONPATH, 1);
Py_Initialize();
pyinitialized = 1;
}
PyObject *module = PyImport_Import(PyString_FromString(argument));
if(module == NULL) {
PyErr_Print();
printf("Error loading module: %s\n", argument);
return NULL;
}
head->pyfunc = PyObject_GetAttrString(module, PYFUNCNAME);
if(head->pyfunc == NULL) {
PyErr_Print();
printf("No function named '"PYFUNCNAME"' in module: %s\n", argument);
return NULL;
}
#endif
} else if(strcmp(command, "end") == 0) {
// now's a good time to make sure the block had everything we care about..
if(head && !(head = check_block_params(head))) {
return NULL;
}
} else {
printf("Unknown command at line %u\n", line_no);
return NULL;
}
}
}
return head;
}