Branch data Line data Source code
1 : : // $Id: OSFinger.h 5857 2008-06-26 23:00:03Z vern $
2 : :
3 : : // Taken with permission from:
4 : : //
5 : : // p0f - passive OS fingerprinting (GNU LESSER GENERAL PUBLIC LICENSE)
6 : : // -------------------------------------------------------------------
7 : : //
8 : : // "If you sit down at a poker game and don't see a sucker,
9 : : // get up. You're the sucker."
10 : : //
11 : : // (C) Copyright 2000-2003 by Michal Zalewski <lcamtuf@coredump.cx>
12 : :
13 : : #ifndef osfinger_h
14 : : #define osfinger_h
15 : :
16 : : #include "util.h"
17 : : #include "Dict.h"
18 : :
19 : :
20 : : // Size limit for size wildcards.
21 : : #define PACKET_BIG 100
22 : :
23 : : // Maximum number of signatures allowed in the config file.
24 : : #define MAXSIGS 1024
25 : :
26 : : // Max signature line length.
27 : : #define MAXLINE 1024
28 : :
29 : : // Maximum distance from a host to be taken seriously. Between 35 and 64
30 : : // is sane. Making it too high might result in some (very rare) false
31 : : // positives, too low will result in needless UNKNOWNs.
32 : : #define MAXDIST 40
33 : :
34 : : // Maximum number of TCP options. A TCP packet can have at most 64 bytes
35 : : // of header, 20 of which are non-options. Thus, if a single option
36 : : // consumes 1 bytes (the minimum, there can only be 44 bytes of options.
37 : : // We err on the safe side.
38 : : #define MAXOPT 64
39 : :
40 [ # # ][ # # ]: 0 : declare(PDict,int);
[ # # ][ # # ]
41 : :
42 : : struct os_type {
43 : : const char* os;
44 : : char* desc;
45 : : uint8 dist;
46 : : uint16 gadgets;
47 : : uint16 match;
48 : : uint32 uptime;
49 : : };
50 : :
51 : : struct fp_entry {
52 : : struct fp_entry* next;
53 : : char* os; // OS genre
54 : : char* desc; // OS description
55 : : uint8 no_detail; // disable guesstimates
56 : : uint8 generic; // generic hit
57 : : uint8 userland; // userland stack
58 : : uint16 wsize; // window size
59 : : uint8 wsize_mod; // MOD_* for wsize
60 : : uint8 ttl; // TTL
61 : : uint8 df; // don't fragment bit
62 : : uint8 zero_stamp; // timestamp option but zero value?
63 : : uint16 size; // packet size
64 : : uint8 optcnt; // option count
65 : : uint8 opt[MAXOPT]; // TCPOPT_*
66 : : uint16 wsc; // window scaling option
67 : : uint16 mss; // MSS option
68 : : uint8 wsc_mod; // modulo for WSCALE (NONE or CONST)
69 : : uint8 mss_mod; // modulo for MSS (NONE or CONST)
70 : : uint32 quirks; // packet quirks and bugs
71 : : uint32 line; // config file line
72 : : };
73 : :
74 : : struct mtu_def {
75 : : uint16 mtu;
76 : : char* dev;
77 : : };
78 : :
79 : : enum FingerprintMode {
80 : : SYN_FINGERPRINT_MODE, SYN_ACK_FINGERPRINT_MODE, RST_FINGERPRINT_MODE,
81 : : };
82 : :
83 : : class OSFingerprint {
84 : : public:
85 : : OSFingerprint(FingerprintMode mode);
86 : 0 : ~OSFingerprint() {}
87 : :
88 : 0 : bool Error() const { return err; }
89 : :
90 : : int FindMatch(struct os_type* retval, uint16 tot, uint8 DF_flag,
91 : : uint8 TTL, uint16 WSS, uint8 ocnt, uint8* op, uint16 MSS,
92 : : uint8 win_scale, uint32 tstamp, uint32 quirks, uint8 ECN) const;
93 : : bool CacheMatch(uint32 addr, int id);
94 : :
95 : : int Get_OS_From_SYN(struct os_type* retval,
96 : : uint16 tot, uint8 DF_flag, uint8 TTL, uint16 WSS,
97 : : uint8 ocnt, uint8* op, uint16 MSS, uint8 win_scale,
98 : : uint32 tstamp, /* uint8 TOS, */ uint32 quirks,
99 : : uint8 ecn) const;
100 : :
101 : : void load_config(const char* file);
102 : :
103 : : protected:
104 : : void collide(uint32 id);
105 : :
106 : 0 : void Error(const char* msg)
107 : : {
108 : 0 : error(msg);
109 : 0 : err = true;
110 : 0 : }
111 : :
112 : 0 : void Error(const char* msg, int n)
113 : : {
114 : 0 : error(msg, n);
115 : 0 : err = true;
116 : 0 : }
117 : :
118 : 0 : void Error(const char* msg, const char* s)
119 : : {
120 : 0 : error(msg, s);
121 : 0 : err = true;
122 : 0 : }
123 : :
124 : : private:
125 : : bool err; // if true, a fatal error has occurred
126 : : unsigned int mode;
127 : : uint32 sigcnt, gencnt;
128 : : uint8 problems;
129 : : struct fp_entry sig[MAXSIGS];
130 : :
131 : : /* By hash */
132 : : #define OSHSIZE 16
133 : : struct fp_entry* bh[OSHSIZE];
134 : :
135 : : PDict(int) os_matches;
136 : : };
137 : :
138 : : #define SIGHASH(tsize, optcnt, q, df) \
139 : : ((uint8(((tsize) << 1) ^ ((optcnt) << 1) ^ (df) ^ (q) )) & 0x0f)
140 : :
141 : : #define MOD_NONE 0
142 : : #define MOD_CONST 1
143 : : #define MOD_MSS 2
144 : : #define MOD_MTU 3
145 : :
146 : : #define QUIRK_PAST 0x1 /* P */
147 : : #define QUIRK_ZEROID 0x2 /* Z */
148 : : #define QUIRK_IPOPT 0x4 /* I */
149 : : #define QUIRK_URG 0x8 /* U */
150 : : #define QUIRK_X2 0x10 /* X */
151 : : #define QUIRK_ACK 0x20 /* A */
152 : : #define QUIRK_T2 0x40 /* T */
153 : : #define QUIRK_FLAGS 0x80 /* F */
154 : : #define QUIRK_DATA 0x100 /* D */
155 : : #define QUIRK_BROKEN 0x200 /* ! */
156 : : #define QUIRK_RSTACK 0x400 /* K */
157 : : #define QUIRK_SEQEQ 0x800 /* Q */
158 : : #define QUIRK_SEQ0 0x1000 /* 0 */
159 : :
160 : : #define GADGETNAT 0x1
161 : : #define GADGETNAT2 0x2
162 : : #define GADGETFIREWALL 0x4
163 : : #define GADGETECN 0x8
164 : : #define GADGETUPTIME 0x10
165 : :
166 : : #define MATCHGENERIC 0x1
167 : : #define MATCHFUZZY 0x2
168 : :
169 : : #endif
|