suricata
detect-smb-share.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect.h"
28 #include "detect-parse.h"
29 
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-engine-state.h"
35 
36 #include "detect-smb-share.h"
37 #include "rust.h"
38 #include "rust-smb-detect-gen.h"
39 
40 #define BUFFER_NAME "smb_named_pipe"
41 #define KEYWORD_NAME "smb.named_pipe"
42 #define KEYWORD_NAME_LEGACY BUFFER_NAME
43 #define KEYWORD_ID DETECT_SMB_NAMED_PIPE
44 
45 static int g_smb_named_pipe_buffer_id = 0;
46 
47 static int DetectSmbNamedPipeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
48 {
49  if (DetectBufferSetActiveList(s, g_smb_named_pipe_buffer_id) < 0)
50  return -1;
51 
53  return -1;
54 
55  return 0;
56 }
57 
58 static InspectionBuffer *GetNamedPipeData(DetectEngineThreadCtx *det_ctx,
59  const DetectEngineTransforms *transforms,
60  Flow *_f, const uint8_t _flow_flags,
61  void *txv, const int list_id)
62 {
63  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
64  if (buffer->inspect == NULL) {
65  uint32_t b_len = 0;
66  uint8_t *b = NULL;
67 
68  if (rs_smb_tx_get_named_pipe(txv, &b, &b_len) != 1)
69  return NULL;
70  if (b == NULL || b_len == 0)
71  return NULL;
72 
73  InspectionBufferSetup(buffer, b, b_len);
74  InspectionBufferApplyTransforms(buffer, transforms);
75  }
76  return buffer;
77 }
78 
80 {
83  sigmatch_table[KEYWORD_ID].Setup = DetectSmbNamedPipeSetup;
85  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB named pipe in tree connect";
86 
88  PrefilterGenericMpmRegister, GetNamedPipeData,
89  ALPROTO_SMB, 1);
90 
93  DetectEngineInspectBufferGeneric, GetNamedPipeData);
94 
95  g_smb_named_pipe_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
96 }
97 
98 #undef BUFFER_NAME
99 #undef KEYWORD_NAME
100 #undef KEYWORD_NAME_LEGACY
101 #undef KEYWORD_ID
102 
103 #define BUFFER_NAME "smb_share"
104 #define KEYWORD_NAME "smb.share"
105 #define KEYWORD_NAME_LEGACY BUFFER_NAME
106 #define KEYWORD_ID DETECT_SMB_SHARE
107 
108 static int g_smb_share_buffer_id = 0;
109 
110 static int DetectSmbShareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
111 {
112  if (DetectBufferSetActiveList(s, g_smb_share_buffer_id) < 0)
113  return -1;
114 
116  return -1;
117 
118  return 0;
119 }
120 
121 static InspectionBuffer *GetShareData(DetectEngineThreadCtx *det_ctx,
122  const DetectEngineTransforms *transforms,
123  Flow *_f, const uint8_t _flow_flags,
124  void *txv, const int list_id)
125 {
126  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
127  if (buffer->inspect == NULL) {
128  uint32_t b_len = 0;
129  uint8_t *b = NULL;
130 
131  if (rs_smb_tx_get_share(txv, &b, &b_len) != 1)
132  return NULL;
133  if (b == NULL || b_len == 0)
134  return NULL;
135 
136  InspectionBufferSetup(buffer, b, b_len);
137  InspectionBufferApplyTransforms(buffer, transforms);
138  }
139  return buffer;
140 }
141 
143 {
146  sigmatch_table[KEYWORD_ID].Setup = DetectSmbShareSetup;
148  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB share name in tree connect";
149 
151  PrefilterGenericMpmRegister, GetShareData,
152  ALPROTO_SMB, 1);
153 
156  DetectEngineInspectBufferGeneric, GetShareData);
157 
158  g_smb_share_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
159 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1409
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1150
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
void DetectSmbShareRegister(void)
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
#define KEYWORD_NAME_LEGACY
#define KEYWORD_ID
#define BUFFER_NAME
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1164
Signature container.
Definition: detect.h:496
main detection engine ctx
Definition: detect.h:724
int DetectBufferTypeGetByName(const char *name)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1356
Data structures and function prototypes for keeping state for the detection engine.
#define SIG_FLAG_TOSERVER
Definition: detect.h:241
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
int DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
const char * desc
Definition: detect.h:1166
const char * alias
Definition: detect.h:1165
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1332
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
#define KEYWORD_NAME
uint16_t flags
Definition: detect.h:1158
Flow data structure.
Definition: flow.h:325
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
void DetectSmbNamedPipeRegister(void)