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 
39 #define BUFFER_NAME "smb_named_pipe"
40 #define KEYWORD_NAME "smb.named_pipe"
41 #define KEYWORD_NAME_LEGACY BUFFER_NAME
42 #define KEYWORD_ID DETECT_SMB_NAMED_PIPE
43 
44 static int g_smb_named_pipe_buffer_id = 0;
45 
46 static int DetectSmbNamedPipeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
47 {
48  if (DetectBufferSetActiveList(de_ctx, s, g_smb_named_pipe_buffer_id) < 0)
49  return -1;
50 
52  return -1;
53 
54  return 0;
55 }
56 
57 static InspectionBuffer *GetNamedPipeData(DetectEngineThreadCtx *det_ctx,
58  const DetectEngineTransforms *transforms,
59  Flow *_f, const uint8_t _flow_flags,
60  void *txv, const int list_id)
61 {
62  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
63  if (buffer->inspect == NULL) {
64  uint32_t b_len = 0;
65  const uint8_t *b = NULL;
66 
67  if (rs_smb_tx_get_named_pipe(txv, &b, &b_len) != 1)
68  return NULL;
69  if (b == NULL || b_len == 0)
70  return NULL;
71 
72  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
73  InspectionBufferApplyTransforms(buffer, transforms);
74  }
75  return buffer;
76 }
77 
79 {
82  sigmatch_table[KEYWORD_ID].Setup = DetectSmbNamedPipeSetup;
84  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB named pipe in tree connect";
85 
87  PrefilterGenericMpmRegister, GetNamedPipeData,
88  ALPROTO_SMB, 1);
89 
92  DetectEngineInspectBufferGeneric, GetNamedPipeData);
93 
94  g_smb_named_pipe_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
95 }
96 
97 #undef BUFFER_NAME
98 #undef KEYWORD_NAME
99 #undef KEYWORD_NAME_LEGACY
100 #undef KEYWORD_ID
101 
102 #define BUFFER_NAME "smb_share"
103 #define KEYWORD_NAME "smb.share"
104 #define KEYWORD_NAME_LEGACY BUFFER_NAME
105 #define KEYWORD_ID DETECT_SMB_SHARE
106 
107 static int g_smb_share_buffer_id = 0;
108 
109 static int DetectSmbShareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
110 {
111  if (DetectBufferSetActiveList(de_ctx, s, g_smb_share_buffer_id) < 0)
112  return -1;
113 
115  return -1;
116 
117  return 0;
118 }
119 
120 static InspectionBuffer *GetShareData(DetectEngineThreadCtx *det_ctx,
121  const DetectEngineTransforms *transforms,
122  Flow *_f, const uint8_t _flow_flags,
123  void *txv, const int list_id)
124 {
125  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
126  if (buffer->inspect == NULL) {
127  uint32_t b_len = 0;
128  const uint8_t *b = NULL;
129 
130  if (rs_smb_tx_get_share(txv, &b, &b_len) != 1)
131  return NULL;
132  if (b == NULL || b_len == 0)
133  return NULL;
134 
135  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
136  InspectionBufferApplyTransforms(buffer, transforms);
137  }
138  return buffer;
139 }
140 
142 {
145  sigmatch_table[KEYWORD_ID].Setup = DetectSmbShareSetup;
147  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB share name in tree connect";
148 
150  PrefilterGenericMpmRegister, GetShareData,
151  ALPROTO_SMB, 1);
152 
155  DetectEngineInspectBufferGeneric, GetShareData);
156 
157  g_smb_share_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
158 }
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1704
detect-engine.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1491
SigTableElmt_::desc
const char * desc
Definition: detect.h:1287
DetectEngineInspectBufferGeneric
uint8_t 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.
Definition: detect-engine.c:2165
SigTableElmt_::name
const char * name
Definition: detect.h:1285
DetectEngineTransforms
Definition: detect.h:400
detect-smb-share.h
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1387
InspectionBuffer
Definition: detect.h:365
Flow_
Flow data structure.
Definition: flow.h:347
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1279
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:827
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1270
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1526
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
DetectSmbShareRegister
void DetectSmbShareRegister(void)
Definition: detect-smb-share.c:141
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1075
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-smb-share.c:103
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-smb-share.c:105
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:216
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-smb-share.c:104
detect-engine-content-inspection.h
SigTableElmt_::alias
const char * alias
Definition: detect.h:1286
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1725
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:366
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-smb-share.c:102
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1621
DetectSmbNamedPipeRegister
void DetectSmbNamedPipeRegister(void)
Definition: detect-smb-share.c:78
detect-parse.h
Signature_
Signature container.
Definition: detect.h:582
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1467