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"
33 #include "detect-engine-prefilter.h"
34 #include "detect-engine-content-inspection.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
DetectSmbNamedPipeSetup(DetectEngineCtx * de_ctx,Signature * s,const char * arg)46 static int DetectSmbNamedPipeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
47 {
48 if (DetectBufferSetActiveList(s, g_smb_named_pipe_buffer_id) < 0)
49 return -1;
50
51 if (DetectSignatureSetAppProto(s, ALPROTO_SMB) < 0)
52 return -1;
53
54 return 0;
55 }
56
GetNamedPipeData(DetectEngineThreadCtx * det_ctx,const DetectEngineTransforms * transforms,Flow * _f,const uint8_t _flow_flags,void * txv,const int list_id)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
DetectSmbNamedPipeRegister(void)78 void DetectSmbNamedPipeRegister(void)
79 {
80 sigmatch_table[KEYWORD_ID].name = KEYWORD_NAME;
81 sigmatch_table[KEYWORD_ID].alias = KEYWORD_NAME_LEGACY;
82 sigmatch_table[KEYWORD_ID].Setup = DetectSmbNamedPipeSetup;
83 sigmatch_table[KEYWORD_ID].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER;
84 sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB named pipe in tree connect";
85
86 DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2,
87 PrefilterGenericMpmRegister, GetNamedPipeData,
88 ALPROTO_SMB, 1);
89
90 DetectAppLayerInspectEngineRegister2(BUFFER_NAME,
91 ALPROTO_SMB, SIG_FLAG_TOSERVER, 0,
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
DetectSmbShareSetup(DetectEngineCtx * de_ctx,Signature * s,const char * arg)109 static int DetectSmbShareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
110 {
111 if (DetectBufferSetActiveList(s, g_smb_share_buffer_id) < 0)
112 return -1;
113
114 if (DetectSignatureSetAppProto(s, ALPROTO_SMB) < 0)
115 return -1;
116
117 return 0;
118 }
119
GetShareData(DetectEngineThreadCtx * det_ctx,const DetectEngineTransforms * transforms,Flow * _f,const uint8_t _flow_flags,void * txv,const int list_id)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
DetectSmbShareRegister(void)141 void DetectSmbShareRegister(void)
142 {
143 sigmatch_table[KEYWORD_ID].name = KEYWORD_NAME;
144 sigmatch_table[KEYWORD_ID].alias = KEYWORD_NAME_LEGACY;
145 sigmatch_table[KEYWORD_ID].Setup = DetectSmbShareSetup;
146 sigmatch_table[KEYWORD_ID].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER;
147 sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB share name in tree connect";
148
149 DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2,
150 PrefilterGenericMpmRegister, GetShareData,
151 ALPROTO_SMB, 1);
152
153 DetectAppLayerInspectEngineRegister2(BUFFER_NAME,
154 ALPROTO_SMB, SIG_FLAG_TOSERVER, 0,
155 DetectEngineInspectBufferGeneric, GetShareData);
156
157 g_smb_share_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
158 }
159