-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp_tcp.c
1341 lines (835 loc) · 30.9 KB
/
fp_tcp.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
p0f - TCP/IP packet matching
----------------------------
Copyright (C) 2012 by Michal Zalewski <[email protected]>
Distributed under the terms and conditions of GNU LGPL.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <ctype.h>
#include "types.h"
#include "config.h"
#include "debug.h"
#include "alloc-inl.h"
#include "process.h"
#include "hash.h"
#include "tcp.h"
#include "readfp.h"
#include "p0f.h"
#include "fp_tcp.h"
/* TCP signature buckets: */
static struct tcp_sig_record* sigs[2][SIG_BUCKETS];
static u32 sig_cnt[2][SIG_BUCKETS];
/* Figure out what the TTL distance might have been for an unknown sig. */
static u8 guess_dist(u8 ttl) {
if (ttl <= 32) return 32 - ttl;
if (ttl <= 64) return 64 - ttl;
if (ttl <= 128) return 128 - ttl;
return 255 - ttl;
}
/* Figure out if window size is a multiplier of MSS or MTU. We don't take window
scaling into account, because neither do TCP stack developers. */
static s16 detect_win_multi(struct tcp_sig* ts, u8* use_mtu, u16 syn_mss) {
u16 win = ts->win;
s32 mss = ts->mss, mss12 = mss - 12;
if (!win || mss < 100 || ts->win_type != WIN_TYPE_NORMAL)
return -1;
#define RET_IF_DIV(_div, _use_mtu, _desc) do { \
if ((_div) && !(win % (_div))) { \
*use_mtu = (_use_mtu); \
DEBUG("[#] Window size %u is a multiple of %s [%u].\n", win, _desc, _div); \
return win / (_div); \
} \
} while (0)
RET_IF_DIV(mss, 0, "MSS");
/* Some systems will sometimes subtract 12 bytes when timestamps are in use. */
if (ts->ts1) RET_IF_DIV(mss12, 0, "MSS - 12");
/* Some systems use MTU on the wrong interface, so let's check for the most
common case. */
RET_IF_DIV(1500 - MIN_TCP4, 0, "MSS (MTU = 1500, IPv4)");
RET_IF_DIV(1500 - MIN_TCP4 - 12, 0, "MSS (MTU = 1500, IPv4 - 12)");
if (ts->ip_ver == IP_VER6) {
RET_IF_DIV(1500 - MIN_TCP6, 0, "MSS (MTU = 1500, IPv6)");
RET_IF_DIV(1500 - MIN_TCP6 - 12, 0, "MSS (MTU = 1500, IPv6 - 12)");
}
/* Some systems use MTU instead of MSS: */
RET_IF_DIV(mss + MIN_TCP4, 1, "MTU (IPv4)");
RET_IF_DIV(mss + ts->tot_hdr, 1, "MTU (actual size)");
if (ts->ip_ver == IP_VER6) RET_IF_DIV(mss + MIN_TCP6, 1, "MTU (IPv6)");
RET_IF_DIV(1500, 1, "MTU (1500)");
/* On SYN+ACKs, some systems use of the peer: */
if (syn_mss) {
RET_IF_DIV(syn_mss, 0, "peer MSS");
RET_IF_DIV(syn_mss - 12, 0, "peer MSS - 12");
}
#undef RET_IF_DIV
return -1;
}
/* See if any of the p0f.fp signatures matches the collected data. */
static void tcp_find_match(u8 to_srv, struct tcp_sig* ts, u8 dupe_det,
u16 syn_mss) {
struct tcp_sig_record* fmatch = NULL;
struct tcp_sig_record* gmatch = NULL;
u32 bucket = ts->opt_hash % SIG_BUCKETS;
u32 i;
u8 use_mtu = 0;
s16 win_multi = detect_win_multi(ts, &use_mtu, syn_mss);
CP(sigs[to_srv][bucket]);
for (i = 0; i < sig_cnt[to_srv][bucket]; i++) {
struct tcp_sig_record* ref = sigs[to_srv][bucket] + i;
struct tcp_sig* refs = CP(ref->sig);
u8 fuzzy = 0;
u32 ref_quirks = refs->quirks;
if (ref->sig->opt_hash != ts->opt_hash) continue;
/* If the p0f.fp signature has no IP version specified, we need
to remove IPv6-specific quirks from it when matching IPv4
packets, and vice versa. */
if (refs->ip_ver == -1)
ref_quirks &= ((ts->ip_ver == IP_VER4) ? ~(QUIRK_FLOW) :
~(QUIRK_DF | QUIRK_NZ_ID | QUIRK_ZERO_ID));
if (ref_quirks != ts->quirks) {
u32 deleted = (ref_quirks ^ ts->quirks) & ref_quirks,
added = (ref_quirks ^ ts->quirks) & ts->quirks;
/* If there is a difference in quirks, but it amounts to 'df' or 'id+'
disappearing, or 'id-' or 'ecn' appearing, allow a fuzzy match. */
if (fmatch || (deleted & ~(QUIRK_DF | QUIRK_NZ_ID)) ||
(added & ~(QUIRK_ZERO_ID | QUIRK_ECN))) continue;
fuzzy = 1;
}
/* Fixed parameters. */
if (refs->opt_eol_pad != ts->opt_eol_pad ||
refs->ip_opt_len != ts->ip_opt_len) continue;
/* TTL matching, with a provision to allow fuzzy match. */
if (ref->bad_ttl) {
if (refs->ttl < ts->ttl) continue;
} else {
if (refs->ttl < ts->ttl || refs->ttl - ts->ttl > MAX_DIST) fuzzy = 1;
}
/* Simple wildcards. */
if (refs->mss != -1 && refs->mss != ts->mss) continue;
if (refs->wscale != -1 && refs->wscale != ts->wscale) continue;
if (refs->pay_class != -1 && refs->pay_class != ts->pay_class) continue;
/* Window size. */
if (ts->win_type != WIN_TYPE_NORMAL) {
/* Comparing two p0f.fp signatures. */
if (refs->win_type != ts->win_type || refs->win != ts->win) continue;
} else {
/* Comparing real-world stuff. */
switch (refs->win_type) {
case WIN_TYPE_NORMAL:
if (refs->win != ts->win) continue;
break;
case WIN_TYPE_MOD:
if (ts->win % refs->win) continue;
break;
case WIN_TYPE_MSS:
if (use_mtu || refs->win != win_multi) continue;
break;
case WIN_TYPE_MTU:
if (!use_mtu || refs->win != win_multi) continue;
break;
/* WIN_TYPE_ANY */
}
}
/* Got a match? If not fuzzy, return. If fuzzy, keep looking. */
if (!fuzzy) {
if (!ref->generic) {
ts->matched = ref;
ts->fuzzy = 0;
ts->dist = refs->ttl - ts->ttl;
return;
} else if (!gmatch) gmatch = ref;
} else if (!fmatch) fmatch = ref;
}
/* OK, no definitive match so far... */
if (dupe_det) return;
/* If we found a generic signature, and nothing better, let's just use
that. */
if (gmatch) {
ts->matched = gmatch;
ts->fuzzy = 0;
ts->dist = gmatch->sig->ttl - ts->ttl;
return;
}
/* No fuzzy matching for userland tools. */
if (fmatch && fmatch->class_id == -1) return;
/* Let's try to guess distance if no match; or if match TTL out of
range. */
if (!fmatch || fmatch->sig->ttl < ts->ttl ||
(!fmatch->bad_ttl && fmatch->sig->ttl - ts->ttl > MAX_DIST))
ts->dist = guess_dist(ts->ttl);
else
ts->dist = fmatch->sig->ttl - ts->ttl;
/* Record the outcome. */
ts->matched = fmatch;
if (fmatch) ts->fuzzy = 1;
}
/* Parse TCP-specific bits and register a signature read from p0f.fp. This
function is too long. */
void tcp_register_sig(u8 to_srv, u8 generic, s32 sig_class, u32 sig_name,
u8* sig_flavor, u32 label_id, u32* sys, u32 sys_cnt,
u8* val, u32 line_no) {
s8 ver, win_type, pay_class;
u8 opt_layout[MAX_TCP_OPT];
u8 opt_cnt = 0, bad_ttl = 0;
s32 ittl, olen, mss, win, scale, opt_eol_pad = 0;
u32 quirks = 0, bucket, opt_hash;
u8* nxt;
struct tcp_sig* tsig;
struct tcp_sig_record* trec;
/* IP version */
switch (*val) {
case '4': ver = IP_VER4; break;
case '6': ver = IP_VER6; break;
case '*': ver = -1; break;
default: FATAL("Unrecognized IP version in line %u.", line_no);
}
if (val[1] != ':') FATAL("Malformed signature in line %u.", line_no);
val += 2;
/* Initial TTL (possibly ttl+dist or ttl-) */
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':' && *nxt != '+' && *nxt != '-')
FATAL("Malformed signature in line %u.", line_no);
ittl = atol((char*)val);
if (ittl < 1 || ittl > 255) FATAL("Bogus initial TTL in line %u.", line_no);
val = nxt + 1;
if (*nxt == '-' && nxt[1] == ':') {
bad_ttl = 1;
val += 2;
} else if (*nxt == '+') {
s32 ittl_add;
nxt++;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no);
ittl_add = atol((char*)val);
if (ittl_add < 0 || ittl + ittl_add > 255)
FATAL("Bogus initial TTL in line %u.", line_no);
ittl += ittl_add;
val = nxt + 1;
}
/* Length of IP options */
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no);
olen = atol((char*)val);
if (olen < 0 || olen > 255)
FATAL("Bogus IP option length in line %u.", line_no);
val = nxt + 1;
/* MSS */
if (*val == '*' && val[1] == ':') {
mss = -1;
val += 2;
} else {
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no);
mss = atol((char*)val);
if (mss < 0 || mss > 65535) FATAL("Bogus MSS in line %u.", line_no);
val = nxt + 1;
}
/* window size, followed by comma */
if (*val == '*' && val[1] == ',') {
win_type = WIN_TYPE_ANY;
win = 0;
val += 2;
} else if (*val == '%') {
win_type = WIN_TYPE_MOD;
val++;
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no);
win = atol((char*)val);
if (win < 2 || win > 65535) FATAL("Bogus '%%' value in line %u.", line_no);
val = nxt + 1;
} else if (!strncmp((char*)val, "mss*", 4) ||
!strncmp((char*)val, "mtu*", 4)) {
win_type = (val[1] == 's') ? WIN_TYPE_MSS : WIN_TYPE_MTU;
val += 4;
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no);
win = atol((char*)val);
if (win < 1 || win > 1000)
FATAL("Bogus MSS/MTU multiplier in line %u.", line_no);
val = nxt + 1;
} else {
win_type = WIN_TYPE_NORMAL;
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no);
win = atol((char*)val);
if (win < 0 || win > 65535) FATAL("Bogus window size in line %u.", line_no);
val = nxt + 1;
}
/* Window scale */
if (*val == '*' && val[1] == ':') {
scale = -1;
val += 2;
} else {
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no);
scale = atol((char*)val);
if (scale < 0 || scale > 255)
FATAL("Bogus window scale in line %u.", line_no);
val = nxt + 1;
}
/* Option layout */
memset(opt_layout, 0, sizeof(opt_layout));
while (*val != ':') {
if (opt_cnt >= MAX_TCP_OPT)
FATAL("Too many TCP options in line %u.", line_no);
if (!strncmp((char*)val, "eol", 3)) {
opt_layout[opt_cnt++] = TCPOPT_EOL;
val += 3;
if (*val != '+')
FATAL("Malformed EOL option in line %u.", line_no);
val++;
nxt = val;
while (isdigit(*nxt)) nxt++;
if (!*nxt) FATAL("Truncated options in line %u.", line_no);
if (*nxt != ':')
FATAL("EOL must be the last option in line %u.", line_no);
opt_eol_pad = atol((char*)val);
if (opt_eol_pad < 0 || opt_eol_pad > 255)
FATAL("Bogus EOL padding in line %u.", line_no);
val = nxt;
} else if (!strncmp((char*)val, "nop", 3)) {
opt_layout[opt_cnt++] = TCPOPT_NOP;
val += 3;
} else if (!strncmp((char*)val, "mss", 3)) {
opt_layout[opt_cnt++] = TCPOPT_MAXSEG;
val += 3;
} else if (!strncmp((char*)val, "ws", 2)) {
opt_layout[opt_cnt++] = TCPOPT_WSCALE;
val += 2;
} else if (!strncmp((char*)val, "sok", 3)) {
opt_layout[opt_cnt++] = TCPOPT_SACKOK;
val += 3;
} else if (!strncmp((char*)val, "sack", 4)) {
opt_layout[opt_cnt++] = TCPOPT_SACK;
val += 4;
} else if (!strncmp((char*)val, "ts", 2)) {
opt_layout[opt_cnt++] = TCPOPT_TSTAMP;
val += 2;
} else if (*val == '?') {
s32 optno;
val++;
nxt = val;
while (isdigit(*nxt)) nxt++;
if (*nxt != ':' && *nxt != ',')
FATAL("Malformed '?' option in line %u.", line_no);
optno = atol((char*)val);
if (optno < 0 || optno > 255)
FATAL("Bogus '?' option in line %u.", line_no);
opt_layout[opt_cnt++] = optno;
val = nxt;
} else {
FATAL("Unrecognized TCP option in line %u.", line_no);
}
if (*val == ':') break;
if (*val != ',')
FATAL("Malformed TCP options in line %u.", line_no);
val++;
}
val++;
opt_hash = hash32(opt_layout, opt_cnt, hash_seed);
/* Quirks */
while (*val != ':') {
if (!strncmp((char*)val, "df", 2)) {
if (ver == IP_VER6)
FATAL("'df' is not valid for IPv6 in line %d.", line_no);
quirks |= QUIRK_DF;
val += 2;
} else if (!strncmp((char*)val, "id+", 3)) {
if (ver == IP_VER6)
FATAL("'id+' is not valid for IPv6 in line %d.", line_no);
quirks |= QUIRK_NZ_ID;
val += 3;
} else if (!strncmp((char*)val, "id-", 3)) {
if (ver == IP_VER6)
FATAL("'id-' is not valid for IPv6 in line %d.", line_no);
quirks |= QUIRK_ZERO_ID;
val += 3;
} else if (!strncmp((char*)val, "ecn", 3)) {
quirks |= QUIRK_ECN;
val += 3;
} else if (!strncmp((char*)val, "0+", 2)) {
if (ver == IP_VER6)
FATAL("'0+' is not valid for IPv6 in line %d.", line_no);
quirks |= QUIRK_NZ_MBZ;
val += 2;
} else if (!strncmp((char*)val, "flow", 4)) {
if (ver == IP_VER4)
FATAL("'flow' is not valid for IPv4 in line %d.", line_no);
quirks |= QUIRK_FLOW;
val += 4;
} else if (!strncmp((char*)val, "seq-", 4)) {
quirks |= QUIRK_ZERO_SEQ;
val += 4;
} else if (!strncmp((char*)val, "ack+", 4)) {
quirks |= QUIRK_NZ_ACK;
val += 4;
} else if (!strncmp((char*)val, "ack-", 4)) {
quirks |= QUIRK_ZERO_ACK;
val += 4;
} else if (!strncmp((char*)val, "uptr+", 5)) {
quirks |= QUIRK_NZ_URG;
val += 5;
} else if (!strncmp((char*)val, "urgf+", 5)) {
quirks |= QUIRK_URG;
val += 5;
} else if (!strncmp((char*)val, "pushf+", 6)) {
quirks |= QUIRK_PUSH;
val += 6;
} else if (!strncmp((char*)val, "ts1-", 4)) {
quirks |= QUIRK_OPT_ZERO_TS1;
val += 4;
} else if (!strncmp((char*)val, "ts2+", 4)) {
quirks |= QUIRK_OPT_NZ_TS2;
val += 4;
} else if (!strncmp((char*)val, "opt+", 4)) {
quirks |= QUIRK_OPT_EOL_NZ;
val += 4;
} else if (!strncmp((char*)val, "exws", 4)) {
quirks |= QUIRK_OPT_EXWS;
val += 4;
} else if (!strncmp((char*)val, "bad", 3)) {
quirks |= QUIRK_OPT_BAD;
val += 3;
} else {
FATAL("Unrecognized quirk in line %u.", line_no);
}
if (*val == ':') break;
if (*val != ',')
FATAL("Malformed quirks in line %u.", line_no);
val++;
}
val++;
/* Payload class */
if (!strcmp((char*)val, "*")) pay_class = -1;
else if (!strcmp((char*)val, "0")) pay_class = 0;
else if (!strcmp((char*)val, "+")) pay_class = 1;
else FATAL("Malformed payload class in line %u.", line_no);
/* Phew, okay, we're done. Now, create tcp_sig... */
tsig = DFL_ck_alloc(sizeof(struct tcp_sig));
tsig->opt_hash = opt_hash;
tsig->opt_eol_pad = opt_eol_pad;
tsig->quirks = quirks;
tsig->ip_opt_len = olen;
tsig->ip_ver = ver;
tsig->ttl = ittl;
tsig->mss = mss;
tsig->win = win;
tsig->win_type = win_type;
tsig->wscale = scale;
tsig->pay_class = pay_class;
/* No need to set ts1, recv_ms, match, fuzzy, dist */
tcp_find_match(to_srv, tsig, 1, 0);
if (tsig->matched)
FATAL("Signature in line %u is already covered by line %u.",
line_no, tsig->matched->line_no);
/* Everything checks out, so let's register it. */
bucket = opt_hash % SIG_BUCKETS;
sigs[to_srv][bucket] = DFL_ck_realloc(sigs[to_srv][bucket],
(sig_cnt[to_srv][bucket] + 1) * sizeof(struct tcp_sig_record));
trec = sigs[to_srv][bucket] + sig_cnt[to_srv][bucket];
sig_cnt[to_srv][bucket]++;
trec->generic = generic;
trec->class_id = sig_class;
trec->name_id = sig_name;
trec->flavor = sig_flavor;
trec->label_id = label_id;
trec->sys = sys;
trec->sys_cnt = sys_cnt;
trec->line_no = line_no;
trec->sig = tsig;
trec->bad_ttl = bad_ttl;
/* All done, phew. */
}
/* Convert struct packet_data to a simplified struct tcp_sig representation
suitable for signature matching. Compute hashes. */
static void packet_to_sig(struct packet_data* pk, struct tcp_sig* ts) {
ts->opt_hash = hash32(pk->opt_layout, pk->opt_cnt, hash_seed);
ts->quirks = pk->quirks;
ts->opt_eol_pad = pk->opt_eol_pad;
ts->ip_opt_len = pk->ip_opt_len;
ts->ip_ver = pk->ip_ver;
ts->ttl = pk->ttl;
ts->mss = pk->mss;
ts->win = pk->win;
ts->win_type = WIN_TYPE_NORMAL; /* Keep as-is. */
ts->wscale = pk->wscale;
ts->pay_class = !!pk->pay_len;
ts->tot_hdr = pk->tot_hdr;
ts->ts1 = pk->ts1;
ts->recv_ms = get_unix_time_ms();
ts->matched = NULL;
ts->fuzzy = 0;
ts->dist = 0;
};
/* Dump unknown signature. */
static u8* dump_sig(struct packet_data* pk, struct tcp_sig* ts, u16 syn_mss) {
static u8* ret;
u32 rlen = 0;
u8 win_mtu;
s16 win_m;
u32 i;
u8 dist = guess_dist(pk->ttl);
#define RETF(_par...) do { \
s32 _len = snprintf(NULL, 0, _par); \
if (_len < 0) FATAL("Whoa, snprintf() fails?!"); \
ret = DFL_ck_realloc_kb(ret, rlen + _len + 1); \
snprintf((char*)ret + rlen, _len + 1, _par); \
rlen += _len; \
} while (0)
if (dist > MAX_DIST) {
RETF("%u:%u+?:%u:", pk->ip_ver, pk->ttl, pk->ip_opt_len);
} else {
RETF("%u:%u+%u:%u:", pk->ip_ver, pk->ttl, dist, pk->ip_opt_len);
}
/* Detect a system echoing back MSS from p0f-sendsyn queries, suggest using
a wildcard in such a case. */
if (pk->mss == SPECIAL_MSS && pk->tcp_type == (TCP_SYN|TCP_ACK)) RETF("*:");
else RETF("%u:", pk->mss);
win_m = detect_win_multi(ts, &win_mtu, syn_mss);
if (win_m > 0) RETF("%s*%u", win_mtu ? "mtu" : "mss", win_m);
else RETF("%u", pk->win);
RETF(",%u:", pk->wscale);
for (i = 0; i < pk->opt_cnt; i++) {
switch (pk->opt_layout[i]) {
case TCPOPT_EOL:
RETF("%seol+%u", i ? "," : "", pk->opt_eol_pad); break;
case TCPOPT_NOP:
RETF("%snop", i ? "," : ""); break;
case TCPOPT_MAXSEG:
RETF("%smss", i ? "," : ""); break;
case TCPOPT_WSCALE:
RETF("%sws", i ? "," : ""); break;
case TCPOPT_SACKOK:
RETF("%ssok", i ? "," : ""); break;
case TCPOPT_SACK:
RETF("%ssack", i ? "," : ""); break;
case TCPOPT_TSTAMP:
RETF("%sts", i ? "," : ""); break;
default:
RETF("%s?%u", i ? "," : "", pk->opt_layout[i]);
}
}
RETF(":");
if (pk->quirks) {
u8 sp = 0;
#define MAYBE_CM(_str) do { \
if (sp) RETF("," _str); else RETF(_str); \
sp = 1; \
} while (0)
if (pk->quirks & QUIRK_DF) MAYBE_CM("df");
if (pk->quirks & QUIRK_NZ_ID) MAYBE_CM("id+");
if (pk->quirks & QUIRK_ZERO_ID) MAYBE_CM("id-");
if (pk->quirks & QUIRK_ECN) MAYBE_CM("ecn");
if (pk->quirks & QUIRK_NZ_MBZ) MAYBE_CM("0+");
if (pk->quirks & QUIRK_FLOW) MAYBE_CM("flow");
if (pk->quirks & QUIRK_ZERO_SEQ) MAYBE_CM("seq-");
if (pk->quirks & QUIRK_NZ_ACK) MAYBE_CM("ack+");
if (pk->quirks & QUIRK_ZERO_ACK) MAYBE_CM("ack-");
if (pk->quirks & QUIRK_NZ_URG) MAYBE_CM("uptr+");
if (pk->quirks & QUIRK_URG) MAYBE_CM("urgf+");
if (pk->quirks & QUIRK_PUSH) MAYBE_CM("pushf+");
if (pk->quirks & QUIRK_OPT_ZERO_TS1) MAYBE_CM("ts1-");
if (pk->quirks & QUIRK_OPT_NZ_TS2) MAYBE_CM("ts2+");
if (pk->quirks & QUIRK_OPT_EOL_NZ) MAYBE_CM("opt+");
if (pk->quirks & QUIRK_OPT_EXWS) MAYBE_CM("exws");
if (pk->quirks & QUIRK_OPT_BAD) MAYBE_CM("bad");
#undef MAYBE_CM
}
if (pk->pay_len) RETF(":+"); else RETF(":0");
return ret;
}
/* Dump signature-related flags. */
static u8* dump_flags(struct packet_data* pk, struct tcp_sig* ts) {
static u8* ret;
u32 rlen = 0;
RETF("");
if (ts->matched) {
if (ts->matched->generic) RETF(" generic");
if (ts->fuzzy) RETF(" fuzzy");
if (ts->matched->bad_ttl) RETF(" random_ttl");
}
if (ts->dist > MAX_DIST) RETF(" excess_dist");
if (pk->tos) RETF(" tos:0x%02x", pk->tos);
if (*ret) return ret + 1; else return (u8*)"none";
#undef RETF
}
/* Compare current signature with historical data, draw conclusions. This
is called only for OS sigs. */
static void score_nat(u8 to_srv, struct tcp_sig* sig, struct packet_flow* f) {
struct host_data* hd;
struct tcp_sig* ref;
u8 score = 0, diff_already = 0;
u16 reason = 0;
s32 ttl_diff;
if (to_srv) {
hd = f->client;
ref = hd->last_syn;
} else {
hd = f->server;
ref = hd->last_synack;
}
if (!ref) {
/* No previous signature of matching type at all. We can perhaps still check
if class / name is the same as on file, as that data might have been
obtained from other types of sigs. */
if (sig->matched && hd->last_class_id != -1) {
if (hd->last_name_id != sig->matched->name_id) {
DEBUG("[#] New TCP signature different OS type than host data.\n");
reason |= NAT_OS_SIG;
score += 8;
}
}
goto log_and_update;
}
/* We have some previous data. */
if (!sig->matched || !ref->matched) {
/* One or both of the signatures are unknown. Let's see if they differ.
The scoring here isn't too strong, because we don't know if the
unrecognized signature isn't originating from userland tools. */
if ((sig->quirks ^ ref->quirks) & ~(QUIRK_ECN|QUIRK_DF|QUIRK_NZ_ID|
QUIRK_ZERO_ID)) {
DEBUG("[#] Non-fuzzy quirks changed compared to previous sig.\n");
reason |= NAT_UNK_DIFF;
score += 2;
} else if (to_srv && sig->opt_hash != ref->opt_hash) {
/* We only match option layout for SYNs; it may change on SYN+ACK,
and the user may have gaps in SYN+ACK sigs if he ignored our
advice on using p0f-sendsyn. */
DEBUG("[#] SYN option layout changed compared to previous sig.\n");
reason |= NAT_UNK_DIFF;
score += 1;
}
/* Progression from known to unknown is also of interest for SYNs. */
if (to_srv && sig->matched != ref->matched) {
DEBUG("[#] SYN signature changed from %s.\n",
sig->matched ? "unknown to known" : "known to unknown");
score += 1;
reason |= NAT_TO_UNK;
}
} else {
/* Both signatures known! */
if (ref->matched->name_id != sig->matched->name_id) {
DEBUG("[#] TCP signature different OS type on previous sig.\n");
score += 8;
reason |= NAT_OS_SIG;