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 #ifdef HAVE_RUST
38 #include "rust.h"
39 #include "rust-smb-detect-gen.h"
40 
41 #define BUFFER_NAME "smb_named_pipe"
42 #define KEYWORD_NAME 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  int r = DetectBufferSetActiveList(s, g_smb_named_pipe_buffer_id);
50  s->alproto = ALPROTO_SMB;
51  return r;
52 }
53 
54 static InspectionBuffer *GetNamedPipeData(DetectEngineThreadCtx *det_ctx,
55  const DetectEngineTransforms *transforms,
56  Flow *_f, const uint8_t _flow_flags,
57  void *txv, const int list_id)
58 {
59  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
60  if (buffer->inspect == NULL) {
61  uint32_t b_len = 0;
62  uint8_t *b = NULL;
63 
64  if (rs_smb_tx_get_named_pipe(txv, &b, &b_len) != 1)
65  return NULL;
66  if (b == NULL || b_len == 0)
67  return NULL;
68 
69  InspectionBufferSetup(buffer, b, b_len);
70  InspectionBufferApplyTransforms(buffer, transforms);
71  }
72  return buffer;
73 }
74 
76 {
78  sigmatch_table[KEYWORD_ID].Setup = DetectSmbNamedPipeSetup;
80  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB named pipe in tree connect";
81 
83  PrefilterGenericMpmRegister, GetNamedPipeData,
84  ALPROTO_SMB, 1);
85 
88  DetectEngineInspectBufferGeneric, GetNamedPipeData);
89 
90  g_smb_named_pipe_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
91 }
92 
93 #undef BUFFER_NAME
94 #undef KEYWORD_NAME
95 #undef KEYWORD_ID
96 
97 #else /* NO RUST */
99 #endif
100 
101 #ifdef HAVE_RUST
102 #define BUFFER_NAME "smb_share"
103 #define KEYWORD_NAME BUFFER_NAME
104 #define KEYWORD_ID DETECT_SMB_SHARE
105 
106 static int g_smb_share_buffer_id = 0;
107 
108 static int DetectSmbShareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
109 {
110  int r = DetectBufferSetActiveList(s, g_smb_share_buffer_id);
111  s->alproto = ALPROTO_SMB;
112  return r;
113 }
114 
115 static InspectionBuffer *GetShareData(DetectEngineThreadCtx *det_ctx,
116  const DetectEngineTransforms *transforms,
117  Flow *_f, const uint8_t _flow_flags,
118  void *txv, const int list_id)
119 {
120  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
121  if (buffer->inspect == NULL) {
122  uint32_t b_len = 0;
123  uint8_t *b = NULL;
124 
125  if (rs_smb_tx_get_share(txv, &b, &b_len) != 1)
126  return NULL;
127  if (b == NULL || b_len == 0)
128  return NULL;
129 
130  InspectionBufferSetup(buffer, b, b_len);
131  InspectionBufferApplyTransforms(buffer, transforms);
132  }
133  return buffer;
134 }
135 
136 void DetectSmbShareRegister(void)
137 {
139  sigmatch_table[KEYWORD_ID].Setup = DetectSmbShareSetup;
141  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB share name in tree connect";
142 
144  PrefilterGenericMpmRegister, GetShareData,
145  ALPROTO_SMB, 1);
146 
149  DetectEngineInspectBufferGeneric, GetShareData);
150 
151  g_smb_share_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
152 }
153 #else
155 #endif
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
void DetectSmbShareRegister(void)
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
#define KEYWORD_ID
main detection engine ctx
Definition: detect.h:720
int DetectBufferTypeGetByName(const char *name)
Data structures and function prototypes for keeping state for the detection engine.
#define KEYWORD_NAME
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
AppProto alproto
Definition: detect.h:496
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:1162
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:1328
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:349
uint16_t flags
Definition: detect.h:1154
Flow data structure.
Definition: flow.h:327
#define BUFFER_NAME
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)