suricata
detect-rfb-sectype.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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  *
20  * \author Sascha Steinbiss <sascha.steinbiss@dcso.de>
21  */
22 
23 #include "suricata-common.h"
24 #include "conf.h"
25 #include "detect.h"
26 #include "detect-parse.h"
27 #include "detect-engine.h"
29 #include "detect-rfb-sectype.h"
30 #include "app-layer-parser.h"
31 #include "util-byte.h"
32 
33 #include "rust-bindings.h"
34 
35 /**
36  * [rfb.sectype]:[<|>|<=|>=]<type>;
37  */
38 #define PARSE_REGEX "^\\s*(<=|>=|<|>)?\\s*([0-9]+)\\s*$"
39 static DetectParseRegex parse_regex;
40 
42  PROCEDURE_EQ = 1, /* equal */
43  PROCEDURE_LT, /* less than */
44  PROCEDURE_LE, /* less than or equal */
45  PROCEDURE_GT, /* greater than */
46  PROCEDURE_GE, /* greater than or equal */
47 };
48 
49 typedef struct {
50  uint32_t version;
53 
54 static DetectRfbSectypeData *DetectRfbSectypeParse (const char *);
55 static int DetectRfbSectypeSetup (DetectEngineCtx *, Signature *s, const char *str);
56 static void DetectRfbSectypeFree(DetectEngineCtx *, void *);
57 static int g_rfb_sectype_buffer_id = 0;
58 
59 static int DetectEngineInspectRfbSectypeGeneric(DetectEngineCtx *de_ctx,
60  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
61  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
62 
63 static int DetectRfbSectypeMatch (DetectEngineThreadCtx *, Flow *,
64  uint8_t, void *, void *, const Signature *,
65  const SigMatchCtx *);
66 
67 /**
68  * \brief Registration function for rfb.sectype keyword.
69  */
71 {
72  sigmatch_table[DETECT_AL_RFB_SECTYPE].name = "rfb.sectype";
73  sigmatch_table[DETECT_AL_RFB_SECTYPE].desc = "match RFB security type";
74  sigmatch_table[DETECT_AL_RFB_SECTYPE].url = "/rules/rfb-keywords.html#rfb-sectype";
75  sigmatch_table[DETECT_AL_RFB_SECTYPE].AppLayerTxMatch = DetectRfbSectypeMatch;
76  sigmatch_table[DETECT_AL_RFB_SECTYPE].Setup = DetectRfbSectypeSetup;
77  sigmatch_table[DETECT_AL_RFB_SECTYPE].Free = DetectRfbSectypeFree;
78 
79  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
80 
82  DetectEngineInspectRfbSectypeGeneric, NULL);
83 
84  g_rfb_sectype_buffer_id = DetectBufferTypeGetByName("rfb.sectype");
85 }
86 
87 static int DetectEngineInspectRfbSectypeGeneric(DetectEngineCtx *de_ctx,
88  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
89  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
90 {
92  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
93 }
94 
95 static inline int SectypeMatch(const uint32_t version,
96  enum DetectRfbSectypeCompareMode mode, uint32_t ref_version)
97 {
98  switch (mode) {
99  case PROCEDURE_EQ:
100  if (version == ref_version)
101  SCReturnInt(1);
102  break;
103  case PROCEDURE_LT:
104  if (version < ref_version)
105  SCReturnInt(1);
106  break;
107  case PROCEDURE_LE:
108  if (version <= ref_version)
109  SCReturnInt(1);
110  break;
111  case PROCEDURE_GT:
112  if (version > ref_version)
113  SCReturnInt(1);
114  break;
115  case PROCEDURE_GE:
116  if (version >= ref_version)
117  SCReturnInt(1);
118  break;
119  }
120  SCReturnInt(0);
121 }
122 
123 /**
124  * \internal
125  * \brief Function to match security type of a RFB TX
126  *
127  * \param det_ctx Pointer to the pattern matcher thread.
128  * \param f Pointer to the current flow.
129  * \param flags Flags.
130  * \param state App layer state.
131  * \param txv Pointer to the RFBTransaction.
132  * \param s Pointer to the Signature.
133  * \param ctx Pointer to the sigmatch that we will cast into DetectRfbSectypeData.
134  *
135  * \retval 0 no match.
136  * \retval 1 match.
137  */
138 static int DetectRfbSectypeMatch (DetectEngineThreadCtx *det_ctx,
139  Flow *f, uint8_t flags, void *state,
140  void *txv, const Signature *s,
141  const SigMatchCtx *ctx)
142 {
143  SCEnter();
144 
145  const DetectRfbSectypeData *dd = (const DetectRfbSectypeData *)ctx;
146  uint32_t version;
147  rs_rfb_tx_get_sectype(txv, &version);
148  if (SectypeMatch(version, dd->mode, dd->version))
149  SCReturnInt(1);
150  SCReturnInt(0);
151 }
152 
153 /**
154  * \internal
155  * \brief Function to parse options passed via rfb.sectype keywords.
156  *
157  * \param rawstr Pointer to the user provided options.
158  *
159  * \retval dd pointer to DetectRfbSectypeData on success.
160  * \retval NULL on failure.
161  */
162 static DetectRfbSectypeData *DetectRfbSectypeParse (const char *rawstr)
163 {
164  DetectRfbSectypeData *dd = NULL;
165  int ret = 0, res = 0;
166  size_t pcre2len;
167  char mode[2] = "";
168  char value1[20] = "";
169 
170  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
171  if (ret < 3 || ret > 5) {
172  SCLogError(SC_ERR_PCRE_MATCH, "Parse error %s", rawstr);
173  goto error;
174  }
175 
176  pcre2len = sizeof(mode);
177  res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)mode, &pcre2len);
178  if (res < 0) {
179  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
180  goto error;
181  }
182 
183  pcre2len = sizeof(value1);
184  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value1, &pcre2len);
185  if (res < 0) {
186  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
187  goto error;
188  }
189 
190  dd = SCCalloc(1, sizeof(DetectRfbSectypeData));
191  if (unlikely(dd == NULL))
192  goto error;
193 
194  if (strlen(mode) == 0) {
195  dd->mode = PROCEDURE_EQ;
196  } else if (strlen(mode) == 1) {
197  if (mode[0] == '<')
198  dd->mode = PROCEDURE_LT;
199  else if (mode[0] == '>')
200  dd->mode = PROCEDURE_GT;
201  } else if (strlen(mode) == 2) {
202  if (strcmp(mode, "<=") == 0)
203  dd->mode = PROCEDURE_LE;
204  if (strcmp(mode, ">=") == 0)
205  dd->mode = PROCEDURE_GE;
206  } else {
207  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid mode for rfb.sectype keyword");
208  goto error;
209  }
210 
211  if (dd->mode == 0) {
212  dd->mode = PROCEDURE_EQ;
213  }
214 
215  /* set the first value */
216  if (ByteExtractStringUint32(&dd->version, 10, strlen(value1), value1) <= 0) {
217  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
218  "to rfb.sectype keyword");
219  goto error;
220  }
221 
222  return dd;
223 
224 error:
225  if (dd)
226  SCFree(dd);
227  return NULL;
228 }
229 
230 /**
231  * \brief Function to add the parsed RFB security type field into the current signature.
232  *
233  * \param de_ctx Pointer to the Detection Engine Context.
234  * \param s Pointer to the Current Signature.
235  * \param rawstr Pointer to the user provided flags options.
236  *
237  * \retval 0 on Success.
238  * \retval -1 on Failure.
239  */
240 static int DetectRfbSectypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
241 {
243  return -1;
244 
245  DetectRfbSectypeData *dd = DetectRfbSectypeParse(rawstr);
246  if (dd == NULL) {
247  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
248  goto error;
249  }
250 
251  /* okay so far so good, lets get this into a SigMatch
252  * and put it in the Signature. */
253  SigMatch *sm = SigMatchAlloc();
254  if (sm == NULL)
255  goto error;
256 
258  sm->ctx = (void *)dd;
259 
260  SigMatchAppendSMToList(s, sm, g_rfb_sectype_buffer_id);
261  return 0;
262 
263 error:
264  DetectRfbSectypeFree(de_ctx, dd);
265  return -1;
266 }
267 
268 /**
269  * \internal
270  * \brief Function to free memory associated with DetectRfbSectypeData.
271  *
272  * \param de_ptr Pointer to DetectRfbSectypeData.
273  */
274 static void DetectRfbSectypeFree(DetectEngineCtx *de_ctx, void *ptr)
275 {
276  SCFree(ptr);
277 }
DetectRfbSectypeData
Definition: detect-rfb-sectype.c:49
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
DetectEngineAppInspectionEngine_
Definition: detect.h:397
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2435
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1191
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1575
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1201
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectRfbSectypeCompareMode
DetectRfbSectypeCompareMode
Definition: detect-rfb-sectype.c:41
Flow_
Flow data structure.
Definition: flow.h:353
detect-rfb-sectype.h
DetectRfbSectypeData::mode
enum DetectRfbSectypeCompareMode mode
Definition: detect-rfb-sectype.c:51
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1172
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
DetectRfbSectypeRegister
void DetectRfbSectypeRegister(void)
Registration function for rfb.sectype keyword.
Definition: detect-rfb-sectype.c:70
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
PROCEDURE_LT
@ PROCEDURE_LT
Definition: detect-rfb-sectype.c:43
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2558
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
PROCEDURE_GT
@ PROCEDURE_GT
Definition: detect-rfb-sectype.c:45
PROCEDURE_LE
@ PROCEDURE_LE
Definition: detect-rfb-sectype.c:44
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:174
conf.h
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:239
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:414
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
version
uint8_t version
Definition: decode-gre.h:1
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
SigMatch_
a single match condition for a signature
Definition: detect.h:320
DetectRfbSectypeData::version
uint32_t version
Definition: detect-rfb-sectype.c:50
PROCEDURE_EQ
@ PROCEDURE_EQ
Definition: detect-rfb-sectype.c:42
SC_Pcre2SubstringCopy
int SC_Pcre2SubstringCopy(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
Definition: detect-parse.c:2534
ALPROTO_RFB
@ ALPROTO_RFB
Definition: app-layer-protos.h:54
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-rfb-sectype.c:38
PROCEDURE_GE
@ PROCEDURE_GE
Definition: detect-rfb-sectype.c:46
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
DETECT_AL_RFB_SECTYPE
@ DETECT_AL_RFB_SECTYPE
Definition: detect-engine-register.h:254
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349