1 //--------------------------------------------------------------------------
2 // Copyright (C) 2016-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 
19 // smb_message.h author Russ Combs <rucombs@cisco.com>
20 
21 // extracted from dce_smb.h originally written by Todd Wease
22 
23 #ifndef SMB_MESSAGE_H
24 #define SMB_MESSAGE_H
25 
26 #include <cstdint>
27 
28 #include "protocols/packet.h"
29 #include "smb_common.h"
30 
31 #pragma pack(1)
32 
33 /********************************************************************
34  * SMB_COM_OPEN
35  ********************************************************************/
36 struct SmbOpenReq   /* smb_wct = 2 */
37 {
38     uint8_t smb_wct;      /* count of 16-bit words that follow */
39     uint16_t smb_mode;    /* r/w/share */
40     uint16_t smb_attr;    /* attribute */
41     uint16_t smb_bcc;     /* min = 2 */
42 };
43 
44 struct SmbOpenResp   /* smb_wct = 7 */
45 {
46     uint8_t smb_wct;      /* count of 16-bit words that follow */
47     uint16_t smb_fid;     /* file handle */
48     uint16_t smb_attr;    /* attribute */
49     uint32_t smb_time;    /* time1 low */
50     uint32_t smb_file_size;   /* file size low */
51     uint16_t smb_access;  /* access allowed */
52     uint16_t smb_bcc;     /* must be 0 */
53 };
54 
55 #define SMB_OPEN_ACCESS_MODE__READ        0x0000
56 #define SMB_OPEN_ACCESS_MODE__WRITE       0x0001
57 #define SMB_OPEN_ACCESS_MODE__READ_WRITE  0x0002
58 #define SMB_OPEN_ACCESS_MODE__EXECUTE     0x0003
59 
SmbOpenRespFid(const SmbOpenResp * resp)60 inline uint16_t SmbOpenRespFid(const SmbOpenResp* resp)
61 {
62     return snort::alignedNtohs(&resp->smb_fid);
63 }
64 
SmbOpenRespFileSize(const SmbOpenResp * resp)65 inline uint32_t SmbOpenRespFileSize(const SmbOpenResp* resp)
66 {
67     return snort::alignedNtohl(&resp->smb_file_size);
68 }
69 
SmbOpenRespFileAttrs(const SmbOpenResp * resp)70 inline uint16_t SmbOpenRespFileAttrs(const SmbOpenResp* resp)
71 {
72     return snort::alignedNtohs(&resp->smb_attr);
73 }
74 
SmbFileAttrsDirectory(const uint16_t file_attrs)75 inline bool SmbFileAttrsDirectory(const uint16_t file_attrs)
76 {
77     if (file_attrs & SMB_FILE_ATTRIBUTE_DIRECTORY)
78         return true;
79     return false;
80 }
81 
SmbOpenRespAccessMode(const SmbOpenResp * resp)82 inline uint16_t SmbOpenRespAccessMode(const SmbOpenResp* resp)
83 {
84     return snort::alignedNtohs(&resp->smb_access);
85 }
86 
SmbOpenForWriting(const uint16_t access_mode)87 inline bool SmbOpenForWriting(const uint16_t access_mode)
88 {
89     return access_mode == SMB_OPEN_ACCESS_MODE__WRITE;
90 }
91 
92 /********************************************************************
93  * SMB_COM_CREATE
94  ********************************************************************/
95 struct SmbCreateReq   /* smb_wct = 3 */
96 {
97     uint8_t smb_wct;
98     uint16_t smb_file_attrs;
99     uint32_t smb_creation_time;
100     uint16_t smb_bcc;
101 };
102 
103 struct SmbCreateResp   /* smb_wct = 1 */
104 {
105     uint8_t smb_wct;
106     uint16_t smb_fid;
107     uint16_t smb_bcc;
108 };
109 
SmbCreateReqFileAttrs(const SmbCreateReq * req)110 inline uint16_t SmbCreateReqFileAttrs(const SmbCreateReq* req)
111 {
112     return snort::alignedNtohs(&req->smb_file_attrs);
113 }
114 
SmbAttrDirectory(const uint16_t file_attrs)115 inline bool SmbAttrDirectory(const uint16_t file_attrs)
116 {
117     if (file_attrs & SMB_FILE_ATTRIBUTE_DIRECTORY)
118         return true;
119     return false;
120 }
121 
SmbCreateRespFid(const SmbCreateResp * resp)122 inline uint16_t SmbCreateRespFid(const SmbCreateResp* resp)
123 {
124     return snort::alignedNtohs(&resp->smb_fid);
125 }
126 
127 /********************************************************************
128  * SMB_COM_CLOSE
129  ********************************************************************/
130 struct SmbCloseReq   /* smb_wct = 3 */
131 {
132     uint8_t smb_wct;      /* count of 16-bit words that follow */
133     uint16_t smb_fid;     /* file handle */
134     uint16_t smb_tlow;    /* time low */
135     uint16_t smb_thigh;   /* time high */
136     uint16_t smb_bcc;     /* must be 0 */
137 };
138 
139 struct SmbCloseResp   /* smb_wct = 0 */
140 {
141     uint8_t smb_wct;      /* count of 16-bit words that follow */
142     uint16_t smb_bcc;     /* must be 0 */
143 };
144 
SmbCloseReqFid(const SmbCloseReq * req)145 inline uint16_t SmbCloseReqFid(const SmbCloseReq* req)
146 {
147     return snort::alignedNtohs(&req->smb_fid);
148 }
149 
150 /********************************************************************
151  * SMB_COM_DELETE
152  ********************************************************************/
153 struct SmbDeleteReq  /* smb_wct = 1 */
154 {
155     uint8_t  smb_wct;
156     uint16_t smb_search_attrs;
157     uint16_t smb_bcc;
158 };
159 
160 /********************************************************************
161  * SMB_COM_READ
162  ********************************************************************/
163 struct SmbReadReq   /* smb_wct = 5 */
164 {
165     uint8_t smb_wct;      /* count of 16-bit words that follow */
166     uint16_t smb_fid;     /* file handle */
167     uint16_t smb_cnt;     /* count of bytes */
168     uint32_t smb_off;     /* offset */
169     uint16_t smb_left;    /* count left */
170     uint16_t smb_bcc;     /* must be 0 */
171 };
172 
173 struct SmbReadResp   /* smb_wct = 5 */
174 {
175     uint8_t smb_wct;      /* count of 16-bit words that follow */
176     uint16_t smb_cnt;     /* count */
177     uint16_t smb_res[4];  /* reserved (MBZ) */
178     uint16_t smb_bcc;     /* length of data + 3 */
179 };
180 
SmbReadReqFid(const SmbReadReq * req)181 inline uint16_t SmbReadReqFid(const SmbReadReq* req)
182 {
183     return snort::alignedNtohs(&req->smb_fid);
184 }
185 
SmbReadReqOffset(const SmbReadReq * req)186 inline uint32_t SmbReadReqOffset(const SmbReadReq* req)
187 {
188     return snort::alignedNtohl(&req->smb_off);
189 }
190 
SmbReadRespCount(const SmbReadResp * resp)191 inline uint16_t SmbReadRespCount(const SmbReadResp* resp)
192 {
193     return snort::alignedNtohs(&resp->smb_cnt);
194 }
195 
196 /********************************************************************
197  * SMB_COM_WRITE
198  ********************************************************************/
199 struct SmbWriteReq   /* smb_wct = 5 */
200 {
201     uint8_t smb_wct;      /* count of 16-bit words that follow */
202     uint16_t smb_fid;     /* file handle */
203     uint16_t smb_cnt;     /* count of bytes */
204     uint32_t smb_offset;  /* file offset in bytes */
205     uint16_t smb_left;    /* count left */
206     uint16_t smb_bcc;     /* length of data + 3 */
207 };
208 
209 struct SmbWriteResp   /* smb_wct = 1 */
210 {
211     uint8_t smb_wct;      /* count of 16-bit words that follow */
212     uint16_t smb_cnt;     /* count */
213     uint16_t smb_bcc;     /* must be 0 */
214 };
215 
SmbWriteReqFid(const SmbWriteReq * req)216 inline uint16_t SmbWriteReqFid(const SmbWriteReq* req)
217 {
218     return snort::alignedNtohs(&req->smb_fid);
219 }
220 
SmbWriteReqCount(const SmbWriteReq * req)221 inline uint16_t SmbWriteReqCount(const SmbWriteReq* req)
222 {
223     return snort::alignedNtohs(&req->smb_cnt);
224 }
225 
SmbWriteReqOffset(const SmbWriteReq * req)226 inline uint32_t SmbWriteReqOffset(const SmbWriteReq* req)
227 {
228     return snort::alignedNtohl(&req->smb_offset);
229 }
230 
SmbWriteRespCount(const SmbWriteResp * resp)231 inline uint16_t SmbWriteRespCount(const SmbWriteResp* resp)
232 {
233     return snort::alignedNtohs(&resp->smb_cnt);
234 }
235 
236 /********************************************************************
237  * SMB_COM_CREATE_NEW
238  ********************************************************************/
239 struct SmbCreateNewReq   /* smb_wct = 3 */
240 {
241     uint8_t smb_wct;
242     uint16_t smb_file_attrs;
243     uint32_t smb_creation_time;
244     uint16_t smb_bcc;
245 };
246 
247 struct SmbCreateNewResp   /* smb_wct = 1 */
248 {
249     uint8_t smb_wct;
250     uint16_t smb_fid;
251     uint16_t smb_bcc;
252 };
253 
SmbCreateNewReqFileAttrs(const SmbCreateNewReq * req)254 inline uint16_t SmbCreateNewReqFileAttrs(const SmbCreateNewReq* req)
255 {
256     return snort::alignedNtohs(&req->smb_file_attrs);
257 }
258 
SmbCreateNewRespFid(const SmbCreateNewResp * resp)259 inline uint16_t SmbCreateNewRespFid(const SmbCreateNewResp* resp)
260 {
261     return snort::alignedNtohs(&resp->smb_fid);
262 }
263 
264 /********************************************************************
265  * SMB_COM_LOCK_AND_READ
266  ********************************************************************/
267 struct SmbLockAndReadReq   /* smb_wct = 5 */
268 {
269     uint8_t smb_wct;      /* count of 16-bit words that follow */
270     uint16_t smb_fid;
271     uint16_t smb_cnt;
272     uint32_t smb_read_offset;
273     uint16_t smb_remaining;
274     uint16_t smb_bcc;     /* must be 0 */
275 };
276 
277 struct SmbLockAndReadResp   /* smb_wct = 5 */
278 {
279     uint8_t smb_wct;
280     uint16_t smb_cnt;
281     uint16_t reserved[4];
282     uint16_t smb_bcc;
283 };
284 
SmbLockAndReadReqFid(const SmbLockAndReadReq * req)285 inline uint16_t SmbLockAndReadReqFid(const SmbLockAndReadReq* req)
286 {
287     return snort::alignedNtohs(&req->smb_fid);
288 }
289 
SmbLockAndReadReqOffset(const SmbLockAndReadReq * req)290 inline uint32_t SmbLockAndReadReqOffset(const SmbLockAndReadReq* req)
291 {
292     return snort::alignedNtohl(&req->smb_read_offset);
293 }
294 
SmbLockAndReadRespCount(const SmbLockAndReadResp * resp)295 inline uint16_t SmbLockAndReadRespCount(const SmbLockAndReadResp* resp)
296 {
297     return snort::alignedNtohs(&resp->smb_cnt);
298 }
299 
300 /********************************************************************
301  * SMB_COM_WRITE_AND_UNLOCK
302  ********************************************************************/
303 struct SmbWriteAndUnlockReq
304 {
305     uint8_t smb_wct;
306     uint16_t smb_fid;
307     uint16_t smb_cnt;
308     uint32_t smb_write_offset;
309     uint16_t smb_estimate_of_remaining;
310     uint16_t smb_bcc;
311 };
312 
313 struct SmbWriteAndUnlockResp   /* smb_wct = 1 */
314 {
315     uint8_t smb_wct;      /* count of 16-bit words that follow */
316     uint16_t smb_cnt;     /* count */
317     uint16_t smb_bcc;     /* must be 0 */
318 };
319 
SmbWriteAndUnlockReqFid(const SmbWriteAndUnlockReq * req)320 inline uint16_t SmbWriteAndUnlockReqFid(const SmbWriteAndUnlockReq* req)
321 {
322     return snort::alignedNtohs(&req->smb_fid);
323 }
324 
SmbWriteAndUnlockReqCount(const SmbWriteAndUnlockReq * req)325 inline uint16_t SmbWriteAndUnlockReqCount(const SmbWriteAndUnlockReq* req)
326 {
327     return snort::alignedNtohs(&req->smb_cnt);
328 }
329 
SmbWriteAndUnlockReqOffset(const SmbWriteAndUnlockReq * req)330 inline uint32_t SmbWriteAndUnlockReqOffset(const SmbWriteAndUnlockReq* req)
331 {
332     return snort::alignedNtohl(&req->smb_write_offset);
333 }
334 
335 /********************************************************************
336  * SMB_COM_OPEN_ANDX
337  ********************************************************************/
338 struct SmbOpenAndXReq   /* smb_wct = 15 */
339 {
340     uint8_t smb_wct;         /* count of 16-bit words that follow */
341     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
342     uint8_t smb_reh2;        /* reserved (must be zero) */
343     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
344     uint16_t smb_flags;      /* additional information:
345                                 bit 0 - if set, return additional information
346                                 bit 1 - if set, set single user total file lock (if only access)
347                                 bit 2 - if set, the server should notify the consumer on any
348                                         action which can modify the file (delete, setattrib,
349                                         rename, etc.). if not set, the server need only notify
350                                         the consumer on another open request. This bit only has
351                                         meaning if bit 1 is set. */
352     uint16_t smb_mode;       /* file open mode */
353     uint16_t smb_sattr;      /* search attributes */
354     uint16_t smb_attr;       /* file attributes (for create) */
355     uint32_t smb_time;       /* create time */
356     uint16_t smb_ofun;       /* open function */
357     uint32_t smb_size;       /* bytes to reserve on "create" or "truncate" */
358     uint32_t smb_timeout;    /* max milliseconds to wait for resource to open */
359     uint32_t smb_rsvd;       /* reserved (must be zero) */
360     uint16_t smb_bcc;        /* minimum value = 1 */
361 };
362 
363 struct SmbOpenAndXResp   /* smb_wct = 15 */
364 {
365     uint8_t smb_wct;         /* count of 16-bit words that follow */
366     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
367     uint8_t smb_res2;        /* reserved (pad to word) */
368     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
369     uint16_t smb_fid;        /* file handle */
370     uint16_t smb_attribute;  /* attributes of file or device */
371     uint32_t smb_time;       /* last modification time */
372     uint32_t smb_size;       /* current file size */
373     uint16_t smb_access;     /* access permissions actually allowed */
374     uint16_t smb_type;       /* file type */
375     uint16_t smb_state;      /* state of IPC device (e.g. pipe) */
376     uint16_t smb_action;     /* action taken */
377     uint32_t smb_fileid;     /* server unique file id */
378     uint16_t smb_rsvd;       /* reserved */
379     uint16_t smb_bcc;        /* value = 0 */
380 };
381 
SmbOpenAndXReqAllocSize(const SmbOpenAndXReq * req)382 inline uint32_t SmbOpenAndXReqAllocSize(const SmbOpenAndXReq* req)
383 {
384     return snort::alignedNtohl(&req->smb_size);
385 }
386 
SmbOpenAndXReqFileAttrs(const SmbOpenAndXReq * req)387 inline uint16_t SmbOpenAndXReqFileAttrs(const SmbOpenAndXReq* req)
388 {
389     return snort::alignedNtohs(&req->smb_attr);
390 }
391 
SmbOpenAndXRespFid(const SmbOpenAndXResp * resp)392 inline uint16_t SmbOpenAndXRespFid(const SmbOpenAndXResp* resp)
393 {
394     return snort::alignedNtohs(&resp->smb_fid);
395 }
396 
SmbOpenAndXRespFileAttrs(const SmbOpenAndXResp * resp)397 inline uint16_t SmbOpenAndXRespFileAttrs(const SmbOpenAndXResp* resp)
398 {
399     return snort::alignedNtohs(&resp->smb_attribute);
400 }
401 
SmbOpenAndXRespFileSize(const SmbOpenAndXResp * resp)402 inline uint32_t SmbOpenAndXRespFileSize(const SmbOpenAndXResp* resp)
403 {
404     return snort::alignedNtohl(&resp->smb_size);
405 }
406 
SmbOpenAndXRespResourceType(const SmbOpenAndXResp * resp)407 inline uint16_t SmbOpenAndXRespResourceType(const SmbOpenAndXResp* resp)
408 {
409     return snort::alignedNtohs(&resp->smb_type);
410 }
411 
412 #define SMB_OPEN_RESULT__EXISTED    0x0001
413 #define SMB_OPEN_RESULT__CREATED    0x0002
414 #define SMB_OPEN_RESULT__TRUNCATED  0x0003
415 
SmbOpenAndXRespOpenResults(const SmbOpenAndXResp * resp)416 inline uint16_t SmbOpenAndXRespOpenResults(const SmbOpenAndXResp* resp)
417 {
418     return snort::alignedNtohs(&resp->smb_action);
419 }
420 
SmbOpenResultRead(const uint16_t open_results)421 inline bool SmbOpenResultRead(const uint16_t open_results)
422 {
423     return ((open_results & 0x00FF) == SMB_OPEN_RESULT__EXISTED);
424 }
425 
SmbResourceTypeDisk(const uint16_t resource_type)426 inline bool SmbResourceTypeDisk(const uint16_t resource_type)
427 {
428     return resource_type == SMB_FILE_TYPE_DISK;
429 }
430 
431 /********************************************************************
432  * SMB_COM_READ_ANDX
433  ********************************************************************/
434 struct SmbReadAndXReq   /* smb_wct = 10 */
435 {
436     uint8_t smb_wct;         /* count of 16-bit words that follow */
437     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
438     uint8_t smb_reh2;        /* reserved (must be zero) */
439     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
440     uint16_t smb_fid;        /* file handle */
441     uint32_t smb_offset;     /* offset in file to begin read */
442     uint16_t smb_maxcnt;     /* max number of bytes to return */
443     uint16_t smb_mincnt;     /* min number of bytes to return */
444     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
445     uint16_t smb_countleft;  /* bytes remaining to satisfy user’s request */
446     uint16_t smb_bcc;        /* value = 0 */
447 };
448 
449 struct SmbReadAndXExtReq   /* smb_wct = 12 */
450 {
451     uint8_t smb_wct;         /* count of 16-bit words that follow */
452     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
453     uint8_t smb_reh2;        /* reserved (must be zero) */
454     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
455     uint16_t smb_fid;        /* file handle */
456     uint32_t smb_offset;     /* low offset in file to begin read */
457     uint16_t smb_maxcnt;     /* max number of bytes to return */
458     uint16_t smb_mincnt;     /* min number of bytes to return */
459     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
460     uint16_t smb_countleft;  /* bytes remaining to satisfy user’s request */
461     uint32_t smb_off_high;   /* high offset in file to begin read */
462     uint16_t smb_bcc;        /* value = 0 */
463 };
464 
465 struct SmbReadAndXResp    /* smb_wct = 12 */
466 {
467     uint8_t smb_wct;         /* count of 16-bit words that follow */
468     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
469     uint8_t smb_res2;        /* reserved (pad to word) */
470     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
471     uint16_t smb_remaining;  /* bytes remaining to be read (pipes/devices only) */
472     uint32_t smb_rsvd;       /* reserved */
473     uint16_t smb_dsize;      /* number of data bytes (minimum value = 0) */
474     uint16_t smb_doff;       /* offset (from start of SMB hdr) to data bytes */
475     uint16_t smb_dsize_high; /* high bytes of data size */
476     uint32_t smb_rsvd1;      /* reserved */
477     uint32_t smb_rsvd2;      /* reserved */
478     uint16_t smb_bcc;        /* total bytes (including pad bytes) following */
479 };
480 
SmbReadAndXReqFid(const SmbReadAndXReq * req)481 inline uint16_t SmbReadAndXReqFid(const SmbReadAndXReq* req)
482 {
483     return snort::alignedNtohs(&req->smb_fid);
484 }
485 
SmbReadAndXReqOffset(const SmbReadAndXExtReq * req)486 inline uint64_t SmbReadAndXReqOffset(const SmbReadAndXExtReq* req)
487 {
488     if (req->smb_wct == 10)
489         return (uint64_t)snort::alignedNtohl(&req->smb_offset);
490 
491     return (uint64_t)snort::alignedNtohl(&req->smb_off_high) << 32
492                     | (uint64_t)snort::alignedNtohl(&req->smb_offset);
493 }
494 
SmbReadAndXRespDataOff(const SmbReadAndXResp * req)495 inline uint16_t SmbReadAndXRespDataOff(const SmbReadAndXResp* req)
496 {
497     return snort::alignedNtohs(&req->smb_doff);
498 }
499 
SmbReadAndXRespDataCnt(const SmbReadAndXResp * resp)500 inline uint32_t SmbReadAndXRespDataCnt(const SmbReadAndXResp* resp)
501 {
502     return (uint32_t)snort::alignedNtohs(&resp->smb_dsize_high) << 16
503                     | (uint32_t)snort::alignedNtohs(&resp->smb_dsize);
504 }
505 
506 /********************************************************************
507  * SMB_COM_WRITE_ANDX
508  ********************************************************************/
509 struct SmbWriteAndXReq   /* smb_wct = 12 */
510 {
511     uint8_t smb_wct;         /* count of 16-bit words that follow */
512     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
513     uint8_t smb_reh2;        /* reserved (must be zero) */
514     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
515     uint16_t smb_fid;        /* file handle */
516     uint32_t smb_offset;     /* offset in file to begin write */
517     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
518     uint16_t smb_wmode;      /* write mode:
519                                 bit0 - complete write before return (write through)
520                                 bit1 - return smb_remaining (pipes/devices only)
521                                 bit2 - use WriteRawNamedPipe (pipes only)
522                                 bit3 - this is the start of a message (pipes only) */
523     uint16_t smb_countleft;  /* bytes remaining to write to satisfy user’s request */
524     uint16_t smb_dsize_high; /* high bytes of data size */
525     uint16_t smb_dsize;      /* number of data bytes in buffer (min value = 0) */
526     uint16_t smb_doff;       /* offset (from start of SMB hdr) to data bytes */
527     uint16_t smb_bcc;        /* total bytes (including pad bytes) following */
528 };
529 
530 struct SmbWriteAndXExtReq   /* smb_wct = 14 */
531 {
532     uint8_t smb_wct;         /* count of 16-bit words that follow */
533     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
534     uint8_t smb_reh2;        /* reserved (must be zero) */
535     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
536     uint16_t smb_fid;        /* file handle */
537     uint32_t smb_offset;     /* low offset in file to begin write */
538     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
539     uint16_t smb_wmode;      /* write mode:
540                                 bit0 - complete write before return (write through)
541                                 bit1 - return smb_remaining (pipes/devices only)
542                                 bit2 - use WriteRawNamedPipe (pipes only)
543                                 bit3 - this is the start of a message (pipes only) */
544     uint16_t smb_countleft;  /* bytes remaining to write to satisfy user’s request */
545     uint16_t smb_dsize_high; /* high bytes of data size */
546     uint16_t smb_dsize;      /* number of data bytes in buffer (min value = 0) */
547     uint16_t smb_doff;       /* offset (from start of SMB hdr) to data bytes */
548     uint32_t smb_off_high;   /* high offset in file to begin write */
549     uint16_t smb_bcc;        /* total bytes (including pad bytes) following */
550 };
551 
552 struct SmbWriteAndXResp   /* smb_wct = 6 */
553 {
554     uint8_t smb_wct;         /* count of 16-bit words that follow */
555     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
556     uint8_t smb_res2;        /* reserved (pad to word) */
557     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
558     uint16_t smb_count;      /* number of bytes written */
559     uint16_t smb_remaining;  /* bytes remaining to be read (pipes/devices only) */
560     uint16_t smb_count_high; /* high order bytes of data count */
561     uint16_t smb_rsvd;       /* reserved */
562     uint16_t smb_bcc;        /* value = 0 */
563 };
564 
SmbWriteAndXReqFid(const SmbWriteAndXReq * req)565 inline uint16_t SmbWriteAndXReqFid(const SmbWriteAndXReq* req)
566 {
567     return snort::alignedNtohs(&req->smb_fid);
568 }
569 
SmbWriteAndXReqDataOff(const SmbWriteAndXReq * req)570 inline uint16_t SmbWriteAndXReqDataOff(const SmbWriteAndXReq* req)
571 {
572     return snort::alignedNtohs(&req->smb_doff);
573 }
574 
SmbWriteAndXReqRemaining(const SmbWriteAndXReq * req)575 inline uint16_t SmbWriteAndXReqRemaining(const SmbWriteAndXReq* req)
576 {
577     return snort::alignedNtohs(&req->smb_countleft);
578 }
579 
SmbWriteAndXReqOffset(const SmbWriteAndXExtReq * req)580 inline uint64_t SmbWriteAndXReqOffset(const SmbWriteAndXExtReq* req)
581 {
582     if (req->smb_wct == 12)
583         return (uint64_t)snort::alignedNtohl(&req->smb_offset);
584 
585     return (uint64_t)snort::alignedNtohl(&req->smb_off_high) << 32
586                     | (uint64_t)snort::alignedNtohl(&req->smb_offset);
587 }
588 
SmbWriteAndXReqDataCnt(const SmbWriteAndXReq * req)589 inline uint32_t SmbWriteAndXReqDataCnt(const SmbWriteAndXReq* req)
590 {
591     return (uint32_t)snort::alignedNtohs(&req->smb_dsize_high) << 16
592                     | (uint32_t)snort::alignedNtohs(&req->smb_dsize);
593 }
594 
SmbWriteAndXReqWriteMode(const SmbWriteAndXReq * req)595 inline uint16_t SmbWriteAndXReqWriteMode(const SmbWriteAndXReq* req)
596 {
597     return snort::alignedNtohs(&req->smb_wmode);
598 }
599 
SmbWriteAndXReqStartRaw(const SmbWriteAndXReq * req)600 inline bool SmbWriteAndXReqStartRaw(const SmbWriteAndXReq* req)
601 {
602     return ((snort::alignedNtohs(&req->smb_wmode) & 0x000c) == 0x000c) ? true : false;
603 }
604 
SmbWriteAndXReqRaw(const SmbWriteAndXReq * req)605 inline bool SmbWriteAndXReqRaw(const SmbWriteAndXReq* req)
606 {
607     return ((snort::alignedNtohs(&req->smb_wmode) & 0x000c) == 0x0004) ? true : false;
608 }
609 
SmbWriteAndXRespCnt(const SmbWriteAndXResp * resp)610 inline uint16_t SmbWriteAndXRespCnt(const SmbWriteAndXResp* resp)
611 {
612     return snort::alignedNtohs(&resp->smb_count);
613 }
614 
615 /********************************************************************
616  * SMB_COM_SESSION_SETUP_ANDX
617  ********************************************************************/
618 struct SmbLm10_SessionSetupAndXReq   /* smb_wct = 10 */
619 {
620     uint8_t smb_wct;       /* count of 16-bit words that follow */
621     uint8_t smb_com2;      /* secondary (X) command, 0xFF = none */
622     uint8_t smb_reh2;      /* reserved (must be zero) */
623     uint16_t smb_off2;     /* offset (from SMB hdr start) to next cmd (@smb_wct) */
624     uint16_t smb_bufsize;  /* the consumers max buffer size */
625     uint16_t smb_mpxmax;   /* actual maximum multiplexed pending requests */
626     uint16_t smb_vc_num;   /* 0 = first (only), non zero - additional VC number */
627     uint32_t smb_sesskey;  /* Session Key (valid only if smb_vc_num != 0) */
628     uint16_t smb_apasslen; /* size of account password (smb_apasswd) */
629     uint32_t smb_rsvd;     /* reserved */
630     uint16_t smb_bcc;      /* minimum value = 0 */
631 };
632 
SmbSessionSetupAndXReqMaxMultiplex(const SmbLm10_SessionSetupAndXReq * req)633 inline uint16_t SmbSessionSetupAndXReqMaxMultiplex(const SmbLm10_SessionSetupAndXReq* req)
634 {
635     return snort::alignedNtohs(&req->smb_mpxmax);
636 }
637 
638 /* Extended request as defined in NT LM 1.0 document */
639 struct SmbNt10_SessionSetupAndXReq   /* smb_wct = 13 */
640 {
641     uint8_t smb_wct;              /* count of 16-bit words that follow */
642     uint8_t smb_com2;             /* secondary (X) command, 0xFF = none */
643     uint8_t smb_reh2;             /* reserved (must be zero) */
644     uint16_t smb_off2;            /* offset (from SMB hdr start) to next cmd (@smb_wct) */
645     uint16_t smb_bufsize;         /* the consumers max buffer size */
646     uint16_t smb_mpxmax;          /* actual maximum multiplexed pending requests */
647     uint16_t smb_vc_num;          /* 0 = first (only), non zero - additional VC number */
648     uint32_t smb_sesskey;         /* Session Key (valid only if smb_vc_num != 0) */
649     uint16_t smb_oem_passlen;     /* case insensitive password length */
650     uint16_t smb_unicode_passlen; /* case sensitive password length */
651     uint32_t smb_rsvd;            /* reserved */
652     uint32_t smb_cap;             /* capabilities */
653     uint16_t smb_bcc;             /* minimum value = 0 */
654 };
655 
SmbNt10SessionSetupAndXReqOemPassLen(const SmbNt10_SessionSetupAndXReq * req)656 inline uint16_t SmbNt10SessionSetupAndXReqOemPassLen(const SmbNt10_SessionSetupAndXReq* req)
657 {
658     return snort::alignedNtohs(&req->smb_oem_passlen);
659 }
660 
SmbNt10SessionSetupAndXReqUnicodePassLen(const SmbNt10_SessionSetupAndXReq * req)661 inline uint16_t SmbNt10SessionSetupAndXReqUnicodePassLen(const SmbNt10_SessionSetupAndXReq* req)
662 {
663     return snort::alignedNtohs(&req->smb_unicode_passlen);
664 }
665 
666 /* Extended request for security blob */
667 struct SmbNt10_SessionSetupAndXExtReq   /* smb_wct = 12 */
668 {
669     uint8_t smb_wct;          /* count of 16-bit words that follow */
670     uint8_t smb_com2;         /* secondary (X) command, 0xFF = none */
671     uint8_t smb_reh2;         /* reserved (must be zero) */
672     uint16_t smb_off2;        /* offset (from SMB hdr start) to next cmd (@smb_wct) */
673     uint16_t smb_bufsize;     /* the consumers max buffer size */
674     uint16_t smb_mpxmax;      /* actual maximum multiplexed pending requests */
675     uint16_t smb_vc_num;      /* 0 = first (only), non zero - additional VC number */
676     uint32_t smb_sesskey;     /* Session Key (valid only if smb_vc_num != 0) */
677     uint16_t smb_blob_len;    /* length of security blob */
678     uint32_t smb_rsvd;        /* reserved */
679     uint32_t smb_cap;         /* capabilities */
680     uint16_t smb_bcc;         /* minimum value = 0 */
681 };
682 
SmbSessionSetupAndXReqBlobLen(const SmbNt10_SessionSetupAndXExtReq * req)683 inline uint16_t SmbSessionSetupAndXReqBlobLen(const SmbNt10_SessionSetupAndXExtReq* req)
684 {
685     return snort::alignedNtohs(&req->smb_blob_len);
686 }
687 
688 /* Extended response for security blob */
689 struct SmbNt10_SessionSetupAndXExtResp   /* smb_wct = 4 */
690 {
691     uint8_t smb_wct;         /* count of 16-bit words that follow */
692     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
693     uint8_t smb_res2;        /* reserved (pad to word) */
694     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
695     uint16_t smb_action;     /* request mode:
696                                 bit0 = Logged in successfully - BUT as GUEST */
697     uint16_t smb_blob_len;   /* length of security blob */
698     uint16_t smb_bcc;        /* min value = 0 */
699 };
700 
SmbSessionSetupAndXRespBlobLen(const SmbNt10_SessionSetupAndXExtResp * resp)701 inline uint16_t SmbSessionSetupAndXRespBlobLen(const SmbNt10_SessionSetupAndXExtResp* resp)
702 {
703     return snort::alignedNtohs(&resp->smb_blob_len);
704 }
705 
706 /********************************************************************
707  * SMB_COM_NEGOTIATE
708  ********************************************************************/
709 /* This is the Lanman response */
710 struct SmbLm10_NegotiateProtocolResp   /* smb_wct = 13 */
711 {
712     uint8_t smb_wct;        /* count of 16-bit words that follow */
713     uint16_t smb_index;     /* index identifying dialect selected */
714     uint16_t smb_secmode;   /* security mode:
715                                bit 0, 1 = User level, 0 = Share level
716                                bit 1, 1 = encrypt passwords, 0 = do not encrypt passwords */
717     uint16_t smb_maxxmt;    /* max transmit buffer size server supports, 1K min */
718     uint16_t smb_maxmux;    /* max pending multiplexed requests server supports */
719     uint16_t smb_maxvcs;    /* max VCs per server/consumer session supported */
720     uint16_t smb_blkmode;   /* block read/write mode support:
721                                bit 0, Read Block Raw supported (65535 bytes max)
722                                bit 1, Write Block Raw supported (65535 bytes max) */
723     uint32_t smb_sesskey;   /* Session Key (unique token identifying session) */
724     uint16_t smb_srv_time;  /* server's current time (hhhhh mmmmmm xxxxx) */
725     uint16_t smb_srv_tzone; /* server's current data (yyyyyyy mmmm ddddd) */
726     uint32_t smb_rsvd;      /* reserved */
727     uint16_t smb_bcc;       /* value = (size of smb_cryptkey) */
728 };
729 
730 /* This is the NT response */
731 struct SmbNt_NegotiateProtocolResp     /* smb_wct = 17 */
732 {
733     uint8_t smb_wct;            /* count of 16-bit words that follow */
734     uint16_t smb_index;         /* index identifying dialect selected */
735     uint8_t smb_secmode;        /* security mode:
736                                    bit 0, 1 = User level, 0 = Share level
737                                    bit 1, 1 = encrypt passwords, 0 = do not encrypt passwords */
738     uint16_t smb_maxmux;        /* max pending multiplexed requests server supports */
739     uint16_t smb_maxvcs;        /* max VCs per server/consumer session supported */
740     uint32_t smb_maxbuf;        /* maximum buffer size supported */
741     uint32_t smb_maxraw;        /* maximum raw buffer size supported */
742     uint32_t smb_sesskey;       /* Session Key (unique token identifying session) */
743     uint32_t smb_cap;           /* capabilities */
744     struct
745     {
746         uint32_t low_time;
747         int32_t high_time;
748     } smb_srv_time;             /* server time */
749     uint16_t smb_srv_tzone;     /* server's current data (yyyyyyy mmmm ddddd) */
750     uint8_t smb_challenge_len;  /* Challenge length */
751     uint16_t smb_bcc;           /* value = (size of smb_cryptkey) */
752 };
753 
SmbLm_NegotiateRespMaxMultiplex(const SmbLm10_NegotiateProtocolResp * resp)754 inline uint16_t SmbLm_NegotiateRespMaxMultiplex(const SmbLm10_NegotiateProtocolResp* resp)
755 {
756     return snort::alignedNtohs(&resp->smb_maxmux);
757 }
758 
SmbNt_NegotiateRespMaxMultiplex(const SmbNt_NegotiateProtocolResp * resp)759 inline uint16_t SmbNt_NegotiateRespMaxMultiplex(const SmbNt_NegotiateProtocolResp* resp)
760 {
761     return snort::alignedNtohs(&resp->smb_maxmux);
762 }
763 
764 /* This is the Core Protocol response */
765 struct SmbCore_NegotiateProtocolResp    /* smb_wct = 1 */
766 {
767     uint8_t smb_wct;      /* count of 16-bit words that follow */
768     uint16_t smb_index;   /* index */
769     uint16_t smb_bcc;     /* must be 0 */
770 };
771 
SmbNegotiateRespDialectIndex(const SmbCore_NegotiateProtocolResp * resp)772 inline uint16_t SmbNegotiateRespDialectIndex(const SmbCore_NegotiateProtocolResp* resp)
773 {
774     return snort::alignedNtohs(&resp->smb_index);
775 }
776 
777 /*********************************************************************
778  * SMB_COM_TREE_CONNECT_ANDX
779  *********************************************************************/
780 struct SmbTreeConnectAndXReq   /* smb_wct = 4 */
781 {
782     uint8_t smb_wct;         /* count of 16-bit words that follow */
783     uint8_t smb_com2;        /* secondary (X) command, 0xFF = none */
784     uint8_t smb_reh2;        /* reserved (must be zero) */
785     uint16_t smb_off2;       /* offset (from SMB hdr start) to next cmd (@smb_wct) */
786     uint16_t smb_flags;      /* additional information:
787                                 bit 0 - if set, disconnect TID in current smb_tid */
788     uint16_t smb_spasslen;   /* length of smb_spasswd */
789     uint16_t smb_bcc;        /* minimum value = 3 */
790 };
791 
SmbTreeConnectAndXReqPassLen(const SmbTreeConnectAndXReq * req)792 inline uint16_t SmbTreeConnectAndXReqPassLen(const SmbTreeConnectAndXReq* req)
793 {
794     return snort::alignedNtohs(&req->smb_spasslen);
795 }
796 
797 /********************************************************************
798  * SMB_COM_NT_TRANSACT
799  ********************************************************************/
800 #define SMB_CREATE_OPTIONS__FILE_SEQUENTIAL_ONLY     0x00000004
801 
802 /********************************************************************
803  * SMB_COM_NT_CREATE_ANDX
804  ********************************************************************/
805 #define SMB_CREATE_DISPOSITSION__FILE_SUPERCEDE      0x00000000
806 #define SMB_CREATE_DISPOSITSION__FILE_OPEN           0x00000001
807 #define SMB_CREATE_DISPOSITSION__FILE_CREATE         0x00000002
808 #define SMB_CREATE_DISPOSITSION__FILE_OPEN_IF        0x00000003
809 #define SMB_CREATE_DISPOSITSION__FILE_OVERWRITE      0x00000004
810 #define SMB_CREATE_DISPOSITSION__FILE_OVERWRITE_IF   0x00000005
811 
812 struct SmbNtCreateAndXReq   /* smb_wct = 24 */
813 {
814     uint8_t smb_wct;            /* count of 16-bit words that follow */
815     uint8_t smb_com2;           /* secondary (X) command, 0xFF = none */
816     uint8_t smb_res2;           /* reserved (pad to word) */
817     uint16_t smb_off2;          /* offset (from SMB hdr start) to next cmd (@smb_wct) */
818     uint8_t smb_res;            /* reserved */
819     uint16_t smb_name_len;      /* length of name of file */
820     uint32_t smb_flags;         /* flags */
821     uint32_t smb_root_fid;      /* fid for previously opened directory */
822     uint32_t smb_access;        /* specifies the type of file access */
823     uint64_t smb_alloc_size;    /* initial allocation size of the file */
824     uint32_t smb_file_attrs;    /* specifies the file attributes for the file */
825     uint32_t smb_share_access;  /* the type of share access */
826     uint32_t smb_create_disp;   /* actions to take if file does or does not exist */
827     uint32_t smb_create_opts;   /* options used when creating or opening file */
828     uint32_t smb_impersonation_level;  /* security impersonation level */
829     uint8_t smb_security_flags;   /* security flags */
830     uint16_t smb_bcc;           /* byte count */
831 };
832 
833 struct SmbNtCreateAndXResp    /* smb_wct = 34 */
834 {
835     uint8_t smb_wct;
836     uint8_t smb_com2;
837     uint8_t smb_res2;
838     uint16_t smb_off2;
839     uint8_t smb_oplock_level;
840     uint16_t smb_fid;
841     uint32_t smb_create_disposition;
842     uint64_t smb_creation_time;
843     uint64_t smb_last_access_time;
844     uint64_t smb_last_write_time;
845     uint64_t smb_change_time;
846     uint32_t smb_file_attrs;
847     uint64_t smb_alloc_size;
848     uint64_t smb_eof;
849     uint16_t smb_resource_type;
850     uint16_t smb_nm_pipe_state;
851     uint8_t smb_directory;
852     uint16_t smb_bcc;
853 };
854 
855 // Word count is always set to 42 though there are actually 50 words
856 struct SmbNtCreateAndXExtResp    /* smb_wct = 42 */
857 {
858     uint8_t smb_wct;
859     uint8_t smb_com2;
860     uint8_t smb_res2;
861     uint16_t smb_off2;
862     uint8_t smb_oplock_level;
863     uint16_t smb_fid;
864     uint32_t smb_create_disposition;
865     uint64_t smb_creation_time;
866     uint64_t smb_last_access_time;
867     uint64_t smb_last_write_time;
868     uint64_t smb_change_time;
869     uint32_t smb_file_attrs;
870     uint64_t smb_alloc_size;
871     uint64_t smb_eof;
872     uint16_t smb_resource_type;
873     uint16_t smb_nm_pipe_state;
874     uint8_t smb_directory;
875     uint8_t smb_volume_guid[16];
876     uint64_t smb_fileid;
877     uint32_t smb_max_access_rights;
878     uint32_t smb_guest_access_rights;
879     uint16_t smb_bcc;
880 };
881 
SmbNtCreateAndXReqFileNameLen(const SmbNtCreateAndXReq * req)882 inline uint16_t SmbNtCreateAndXReqFileNameLen(const SmbNtCreateAndXReq* req)
883 {
884     return snort::alignedNtohs(&req->smb_name_len);
885 }
886 
SmbNtCreateAndXReqCreateDisposition(const SmbNtCreateAndXReq * req)887 inline uint32_t SmbNtCreateAndXReqCreateDisposition(const SmbNtCreateAndXReq* req)
888 {
889     return snort::alignedNtohl(&req->smb_create_disp);
890 }
891 
SmbCreateDispositionRead(const uint32_t create_disposition)892 inline bool SmbCreateDispositionRead(const uint32_t create_disposition)
893 {
894     return (create_disposition == SMB_CREATE_DISPOSITSION__FILE_OPEN)
895            || (create_disposition > SMB_CREATE_DISPOSITSION__FILE_OVERWRITE_IF);
896 }
897 
SmbNtCreateAndXReqAllocSize(const SmbNtCreateAndXReq * req)898 inline uint64_t SmbNtCreateAndXReqAllocSize(const SmbNtCreateAndXReq* req)
899 {
900     return snort::alignedNtohq(&req->smb_alloc_size);
901 }
902 
SmbNtCreateAndXReqSequentialOnly(const SmbNtCreateAndXReq * req)903 inline bool SmbNtCreateAndXReqSequentialOnly(const SmbNtCreateAndXReq* req)
904 {
905     return ((snort::alignedNtohl(&req->smb_create_opts) & SMB_CREATE_OPTIONS__FILE_SEQUENTIAL_ONLY) != 0);
906 }
907 
SmbNtCreateAndXReqFileAttrs(const SmbNtCreateAndXReq * req)908 inline uint32_t SmbNtCreateAndXReqFileAttrs(const SmbNtCreateAndXReq* req)
909 {
910     return snort::alignedNtohl(&req->smb_file_attrs);
911 }
912 
SmbNtCreateAndXRespFid(const SmbNtCreateAndXResp * resp)913 inline uint16_t SmbNtCreateAndXRespFid(const SmbNtCreateAndXResp* resp)
914 {
915     return snort::alignedNtohs(&resp->smb_fid);
916 }
917 
SmbNtCreateAndXRespCreateDisposition(const SmbNtCreateAndXResp * resp)918 inline uint32_t SmbNtCreateAndXRespCreateDisposition(const SmbNtCreateAndXResp* resp)
919 {
920     return snort::alignedNtohl(&resp->smb_create_disposition);
921 }
922 
SmbNtCreateAndXRespDirectory(const SmbNtCreateAndXResp * resp)923 inline bool SmbNtCreateAndXRespDirectory(const SmbNtCreateAndXResp* resp)
924 {
925     return (resp->smb_directory ? true : false);
926 }
927 
SmbNtCreateAndXRespResourceType(const SmbNtCreateAndXResp * resp)928 inline uint16_t SmbNtCreateAndXRespResourceType(const SmbNtCreateAndXResp* resp)
929 {
930     return snort::alignedNtohs(&resp->smb_resource_type);
931 }
932 
SmbNtCreateAndXRespEndOfFile(const SmbNtCreateAndXResp * resp)933 inline uint64_t SmbNtCreateAndXRespEndOfFile(const SmbNtCreateAndXResp* resp)
934 {
935     return snort::alignedNtohq(&resp->smb_eof);
936 }
937 
938 /********************************************************************
939  * SMB_COM_TRANSACTION
940  ********************************************************************/
941 struct SmbTransactionReq   /* smb_wct = 14 + value of smb_suwcnt */
942 {
943     /* Note all subcommands use a setup count of 2 */
944     uint8_t smb_wct;       /* count of 16-bit words that follow */
945     uint16_t smb_tpscnt;   /* total number of parameter bytes being sent */
946     uint16_t smb_tdscnt;   /* total number of data bytes being sent */
947     uint16_t smb_mprcnt;   /* max number of parameter bytes to return */
948     uint16_t smb_mdrcnt;   /* max number of data bytes to return */
949     uint8_t smb_msrcnt;    /* max number of setup words to return */
950     uint8_t smb_rsvd;      /* reserved (pad above to word) */
951     uint16_t smb_flags;    /* additional information:
952                               bit 0 - if set, also disconnect TID in smb_tid
953                               bit 1 - if set, transaction is one way (no final response) */
954     uint32_t smb_timeout;  /* number of milliseconds to wait for completion */
955     uint16_t smb_rsvd1;    /* reserved */
956     uint16_t smb_pscnt;    /* number of parameter bytes being sent this buffer */
957     uint16_t smb_psoff;    /* offset (from start of SMB hdr) to parameter bytes */
958     uint16_t smb_dscnt;    /* number of data bytes being sent this buffer */
959     uint16_t smb_dsoff;    /* offset (from start of SMB hdr) to data bytes */
960     uint8_t smb_suwcnt;    /* set up word count */
961     uint8_t smb_rsvd2;     /* reserved (pad above to word) */
962     uint16_t smb_setup1;   /* function (see below)
963                                 TRANS_SET_NM_PIPE_STATE   = 0x0001
964                                 TRANS_RAW_READ_NMPIPE     = 0x0011
965                                 TRANS_QUERY_NMPIPE_STATE  = 0x0021
966                                 TRANS_QUERY_NMPIPE_INFO   = 0x0022
967                                 TRANS_PEEK_NMPIPE         = 0x0023
968                                 TRANS_TRANSACT_NMPIPE     = 0x0026
969                                 TRANS_RAW_WRITE_NMPIPE    = 0x0031
970                                 TRANS_READ_NMPIPE         = 0x0036
971                                 TRANS_WRITE_NMPIPE        = 0x0037
972                                 TRANS_WAIT_NMPIPE         = 0x0053
973                                 TRANS_CALL_NMPIPE         = 0x0054  */
974     uint16_t smb_setup2;   /* FID (handle) of pipe (if needed), or priority */
975     uint16_t smb_bcc;      /* total bytes (including pad bytes) following */
976 };
977 
978 struct SmbTransactionInterimResp    /* smb_wct = 0 */
979 {
980     uint8_t smb_wct;        /* count of 16-bit words that follow */
981     uint16_t smb_bcc;       /* must be 0 */
982 };
983 
984 struct SmbTransactionResp   /* smb_wct = 10 + value of smb_suwcnt */
985 {
986     /* Note all subcommands use a setup count of 0 */
987     uint8_t smb_wct;       /* count of 16-bit words that follow */
988     uint16_t smb_tprcnt;   /* total number of parameter bytes being returned */
989     uint16_t smb_tdrcnt;   /* total number of data bytes being returned */
990     uint16_t smb_rsvd;     /* reserved */
991     uint16_t smb_prcnt;    /* number of parameter bytes being returned this buf */
992     uint16_t smb_proff;    /* offset (from start of SMB hdr) to parameter bytes */
993     uint16_t smb_prdisp;   /* byte displacement for these parameter bytes */
994     uint16_t smb_drcnt;    /* number of data bytes being returned this buffer */
995     uint16_t smb_droff;    /* offset (from start of SMB hdr) to data bytes */
996     uint16_t smb_drdisp;   /* byte displacement for these data bytes */
997     uint8_t smb_suwcnt;    /* set up return word count */
998     uint8_t smb_rsvd1;     /* reserved (pad above to word) */
999     uint16_t smb_bcc;      /* total bytes (including pad bytes) following */
1000 };
1001 
SmbTransactionReqSubCom(const SmbTransactionReq * req)1002 inline uint16_t SmbTransactionReqSubCom(const SmbTransactionReq* req)
1003 {
1004     return snort::alignedNtohs(&req->smb_setup1);
1005 }
1006 
SmbTransactionReqFid(const SmbTransactionReq * req)1007 inline uint16_t SmbTransactionReqFid(const SmbTransactionReq* req)
1008 {
1009     return snort::alignedNtohs(&req->smb_setup2);
1010 }
1011 
SmbTransactionReqDisconnectTid(const SmbTransactionReq * req)1012 inline bool SmbTransactionReqDisconnectTid(const SmbTransactionReq* req)
1013 {
1014     return (snort::alignedNtohs(&req->smb_flags) & 0x0001) ? true : false;
1015 }
1016 
SmbTransactionReqOneWay(const SmbTransactionReq * req)1017 inline bool SmbTransactionReqOneWay(const SmbTransactionReq* req)
1018 {
1019     return (snort::alignedNtohs(&req->smb_flags) & 0x0002) ? true : false;
1020 }
1021 
SmbTransactionReqSetupCnt(const SmbTransactionReq * req)1022 inline uint8_t SmbTransactionReqSetupCnt(const SmbTransactionReq* req)
1023 {
1024     return req->smb_suwcnt;
1025 }
1026 
SmbTransactionReqTotalDataCnt(const SmbTransactionReq * req)1027 inline uint16_t SmbTransactionReqTotalDataCnt(const SmbTransactionReq* req)
1028 {
1029     return snort::alignedNtohs(&req->smb_tdscnt);
1030 }
1031 
SmbTransactionReqDataCnt(const SmbTransactionReq * req)1032 inline uint16_t SmbTransactionReqDataCnt(const SmbTransactionReq* req)
1033 {
1034     return snort::alignedNtohs(&req->smb_dscnt);
1035 }
1036 
SmbTransactionReqDataOff(const SmbTransactionReq * req)1037 inline uint16_t SmbTransactionReqDataOff(const SmbTransactionReq* req)
1038 {
1039     return snort::alignedNtohs(&req->smb_dsoff);
1040 }
1041 
SmbTransactionReqTotalParamCnt(const SmbTransactionReq * req)1042 inline uint16_t SmbTransactionReqTotalParamCnt(const SmbTransactionReq* req)
1043 {
1044     return snort::alignedNtohs(&req->smb_tpscnt);
1045 }
1046 
SmbTransactionReqParamCnt(const SmbTransactionReq * req)1047 inline uint16_t SmbTransactionReqParamCnt(const SmbTransactionReq* req)
1048 {
1049     return snort::alignedNtohs(&req->smb_pscnt);
1050 }
1051 
SmbTransactionReqParamOff(const SmbTransactionReq * req)1052 inline uint16_t SmbTransactionReqParamOff(const SmbTransactionReq* req)
1053 {
1054     return snort::alignedNtohs(&req->smb_psoff);
1055 }
1056 
SmbTransactionRespTotalDataCnt(const SmbTransactionResp * resp)1057 inline uint16_t SmbTransactionRespTotalDataCnt(const SmbTransactionResp* resp)
1058 {
1059     return snort::alignedNtohs(&resp->smb_tdrcnt);
1060 }
1061 
SmbTransactionRespDataCnt(const SmbTransactionResp * resp)1062 inline uint16_t SmbTransactionRespDataCnt(const SmbTransactionResp* resp)
1063 {
1064     return snort::alignedNtohs(&resp->smb_drcnt);
1065 }
1066 
SmbTransactionRespDataOff(const SmbTransactionResp * resp)1067 inline uint16_t SmbTransactionRespDataOff(const SmbTransactionResp* resp)
1068 {
1069     return snort::alignedNtohs(&resp->smb_droff);
1070 }
1071 
SmbTransactionRespDataDisp(const SmbTransactionResp * resp)1072 inline uint16_t SmbTransactionRespDataDisp(const SmbTransactionResp* resp)
1073 {
1074     return snort::alignedNtohs(&resp->smb_drdisp);
1075 }
1076 
SmbTransactionRespTotalParamCnt(const SmbTransactionResp * resp)1077 inline uint16_t SmbTransactionRespTotalParamCnt(const SmbTransactionResp* resp)
1078 {
1079     return snort::alignedNtohs(&resp->smb_tprcnt);
1080 }
1081 
SmbTransactionRespParamCnt(const SmbTransactionResp * resp)1082 inline uint16_t SmbTransactionRespParamCnt(const SmbTransactionResp* resp)
1083 {
1084     return snort::alignedNtohs(&resp->smb_prcnt);
1085 }
1086 
SmbTransactionRespParamOff(const SmbTransactionResp * resp)1087 inline uint16_t SmbTransactionRespParamOff(const SmbTransactionResp* resp)
1088 {
1089     return snort::alignedNtohs(&resp->smb_proff);
1090 }
1091 
SmbTransactionRespParamDisp(const SmbTransactionResp * resp)1092 inline uint16_t SmbTransactionRespParamDisp(const SmbTransactionResp* resp)
1093 {
1094     return snort::alignedNtohs(&resp->smb_prdisp);
1095 }
1096 
1097 // Flags for TRANS_SET_NMPIPE_STATE parameters
1098 #define PIPE_STATE_NON_BLOCKING  0x8000
1099 #define PIPE_STATE_MESSAGE_MODE  0x0100
1100 
1101 /********************************************************************
1102  * SMB_COM_TRANSACTION2
1103  ********************************************************************/
1104 struct SmbTransaction2Req
1105 {
1106     uint8_t smb_wct;
1107     uint16_t smb_total_param_count;
1108     uint16_t smb_total_data_count;
1109     uint16_t smb_max_param_count;
1110     uint16_t smb_max_data_count;
1111     uint8_t smb_max_setup_count;
1112     uint8_t smb_res;
1113     uint16_t smb_flags;
1114     uint32_t smb_timeout;
1115     uint16_t smb_res2;
1116     uint16_t smb_param_count;
1117     uint16_t smb_param_offset;
1118     uint16_t smb_data_count;
1119     uint16_t smb_data_offset;
1120     uint8_t smb_setup_count;    /* Should be 1 for all subcommands */
1121     uint8_t smb_res3;
1122     uint16_t smb_setup;  /* This is the subcommand */
1123     uint16_t smb_bcc;
1124 };
1125 
1126 struct SmbTransaction2InterimResp
1127 {
1128     uint8_t smb_wct;
1129     uint16_t smb_bcc;
1130 };
1131 
1132 struct SmbTransaction2Resp
1133 {
1134     uint8_t smb_wct;
1135     uint16_t smb_total_param_count;
1136     uint16_t smb_total_data_count;
1137     uint16_t smb_res;
1138     uint16_t smb_param_count;
1139     uint16_t smb_param_offset;
1140     uint16_t smb_param_disp;
1141     uint16_t smb_data_count;
1142     uint16_t smb_data_offset;
1143     uint16_t smb_data_disp;
1144     uint16_t smb_setup_count;  /* 0 or 1 word */
1145     uint8_t smb_res2;
1146 };
1147 
SmbTransaction2ReqSubCom(const SmbTransaction2Req * req)1148 inline uint16_t SmbTransaction2ReqSubCom(const SmbTransaction2Req* req)
1149 {
1150     return snort::alignedNtohs(&req->smb_setup);
1151 }
1152 
SmbTransaction2ReqTotalParamCnt(const SmbTransaction2Req * req)1153 inline uint16_t SmbTransaction2ReqTotalParamCnt(const SmbTransaction2Req* req)
1154 {
1155     return snort::alignedNtohs(&req->smb_total_param_count);
1156 }
1157 
SmbTransaction2ReqParamCnt(const SmbTransaction2Req * req)1158 inline uint16_t SmbTransaction2ReqParamCnt(const SmbTransaction2Req* req)
1159 {
1160     return snort::alignedNtohs(&req->smb_param_count);
1161 }
1162 
SmbTransaction2ReqParamOff(const SmbTransaction2Req * req)1163 inline uint16_t SmbTransaction2ReqParamOff(const SmbTransaction2Req* req)
1164 {
1165     return snort::alignedNtohs(&req->smb_param_offset);
1166 }
1167 
SmbTransaction2ReqTotalDataCnt(const SmbTransaction2Req * req)1168 inline uint16_t SmbTransaction2ReqTotalDataCnt(const SmbTransaction2Req* req)
1169 {
1170     return snort::alignedNtohs(&req->smb_total_data_count);
1171 }
1172 
SmbTransaction2ReqDataCnt(const SmbTransaction2Req * req)1173 inline uint16_t SmbTransaction2ReqDataCnt(const SmbTransaction2Req* req)
1174 {
1175     return snort::alignedNtohs(&req->smb_data_count);
1176 }
1177 
SmbTransaction2ReqDataOff(const SmbTransaction2Req * req)1178 inline uint16_t SmbTransaction2ReqDataOff(const SmbTransaction2Req* req)
1179 {
1180     return snort::alignedNtohs(&req->smb_data_offset);
1181 }
1182 
SmbTransaction2ReqSetupCnt(const SmbTransaction2Req * req)1183 inline uint8_t SmbTransaction2ReqSetupCnt(const SmbTransaction2Req* req)
1184 {
1185     return req->smb_setup_count;
1186 }
1187 
SmbTransaction2RespTotalParamCnt(const SmbTransaction2Resp * resp)1188 inline uint16_t SmbTransaction2RespTotalParamCnt(const SmbTransaction2Resp* resp)
1189 {
1190     return snort::alignedNtohs(&resp->smb_total_param_count);
1191 }
1192 
SmbTransaction2RespParamCnt(const SmbTransaction2Resp * resp)1193 inline uint16_t SmbTransaction2RespParamCnt(const SmbTransaction2Resp* resp)
1194 {
1195     return snort::alignedNtohs(&resp->smb_param_count);
1196 }
1197 
SmbTransaction2RespParamOff(const SmbTransaction2Resp * resp)1198 inline uint16_t SmbTransaction2RespParamOff(const SmbTransaction2Resp* resp)
1199 {
1200     return snort::alignedNtohs(&resp->smb_param_offset);
1201 }
1202 
SmbTransaction2RespParamDisp(const SmbTransaction2Resp * resp)1203 inline uint16_t SmbTransaction2RespParamDisp(const SmbTransaction2Resp* resp)
1204 {
1205     return snort::alignedNtohs(&resp->smb_param_disp);
1206 }
1207 
SmbTransaction2RespTotalDataCnt(const SmbTransaction2Resp * resp)1208 inline uint16_t SmbTransaction2RespTotalDataCnt(const SmbTransaction2Resp* resp)
1209 {
1210     return snort::alignedNtohs(&resp->smb_total_data_count);
1211 }
1212 
SmbTransaction2RespDataCnt(const SmbTransaction2Resp * resp)1213 inline uint16_t SmbTransaction2RespDataCnt(const SmbTransaction2Resp* resp)
1214 {
1215     return snort::alignedNtohs(&resp->smb_data_count);
1216 }
1217 
SmbTransaction2RespDataOff(const SmbTransaction2Resp * resp)1218 inline uint16_t SmbTransaction2RespDataOff(const SmbTransaction2Resp* resp)
1219 {
1220     return snort::alignedNtohs(&resp->smb_data_offset);
1221 }
1222 
SmbTransaction2RespDataDisp(const SmbTransaction2Resp * resp)1223 inline uint16_t SmbTransaction2RespDataDisp(const SmbTransaction2Resp* resp)
1224 {
1225     return snort::alignedNtohs(&resp->smb_data_disp);
1226 }
1227 
1228 struct SmbTrans2Open2ReqParams
1229 {
1230     uint16_t Flags;
1231     uint16_t AccessMode;
1232     uint16_t Reserved1;
1233     uint16_t FileAttributes;
1234     uint32_t CreationTime;
1235     uint16_t OpenMode;
1236     uint32_t AllocationSize;
1237     uint16_t Reserved[5];
1238 };
1239 
1240 typedef SmbTransaction2Req SmbTrans2Open2Req;
1241 
SmbTrans2Open2ReqAccessMode(const SmbTrans2Open2ReqParams * req)1242 inline uint16_t SmbTrans2Open2ReqAccessMode(const SmbTrans2Open2ReqParams* req)
1243 {
1244     return snort::alignedNtohs(&req->AccessMode);
1245 }
1246 
SmbTrans2Open2ReqFileAttrs(const SmbTrans2Open2ReqParams * req)1247 inline uint16_t SmbTrans2Open2ReqFileAttrs(const SmbTrans2Open2ReqParams* req)
1248 {
1249     return snort::alignedNtohs(&req->FileAttributes);
1250 }
1251 
SmbTrans2Open2ReqOpenMode(const SmbTrans2Open2ReqParams * req)1252 inline uint16_t SmbTrans2Open2ReqOpenMode(const SmbTrans2Open2ReqParams* req)
1253 {
1254     return snort::alignedNtohs(&req->OpenMode);
1255 }
1256 
SmbTrans2Open2ReqAllocSize(const SmbTrans2Open2ReqParams * req)1257 inline uint32_t SmbTrans2Open2ReqAllocSize(const SmbTrans2Open2ReqParams* req)
1258 {
1259     return snort::alignedNtohl(&req->AllocationSize);
1260 }
1261 
1262 struct SmbTrans2Open2RespParams
1263 {
1264     uint16_t smb_fid;
1265     uint16_t file_attributes;
1266     uint32_t creation_time;
1267     uint32_t file_data_size;
1268     uint16_t access_mode;
1269     uint16_t resource_type;
1270     uint16_t nm_pipe_status;
1271     uint16_t action_taken;
1272     uint32_t reserved;
1273     uint16_t extended_attribute_error_offset;
1274     uint32_t extended_attribute_length;
1275 };
1276 
SmbTrans2Open2RespFid(const SmbTrans2Open2RespParams * resp)1277 inline uint16_t SmbTrans2Open2RespFid(const SmbTrans2Open2RespParams* resp)
1278 {
1279     return snort::alignedNtohs(&resp->smb_fid);
1280 }
1281 
SmbTrans2Open2RespFileAttrs(const SmbTrans2Open2RespParams * resp)1282 inline uint16_t SmbTrans2Open2RespFileAttrs(const SmbTrans2Open2RespParams* resp)
1283 {
1284     return snort::alignedNtohs(&resp->file_attributes);
1285 }
1286 
SmbTrans2Open2RespFileDataSize(const SmbTrans2Open2RespParams * resp)1287 inline uint32_t SmbTrans2Open2RespFileDataSize(const SmbTrans2Open2RespParams* resp)
1288 {
1289     return snort::alignedNtohl(&resp->file_data_size);
1290 }
1291 
SmbTrans2Open2RespResourceType(const SmbTrans2Open2RespParams * resp)1292 inline uint16_t SmbTrans2Open2RespResourceType(const SmbTrans2Open2RespParams* resp)
1293 {
1294     return snort::alignedNtohs(&resp->resource_type);
1295 }
1296 
SmbTrans2Open2RespActionTaken(const SmbTrans2Open2RespParams * resp)1297 inline uint16_t SmbTrans2Open2RespActionTaken(const SmbTrans2Open2RespParams* resp)
1298 {
1299     return snort::alignedNtohs(&resp->action_taken);
1300 }
1301 
1302 struct SmbTrans2Open2Resp
1303 {
1304     uint8_t smb_wct;
1305     uint16_t smb_total_param_count;
1306     uint16_t smb_total_data_count;
1307     uint16_t smb_res;
1308     uint16_t smb_param_count;
1309     uint16_t smb_param_offset;
1310     uint16_t smb_param_disp;
1311     uint16_t smb_data_count;
1312     uint16_t smb_data_offset;
1313     uint16_t smb_data_disp;
1314     uint16_t smb_setup_count;  /* 0 */
1315     uint8_t smb_res2;
1316     uint16_t smb_bcc;
1317 };
1318 
1319 // See MS-CIFS Section 2.2.2.3.3
1320 #define SMB_INFO_STANDARD               0x0001
1321 #define SMB_INFO_QUERY_EA_SIZE          0x0002
1322 #define SMB_INFO_QUERY_EAS_FROM_LIST    0x0003
1323 #define SMB_INFO_QUERY_ALL_EAS          0x0004
1324 #define SMB_INFO_IS_NAME_VALID          0x0006
1325 #define SMB_QUERY_FILE_BASIC_INFO       0x0101
1326 #define SMB_QUERY_FILE_STANDARD_INFO    0x0102
1327 #define SMB_QUERY_FILE_EA_INFO          0x0103
1328 #define SMB_QUERY_FILE_NAME_INFO        0x0104
1329 #define SMB_QUERY_FILE_ALL_INFO         0x0107
1330 #define SMB_QUERY_FILE_ALT_NAME_INFO    0x0108
1331 #define SMB_QUERY_FILE_STREAM_INFO      0x0109
1332 #define SMB_QUERY_FILE_COMPRESSION_INFO 0x010b
1333 
1334 // See MS-SMB Section 2.2.2.3.5
1335 // For added value, see below from MS-FSCC
1336 #define SMB_INFO_PASSTHROUGH  0x03e8
1337 #define SMB_INFO_PT_FILE_STANDARD_INFO  (SMB_INFO_PASSTHROUGH+5)
1338 #define SMB_INFO_PT_FILE_ALL_INFO       (SMB_INFO_PASSTHROUGH+18)
1339 #define SMB_INFO_PT_FILE_STREAM_INFO    (SMB_INFO_PASSTHROUGH+22)
1340 #define SMB_INFO_PT_NETWORK_OPEN_INFO   (SMB_INFO_PASSTHROUGH+34)
1341 
1342 struct SmbTrans2QueryFileInfoReqParams
1343 {
1344     uint16_t fid;
1345     uint16_t information_level;
1346 };
1347 
SmbTrans2QueryFileInfoReqFid(const SmbTrans2QueryFileInfoReqParams * req)1348 inline uint16_t SmbTrans2QueryFileInfoReqFid(const SmbTrans2QueryFileInfoReqParams* req)
1349 {
1350     return snort::alignedNtohs(&req->fid);
1351 }
1352 
SmbTrans2QueryFileInfoReqInfoLevel(const SmbTrans2QueryFileInfoReqParams * req)1353 inline uint16_t SmbTrans2QueryFileInfoReqInfoLevel(const SmbTrans2QueryFileInfoReqParams* req)
1354 {
1355     return snort::alignedNtohs(&req->information_level);
1356 }
1357 
1358 struct SmbQueryInfoStandard
1359 {
1360     uint16_t CreationDate;
1361     uint16_t CreationTime;
1362     uint16_t LastAccessDate;
1363     uint16_t LastAccessTime;
1364     uint16_t LastWriteDate;
1365     uint16_t LastWriteTime;
1366     uint32_t FileDataSize;
1367     uint32_t AllocationSize;
1368     uint16_t Attributes;
1369 };
1370 
SmbQueryInfoStandardFileDataSize(const SmbQueryInfoStandard * q)1371 inline uint32_t SmbQueryInfoStandardFileDataSize(const SmbQueryInfoStandard* q)
1372 {
1373     return snort::alignedNtohl(&q->FileDataSize);
1374 }
1375 
1376 struct SmbQueryInfoQueryEaSize
1377 {
1378     uint16_t CreationDate;
1379     uint16_t CreationTime;
1380     uint16_t LastAccessDate;
1381     uint16_t LastAccessTime;
1382     uint16_t LastWriteDate;
1383     uint16_t LastWriteTime;
1384     uint32_t FileDataSize;
1385     uint32_t AllocationSize;
1386     uint16_t Attributes;
1387     uint32_t EaSize;
1388 };
1389 
SmbQueryInfoQueryEaSizeFileDataSize(const SmbQueryInfoQueryEaSize * q)1390 inline uint32_t SmbQueryInfoQueryEaSizeFileDataSize(const SmbQueryInfoQueryEaSize* q)
1391 {
1392     return snort::alignedNtohl(&q->FileDataSize);
1393 }
1394 
1395 struct SmbQueryFileStandardInfo
1396 {
1397     uint64_t AllocationSize;
1398     uint64_t EndOfFile;
1399     uint32_t NumberOfLinks;
1400     uint8_t DeletePending;
1401     uint8_t Directory;
1402     uint16_t Reserved;
1403 };
1404 
SmbQueryFileStandardInfoEndOfFile(const SmbQueryFileStandardInfo * q)1405 inline uint64_t SmbQueryFileStandardInfoEndOfFile(const SmbQueryFileStandardInfo* q)
1406 {
1407     return snort::alignedNtohq(&q->EndOfFile);
1408 }
1409 
1410 struct SmbQueryFileAllInfo
1411 {
1412     // Basic Info
1413     uint64_t CreationTime;
1414     uint64_t LastAccessTime;
1415     uint64_t LastWriteTime;
1416     uint64_t LastChangeTime;
1417     uint32_t ExtFileAttributes;
1418     uint32_t Reserved1;
1419     uint64_t AllocationSize;
1420     uint64_t EndOfFile;
1421     uint32_t NumberOfLinks;
1422     uint8_t DeletePending;
1423     uint8_t Directory;
1424     uint16_t Reserved2;
1425     uint32_t EaSize;
1426     uint32_t FileNameLength;
1427 };
1428 
SmbQueryFileAllInfoEndOfFile(const SmbQueryFileAllInfo * q)1429 inline uint64_t SmbQueryFileAllInfoEndOfFile(const SmbQueryFileAllInfo* q)
1430 {
1431     return snort::alignedNtohq(&q->EndOfFile);
1432 }
1433 
1434 struct SmbQueryPTFileAllInfo
1435 {
1436     // Basic Info
1437     uint64_t CreationTime;
1438     uint64_t LastAccessTime;
1439     uint64_t LastWriteTime;
1440     uint64_t LastChangeTime;
1441     uint32_t ExtFileAttributes;
1442     uint32_t Reserved1;
1443 
1444     // Standard Info
1445     uint64_t AllocationSize;
1446     uint64_t EndOfFile;
1447     uint32_t NumberOfLinks;
1448     uint8_t DeletePending;
1449     uint8_t Directory;
1450     uint16_t Reserved2;
1451 
1452     // Internal Info
1453     uint64_t IndexNumber;
1454 
1455     // EA Info
1456     uint32_t EaSize;
1457 
1458     // Access Info
1459     uint32_t AccessFlags;
1460 
1461     // Position Info
1462     uint64_t CurrentByteOffset;
1463 
1464     // Mode Info
1465     uint32_t Mode;
1466 
1467     // Alignment Info
1468     uint32_t AlignmentRequirement;
1469 
1470     // Name Info
1471     uint32_t FileNameLength;
1472 };
1473 
SmbQueryPTFileAllInfoEndOfFile(const SmbQueryPTFileAllInfo * q)1474 inline uint64_t SmbQueryPTFileAllInfoEndOfFile(const SmbQueryPTFileAllInfo* q)
1475 {
1476     return snort::alignedNtohq(&q->EndOfFile);
1477 }
1478 
1479 struct SmbQueryPTNetworkOpenInfo
1480 {
1481     uint64_t CreationTime;
1482     uint64_t LastAccessTime;
1483     uint64_t LastWriteTime;
1484     uint64_t LastChangeTime;
1485     uint64_t AllocationSize;
1486     uint64_t EndOfFile;
1487     uint32_t FileAttributes;
1488     uint32_t Reserved;
1489 };
1490 
SmbQueryPTNetworkOpenInfoEndOfFile(const SmbQueryPTNetworkOpenInfo * q)1491 inline uint64_t SmbQueryPTNetworkOpenInfoEndOfFile(const SmbQueryPTNetworkOpenInfo* q)
1492 {
1493     return snort::alignedNtohq(&q->EndOfFile);
1494 }
1495 
1496 struct SmbQueryPTFileStreamInfo
1497 {
1498     uint32_t NextEntryOffset;
1499     uint32_t StreamNameLength;
1500     uint64_t StreamSize;
1501     uint64_t StreamAllocationSize;
1502 };
1503 
SmbQueryPTFileStreamInfoStreamSize(const SmbQueryPTFileStreamInfo * q)1504 inline uint64_t SmbQueryPTFileStreamInfoStreamSize(const SmbQueryPTFileStreamInfo* q)
1505 {
1506     return snort::alignedNtohq(&q->StreamSize);
1507 }
1508 
1509 struct SmbTrans2QueryFileInformationResp
1510 {
1511     uint8_t smb_wct;
1512     uint16_t smb_total_param_count;
1513     uint16_t smb_total_data_count;
1514     uint16_t smb_res;
1515     uint16_t smb_param_count;
1516     uint16_t smb_param_offset;
1517     uint16_t smb_param_disp;
1518     uint16_t smb_data_count;
1519     uint16_t smb_data_offset;
1520     uint16_t smb_data_disp;
1521     uint16_t smb_setup_count;  /* 0 */
1522     uint8_t smb_res2;
1523     uint16_t smb_bcc;
1524 };
1525 
1526 #define SMB_INFO_SET_EAS               0x0002
1527 #define SMB_SET_FILE_BASIC_INFO        0x0101
1528 #define SMB_SET_FILE_DISPOSITION_INFO  0x0102
1529 #define SMB_SET_FILE_ALLOCATION_INFO   0x0103
1530 #define SMB_SET_FILE_END_OF_FILE_INFO  0x0104
1531 
1532 // For added value, see above File Information Classes
1533 #define SMB_INFO_PT_SET_FILE_BASIC_FILE_INFO   (SMB_INFO_PASSTHROUGH+4)
1534 #define SMB_INFO_PT_SET_FILE_END_OF_FILE_INFO  (SMB_INFO_PASSTHROUGH+20)
1535 
1536 struct SmbTrans2SetFileInfoReqParams
1537 {
1538     uint16_t fid;
1539     uint16_t information_level;
1540     uint16_t reserved;
1541 };
1542 
SmbTrans2SetFileInfoReqFid(const SmbTrans2SetFileInfoReqParams * req)1543 inline uint16_t SmbTrans2SetFileInfoReqFid(const SmbTrans2SetFileInfoReqParams* req)
1544 {
1545     return snort::alignedNtohs(&req->fid);
1546 }
1547 
SmbTrans2SetFileInfoReqInfoLevel(const SmbTrans2SetFileInfoReqParams * req)1548 inline uint16_t SmbTrans2SetFileInfoReqInfoLevel(const SmbTrans2SetFileInfoReqParams* req)
1549 {
1550     return snort::alignedNtohs(&req->information_level);
1551 }
1552 
SmbSetFileInfoEndOfFile(const uint16_t info_level)1553 inline bool SmbSetFileInfoEndOfFile(const uint16_t info_level)
1554 {
1555     return ((info_level == SMB_SET_FILE_END_OF_FILE_INFO)
1556            || (info_level == SMB_INFO_PT_SET_FILE_END_OF_FILE_INFO));
1557 }
1558 
1559 struct SmbSetFileBasicInfo
1560 {
1561     uint64_t CreationTime;
1562     uint64_t LastAccessTime;
1563     uint64_t LastWriteTime;
1564     uint64_t ChangeTime;
1565     uint32_t ExtFileAttributes;
1566     uint32_t Reserved;
1567 };
1568 
SmbSetFileInfoExtFileAttrs(const SmbSetFileBasicInfo * info)1569 inline uint32_t SmbSetFileInfoExtFileAttrs(const SmbSetFileBasicInfo* info)
1570 {
1571     return snort::alignedNtohl(&info->ExtFileAttributes);
1572 }
1573 
SmbSetFileInfoSetFileBasicInfo(const uint16_t info_level)1574 inline bool SmbSetFileInfoSetFileBasicInfo(const uint16_t info_level)
1575 {
1576     return ((info_level == SMB_SET_FILE_BASIC_INFO)
1577            || (info_level == SMB_INFO_PT_SET_FILE_BASIC_FILE_INFO));
1578 }
1579 
1580 /********************************************************************
1581  * SMB_COM_NT_TRANSACT
1582  ********************************************************************/
1583 #define SMB_CREATE_OPTIONS__FILE_SEQUENTIAL_ONLY     0x00000004
1584 
1585 struct SmbNtTransactReq
1586 {
1587     uint8_t smb_wct;
1588     uint8_t smb_max_setup_count;
1589     uint16_t smb_res;
1590     uint32_t smb_total_param_count;
1591     uint32_t smb_total_data_count;
1592     uint32_t smb_max_param_count;
1593     uint32_t smb_max_data_count;
1594     uint32_t smb_param_count;
1595     uint32_t smb_param_offset;
1596     uint32_t smb_data_count;
1597     uint32_t smb_data_offset;
1598     uint8_t smb_setup_count;
1599     uint16_t smb_function;
1600 };
1601 
1602 struct SmbNtTransactInterimResp
1603 {
1604     uint8_t smb_wct;
1605     uint16_t smb_bcc;
1606 };
1607 
1608 struct SmbNtTransactResp
1609 {
1610     uint8_t smb_wct;
1611     uint8_t smb_res[3];
1612     uint32_t smb_total_param_count;
1613     uint32_t smb_total_data_count;
1614     uint32_t smb_param_count;
1615     uint32_t smb_param_offset;
1616     uint32_t smb_param_disp;
1617     uint32_t smb_data_count;
1618     uint32_t smb_data_offset;
1619     uint32_t smb_data_disp;
1620     uint8_t smb_setup_count;
1621 };
1622 
SmbNtTransactReqSubCom(const SmbNtTransactReq * req)1623 inline uint16_t SmbNtTransactReqSubCom(const SmbNtTransactReq* req)
1624 {
1625     return snort::alignedNtohs(&req->smb_function);
1626 }
1627 
SmbNtTransactReqSetupCnt(const SmbNtTransactReq * req)1628 inline uint8_t SmbNtTransactReqSetupCnt(const SmbNtTransactReq* req)
1629 {
1630     return req->smb_setup_count;
1631 }
1632 
SmbNtTransactReqTotalParamCnt(const SmbNtTransactReq * req)1633 inline uint32_t SmbNtTransactReqTotalParamCnt(const SmbNtTransactReq* req)
1634 {
1635     return snort::alignedNtohl(&req->smb_total_param_count);
1636 }
1637 
SmbNtTransactReqParamCnt(const SmbNtTransactReq * req)1638 inline uint32_t SmbNtTransactReqParamCnt(const SmbNtTransactReq* req)
1639 {
1640     return snort::alignedNtohl(&req->smb_param_count);
1641 }
1642 
SmbNtTransactReqParamOff(const SmbNtTransactReq * req)1643 inline uint32_t SmbNtTransactReqParamOff(const SmbNtTransactReq* req)
1644 {
1645     return snort::alignedNtohl(&req->smb_param_offset);
1646 }
1647 
SmbNtTransactReqTotalDataCnt(const SmbNtTransactReq * req)1648 inline uint32_t SmbNtTransactReqTotalDataCnt(const SmbNtTransactReq* req)
1649 {
1650     return snort::alignedNtohl(&req->smb_total_data_count);
1651 }
1652 
SmbNtTransactReqDataCnt(const SmbNtTransactReq * req)1653 inline uint32_t SmbNtTransactReqDataCnt(const SmbNtTransactReq* req)
1654 {
1655     return snort::alignedNtohl(&req->smb_data_count);
1656 }
1657 
SmbNtTransactReqDataOff(const SmbNtTransactReq * req)1658 inline uint32_t SmbNtTransactReqDataOff(const SmbNtTransactReq* req)
1659 {
1660     return snort::alignedNtohl(&req->smb_data_offset);
1661 }
1662 
SmbNtTransactRespTotalParamCnt(const SmbNtTransactResp * resp)1663 inline uint32_t SmbNtTransactRespTotalParamCnt(const SmbNtTransactResp* resp)
1664 {
1665     return snort::alignedNtohl(&resp->smb_total_param_count);
1666 }
1667 
SmbNtTransactRespParamCnt(const SmbNtTransactResp * resp)1668 inline uint32_t SmbNtTransactRespParamCnt(const SmbNtTransactResp* resp)
1669 {
1670     return snort::alignedNtohl(&resp->smb_param_count);
1671 }
1672 
SmbNtTransactRespParamOff(const SmbNtTransactResp * resp)1673 inline uint32_t SmbNtTransactRespParamOff(const SmbNtTransactResp* resp)
1674 {
1675     return snort::alignedNtohl(&resp->smb_param_offset);
1676 }
1677 
SmbNtTransactRespParamDisp(const SmbNtTransactResp * resp)1678 inline uint32_t SmbNtTransactRespParamDisp(const SmbNtTransactResp* resp)
1679 {
1680     return snort::alignedNtohl(&resp->smb_param_disp);
1681 }
1682 
SmbNtTransactRespTotalDataCnt(const SmbNtTransactResp * resp)1683 inline uint32_t SmbNtTransactRespTotalDataCnt(const SmbNtTransactResp* resp)
1684 {
1685     return snort::alignedNtohl(&resp->smb_total_data_count);
1686 }
1687 
SmbNtTransactRespDataCnt(const SmbNtTransactResp * resp)1688 inline uint32_t SmbNtTransactRespDataCnt(const SmbNtTransactResp* resp)
1689 {
1690     return snort::alignedNtohl(&resp->smb_data_count);
1691 }
1692 
SmbNtTransactRespDataOff(const SmbNtTransactResp * resp)1693 inline uint32_t SmbNtTransactRespDataOff(const SmbNtTransactResp* resp)
1694 {
1695     return snort::alignedNtohl(&resp->smb_data_offset);
1696 }
1697 
SmbNtTransactRespDataDisp(const SmbNtTransactResp * resp)1698 inline uint32_t SmbNtTransactRespDataDisp(const SmbNtTransactResp* resp)
1699 {
1700     return snort::alignedNtohl(&resp->smb_data_disp);
1701 }
1702 
1703 struct SmbNtTransactCreateReqParams
1704 {
1705     uint32_t flags;
1706     uint32_t root_dir_fid;
1707     uint32_t desired_access;
1708     uint64_t allocation_size;
1709     uint32_t ext_file_attributes;
1710     uint32_t share_access;
1711     uint32_t create_disposition;
1712     uint32_t create_options;
1713     uint32_t security_descriptor_length;
1714     uint32_t ea_length;
1715     uint32_t name_length;
1716     uint32_t impersonation_level;
1717     uint8_t security_flags;
1718 };
1719 
SmbNtTransactCreateReqAllocSize(const SmbNtTransactCreateReqParams * req)1720 inline uint64_t SmbNtTransactCreateReqAllocSize(const SmbNtTransactCreateReqParams* req)
1721 {
1722     return snort::alignedNtohq(&req->allocation_size);
1723 }
1724 
SmbNtTransactCreateReqFileNameLength(const SmbNtTransactCreateReqParams * req)1725 inline uint32_t SmbNtTransactCreateReqFileNameLength(const SmbNtTransactCreateReqParams* req)
1726 {
1727     return snort::alignedNtohl(&req->name_length);
1728 }
1729 
SmbNtTransactCreateReqFileAttrs(const SmbNtTransactCreateReqParams * req)1730 inline uint32_t SmbNtTransactCreateReqFileAttrs(const SmbNtTransactCreateReqParams* req)
1731 {
1732     return snort::alignedNtohl(&req->ext_file_attributes);
1733 }
1734 
SmbNtTransactCreateReqSequentialOnly(const SmbNtTransactCreateReqParams * req)1735 inline bool SmbNtTransactCreateReqSequentialOnly(const SmbNtTransactCreateReqParams* req)
1736 {
1737     return ((snort::alignedNtohl(&req->create_options) & SMB_CREATE_OPTIONS__FILE_SEQUENTIAL_ONLY) != 0);
1738 }
1739 
1740 struct SmbNtTransactCreateReq
1741 {
1742     uint8_t smb_wct;
1743     uint8_t smb_max_setup_count;
1744     uint16_t smb_res;
1745     uint32_t smb_total_param_count;
1746     uint32_t smb_total_data_count;
1747     uint32_t smb_max_param_count;
1748     uint32_t smb_max_data_count;
1749     uint32_t smb_param_count;
1750     uint32_t smb_param_offset;
1751     uint32_t smb_data_count;
1752     uint32_t smb_data_offset;
1753     uint8_t smb_setup_count;    /* Must be 0x00 */
1754     uint16_t smb_function;      /* NT_TRANSACT_CREATE */
1755     uint16_t smb_bcc;
1756 };
1757 
1758 struct SmbNtTransactCreateRespParams
1759 {
1760     uint8_t op_lock_level;
1761     uint8_t reserved;
1762     uint16_t smb_fid;
1763     uint32_t create_action;
1764     uint32_t ea_error_offset;
1765     uint64_t creation_time;
1766     uint64_t last_access_time;
1767     uint64_t last_write_time;
1768     uint64_t last_change_time;
1769     uint32_t ext_file_attributes;
1770     uint64_t allocation_size;
1771     uint64_t end_of_file;
1772     uint16_t resource_type;
1773     uint16_t nm_pipe_status;
1774     uint8_t directory;
1775 };
1776 
SmbNtTransactCreateRespFid(const SmbNtTransactCreateRespParams * resp)1777 inline uint16_t SmbNtTransactCreateRespFid(const SmbNtTransactCreateRespParams* resp)
1778 {
1779     return snort::alignedNtohs(&resp->smb_fid);
1780 }
1781 
SmbNtTransactCreateRespCreateAction(const SmbNtTransactCreateRespParams * resp)1782 inline uint32_t SmbNtTransactCreateRespCreateAction(const SmbNtTransactCreateRespParams* resp)
1783 {
1784     return snort::alignedNtohl(&resp->create_action);
1785 }
1786 
SmbNtTransactCreateRespEndOfFile(const SmbNtTransactCreateRespParams * resp)1787 inline uint64_t SmbNtTransactCreateRespEndOfFile(const SmbNtTransactCreateRespParams* resp)
1788 {
1789     return snort::alignedNtohq(&resp->end_of_file);
1790 }
1791 
SmbNtTransactCreateRespResourceType(const SmbNtTransactCreateRespParams * resp)1792 inline uint16_t SmbNtTransactCreateRespResourceType(const SmbNtTransactCreateRespParams* resp)
1793 {
1794     return snort::alignedNtohs(&resp->resource_type);
1795 }
1796 
SmbNtTransactCreateRespDirectory(const SmbNtTransactCreateRespParams * resp)1797 inline bool SmbNtTransactCreateRespDirectory(const SmbNtTransactCreateRespParams* resp)
1798 {
1799     return (resp->directory ? true : false);
1800 }
1801 
1802 struct SmbNtTransactCreateResp
1803 {
1804     uint8_t smb_wct;
1805     uint8_t smb_res[3];
1806     uint32_t smb_total_param_count;
1807     uint32_t smb_total_data_count;
1808     uint32_t smb_param_count;
1809     uint32_t smb_param_offset;
1810     uint32_t smb_param_disp;
1811     uint32_t smb_data_count;
1812     uint32_t smb_data_offset;
1813     uint32_t smb_data_disp;
1814     uint8_t smb_setup_count;    /* 0x00 */
1815     uint16_t smb_bcc;
1816 };
1817 
1818 /********************************************************************
1819  * SMB_COM_TRANSACTION_SECONDARY
1820  *  Continuation command for SMB_COM_TRANSACTION requests if all
1821  *  data wasn't sent.
1822  ********************************************************************/
1823 struct SmbTransactionSecondaryReq   /* smb_wct = 8 */
1824 {
1825     uint8_t smb_wct;       /* count of 16-bit words that follow */
1826     uint16_t smb_tpscnt;   /* total number of parameter bytes being sent */
1827     uint16_t smb_tdscnt;   /* total number of data bytes being sent */
1828     uint16_t smb_pscnt;    /* number of parameter bytes being sent this buffer */
1829     uint16_t smb_psoff;    /* offset (from start of SMB hdr) to parameter bytes */
1830     uint16_t smb_psdisp;   /* byte displacement for these parameter bytes */
1831     uint16_t smb_dscnt;    /* number of data bytes being sent this buffer */
1832     uint16_t smb_dsoff;    /* offset (from start of SMB hdr) to data bytes */
1833     uint16_t smb_dsdisp;   /* byte displacement for these data bytes */
1834     uint16_t smb_bcc;      /* total bytes (including pad bytes) following */
1835 };
1836 
SmbTransactionSecondaryReqTotalDataCnt(const SmbTransactionSecondaryReq * req)1837 inline uint16_t SmbTransactionSecondaryReqTotalDataCnt(const SmbTransactionSecondaryReq* req)
1838 {
1839     return snort::alignedNtohs(&req->smb_tdscnt);
1840 }
1841 
SmbTransactionSecondaryReqDataCnt(const SmbTransactionSecondaryReq * req)1842 inline uint16_t SmbTransactionSecondaryReqDataCnt(const SmbTransactionSecondaryReq* req)
1843 {
1844     return snort::alignedNtohs(&req->smb_dscnt);
1845 }
1846 
SmbTransactionSecondaryReqDataOff(const SmbTransactionSecondaryReq * req)1847 inline uint16_t SmbTransactionSecondaryReqDataOff(const SmbTransactionSecondaryReq* req)
1848 {
1849     return snort::alignedNtohs(&req->smb_dsoff);
1850 }
1851 
SmbTransactionSecondaryReqDataDisp(const SmbTransactionSecondaryReq * req)1852 inline uint16_t SmbTransactionSecondaryReqDataDisp(const SmbTransactionSecondaryReq* req)
1853 {
1854     return snort::alignedNtohs(&req->smb_dsdisp);
1855 }
1856 
SmbTransactionSecondaryReqTotalParamCnt(const SmbTransactionSecondaryReq * req)1857 inline uint16_t SmbTransactionSecondaryReqTotalParamCnt(const SmbTransactionSecondaryReq* req)
1858 {
1859     return snort::alignedNtohs(&req->smb_tpscnt);
1860 }
1861 
SmbTransactionSecondaryReqParamCnt(const SmbTransactionSecondaryReq * req)1862 inline uint16_t SmbTransactionSecondaryReqParamCnt(const SmbTransactionSecondaryReq* req)
1863 {
1864     return snort::alignedNtohs(&req->smb_pscnt);
1865 }
1866 
SmbTransactionSecondaryReqParamOff(const SmbTransactionSecondaryReq * req)1867 inline uint16_t SmbTransactionSecondaryReqParamOff(const SmbTransactionSecondaryReq* req)
1868 {
1869     return snort::alignedNtohs(&req->smb_psoff);
1870 }
1871 
SmbTransactionSecondaryReqParamDisp(const SmbTransactionSecondaryReq * req)1872 inline uint16_t SmbTransactionSecondaryReqParamDisp(const SmbTransactionSecondaryReq* req)
1873 {
1874     return snort::alignedNtohs(&req->smb_psdisp);
1875 }
1876 
1877 /********************************************************************
1878  * SMB_COM_TRANSACTION2_SECONDARY
1879  *  Continuation command for SMB_COM_TRANSACTION2 requests if all
1880  *  data wasn't sent.
1881  ********************************************************************/
1882 struct SmbTransaction2SecondaryReq
1883 {
1884     uint8_t smb_wct;
1885     uint16_t smb_total_param_count;
1886     uint16_t smb_total_data_count;
1887     uint16_t smb_param_count;
1888     uint16_t smb_param_offset;
1889     uint16_t smb_param_disp;
1890     uint16_t smb_data_count;
1891     uint16_t smb_data_offset;
1892     uint16_t smb_data_disp;
1893     uint16_t smb_fid;
1894     uint16_t smb_bcc;
1895 };
1896 
SmbTransaction2SecondaryReqTotalParamCnt(const SmbTransaction2SecondaryReq * req)1897 inline uint16_t SmbTransaction2SecondaryReqTotalParamCnt(const SmbTransaction2SecondaryReq* req)
1898 {
1899     return snort::alignedNtohs(&req->smb_total_param_count);
1900 }
1901 
SmbTransaction2SecondaryReqParamCnt(const SmbTransaction2SecondaryReq * req)1902 inline uint16_t SmbTransaction2SecondaryReqParamCnt(const SmbTransaction2SecondaryReq* req)
1903 {
1904     return snort::alignedNtohs(&req->smb_param_count);
1905 }
1906 
SmbTransaction2SecondaryReqParamOff(const SmbTransaction2SecondaryReq * req)1907 inline uint16_t SmbTransaction2SecondaryReqParamOff(const SmbTransaction2SecondaryReq* req)
1908 {
1909     return snort::alignedNtohs(&req->smb_param_offset);
1910 }
1911 
SmbTransaction2SecondaryReqParamDisp(const SmbTransaction2SecondaryReq * req)1912 inline uint16_t SmbTransaction2SecondaryReqParamDisp(const SmbTransaction2SecondaryReq* req)
1913 {
1914     return snort::alignedNtohs(&req->smb_param_disp);
1915 }
1916 
SmbTransaction2SecondaryReqTotalDataCnt(const SmbTransaction2SecondaryReq * req)1917 inline uint16_t SmbTransaction2SecondaryReqTotalDataCnt(const SmbTransaction2SecondaryReq* req)
1918 {
1919     return snort::alignedNtohs(&req->smb_total_data_count);
1920 }
1921 
SmbTransaction2SecondaryReqDataCnt(const SmbTransaction2SecondaryReq * req)1922 inline uint16_t SmbTransaction2SecondaryReqDataCnt(const SmbTransaction2SecondaryReq* req)
1923 {
1924     return snort::alignedNtohs(&req->smb_data_count);
1925 }
1926 
SmbTransaction2SecondaryReqDataOff(const SmbTransaction2SecondaryReq * req)1927 inline uint16_t SmbTransaction2SecondaryReqDataOff(const SmbTransaction2SecondaryReq* req)
1928 {
1929     return snort::alignedNtohs(&req->smb_data_offset);
1930 }
1931 
SmbTransaction2SecondaryReqDataDisp(const SmbTransaction2SecondaryReq * req)1932 inline uint16_t SmbTransaction2SecondaryReqDataDisp(const SmbTransaction2SecondaryReq* req)
1933 {
1934     return snort::alignedNtohs(&req->smb_data_disp);
1935 }
1936 
1937 /********************************************************************
1938  * SMB_COM_NT_TRANSACT_SECONDARY
1939  ********************************************************************/
1940 struct SmbNtTransactSecondaryReq
1941 {
1942     uint8_t smb_wct;
1943     uint8_t smb_res[3];
1944     uint32_t smb_total_param_count;
1945     uint32_t smb_total_data_count;
1946     uint32_t smb_param_count;
1947     uint32_t smb_param_offset;
1948     uint32_t smb_param_disp;
1949     uint32_t smb_data_count;
1950     uint32_t smb_data_offset;
1951     uint32_t smb_data_disp;
1952     uint8_t smb_res2;
1953 };
1954 
SmbNtTransactSecondaryReqTotalParamCnt(const SmbNtTransactSecondaryReq * req)1955 inline uint32_t SmbNtTransactSecondaryReqTotalParamCnt(const SmbNtTransactSecondaryReq* req)
1956 {
1957     return snort::alignedNtohl(&req->smb_total_param_count);
1958 }
1959 
SmbNtTransactSecondaryReqParamCnt(const SmbNtTransactSecondaryReq * req)1960 inline uint32_t SmbNtTransactSecondaryReqParamCnt(const SmbNtTransactSecondaryReq* req)
1961 {
1962     return snort::alignedNtohl(&req->smb_param_count);
1963 }
1964 
SmbNtTransactSecondaryReqParamOff(const SmbNtTransactSecondaryReq * req)1965 inline uint32_t SmbNtTransactSecondaryReqParamOff(const SmbNtTransactSecondaryReq* req)
1966 {
1967     return snort::alignedNtohl(&req->smb_param_offset);
1968 }
1969 
SmbNtTransactSecondaryReqParamDisp(const SmbNtTransactSecondaryReq * req)1970 inline uint32_t SmbNtTransactSecondaryReqParamDisp(const SmbNtTransactSecondaryReq* req)
1971 {
1972     return snort::alignedNtohl(&req->smb_param_disp);
1973 }
1974 
SmbNtTransactSecondaryReqTotalDataCnt(const SmbNtTransactSecondaryReq * req)1975 inline uint32_t SmbNtTransactSecondaryReqTotalDataCnt(const SmbNtTransactSecondaryReq* req)
1976 {
1977     return snort::alignedNtohl(&req->smb_total_data_count);
1978 }
1979 
SmbNtTransactSecondaryReqDataCnt(const SmbNtTransactSecondaryReq * req)1980 inline uint32_t SmbNtTransactSecondaryReqDataCnt(const SmbNtTransactSecondaryReq* req)
1981 {
1982     return snort::alignedNtohl(&req->smb_data_count);
1983 }
1984 
SmbNtTransactSecondaryReqDataOff(const SmbNtTransactSecondaryReq * req)1985 inline uint32_t SmbNtTransactSecondaryReqDataOff(const SmbNtTransactSecondaryReq* req)
1986 {
1987     return snort::alignedNtohl(&req->smb_data_offset);
1988 }
1989 
SmbNtTransactSecondaryReqDataDisp(const SmbNtTransactSecondaryReq * req)1990 inline uint32_t SmbNtTransactSecondaryReqDataDisp(const SmbNtTransactSecondaryReq* req)
1991 {
1992     return snort::alignedNtohl(&req->smb_data_disp);
1993 }
1994 
1995 /********************************************************************
1996  * SMB_COM_READ_RAW
1997  ********************************************************************/
1998 struct SmbReadRawReq   /* smb_wct = 8 */
1999 {
2000     uint8_t smb_wct;         /* count of 16-bit words that follow */
2001     uint16_t smb_fid;        /* file handle */
2002     uint32_t smb_offset;     /* offset in file to begin read */
2003     uint16_t smb_maxcnt;     /* max number of bytes to return (max 65,535) */
2004     uint16_t smb_mincnt;     /* min number of bytes to return (normally 0) */
2005     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
2006     uint16_t smb_rsvd;       /* reserved */
2007     uint16_t smb_bcc;        /* value = 0 */
2008 };
2009 
2010 struct SmbReadRawExtReq   /* smb_wct = 10 */
2011 {
2012     uint8_t smb_wct;         /* count of 16-bit words that follow */
2013     uint16_t smb_fid;        /* file handle */
2014     uint32_t smb_offset;     /* offset in file to begin read */
2015     uint16_t smb_maxcnt;     /* max number of bytes to return (max 65,535) */
2016     uint16_t smb_mincnt;     /* min number of bytes to return (normally 0) */
2017     uint32_t smb_timeout;    /* number of milliseconds to wait for completion */
2018     uint16_t smb_rsvd;       /* reserved */
2019     uint32_t smb_off_high;   /* high offset in file to begin write */
2020     uint16_t smb_bcc;        /* value = 0 */
2021 };
2022 
2023 /* Read Raw response is raw data wrapped in NetBIOS header */
2024 
SmbReadRawReqFid(const SmbReadRawReq * req)2025 inline uint16_t SmbReadRawReqFid(const SmbReadRawReq* req)
2026 {
2027     return snort::alignedNtohs(&req->smb_fid);
2028 }
2029 
SmbReadRawReqOffset(const SmbReadRawExtReq * req)2030 inline uint64_t SmbReadRawReqOffset(const SmbReadRawExtReq* req)
2031 {
2032     if (req->smb_wct == 8)
2033         return (uint64_t)snort::alignedNtohl(&req->smb_offset);
2034 
2035     return (uint64_t)snort::alignedNtohl(&req->smb_off_high) << 32
2036                     | (uint64_t)snort::alignedNtohl(&req->smb_offset);
2037 }
2038 
2039 /********************************************************************
2040  * SMB_COM_WRITE_RAW
2041  ********************************************************************/
2042 struct SmbWriteRawReq
2043 {
2044     uint8_t smb_wct;       /* value = 12 */
2045     uint16_t smb_fid;      /* file handle */
2046     uint16_t smb_tcount;   /* total bytes (including this buf, 65,535 max ) */
2047     uint16_t smb_rsvd;     /* reserved */
2048     uint32_t smb_offset;   /* offset in file to begin write */
2049     uint32_t smb_timeout;  /* number of milliseconds to wait for completion */
2050     uint16_t smb_wmode;    /* write mode:
2051                               bit0 - complete write to disk and send final result response
2052                               bit1 - return smb_remaining (pipes/devices only) */
2053     uint32_t smb_rsvd2;    /* reserved */
2054     uint16_t smb_dsize;    /* number of data bytes this buffer (min value = 0) */
2055     uint16_t smb_doff;     /* offset (from start of SMB hdr) to data bytes */
2056     uint16_t smb_bcc;      /* total bytes (including pad bytes) following */
2057 };
2058 
2059 struct SmbWriteRawExtReq
2060 {
2061     uint8_t smb_wct;       /* value = 14 */
2062     uint16_t smb_fid;      /* file handle */
2063     uint16_t smb_tcount;   /* total bytes (including this buf, 65,535 max ) */
2064     uint16_t smb_rsvd;     /* reserved */
2065     uint32_t smb_offset;   /* offset in file to begin write */
2066     uint32_t smb_timeout;  /* number of milliseconds to wait for completion */
2067     uint16_t smb_wmode;    /* write mode:
2068                               bit0 - complete write to disk and send final result response
2069                               bit1 - return smb_remaining (pipes/devices only) */
2070     uint32_t smb_rsvd2;    /* reserved */
2071     uint16_t smb_dsize;    /* number of data bytes this buffer (min value = 0) */
2072     uint16_t smb_doff;     /* offset (from start of SMB hdr) to data bytes */
2073     uint32_t smb_off_high; /* high offset in file to begin write */
2074     uint16_t smb_bcc;      /* total bytes (including pad bytes) following */
2075 };
2076 
2077 struct SmbWriteRawInterimResp
2078 {
2079     uint8_t smb_wct;         /* value = 1 */
2080     uint16_t smb_remaining;  /* bytes remaining to be read (pipes/devices only) */
2081     uint16_t smb_bcc;        /* value = 0 */
2082 };
2083 
SmbWriteRawReqTotalCount(const SmbWriteRawReq * req)2084 inline uint16_t SmbWriteRawReqTotalCount(const SmbWriteRawReq* req)
2085 {
2086     return snort::alignedNtohs(&req->smb_tcount);
2087 }
2088 
SmbWriteRawReqWriteThrough(const SmbWriteRawReq * req)2089 inline bool SmbWriteRawReqWriteThrough(const SmbWriteRawReq* req)
2090 {
2091     return snort::alignedNtohs(&req->smb_wmode) & 0x0001;
2092 }
2093 
SmbWriteRawReqFid(const SmbWriteRawReq * req)2094 inline uint16_t SmbWriteRawReqFid(const SmbWriteRawReq* req)
2095 {
2096     return snort::alignedNtohs(&req->smb_fid);
2097 }
2098 
SmbWriteRawReqDataOff(const SmbWriteRawReq * req)2099 inline uint16_t SmbWriteRawReqDataOff(const SmbWriteRawReq* req)
2100 {
2101     return snort::alignedNtohs(&req->smb_doff);
2102 }
2103 
SmbWriteRawReqDataCnt(const SmbWriteRawReq * req)2104 inline uint16_t SmbWriteRawReqDataCnt(const SmbWriteRawReq* req)
2105 {
2106     return snort::alignedNtohs(&req->smb_dsize);
2107 }
2108 
SmbWriteRawReqOffset(const SmbWriteRawExtReq * req)2109 inline uint64_t SmbWriteRawReqOffset(const SmbWriteRawExtReq* req)
2110 {
2111     if (req->smb_wct == 12)
2112         return (uint64_t)snort::alignedNtohl(&req->smb_offset);
2113 
2114     return (uint64_t)snort::alignedNtohl(&req->smb_off_high) << 32 |
2115                     (uint64_t)snort::alignedNtohl(&req->smb_offset);
2116 }
2117 
SmbWriteRawInterimRespRemaining(const SmbWriteRawInterimResp * resp)2118 inline uint16_t SmbWriteRawInterimRespRemaining(const SmbWriteRawInterimResp* resp)
2119 {
2120     return snort::alignedNtohs(&resp->smb_remaining);
2121 }
2122 
2123 /********************************************************************
2124  * SMB_COM_WRITE_COMPLETE - final response to an SMB_COM_WRITE_RAW
2125  ********************************************************************/
2126 struct SmbWriteCompleteResp
2127 {
2128     uint8_t smb_wct;     /* value = 1 */
2129     uint16_t smb_count;  /* total number of bytes written */
2130     uint16_t smb_bcc;    /* value = 0 */
2131 };
2132 
SmbWriteCompleteRespCount(const SmbWriteCompleteResp * resp)2133 inline uint16_t SmbWriteCompleteRespCount(const SmbWriteCompleteResp* resp)
2134 {
2135     return snort::alignedNtohs(&resp->smb_count);
2136 }
2137 
2138 /********************************************************************
2139  * SMB_COM_WRITE_AND_CLOSE
2140  ********************************************************************/
2141 struct SmbWriteAndCloseReq   /* smb_wct = 6 */
2142 {
2143     uint8_t smb_wct;      /* count of 16-bit words that follow */
2144     uint16_t smb_fid;     /* file handle (close after write) */
2145     uint16_t smb_count;   /* number of bytes to write */
2146     uint32_t smb_offset;  /* offset in file to begin write */
2147     uint32_t smb_mtime;   /* modification time */
2148     uint16_t smb_bcc;     /* 1 (for pad) + value of smb_count */
2149 };
2150 
2151 struct SmbWriteAndCloseExtReq   /* smb_wct = 12 */
2152 {
2153     uint8_t smb_wct;      /* count of 16-bit words that follow */
2154     uint16_t smb_fid;     /* file handle (close after write) */
2155     uint16_t smb_count;   /* number of bytes to write */
2156     uint32_t smb_offset;  /* offset in file to begin write */
2157     uint32_t smb_mtime;   /* modification time */
2158     uint32_t smb_rsvd1;   /* Optional */
2159     uint32_t smb_rsvd2;   /* Optional */
2160     uint32_t smb_rsvd3;   /* Optional */
2161     uint16_t smb_bcc;     /* 1 (for pad) + value of smb_count */
2162 };
2163 
2164 struct SmbWriteAndCloseResp   /* smb_wct = 1 */
2165 {
2166     uint8_t smb_wct;     /* count of 16-bit words that follow */
2167     uint16_t smb_count;  /* number of bytes written */
2168     uint16_t smb_bcc;    /* must be 0 */
2169 };
2170 
SmbWriteAndCloseReqFid(const SmbWriteAndCloseReq * req)2171 inline uint16_t SmbWriteAndCloseReqFid(const SmbWriteAndCloseReq* req)
2172 {
2173     return snort::alignedNtohs(&req->smb_fid);
2174 }
2175 
SmbWriteAndCloseReqCount(const SmbWriteAndCloseReq * req)2176 inline uint16_t SmbWriteAndCloseReqCount(const SmbWriteAndCloseReq* req)
2177 {
2178     return snort::alignedNtohs(&req->smb_count);
2179 }
2180 
SmbWriteAndCloseReqOffset(const SmbWriteAndCloseReq * req)2181 inline uint32_t SmbWriteAndCloseReqOffset(const SmbWriteAndCloseReq* req)
2182 {
2183     return snort::alignedNtohl(&req->smb_offset);
2184 }
2185 
SmbWriteAndCloseRespCount(const SmbWriteAndCloseResp * resp)2186 inline uint16_t SmbWriteAndCloseRespCount(const SmbWriteAndCloseResp* resp)
2187 {
2188     return snort::alignedNtohs(&resp->smb_count);
2189 }
2190 
2191 #pragma pack()
2192 
2193 void DCE2_Smb1Process(struct DCE2_SmbSsnData*);
2194 void DCE2_SmbDataFree(DCE2_SmbSsnData*);
2195 
2196 #endif
2197 
2198