This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
p0f.c
1883 lines (1388 loc) · 47.3 KB
/
p0f.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 - passive OS fingerprinting
-------------------------------
"If you sit down at a poker game and don't see a sucker,
get up. You're the sucker."
(C) Copyright 2000-2006 by Michal Zalewski <lcamtuf@coredump.cx>
WIN32 port (C) Copyright 2003-2004 by Michael A. Davis <mike@datanerds.net>
(C) Copyright 2003-2004 by Kirby Kuehl <kkuehl@cisco.com>
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifndef WIN32
# include <netinet/in.h>
# include <arpa/inet.h>
# include <unistd.h>
# include <netdb.h>
# include <sys/socket.h>
# include <sys/un.h>
# include <pwd.h>
# include <grp.h>
#else
# include "getopt.h"
# include <stdarg.h>
# pragma comment (lib, "wpcap.lib")
#endif /* ^WIN32 */
#include <stdio.h>
#include <pcap.h>
#include <signal.h>
#ifdef USE_BPF
#include USE_BPF
#else
#include <pcap-bpf.h>
#endif /* ^USE_BPF */
#include <time.h>
#include <ctype.h>
/* #define DEBUG_HASH - display signature hash table stats */
#include "config.h"
#include "types.h"
#include "tcp.h"
#include "mtu.h"
#include "tos.h"
#include "fpentry.h"
#include "p0f-query.h"
#include "crc32.h"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif /* ! MSG_NOSIGNAL */
static pcap_dumper_t *dumper;
#ifdef WIN32
static inline void debug(_u8* format, ...) {
_u8 buff[1024];
va_list args;
va_start(args, format);
memset(buff, 0, sizeof(buff));
_vsnprintf( buff, sizeof(buff) - 1, format, args);
fprintf(stderr, buff);
va_end(args);
}
static inline void fatal(_u8* format, ...) {
_u8 buff[1024];
va_list args;
va_start(args, format);
memset(buff, 0, sizeof(buff));
vsnprintf( buff, sizeof(buff) - 1, format, args);
fprintf(stderr, "[-] ERROR: %s", buff);
va_end(args);
exit(1);
}
#else
# define debug(x...) fprintf(stderr,x)
# define fatal(x...) do { debug("[-] ERROR: " x); exit(1); } while (0)
#endif /* ^WIN32 */
#define pfatal(x) do { debug("[-] ERROR: "); perror(x); exit(1); } while (0)
static struct fp_entry sig[MAXSIGS];
static _u32 sigcnt,gencnt;
/* By hash */
static struct fp_entry* bh[16];
#define SIGHASH(tsize,optcnt,q,df) \
(( (_u8) (((tsize) << 1) ^ ((optcnt) << 1) ^ (df) ^ (q) )) & 0x0f)
static _u8 *config_file,
*use_iface,
*use_dump,
*write_dump,
*use_cache,
#ifndef WIN32
*set_user,
#endif /* !WIN32 */
*use_rule = "tcp[13] & 0x17 == 2";
static _u32 query_cache = DEFAULT_QUERY_CACHE;
static _s32 masq_thres;
static _s32 capture_timeout = 1;
static _u8 no_extra,
find_masq,
masq_flags,
no_osdesc,
no_known,
no_unknown,
no_banner,
use_promisc,
add_timestamp,
header_len,
ack_mode,
rst_mode,
open_mode,
go_daemon,
use_logfile,
mode_oneline,
always_sig,
do_resolve,
check_collide,
full_dump,
use_fuzzy,
use_vlan,
payload_dump,
port0_wild;
static pcap_t *pt;
static struct bpf_program flt;
/* Exports for p0f statistics */
_u32 packet_count;
_u8 operating_mode;
_u32 st_time;
_u32 file_cksum;
static void die_nicely(_s32 sig) {
if (sig) debug("+++ Exiting on signal %d +++\n",sig);
if (pt) pcap_close(pt);
if (dumper) pcap_dump_close(dumper);
if (!no_banner && packet_count) {
float r = packet_count * 60;
r /= (time(0) - st_time);
debug("[+] Average packet ratio: %0.2f per minute",r);
if (use_cache || find_masq)
debug(" (cache: %0.2f seconds).\n",query_cache * 60 / r);
else
debug(".\n");
}
exit(sig);
}
static void set_header_len(_u32 type) {
switch(type) {
case DLT_SLIP:
case DLT_RAW: break;
#ifdef DLT_C_HDLC
case DLT_C_HDLC:
#endif
case DLT_NULL: header_len=4; break;
case DLT_EN10MB: header_len=14; break;
#ifdef DLT_LOOP
case DLT_LOOP:
#endif
#ifdef DLT_PPP_SERIAL
case DLT_PPP_SERIAL: /* NetBSD oddity */
#endif
#ifdef DLT_PPP_ETHER
case DLT_PPP_ETHER: /* PPPoE on NetBSD */
header_len=8;
break;
#endif
case DLT_PPP: header_len=4; break;
case DLT_IEEE802:
header_len=22;
break;
#ifdef DLT_IEEE802_11
case DLT_IEEE802_11: header_len=32; break;
#endif
#ifdef DLT_PFLOG
case DLT_PFLOG:
header_len=28;
break;
#endif
#ifdef DLT_LINUX_SLL
case DLT_LINUX_SLL:
header_len=16;
break;
#endif
default:
debug("[!] WARNING: Unknown datalink type %d, assuming no header.\n",type);
break;
}
}
static void usage(_u8* name) {
fprintf(stderr,
"\nUsage: %s [ -f file ] [ -i device ] [ -s file ] [ -o file ]\n"
#ifndef WIN32
" [ -w file ] [ -Q sock [ -0 ] ] [ -u user ] [ -FXVNDUKASCMROqtpvdlrx ]\n"
" [ -c size ] [ -T nn ] [ -e nn ] [ 'filter rule' ]\n"
#else
" [ -w file ] [ -FXVNDUKASCMLROqtpvdlrx ]\n"
" [ -c size] [ -T nn ] [ -e nn ] [ 'filter rule' ]\n"
#endif /* ^WIN32 */
" -f file - read fingerprints from file\n"
" -i device - listen on this device\n"
" -s file - read packets from tcpdump snapshot\n"
" -o file - write to this logfile (implies -t)\n"
" -w file - save packets to tcpdump snapshot\n"
#ifndef WIN32
" -u user - chroot and setuid to this user\n"
" -Q sock - listen on local socket for queries\n"
" -0 - make src port 0 a wildcard (in query mode)\n"
#endif /* !WIN32 */
" -e ms - pcap capture timeout in milliseconds (default: 1)\n"
" -c size - cache size for -Q and -M options\n"
" -M - run masquerade detection\n"
" -T nn - set masquerade detection threshold (1-200)\n"
" -V - verbose masquerade flags reporting\n"
" -F - use fuzzy matching (do not combine with -R)\n"
" -N - do not report distances and link media\n"
" -D - do not report OS details (just genre)\n"
" -U - do not display unknown signatures\n"
" -K - do not display known signatures (for tests)\n"
" -S - report signatures even for known systems\n"
" -A - go into SYN+ACK mode (semi-supported)\n"
" -R - go into RST/RST+ACK mode (semi-supported)\n"
" -O - go into stray ACK mode (barely supported)\n"
" -r - resolve host names (not recommended)\n"
" -q - be quiet - no banner\n"
" -v - enable support for 802.1Q VLAN frames\n"
" -p - switch card to promiscuous mode\n"
" -d - daemon mode (fork into background)\n"
" -l - use single-line output (easier to grep)\n"
" -x - include full packet dump (for debugging)\n"
" -X - display payload string (useful in RST mode)\n"
" -C - run signature collision check\n"
#ifdef WIN32
" -L - list all available interfaces\n"
#endif /* ^WIN32 */
" -t - add timestamps to every entry\n\n"
" 'Filter rule' is an optional pcap-style BPF expression (man tcpdump).\n\n",name);
exit(1);
}
static _u8 problems;
static void collide(_u32 id) {
_u32 i,j;
_u32 cur;
if (sig[id].ttl % 32 && sig[id].ttl != 255 && sig[id].ttl % 30) {
problems=1;
debug("[!] Unusual TTL (%d) for signature '%s %s' (line %d).\n",
sig[id].ttl,sig[id].os,sig[id].desc,sig[id].line);
}
for (i=0;i<id;i++) {
if (!strcmp(sig[i].os,sig[id].os) &&
!strcmp(sig[i].desc,sig[id].desc)) {
problems=1;
debug("[!] Duplicate signature name: '%s %s' (line %d and %d).\n",
sig[i].os,sig[i].desc,sig[i].line,sig[id].line);
}
/* If TTLs are sufficiently away from each other, the risk of
a collision is lower. */
if (abs((_s32)sig[id].ttl - (_s32)sig[i].ttl) > 25) continue;
if (sig[id].df ^ sig[i].df) continue;
if (sig[id].zero_stamp ^ sig[i].zero_stamp) continue;
/* Zero means >= PACKET_BIG */
if (sig[id].size) { if (sig[id].size ^ sig[i].size) continue; }
else if (sig[i].size < PACKET_BIG) continue;
if (sig[id].optcnt ^ sig[i].optcnt) continue;
if (sig[id].quirks ^ sig[i].quirks) continue;
switch (sig[id].wsize_mod) {
case 0: /* Current: const */
cur=sig[id].wsize;
do_const:
switch (sig[i].wsize_mod) {
case 0: /* Previous is also const */
/* A problem if values match */
if (cur ^ sig[i].wsize) continue;
break;
case MOD_CONST: /* Current: const, prev: modulo (or *) */
/* A problem if current value is a multiple of that modulo */
if (cur % sig[i].wsize) continue;
break;
case MOD_MSS: /* Current: const, prev: mod MSS */
if (sig[i].mss_mod || sig[i].wsize *
(sig[i].mss ? sig[i].mss : 1460 ) != cur)
continue;
break;
case MOD_MTU: /* Current: const, prev: mod MTU */
if (sig[i].mss_mod || sig[i].wsize * (
(sig[i].mss ? sig[i].mss : 1460 )+40) != cur)
continue;
break;
}
break;
case 1: /* Current signature is modulo something */
/* A problem only if this modulo is a multiple of the
previous modulo */
if (sig[i].wsize_mod != MOD_CONST) continue;
if (sig[id].wsize % sig[i].wsize) continue;
break;
case MOD_MSS: /* Current is modulo MSS */
/* There's likely a problem only if the previous one is close
to '*'; we do not check known MTUs, because this particular
signature can be made with some uncommon MTUs in mind. The
problem would also appear if current signature has a fixed
MSS. */
if (sig[i].wsize_mod != MOD_CONST || sig[i].wsize >= 8) {
if (!sig[id].mss_mod) {
cur = (sig[id].mss ? sig[id].mss : 1460 ) * sig[id].wsize;
goto do_const;
}
continue;
}
break;
case MOD_MTU: /* Current is modulo MTU */
if (sig[i].wsize_mod != MOD_CONST || sig[i].wsize <= 8) {
if (!sig[id].mss_mod) {
cur = ( (sig[id].mss ? sig[id].mss : 1460 ) +40) * sig[id].wsize;
goto do_const;
}
continue;
}
break;
}
/* Same for wsc */
switch (sig[id].wsc_mod) {
case 0: /* Current: const */
cur=sig[id].wsc;
switch (sig[i].wsc_mod) {
case 0: /* Previous is also const */
/* A problem if values match */
if (cur ^ sig[i].wsc) continue;
break;
case 1: /* Current: const, prev: modulo (or *) */
/* A problem if current value is a multiple of that modulo */
if (cur % sig[i].wsc) continue;
break;
}
break;
case MOD_CONST: /* Current signature is modulo something */
/* A problem only if this modulo is a multiple of the
previous modulo */
if (!sig[i].wsc_mod) continue;
if (sig[id].wsc % sig[i].wsc) continue;
break;
}
/* Same for mss */
switch (sig[id].mss_mod) {
case 0: /* Current: const */
cur=sig[id].mss;
switch (sig[i].mss_mod) {
case 0: /* Previous is also const */
/* A problem if values match */
if (cur ^ sig[i].mss) continue;
break;
case 1: /* Current: const, prev: modulo (or *) */
/* A problem if current value is a multiple of that modulo */
if (cur % sig[i].mss) continue;
break;
}
break;
case MOD_CONST: /* Current signature is modulo something */
/* A problem only if this modulo is a multiple of the
previous modulo */
if (!sig[i].mss_mod) continue;
if ((sig[id].mss ? sig[id].mss : 1460 ) %
(sig[i].mss ? sig[i].mss : 1460 )) continue;
break;
}
/* Now check option sequence */
for (j=0;j<sig[id].optcnt;j++)
if (sig[id].opt[j] ^ sig[i].opt[j]) goto reloop;
problems=1;
debug("[!] Signature '%s %s' (line %d)\n"
" is already covered by '%s %s' (line %d).\n",
sig[id].os,sig[id].desc,sig[id].line,sig[i].os,sig[i].desc,
sig[i].line);
reloop:
;
}
}
static void load_config(_u8* file) {
_u32 ln=0;
_u8 buf[MAXLINE];
_u8* p;
FILE* c = fopen(file?file:(_u8*)
(ack_mode?SYNACK_DB:(rst_mode?RST_DB:(open_mode?OPEN_DB:SYN_DB))),
"r");
if (!c) {
if (!file) load_config(ack_mode? CONFIG_DIR "/" SYNACK_DB :
( rst_mode ? CONFIG_DIR "/" RST_DB :
( open_mode ? CONFIG_DIR "/" OPEN_DB :
CONFIG_DIR "/" SYN_DB )));
else pfatal(file);
return;
}
while ((p=fgets(buf,sizeof(buf),c))) {
_u32 l;
_u8 obuf[MAXLINE],genre[MAXLINE],desc[MAXLINE],quirks[MAXLINE];
_u8 w[MAXLINE],sb[MAXLINE];
_u8* gptr = genre;
_u32 t,d,s;
struct fp_entry* e;
file_cksum ^= crc32(buf, strlen(buf));
ln++;
/* Remove leading and trailing blanks */
while (isspace(*p)) p++;
l=strlen(p);
while (l && isspace(*(p+l-1))) *(p+(l--)-1)=0;
/* Skip empty lines and comments */
if (!l) continue;
if (*p == '#') continue;
if (sscanf(p,"%[0-9%*()ST]:%d:%d:%[0-9()*]:%[^:]:%[^ :]:%[^:]:%[^:]",
w, &t,&d,sb, obuf, quirks,genre,desc) != 8)
fatal("Syntax error in config line %d.\n",ln);
gptr = genre;
if (*sb != '*') {
if (open_mode)
fatal("Packet size must be '*' in -O mode (line %d).\n",ln);
s = atoi(sb);
} else s = 0;
reparse_ptr:
switch (*gptr) {
case '-': sig[sigcnt].userland = 1; gptr++; goto reparse_ptr;
case '*': sig[sigcnt].no_detail = 1; gptr++; goto reparse_ptr;
case '@': sig[sigcnt].generic = 1; gptr++; gencnt++; goto reparse_ptr;
case 0: fatal("Empty OS genre in line %d.\n",ln);
}
sig[sigcnt].os = strdup(gptr);
sig[sigcnt].desc = strdup(desc);
sig[sigcnt].ttl = t;
sig[sigcnt].size = s;
sig[sigcnt].df = d;
if (w[0] == '*') {
sig[sigcnt].wsize = 1;
sig[sigcnt].wsize_mod = MOD_CONST;
} else if (tolower(w[0]) == 's') {
sig[sigcnt].wsize_mod = MOD_MSS;
if (!isdigit(*(w+1))) fatal("Bad Snn value in WSS in line %d.\n",ln);
sig[sigcnt].wsize = atoi(w+1);
} else if (tolower(w[0]) == 't') {
sig[sigcnt].wsize_mod = MOD_MTU;
if (!isdigit(*(w+1))) fatal("Bad Tnn value in WSS in line %d.\n",ln);
sig[sigcnt].wsize = atoi(w+1);
} else if (w[0] == '%') {
if (!(sig[sigcnt].wsize = atoi(w+1)))
fatal("Null modulo for window size in config line %d.\n",ln);
sig[sigcnt].wsize_mod = MOD_CONST;
} else sig[sigcnt].wsize = atoi(w);
/* Now let's parse options */
p=obuf;
sig[sigcnt].zero_stamp = 1;
if (*p=='.') p++;
while (*p) {
_u8 optcnt = sig[sigcnt].optcnt;
switch (tolower(*p)) {
case 'n': sig[sigcnt].opt[optcnt] = TCPOPT_NOP;
break;
case 'e': sig[sigcnt].opt[optcnt] = TCPOPT_EOL;
if (*(p+1))
fatal("EOL not the last option (line %d).\n",ln);
break;
case 's': sig[sigcnt].opt[optcnt] = TCPOPT_SACKOK;
break;
case 't': sig[sigcnt].opt[optcnt] = TCPOPT_TIMESTAMP;
if (*(p+1)!='0') {
sig[sigcnt].zero_stamp=0;
if (isdigit(*(p+1)))
fatal("Bogus Tstamp specification in line %d.\n",ln);
}
break;
case 'w': sig[sigcnt].opt[optcnt] = TCPOPT_WSCALE;
if (p[1] == '*') {
sig[sigcnt].wsc = 1;
sig[sigcnt].wsc_mod = MOD_CONST;
} else if (p[1] == '%') {
if (!(sig[sigcnt].wsc = atoi(p+2)))
fatal("Null modulo for wscale in config line %d.\n",ln);
sig[sigcnt].wsc_mod = MOD_CONST;
} else if (!isdigit(*(p+1)))
fatal("Incorrect W value in line %d.\n",ln);
else sig[sigcnt].wsc = atoi(p+1);
break;
case 'm': sig[sigcnt].opt[optcnt] = TCPOPT_MAXSEG;
if (p[1] == '*') {
sig[sigcnt].mss = 1;
sig[sigcnt].mss_mod = MOD_CONST;
} else if (p[1] == '%') {
if (!(sig[sigcnt].mss = atoi(p+2)))
fatal("Null modulo for MSS in config line %d.\n",ln);
sig[sigcnt].mss_mod = MOD_CONST;
} else if (!isdigit(*(p+1)))
fatal("Incorrect M value in line %d.\n",ln);
else sig[sigcnt].mss = atoi(p+1);
break;
/* Yuck! */
case '?': if (!isdigit(*(p+1)))
fatal("Bogus ?nn value in line %d.\n",ln);
else sig[sigcnt].opt[optcnt] = atoi(p+1);
break;
default: fatal("Unknown TCP option '%c' in config line %d.\n",*p,ln);
}
if (++sig[sigcnt].optcnt >= MAXOPT)
fatal("Too many TCP options specified in config line %d.\n",ln);
/* Skip separators */
do { p++; } while (*p && !isalpha(*p) && *p != '?');
}
sig[sigcnt].line = ln;
p = quirks;
while (*p)
switch (toupper(*(p++))) {
case 'E':
fatal("Quirk 'E' (line %d) is obsolete. Remove it, append E to the "
"options.\n",ln);
case 'K':
if (!rst_mode) fatal("Quirk 'K' (line %d) is valid only in RST+ (-R)"
" mode (wrong config file?).\n",ln);
sig[sigcnt].quirks |= QUIRK_RSTACK;
break;
case 'D':
if (open_mode) fatal("Quirk 'D' (line %d) is not valid in OPEN (-O) "
"mode (wrong config file?).\n",ln);
sig[sigcnt].quirks |= QUIRK_DATA;
break;
case 'Q': sig[sigcnt].quirks |= QUIRK_SEQEQ; break;
case '0': sig[sigcnt].quirks |= QUIRK_SEQ0; break;
case 'P': sig[sigcnt].quirks |= QUIRK_PAST; break;
case 'Z': sig[sigcnt].quirks |= QUIRK_ZEROID; break;
case 'I': sig[sigcnt].quirks |= QUIRK_IPOPT; break;
case 'U': sig[sigcnt].quirks |= QUIRK_URG; break;
case 'X': sig[sigcnt].quirks |= QUIRK_X2; break;
case 'A': sig[sigcnt].quirks |= QUIRK_ACK; break;
case 'T': sig[sigcnt].quirks |= QUIRK_T2; break;
case 'F': sig[sigcnt].quirks |= QUIRK_FLAGS; break;
case '!': sig[sigcnt].quirks |= QUIRK_BROKEN; break;
case '.': break;
default: fatal("Bad quirk '%c' in line %d.\n",*(p-1),ln);
}
e = bh[SIGHASH(s,sig[sigcnt].optcnt,sig[sigcnt].quirks,d)];
if (!e) {
bh[SIGHASH(s,sig[sigcnt].optcnt,sig[sigcnt].quirks,d)] = sig + sigcnt;
} else {
while (e->next) e = e->next;
e->next = sig + sigcnt;
}
if (check_collide) collide(sigcnt);
if (++sigcnt >= MAXSIGS)
fatal("Maximum signature count exceeded.\n");
}
fclose(c);
#ifdef DEBUG_HASH
{
int i;
struct fp_entry* p;
printf("Hash table layout: ");
for (i=0;i<16;i++) {
int z=0;
p = bh[i];
while (p) { p=p->next; z++; }
printf("%d ",z);
}
putchar('\n');
}
#endif /* DEBUG_HASH */
if (check_collide && !problems)
debug("[+] Signature collision check successful.\n");
if (!sigcnt)
debug("[!] WARNING: no signatures loaded from config file.\n");
}
static _u8* lookup_link(_u16 mss,_u8 txt) {
_u32 i;
static _u8 tmp[32];
if (!mss) return txt ? "unspecified" : 0;
mss += 40;
for (i=0;i<MTU_CNT;i++) {
if (mss == mtu[i].mtu) return mtu[i].dev;
if (mss < mtu[i].mtu) goto unknown;
}
unknown:
if (!txt) return 0;
sprintf(tmp,"unknown-%d",mss);
return tmp;
}
static _u8* lookup_tos(_u8 t) {
_u32 i;
if (!t) return 0;
for (i=0;i<TOS_CNT;i++) {
if (t == tos[i].tos) return tos[i].desc;
if (t < tos[i].tos) break;
}
return 0;
}
static void put_date(struct timeval tval) {
_u8* x;
struct tm *tmval;
switch (add_timestamp) {
case 1: /* localtime */
case 2: /* UTC */
x = asctime((add_timestamp == 1) ? localtime(&tval.tv_sec) :
gmtime(&tval.tv_sec));
if (x[strlen(x)-1]=='\n') x[strlen(x)-1]=0;
printf("<%s> ",x);
break;
case 3: /* seconds since the epoch */
printf("<%u.%06u> ", (_u32)tval.tv_sec, (_u32)tval.tv_usec);
break;
case 4: /* RFC3339 */
default:
tmval = gmtime(&tval.tv_sec);
printf("<%04u-%02u-%02uT%02u:%02u:%02u.%06uZ> ",
tmval->tm_year + 1900, tmval->tm_mon + 1, tmval->tm_mday,
tmval->tm_hour, tmval->tm_min, tmval->tm_sec,
(_u32)tval.tv_usec);
break;
}
}
#define MY_MAXDNS 32
static inline _u8* grab_name(_u8* a) {
struct hostent* r;
static _u8 rbuf[MY_MAXDNS+6] = "/";
_u32 j;
_u8 *s,*d = rbuf+1;
if (!do_resolve) return "";
r = gethostbyaddr(a,4,AF_INET);
if (!r || !(s = r->h_name) || !(j = strlen(s))) return "";
if (j > MY_MAXDNS) return "";
while (j--) {
if (isalnum(*s) || *s == '-' || *s == '.') *d = *s;
else *d = '?';
d++; s++;
}
*d=0;
return rbuf;
}
static inline void display_signature(_u8 ttl,_u16 tot,_u8 df,_u8* op,_u8 ocnt,
_u16 mss,_u16 wss,_u8 wsc,_u32 tstamp,
_u32 quirks) {
_u32 j;
_u8 d=0;
if (mss && wss && !(wss % mss)) printf("S%d",wss/mss); else
if (wss && !(wss % 1460)) printf("S%d",wss/1460); else
if (mss && wss && !(wss % (mss+40))) printf("T%d",wss/(mss+40)); else
if (wss && !(wss % 1500)) printf("T%d",wss/1500); else
if (wss == 12345) printf("*(12345)"); else printf("%d",wss);
if (!open_mode) {
if (tot < PACKET_BIG) printf(":%d:%d:%d:",ttl,df,tot);
else printf(":%d:%d:*(%d):",ttl,df,tot);
} else printf(":%d:%d:*:",ttl,df);
for (j=0;j<ocnt;j++) {
switch (op[j]) {
case TCPOPT_NOP: putchar('N'); d=1; break;
case TCPOPT_WSCALE: printf("W%d",wsc); d=1; break;
case TCPOPT_MAXSEG: printf("M%d",mss); d=1; break;
case TCPOPT_TIMESTAMP: putchar('T');
if (!tstamp) putchar('0'); d=1; break;
case TCPOPT_SACKOK: putchar('S'); d=1; break;
case TCPOPT_EOL: putchar('E'); d=1; break;
default: printf("?%d",op[j]); d=1; break;
}
if (j != ocnt-1) putchar(',');
}
if (!d) putchar('.');
putchar(':');
if (!quirks) putchar('.'); else {
if (quirks & QUIRK_RSTACK) putchar('K');
if (quirks & QUIRK_SEQEQ) putchar('Q');
if (quirks & QUIRK_SEQ0) putchar('0');
if (quirks & QUIRK_PAST) putchar('P');
if (quirks & QUIRK_ZEROID) putchar('Z');
if (quirks & QUIRK_IPOPT) putchar('I');
if (quirks & QUIRK_URG) putchar('U');
if (quirks & QUIRK_X2) putchar('X');
if (quirks & QUIRK_ACK) putchar('A');
if (quirks & QUIRK_T2) putchar('T');
if (quirks & QUIRK_FLAGS) putchar('F');
if (quirks & QUIRK_DATA) putchar('D');
if (quirks & QUIRK_BROKEN) putchar('!');
}
}
static void dump_packet(_u8* pkt,_u16 plen) {
_u32 i;
_u8 tbuf[PKT_DLEN+1];
_u8* t = tbuf;
for (i=0;i<plen;i++) {
_u8 c = *(pkt++);
if (!(i % PKT_DLEN)) printf(" [%02x] ",i);
printf("%02x ",c);
*(t++) = isprint(c) ? c : '.';
if (!((i+1) % PKT_DLEN)) {
*t=0;
printf(" | %s\n",(t=tbuf));
}
}
if (plen % PKT_DLEN) {
*t=0;
while (plen++ % PKT_DLEN) printf(" ");
printf(" | %s\n",tbuf);
}
}
static void dump_payload(_u8* data,_u16 dlen) {
_u8 tbuf[PKT_MAXPAY+2];
_u8* t = tbuf;
_u8 i;
_u8 max = dlen > PKT_MAXPAY ? PKT_MAXPAY : dlen;
if (!dlen) return;
for (i=0;i<max;i++) {
if (isprint(*data)) *(t++) = *data;
else if (!*data) *(t++) = '?';
else *(t++) = '.';
data++;
}
*t = 0;
if (!mode_oneline) putchar('\n');
printf(" # Payload: \"%s\"%s",tbuf,dlen > PKT_MAXPAY ? "..." : "");
}
_u32 matched_packets;
static inline void find_match(_u16 tot,_u8 df,_u8 ttl,_u16 wss,_u32 src,
_u32 dst,_u16 sp,_u16 dp,_u8 ocnt,_u8* op,_u16 mss,
_u8 wsc,_u32 tstamp,_u8 tos,_u32 quirks,_u8 ecn,
_u8* pkt,_u8 plen,_u8* pay, struct timeval pts) {
_u32 j;
_u8* a;
_u8 nat=0;
struct fp_entry* p;
_u8 orig_df = df;
_u8* tos_desc = 0;
struct fp_entry* fuzzy = 0;
_u8 fuzzy_now = 0;
re_lookup:
p = bh[SIGHASH(tot,ocnt,quirks,df)];
if (tos) tos_desc = lookup_tos(tos);
while (p) {
/* Cheap and specific checks first... */
/* psize set to zero means >= PACKET_BIG */
if (!open_mode) {
if (p->size) { if (tot ^ p->size) { p = p->next; continue; } }
else if (tot < PACKET_BIG) { p = p->next; continue; }
}
if (ocnt ^ p->optcnt) { p = p->next; continue; }
if (p->zero_stamp ^ (!tstamp)) { p = p->next; continue; }
if (p->df ^ df) { p = p->next; continue; }
if (p->quirks ^ quirks) { p = p->next; continue; }
/* Check MSS and WSCALE... */
if (!p->mss_mod) {
if (mss ^ p->mss) { p = p->next; continue; }
} else if (mss % p->mss) { p = p->next; continue; }
if (!p->wsc_mod) {
if (wsc ^ p->wsc) { p = p->next; continue; }
} else if (wsc % p->wsc) { p = p->next; continue; }
/* Then proceed with the most complex WSS check... */
switch (p->wsize_mod) {
case 0:
if (wss ^ p->wsize) { p = p->next; continue; }
break;