suricata
detect-websocket.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 Philippe Antoine
22  */
23 
24 #include "suricata-common.h"
25 #include "detect.h"
26 #include "detect-parse.h"
27 #include "detect-engine.h"
29 #include "detect-engine-uint.h"
31 #include "detect-websocket.h"
32 
33 #include "rust.h"
34 
35 static int websocket_tx_id = 0;
36 static int websocket_payload_id = 0;
37 
38 /**
39  * \internal
40  * \brief this function will free memory associated with DetectWebSocketOpcodeData
41  *
42  * \param de pointer to DetectWebSocketOpcodeData
43  */
44 static void DetectWebSocketOpcodeFree(DetectEngineCtx *de_ctx, void *de_ptr)
45 {
46  rs_detect_u8_free(de_ptr);
47 }
48 
49 /**
50  * \internal
51  * \brief Function to match opcode of a websocket tx
52  *
53  * \param det_ctx Pointer to the pattern matcher thread.
54  * \param f Pointer to the current flow.
55  * \param flags Flags.
56  * \param state App layer state.
57  * \param txv Pointer to the transaction.
58  * \param s Pointer to the Signature.
59  * \param ctx Pointer to the sigmatch that we will cast into DetectWebSocketOpcodeData.
60  *
61  * \retval 0 no match.
62  * \retval 1 match.
63  */
64 static int DetectWebSocketOpcodeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
65  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
66 {
67  const DetectU8Data *de = (const DetectU8Data *)ctx;
68  uint8_t opc = SCWebSocketGetOpcode(txv);
69  return DetectU8Match(opc, de);
70 }
71 
72 /**
73  * \internal
74  * \brief this function is used to add the parsed sigmatch into the current signature
75  *
76  * \param de_ctx pointer to the Detection Engine Context
77  * \param s pointer to the Current Signature
78  * \param rawstr pointer to the user provided options
79  *
80  * \retval 0 on Success
81  * \retval -1 on Failure
82  */
83 static int DetectWebSocketOpcodeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
84 {
86  return -1;
87 
88  DetectU8Data *de = SCWebSocketParseOpcode(rawstr);
89  if (de == NULL)
90  return -1;
91 
93  de_ctx, s, DETECT_WEBSOCKET_OPCODE, (SigMatchCtx *)de, websocket_tx_id) == NULL) {
94  DetectWebSocketOpcodeFree(de_ctx, de);
95  return -1;
96  }
97 
98  return 0;
99 }
100 
101 /**
102  * \internal
103  * \brief this function will free memory associated with DetectWebSocketMaskData
104  *
105  * \param de pointer to DetectWebSocketMaskData
106  */
107 static void DetectWebSocketMaskFree(DetectEngineCtx *de_ctx, void *de_ptr)
108 {
109  rs_detect_u32_free(de_ptr);
110 }
111 
112 static int DetectWebSocketMaskMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
113  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
114 {
115  uint32_t val;
116  const DetectU32Data *du32 = (const DetectU32Data *)ctx;
117  if (SCWebSocketGetMask(txv, &val)) {
118  return DetectU32Match(val, du32);
119  }
120  return 0;
121 }
122 
123 static int DetectWebSocketMaskSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
124 {
126  return -1;
127 
128  DetectU32Data *du32 = DetectU32Parse(rawstr);
129  if (du32 == NULL)
130  return -1;
131 
133  de_ctx, s, DETECT_WEBSOCKET_MASK, (SigMatchCtx *)du32, websocket_tx_id) == NULL) {
134  DetectWebSocketMaskFree(de_ctx, du32);
135  return -1;
136  }
137 
138  return 0;
139 }
140 
141 static void DetectWebSocketFlagsFree(DetectEngineCtx *de_ctx, void *de_ptr)
142 {
143  rs_detect_u8_free(de_ptr);
144 }
145 
146 static int DetectWebSocketFlagsMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
147  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
148 {
149  const DetectU8Data *de = (const DetectU8Data *)ctx;
150  uint8_t val = SCWebSocketGetFlags(txv);
151  return DetectU8Match(val, de);
152 }
153 
154 static int DetectWebSocketFlagsSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
155 {
157  return -1;
158 
159  DetectU8Data *de = SCWebSocketParseFlags(rawstr);
160  if (de == NULL)
161  return -1;
162 
164  de_ctx, s, DETECT_WEBSOCKET_FLAGS, (SigMatchCtx *)de, websocket_tx_id) == NULL) {
165  DetectWebSocketOpcodeFree(de_ctx, de);
166  return -1;
167  }
168 
169  return 0;
170 }
171 
172 static int DetectWebSocketPayloadSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rulestr)
173 {
174  if (DetectBufferSetActiveList(de_ctx, s, websocket_payload_id) < 0)
175  return -1;
176 
178  return -1;
179 
180  return 0;
181 }
182 
183 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
184  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
185  const int list_id)
186 {
187  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
188  if (buffer->inspect == NULL) {
189  const uint8_t *b = NULL;
190  uint32_t b_len = 0;
191 
192  if (!SCWebSocketGetPayload(txv, &b, &b_len))
193  return NULL;
194  if (b == NULL || b_len == 0)
195  return NULL;
196 
197  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
198  InspectionBufferApplyTransforms(buffer, transforms);
199  }
200  return buffer;
201 }
202 
203 /**
204  * \brief Registration function for websocket.opcode: keyword
205  */
207 {
208  sigmatch_table[DETECT_WEBSOCKET_OPCODE].name = "websocket.opcode";
209  sigmatch_table[DETECT_WEBSOCKET_OPCODE].desc = "match WebSocket opcode";
210  sigmatch_table[DETECT_WEBSOCKET_OPCODE].url = "/rules/websocket-keywords.html#websocket-opcode";
211  sigmatch_table[DETECT_WEBSOCKET_OPCODE].AppLayerTxMatch = DetectWebSocketOpcodeMatch;
212  sigmatch_table[DETECT_WEBSOCKET_OPCODE].Setup = DetectWebSocketOpcodeSetup;
213  sigmatch_table[DETECT_WEBSOCKET_OPCODE].Free = DetectWebSocketOpcodeFree;
214 
219 
220  websocket_tx_id = DetectBufferTypeGetByName("websocket.tx");
221 
222  sigmatch_table[DETECT_WEBSOCKET_MASK].name = "websocket.mask";
223  sigmatch_table[DETECT_WEBSOCKET_MASK].desc = "match WebSocket mask";
224  sigmatch_table[DETECT_WEBSOCKET_MASK].url = "/rules/websocket-keywords.html#websocket-mask";
225  sigmatch_table[DETECT_WEBSOCKET_MASK].AppLayerTxMatch = DetectWebSocketMaskMatch;
226  sigmatch_table[DETECT_WEBSOCKET_MASK].Setup = DetectWebSocketMaskSetup;
227  sigmatch_table[DETECT_WEBSOCKET_MASK].Free = DetectWebSocketMaskFree;
228 
229  sigmatch_table[DETECT_WEBSOCKET_FLAGS].name = "websocket.flags";
230  sigmatch_table[DETECT_WEBSOCKET_FLAGS].desc = "match WebSocket flags";
231  sigmatch_table[DETECT_WEBSOCKET_FLAGS].url = "/rules/websocket-keywords.html#websocket-flags";
232  sigmatch_table[DETECT_WEBSOCKET_FLAGS].AppLayerTxMatch = DetectWebSocketFlagsMatch;
233  sigmatch_table[DETECT_WEBSOCKET_FLAGS].Setup = DetectWebSocketFlagsSetup;
234  sigmatch_table[DETECT_WEBSOCKET_FLAGS].Free = DetectWebSocketFlagsFree;
235 
236  sigmatch_table[DETECT_WEBSOCKET_PAYLOAD].name = "websocket.payload";
237  sigmatch_table[DETECT_WEBSOCKET_PAYLOAD].desc = "match WebSocket payload";
239  "/rules/websocket-keywords.html#websocket-payload";
240  sigmatch_table[DETECT_WEBSOCKET_PAYLOAD].Setup = DetectWebSocketPayloadSetup;
246  DetectAppLayerMpmRegister("websocket.payload", SIG_FLAG_TOSERVER, 2,
248  DetectAppLayerMpmRegister("websocket.payload", SIG_FLAG_TOCLIENT, 2,
250  websocket_payload_id = DetectBufferTypeGetByName("websocket.payload");
251 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-engine.h
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
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:2122
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
SigTableElmt_::name
const char * name
Definition: detect.h:1296
DetectEngineTransforms
Definition: detect.h:409
DetectU32Parse
DetectUintData_u32 * DetectU32Parse(const char *u32str)
This function is used to parse u32 options passed via some u32 keyword.
Definition: detect-engine-uint.c:45
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
InspectionBuffer
Definition: detect.h:374
Flow_
Flow data structure.
Definition: flow.h:355
DetectWebsocketRegister
void DetectWebsocketRegister(void)
Registration function for websocket.opcode: keyword.
Definition: detect-websocket.c:206
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1267
rust.h
DETECT_WEBSOCKET_PAYLOAD
@ DETECT_WEBSOCKET_PAYLOAD
Definition: detect-engine-register.h:325
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:267
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1479
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
DETECT_WEBSOCKET_FLAGS
@ DETECT_WEBSOCKET_FLAGS
Definition: detect-engine-register.h:324
detect-websocket.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
de
uint8_t de
Definition: app-layer-htp.c:581
detect.h
DETECT_WEBSOCKET_OPCODE
@ DETECT_WEBSOCKET_OPCODE
Definition: detect-engine-register.h:323
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(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
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:345
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
ALPROTO_WEBSOCKET
@ ALPROTO_WEBSOCKET
Definition: app-layer-protos.h:59
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct 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:2079
DETECT_WEBSOCKET_MASK
@ DETECT_WEBSOCKET_MASK
Definition: detect-engine-register.h:322
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447