Branch data Line data Source code
1 : : // $Id: SMB.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 smb_h
6 : : #define smb_h
7 : :
8 : : // SMB (CIFS) analyzer.
9 : : // Reference: http://www.snia.org/tech_activities/CIFS/CIFS-TR-1p00_FINAL.pdf
10 : :
11 : : #include "TCP.h"
12 : : #include "DCE_RPC.h"
13 : : #include "smb_pac.h"
14 : :
15 : : enum IPC_named_pipe {
16 : : IPC_NONE,
17 : : IPC_LOCATOR,
18 : : IPC_EPMAPPER,
19 : : IPC_SAMR, // Security Account Manager
20 : : };
21 : :
22 : 0 : class SMB_Body : public binpac::SMB::SMB_body {
23 : : public:
24 : 0 : SMB_Body(const u_char* data, const u_char* data_end)
25 : 0 : : binpac::SMB::SMB_body()
26 : : {
27 : 0 : data_ = data;
28 : 0 : Parse(data, data_end);
29 : 0 : data_length_ = body_length();
30 [ # # ]: 0 : if ( data + data_length_ > data_end )
31 : 0 : data_length_ = data_end - data;
32 : 0 : }
33 : :
34 : 0 : const u_char* data() const { return data_; }
35 : 0 : int length() const { return data_length_; }
36 : :
37 : : protected:
38 : : const u_char* data_;
39 : : int data_length_;
40 : : };
41 : :
42 : : class SMB_Session {
43 : : public:
44 : : SMB_Session(Analyzer* analyzer);
45 : : ~SMB_Session();
46 : :
47 : : void Deliver(int is_orig, int len, const u_char* msg);
48 : :
49 : 2 : static bool any_smb_event()
50 : : {
51 : : return smb_message ||
52 : : smb_com_tree_connect_andx ||
53 : : smb_com_nt_create_andx || smb_com_transaction ||
54 : : smb_com_transaction2 || smb_com_read_andx ||
55 [ + - ][ + - ]: 2 : smb_com_write_andx;
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
56 : : }
57 : :
58 : : protected:
59 : : void ParseMessage(int is_orig, int cmd,
60 : : binpac::SMB::SMB_header const &hdr,
61 : : SMB_Body const &body);
62 : :
63 : : int ParseNegotiate(binpac::SMB::SMB_header const &hdr,
64 : : SMB_Body const &body);
65 : :
66 : : int ParseNegotiateResponse(binpac::SMB::SMB_header const &hdr,
67 : : SMB_Body const &body);
68 : :
69 : : int ParseAndx(int is_orig, binpac::SMB::SMB_header const &hdr,
70 : : SMB_Body const &body);
71 : :
72 : : int ParseClose(int is_orig, binpac::SMB::SMB_header const &hdr,
73 : : SMB_Body const &body);
74 : :
75 : : int ParseLogoffAndx(int is_orig, binpac::SMB::SMB_header const &hdr,
76 : : SMB_Body const &body);
77 : :
78 : : int ParseSetupAndx(int is_orig, binpac::SMB::SMB_header const &hdr,
79 : : SMB_Body const &body);
80 : :
81 : : int ParseTreeConnectAndx(binpac::SMB::SMB_header const &hdr,
82 : : SMB_Body const &body);
83 : :
84 : : int ParseTreeDisconnect(int is_orig, binpac::SMB::SMB_header const &hdr,
85 : : SMB_Body const &body);
86 : :
87 : : int ParseNtCreateAndx(binpac::SMB::SMB_header const &hdr,
88 : : SMB_Body const &body);
89 : :
90 : : int ParseReadAndx(binpac::SMB::SMB_header const &hdr,
91 : : SMB_Body const &body);
92 : :
93 : : int ParseReadAndxResponse(binpac::SMB::SMB_header const &hdr,
94 : : SMB_Body const &body);
95 : :
96 : : int ParseWriteAndx(binpac::SMB::SMB_header const &hdr,
97 : : SMB_Body const &body);
98 : :
99 : : int ParseWriteAndxResponse(binpac::SMB::SMB_header const &hdr,
100 : : SMB_Body const &body);
101 : :
102 : : int ParseTransaction(int is_orig, int cmd,
103 : : binpac::SMB::SMB_header const &hdr,
104 : : SMB_Body const &body);
105 : :
106 : : int TransactionEvent(EventHandlerPtr f, int is_orig,
107 : : binpac::SMB::SMB_header const &hdr,
108 : : binpac::SMB::SMB_transaction const &trans,
109 : : int data_count,
110 : : binpac::SMB::SMB_transaction_data* data);
111 : :
112 : : int TransactionEvent(EventHandlerPtr f, int is_orig,
113 : : binpac::SMB::SMB_header const &hdr,
114 : : binpac::SMB::SMB_transaction_secondary const &trans,
115 : : int data_count,
116 : : binpac::SMB::SMB_transaction_data* data);
117 : :
118 : : int TransactionEvent(EventHandlerPtr f, int is_orig,
119 : : binpac::SMB::SMB_header const &hdr,
120 : : binpac::SMB::SMB_transaction_response const &trans,
121 : : int data_count,
122 : : binpac::SMB::SMB_transaction_data* data);
123 : :
124 : : int ParseTransactionRequest(int cmd,
125 : : binpac::SMB::SMB_header const &hdr,
126 : : SMB_Body const &body);
127 : :
128 : : int ParseTransactionSecondaryRequest(int cmd,
129 : : binpac::SMB::SMB_header const &hdr,
130 : : SMB_Body const &body);
131 : :
132 : : int ParseTransactionResponse(int cmd,
133 : : binpac::SMB::SMB_header const &hdr,
134 : : SMB_Body const &body);
135 : :
136 : : int ParseGetDFSReferral(binpac::SMB::SMB_header const &hdr,
137 : : int param_count, const u_char* param);
138 : :
139 : : BroString* ExtractString(binpac::SMB::SMB_string const* s);
140 : : BroString* ExtractString(binpac::SMB::SMB_ascii_string const* s);
141 : : BroString* ExtractString(binpac::SMB::SMB_unicode_string const* s);
142 : :
143 : : bool LooksLikeRPC(int len, const u_char* msg);
144 : : bool CheckRPC(int is_orig, int len, const u_char* msg);
145 : :
146 : : int AndxOffset(int is_orig, int &next_command) const;
147 : :
148 : : void Weird(const char* msg);
149 : :
150 : 0 : const binpac::SMB::SMB_andx* const andx(int is_orig) const
151 : : {
152 [ # # ]: 0 : return is_orig ? andx_[1] : andx_[0];
153 : : }
154 : :
155 : : void set_andx(int is_orig, binpac::SMB::SMB_andx* andx);
156 : :
157 : : Val* BuildHeaderVal(binpac::SMB::SMB_header const &hdr);
158 : : Val* BuildTransactionVal(binpac::SMB::SMB_transaction const &trans);
159 : : Val* BuildTransactionVal(binpac::SMB::SMB_transaction_secondary const &trans);
160 : : Val* BuildTransactionVal(binpac::SMB::SMB_transaction_response const &trans);
161 : : Val* BuildTransactionDataVal(binpac::SMB::SMB_transaction_data* data);
162 : :
163 : : Analyzer* analyzer;
164 : : DCE_RPC_Session* dce_rpc_session;
165 : : enum IPC_named_pipe IPC_pipe;
166 : : int is_IPC;
167 : : int req_cmd;
168 : : uint16 transaction_subcmd;
169 : : bool smb_mailslot_prot;
170 : : bool smb_pipe_prot;
171 : : StringVal* transaction_name;
172 : : binpac::SMB::SMB_andx* andx_[2];
173 : : };
174 : :
175 : : class Contents_SMB : public TCP_SupportAnalyzer {
176 : : public:
177 : : Contents_SMB(Connection* conn, bool orig, SMB_Session* smb_session);
178 : : ~Contents_SMB();
179 : :
180 : : virtual void DeliverStream(int len, const u_char* data, bool orig);
181 : :
182 : : protected:
183 : : void InitMsgBuf();
184 : :
185 : : void DeliverSMB(int len, const u_char* data);
186 : :
187 : : SMB_Session* smb_session;
188 : : u_char dshdr[4];
189 : : u_char* msg_buf;
190 : : int msg_len;
191 : : int buf_n; // number of bytes in msg_buf
192 : : int buf_len; // size off msg_buf
193 : : };
194 : :
195 : : class SMB_Analyzer : public TCP_ApplicationAnalyzer {
196 : : public:
197 : : SMB_Analyzer(Connection* conn);
198 : : ~SMB_Analyzer();
199 : :
200 : 0 : static Analyzer* InstantiateAnalyzer(Connection* conn)
201 : 0 : { return new SMB_Analyzer(conn); }
202 : :
203 : 1 : static bool Available()
204 : : {
205 : : return SMB_Session::any_smb_event() ||
206 [ + - ][ - + ]: 1 : DCE_RPC_Session::any_dce_rpc_event();
207 : : }
208 : :
209 : 0 : int RewritingTrace() { return rewriting_smb_trace; }
210 : :
211 : : protected:
212 : : SMB_Session* smb_session;
213 : : Contents_SMB* o_smb;
214 : : Contents_SMB* r_smb;
215 : : };
216 : :
217 : : #endif
|