-
Notifications
You must be signed in to change notification settings - Fork 1
/
readfp.c
450 lines (264 loc) · 9.35 KB
/
readfp.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
/*
p0f - p0f.fp file parser
------------------------
Every project has this one really ugly C file. This is ours.
Copyright (C) 2012 by Michal Zalewski <[email protected]>
Distributed under the terms and conditions of GNU LGPL.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "types.h"
#include "system.h"
#include "debug.h"
#include "alloc-inl.h"
#include "fp_tcp.h"
#include "fp_mtu.h"
#include "fp_http.h"
#include "readfp.h"
static u32 sig_cnt; /* Total number of p0f.fp sigs */
static u8 state = CF_NEED_SECT, /* Parser state (CF_NEED_*) */
mod_type, /* Current module (CF_MOD_*) */
mod_to_srv, /* Traffic direction */
generic; /* Generic signature? */
static s32 sig_class; /* Signature class ID (-1 = userland) */
static u32 sig_name; /* Signature name */
static u8* sig_flavor; /* Signature flavor */
static u32* cur_sys; /* Current 'sys' values */
static u32 cur_sys_cnt; /* Number of 'sys' entries */
u8 **fp_os_classes, /* Map of OS classes */
**fp_os_names; /* Map of OS names */
static u32 class_cnt, /* Sizes for maps */
name_cnt,
label_id, /* Current label ID */
line_no; /* Current line number */
/* Parse 'classes' parameter by populating fp_os_classes. */
static void config_parse_classes(u8* val) {
while (*val) {
u8* nxt;
while (isblank(*val) || *val == ',') val++;
nxt = val;
while (isalnum(*nxt)) nxt++;
if (nxt == val || (*nxt && *nxt != ','))
FATAL("Malformed class entry in line %u.", line_no);
fp_os_classes = DFL_ck_realloc(fp_os_classes, (class_cnt + 1) * sizeof(u8*));
fp_os_classes[class_cnt++] = DFL_ck_memdup_str(val, nxt - val);
val = nxt;
}
}
/* Look up or create OS or application id. */
u32 lookup_name_id(u8* name, u8 len) {
u32 i;
for (i = 0; i < name_cnt; i++)
if (!strncasecmp((char*)name, (char*)fp_os_names[i], len)
&& !fp_os_names[i][len]) break;
if (i == name_cnt) {
sig_name = name_cnt;
fp_os_names = DFL_ck_realloc(fp_os_names, (name_cnt + 1) * sizeof(u8*));
fp_os_names[name_cnt++] = DFL_ck_memdup_str(name, len);
}
return i;
}
/* Parse 'label' parameter by looking up ID and recording name / flavor. */
static void config_parse_label(u8* val) {
u8* nxt;
u32 i;
/* Simplified handling for [mtu] signatures. */
if (mod_type == CF_MOD_MTU) {
if (!*val) FATAL("Empty MTU label in line %u.\n", line_no);
sig_flavor = DFL_ck_strdup(val);
return;
}
if (*val == 'g') generic = 1;
else if (*val == 's') generic = 0;
else FATAL("Malformed class entry in line %u.", line_no);
if (val[1] != ':') FATAL("Malformed class entry in line %u.", line_no);
val += 2;
nxt = val;
while (isalnum(*nxt) || *nxt == '!') nxt++;
if (nxt == val || *nxt != ':') FATAL("Malformed class entry in line %u.", line_no);
if (*val == '!' && val[1] == ':') {
sig_class = -1;
} else {
*nxt = 0;
for (i = 0; i < class_cnt; i++)
if (!strcasecmp((char*)val, (char*)fp_os_classes[i])) break;
if (i == class_cnt) FATAL("Unknown class '%s' in line %u.", val, line_no);
sig_class = i;
}
nxt++;
val = nxt;
while (isalnum(*nxt) || (*nxt && strchr(NAME_CHARS, *nxt))) nxt++;
if (nxt == val || *nxt != ':') FATAL("Malformed name in line %u.", line_no);
sig_name = lookup_name_id(val, nxt - val);
if (nxt[1]) sig_flavor = DFL_ck_strdup(nxt + 1);
else sig_flavor = NULL;
label_id++;
}
/* Parse 'sys' parameter into cur_sys[]. */
static void config_parse_sys(u8* val) {
if (cur_sys) {
cur_sys = NULL;
cur_sys_cnt = 0;
}
while (*val) {
u8* nxt;
u8 is_cl = 0, orig;
u32 i;
while (isblank(*val) || *val == ',') val++;
if (*val == '@') { is_cl = 1; val++; }
nxt = val;
while (isalnum(*nxt) || (*nxt && strchr(NAME_CHARS, *nxt))) nxt++;
if (nxt == val || (*nxt && *nxt != ','))
FATAL("Malformed sys entry in line %u.", line_no);
orig = *nxt;
*nxt = 0;
if (is_cl) {
for (i = 0; i < class_cnt; i++)
if (!strcasecmp((char*)val, (char*)fp_os_classes[i])) break;
if (i == class_cnt)
FATAL("Unknown class '%s' in line %u.", val, line_no);
i |= SYS_CLASS_FLAG;
} else {
for (i = 0; i < name_cnt; i++)
if (!strcasecmp((char*)val, (char*)fp_os_names[i])) break;
if (i == name_cnt) {
fp_os_names = DFL_ck_realloc(fp_os_names, (name_cnt + 1) * sizeof(u8*));
fp_os_names[name_cnt++] = DFL_ck_memdup_str(val, nxt - val);
}
}
cur_sys = DFL_ck_realloc(cur_sys, (cur_sys_cnt + 1) * 4);
cur_sys[cur_sys_cnt++] = i;
*nxt = orig;
val = nxt;
}
}
/* Read p0f.fp line, dispatching it to fingerprinting modules as necessary. */
static void config_parse_line(u8* line) {
u8 *val,*eon;
/* Special handling for [module:direction]... */
if (*line == '[') {
u8* dir;
line++;
/* Simplified case for [mtu]. */
if (!strcmp((char*)line, "mtu]")) {
mod_type = CF_MOD_MTU;
state = CF_NEED_LABEL;
return;
}
dir = (u8*)strchr((char*)line, ':');
if (!dir) FATAL("Malformed section identifier in line %u.", line_no);
*dir = 0; dir++;
if (!strcmp((char*)line, "tcp")) {
mod_type = CF_MOD_TCP;
} else if (!strcmp((char*)line, "http")) {
mod_type = CF_MOD_HTTP;
} else {
FATAL("Unrecognized fingerprinting module '%s' in line %u.", line, line_no);
}
if (!strcmp((char*)dir, "request]")) {
mod_to_srv = 1;
} else if (!strcmp((char*)dir, "response]")) {
mod_to_srv = 0;
} else {
FATAL("Unrecognized traffic direction in line %u.", line_no);
}
state = CF_NEED_LABEL;
return;
}
/* Everything else follows the 'name = value' approach. */
val = line;
while (isalpha(*val) || *val == '_') val++;
eon = val;
while (isblank(*val)) val++;
if (line == val || *val != '=')
FATAL("Unexpected statement in line %u.", line_no);
while (isblank(*++val));
*eon = 0;
if (!strcmp((char*)line, "classes")) {
if (state != CF_NEED_SECT)
FATAL("misplaced 'classes' in line %u.", line_no);
config_parse_classes(val);
} else if (!strcmp((char*)line, "ua_os")) {
if (state != CF_NEED_LABEL || mod_to_srv != 1 || mod_type != CF_MOD_HTTP)
FATAL("misplaced 'us_os' in line %u.", line_no);
http_parse_ua(val, line_no);
} else if (!strcmp((char*)line, "label")) {
/* We will drop sig_sys / sig_flavor on the floor if no signatures
actually created, but it's not worth tracking that. */
if (state != CF_NEED_LABEL && state != CF_NEED_SIG)
FATAL("misplaced 'label' in line %u.", line_no);
config_parse_label(val);
if (mod_type != CF_MOD_MTU && sig_class < 0) state = CF_NEED_SYS;
else state = CF_NEED_SIG;
} else if (!strcmp((char*)line, "sys")) {
if (state != CF_NEED_SYS)
FATAL("Misplaced 'sys' in line %u.", line_no);
config_parse_sys(val);
state = CF_NEED_SIG;
} else if (!strcmp((char*)line, "sig")) {
if (state != CF_NEED_SIG) FATAL("Misplaced 'sig' in line %u.", line_no);
switch (mod_type) {
case CF_MOD_TCP:
tcp_register_sig(mod_to_srv, generic, sig_class, sig_name, sig_flavor,
label_id, cur_sys, cur_sys_cnt, val, line_no);
break;
case CF_MOD_MTU:
mtu_register_sig(sig_flavor, val, line_no);
break;
case CF_MOD_HTTP:
http_register_sig(mod_to_srv, generic, sig_class, sig_name, sig_flavor,
label_id, cur_sys, cur_sys_cnt, val, line_no);
break;
}
sig_cnt++;
} else {
FATAL("Unrecognized field '%s' in line %u.", line, line_no);
}
}
/* Top-level file parsing. */
void read_config(u8* fname) {
s32 f;
struct stat st;
u8 *data, *cur;
f = open((char*)fname, O_RDONLY);
if (f < 0) PFATAL("Cannot open '%s' for reading.", fname);
if (fstat(f, &st)) PFATAL("fstat() on '%s' failed.", fname);
if (!st.st_size) {
close(f);
goto end_fp_read;
}
cur = data = ck_alloc(st.st_size + 1);
if (read(f, data, st.st_size) != st.st_size)
FATAL("Short read from '%s'.", fname);
data[st.st_size] = 0;
close(f);
/* If you put NUL in your p0f.fp... Well, sucks to be you. */
while (1) {
u8 *eol;
line_no++;
while (isblank(*cur)) cur++;
eol = cur;
while (*eol && *eol != '\n') eol++;
if (*cur != ';' && cur != eol) {
u8* line = ck_memdup_str(cur, eol - cur);
config_parse_line(line);
ck_free(line);
}
if (!*eol) break;
cur = eol + 1;
}
ck_free(data);
end_fp_read:
if (!sig_cnt)
SAYF("[!] No signatures found in '%s'.\n", fname);
else
SAYF("[+] Loaded %u signature%s from '%s'.\n", sig_cnt,
sig_cnt == 1 ? "" : "s", fname);
}