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  GetNamedPipeData, ALPROTO_SMB, 1);
88 
90  DetectEngineInspectBufferGeneric, GetNamedPipeData);
91 
92  g_smb_named_pipe_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
93 }
94 
95 #undef BUFFER_NAME
96 #undef KEYWORD_NAME
97 #undef KEYWORD_NAME_LEGACY
98 #undef KEYWORD_ID
99 
100 #define BUFFER_NAME "smb_share"
101 #define KEYWORD_NAME "smb.share"
102 #define KEYWORD_NAME_LEGACY BUFFER_NAME
103 #define KEYWORD_ID DETECT_SMB_SHARE
104 
105 static int g_smb_share_buffer_id = 0;
106 
107 static int DetectSmbShareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
108 {
109  if (DetectBufferSetActiveList(de_ctx, s, g_smb_share_buffer_id) < 0)
110  return -1;
111 
113  return -1;
114 
115  return 0;
116 }
117 
118 static InspectionBuffer *GetShareData(DetectEngineThreadCtx *det_ctx,
119  const DetectEngineTransforms *transforms,
120  Flow *_f, const uint8_t _flow_flags,
121  void *txv, const int list_id)
122 {
123  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
124  if (buffer->inspect == NULL) {
125  uint32_t b_len = 0;
126  const uint8_t *b = NULL;
127 
128  if (rs_smb_tx_get_share(txv, &b, &b_len) != 1)
129  return NULL;
130  if (b == NULL || b_len == 0)
131  return NULL;
132 
133  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
134  InspectionBufferApplyTransforms(buffer, transforms);
135  }
136  return buffer;
137 }
138 
140 {
143  sigmatch_table[KEYWORD_ID].Setup = DetectSmbShareSetup;
145  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB share name in tree connect";
146 
148  GetShareData, ALPROTO_SMB, 1);
149 
151  DetectEngineInspectBufferGeneric, GetShareData);
152 
153  g_smb_share_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
154 }
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
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:2154
SigTableElmt_::name
const char * name
Definition: detect.h:1304
DetectEngineTransforms
Definition: detect.h:408
detect-smb-share.h
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
DetectSmbShareRegister
void DetectSmbShareRegister(void)
Definition: detect-smb-share.c:139
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-smb-share.c:101
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:750
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-smb-share.c:103
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:102
detect-engine-content-inspection.h
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-smb-share.c:100
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:1595
DetectSmbNamedPipeRegister
void DetectSmbNamedPipeRegister(void)
Definition: detect-smb-share.c:78
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240