Branch data Line data Source code
1 : : // $Id: NetbiosSSN.h 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef netbios_ssn_h
6 : : #define netbios_ssn_h
7 : :
8 : : #include "UDP.h"
9 : : #include "TCP.h"
10 : : #include "SMB.h"
11 : :
12 : : typedef enum {
13 : : NETBIOS_SSN_MSG = 0x0,
14 : : NETBIOS_DGM_DIRECT_UNIQUE = 0x10,
15 : : NETBIOS_DGM_DIRECT_GROUP = 0x11,
16 : : NETBIOS_DGM_BROADCAST = 0x12,
17 : : NETBIOS_DGM_ERROR = 0x13,
18 : : NETBIOS_DGG_QUERY_REQ = 0x14,
19 : : NETBIOS_DGM_POS_RESP = 0x15,
20 : : NETBIOS_DGM_NEG_RESP = 0x16,
21 : : NETBIOS_SSN_REQ = 0x81,
22 : : NETBIOS_SSN_POS_RESP = 0x82,
23 : : NETBIOS_SSN_NEG_RESP = 0x83,
24 : : NETBIOS_SSN_RETARG_RESP = 0x84,
25 : : NETBIOS_SSN_KEEP_ALIVE = 0x85,
26 : : } NetbiosSSN_Opcode;
27 : :
28 : : // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 : : // | TYPE | FLAGS | LENGTH |
31 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 : :
33 : : struct NetbiosSSN_RawMsgHdr {
34 : : NetbiosSSN_RawMsgHdr(const u_char*& data, int& len);
35 : :
36 : : unsigned int type:8;
37 : : unsigned int flags:8;
38 : : unsigned int length:16;
39 : : };
40 : :
41 : : // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
42 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 : : // | MSG_TYPE | FLAGS | DGM_ID |
44 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 : : // | SOURCE_IP |
46 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 : : // | SOURCE_PORT | DGM_LENGTH |
48 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 : : // | PACKET_OFFSET | |
50 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
51 : :
52 : : struct NetbiosDGM_RawMsgHdr {
53 : : NetbiosDGM_RawMsgHdr(const u_char*& data, int& len);
54 : :
55 : : unsigned int type:8;
56 : : unsigned int flags:8;
57 : : unsigned int id:16;
58 : : unsigned int srcip:32;
59 : : unsigned int srcport:16;
60 : : unsigned int length:16;
61 : : unsigned int offset:16;
62 : : };
63 : :
64 : :
65 : : class NetbiosSSN_Interpreter {
66 : : public:
67 : : NetbiosSSN_Interpreter(Analyzer* analyzer, SMB_Session* smb_session);
68 : :
69 : : int ParseMessage(unsigned int type, unsigned int flags,
70 : : const u_char* data, int len, int is_query);
71 : :
72 : : // Version used when data points to type/flags/length.
73 : : int ParseMessageTCP(const u_char* data, int len, int is_query);
74 : : int ParseMessageUDP(const u_char* data, int len, int is_query);
75 : :
76 : 0 : void Timeout() { }
77 : :
78 : 1 : static bool any_netbios_ssn_event()
79 : : {
80 : : return netbios_session_message ||
81 : : netbios_session_request ||
82 : : netbios_session_accepted ||
83 : : netbios_session_rejected ||
84 : : netbios_session_raw_message ||
85 : : netbios_session_ret_arg_resp ||
86 [ + - ][ + - ]: 1 : netbios_session_keepalive;
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
87 : : }
88 : :
89 : : protected:
90 : : int ParseSessionMsg(const u_char* data, int len, int is_query);
91 : : int ParseSessionReq(const u_char* data, int len, int is_query);
92 : : int ParseSessionPosResp(const u_char* data, int len, int is_query);
93 : : int ParseSessionNegResp(const u_char* data, int len, int is_query);
94 : : int ParseRetArgResp(const u_char* data, int len, int is_query);
95 : : int ParseKeepAlive(const u_char* data, int len, int is_query);
96 : :
97 : : // Datagram parsing
98 : : int ParseBroadcast(const u_char* data, int len, int is_query);
99 : : int ParseDatagram(const u_char* data, int len, int is_query);
100 : :
101 : : int ParseSambaMsg(const u_char* data, int len, int is_query);
102 : :
103 : : void Event(EventHandlerPtr event, const u_char* data, int len,
104 : : int is_orig = -1);
105 : :
106 : : // Pass in name/length, returns in xname/xlen the converted
107 : : // name/length. Returns 0 on failure; xname may still be
108 : : // allocated and hold partial results at that point.
109 : : int ConvertName(const u_char* name, int name_len,
110 : : u_char*& xname, int& xlen);
111 : :
112 : : protected:
113 : : Analyzer* analyzer;
114 : : SMB_Session* smb_session;
115 : : };
116 : :
117 : :
118 : : typedef enum {
119 : : NETBIOS_SSN_TYPE, // looking for type field
120 : : NETBIOS_SSN_FLAGS, // looking for flag field
121 : : NETBIOS_SSN_LEN_HI, // looking for high-order byte of length
122 : : NETBIOS_SSN_LEN_LO, // looking for low-order byte of length
123 : : NETBIOS_SSN_BUF, // building up the message in the buffer
124 : : } NetbiosSSN_State;
125 : :
126 : : // ### This should be merged with TCP_Contents_RPC, TCP_Contents_DNS.
127 : : class Contents_NetbiosSSN : public TCP_SupportAnalyzer {
128 : : public:
129 : : Contents_NetbiosSSN(Connection* conn, bool orig,
130 : : NetbiosSSN_Interpreter* interp);
131 : : ~Contents_NetbiosSSN();
132 : :
133 : : void Flush(); // process any partially-received data
134 : :
135 : : NetbiosSSN_State State() const { return state; }
136 : :
137 : : protected:
138 : : virtual void DeliverStream(int len, const u_char* data, bool orig);
139 : :
140 : : NetbiosSSN_Interpreter* interp;
141 : :
142 : : unsigned int type;
143 : : unsigned int flags;
144 : :
145 : : u_char* msg_buf;
146 : : int buf_n; // number of bytes in msg_buf
147 : : int buf_len; // size of msg_buf
148 : : int msg_size; // expected size of message
149 : :
150 : : NetbiosSSN_State state;
151 : : };
152 : :
153 : : class NetbiosSSN_Analyzer : public TCP_ApplicationAnalyzer {
154 : : public:
155 : : NetbiosSSN_Analyzer(Connection* conn);
156 : : ~NetbiosSSN_Analyzer();
157 : :
158 : : virtual void Done();
159 : : virtual void DeliverPacket(int len, const u_char* data, bool orig,
160 : : int seq, const IP_Hdr* ip, int caplen);
161 : :
162 : 0 : static Analyzer* InstantiateAnalyzer(Connection* conn)
163 : 0 : { return new NetbiosSSN_Analyzer(conn); }
164 : :
165 : 1 : static bool Available()
166 : : {
167 : : return NetbiosSSN_Interpreter::any_netbios_ssn_event() ||
168 : : SMB_Session::any_smb_event() ||
169 [ + - ][ + - ]: 1 : DCE_RPC_Session::any_dce_rpc_event();
[ - + ]
170 : : }
171 : :
172 : : protected:
173 : : virtual void ConnectionClosed(TCP_Endpoint* endpoint,
174 : : TCP_Endpoint* peer, int gen_event);
175 : : virtual void EndpointEOF(bool is_orig);
176 : :
177 : : void ExpireTimer(double t);
178 : :
179 : : NetbiosSSN_Interpreter* interp;
180 : : SMB_Session* smb_session;
181 : : Contents_NetbiosSSN* orig_netbios;
182 : : Contents_NetbiosSSN* resp_netbios;
183 : : int did_session_done;
184 : : };
185 : :
186 : : // FIXME: Doesn't really fit into new analyzer structure. What to do?
187 : : int IsReuse(double t, const u_char* pkt);
188 : :
189 : : #endif
|