suricata
detect-rfb-secresult.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2021 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 Sascha Steinbiss <sascha.steinbiss@dcso.de>
22  */
23 
24 #include "suricata-common.h"
25 #include "conf.h"
26 #include "detect.h"
27 #include "detect-parse.h"
28 #include "detect-engine.h"
30 #include "detect-rfb-secresult.h"
31 #include "util-unittest.h"
32 
33 #include "rust.h"
34 
35 #define PARSE_REGEX "\\S[A-z]"
36 static DetectParseRegex parse_regex;
37 
38 static int rfb_secresult_id = 0;
39 
40 static int DetectRfbSecresultMatch(DetectEngineThreadCtx *det_ctx,
41  Flow *f, uint8_t flags, void *state,
42  void *txv, const Signature *s,
43  const SigMatchCtx *ctx);
44 static int DetectRfbSecresultSetup (DetectEngineCtx *, Signature *, const char *);
45 #ifdef UNITTESTS
46 static void RfbSecresultRegisterTests(void);
47 #endif
49 
50 typedef struct DetectRfbSecresultData_ {
51  uint32_t result; /** result code */
53 
54 /**
55  * \brief Registration function for rfb.secresult: keyword
56  */
58 {
59  sigmatch_table[DETECT_AL_RFB_SECRESULT].name = "rfb.secresult";
60  sigmatch_table[DETECT_AL_RFB_SECRESULT].desc = "match RFB security result";
61  sigmatch_table[DETECT_AL_RFB_SECRESULT].url = "/rules/rfb-keywords.html#rfb-secresult";
62  sigmatch_table[DETECT_AL_RFB_SECRESULT].AppLayerTxMatch = DetectRfbSecresultMatch;
63  sigmatch_table[DETECT_AL_RFB_SECRESULT].Setup = DetectRfbSecresultSetup;
65 #ifdef UNITTESTS
66  sigmatch_table[DETECT_AL_RFB_SECRESULT].RegisterTests = RfbSecresultRegisterTests;
67 #endif
68  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
69 
72 
73  rfb_secresult_id = DetectBufferTypeGetByName("rfb.secresult");
74 }
75 
76 enum {
81 };
82 
83 /**
84  * \struct DetectRfbSecresult_
85  * DetectRfbSecresult_ is used to store values
86  */
87 
89  const char *result;
90  uint16_t code;
91 } results[] = {
92  { "ok", RFB_SECRESULT_OK, },
93  { "fail", RFB_SECRESULT_FAIL, },
94  { "toomany", RFB_SECRESULT_TOOMANY, },
95  { "unknown", RFB_SECRESULT_UNKNOWN, },
96  { NULL, 0 },
97 };
98 
99 /**
100  * \internal
101  * \brief Function to match security result of a RFB TX
102  *
103  * \param det_ctx Pointer to the pattern matcher thread.
104  * \param f Pointer to the current flow.
105  * \param flags Flags.
106  * \param state App layer state.
107  * \param txv Pointer to the RFBTransaction.
108  * \param s Pointer to the Signature.
109  * \param ctx Pointer to the sigmatch that we will cast into DetectRfbSecresultData.
110  *
111  * \retval 0 no match.
112  * \retval 1 match.
113  */
114 static int DetectRfbSecresultMatch(DetectEngineThreadCtx *det_ctx,
115  Flow *f, uint8_t flags, void *state,
116  void *txv, const Signature *s,
117  const SigMatchCtx *ctx)
118 {
119  const DetectRfbSecresultData *de = (const DetectRfbSecresultData *)ctx;
120  uint32_t resultcode;
121  int ret = 0;
122 
123  if (!de)
124  return 0;
125 
126  ret = rs_rfb_tx_get_secresult(txv, &resultcode);
127  if (ret == 0) {
128  return 0;
129  }
130 
131  if (de->result < 3) {
132  /* we are asking for a defined code... */
133  if (resultcode == de->result) {
134  /* ... which needs to match */
135  return 1;
136  }
137  } else {
138  /* we are asking for an unknown code */
139  if (resultcode > 2) {
140  /* match any unknown code */
141  return 1;
142  }
143  }
144 
145  return 0;
146 }
147 
148 /**
149  * \internal
150  * \brief This function is used to parse options passed via rfb.secresults: keyword
151  *
152  * \param rawstr Pointer to the user provided secresult options
153  *
154  * \retval de pointer to DetectRfbSecresultData on success
155  * \retval NULL on failure
156  */
157 static DetectRfbSecresultData *DetectRfbSecresultParse (const char *rawstr)
158 {
159  int i;
160  DetectRfbSecresultData *de = NULL;
161  int found = 0;
162 
163  pcre2_match_data *match = NULL;
164  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
165  if (ret < 1) {
166  SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
167  goto error;
168  }
169 
170  for(i = 0; results[i].result != NULL; i++) {
171  if((strcasecmp(results[i].result,rawstr)) == 0) {
172  found = 1;
173  break;
174  }
175  }
176 
177  if(found == 0) {
178  SCLogError("unknown secresult value %s", rawstr);
179  goto error;
180  }
181 
183  if (unlikely(de == NULL))
184  goto error;
185 
186  de->result = results[i].code;
187 
188  pcre2_match_data_free(match);
189  return de;
190 
191 error:
192  if (match) {
193  pcre2_match_data_free(match);
194  }
195  if (de) SCFree(de);
196  return NULL;
197 }
198 
199 /**
200  * \internal
201  * \brief this function is used to add the parsed secresult into the current signature
202  *
203  * \param de_ctx pointer to the Detection Engine Context
204  * \param s pointer to the Current Signature
205  * \param rawstr pointer to the user provided secresult options
206  *
207  * \retval 0 on Success
208  * \retval -1 on Failure
209  */
210 static int DetectRfbSecresultSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
211 {
212  DetectRfbSecresultData *de = NULL;
213 
215  return -1;
216 
217  de = DetectRfbSecresultParse(rawstr);
218  if (de == NULL)
219  goto error;
220 
222  de_ctx, s, DETECT_AL_RFB_SECRESULT, (SigMatchCtx *)de, rfb_secresult_id) == NULL) {
223  goto error;
224  }
225 
226  return 0;
227 
228 error:
229  if (de)
230  SCFree(de);
231  return -1;
232 }
233 
234 /**
235  * \internal
236  * \brief this function will free memory associated with DetectRfbSecresultData
237  *
238  * \param de pointer to DetectRfbSecresultData
239  */
241 {
243  if(de) SCFree(de);
244 }
245 
246 /*
247  * ONLY TESTS BELOW THIS COMMENT
248  */
249 
250 #ifdef UNITTESTS
251 /**
252  * \test RfbSecresultTestParse01 is a test for a valid secresult value
253  */
254 static int RfbSecresultTestParse01 (void)
255 {
256  DetectRfbSecresultData *de = DetectRfbSecresultParse("fail");
257 
258  FAIL_IF_NULL(de);
259 
260  DetectRfbSecresultFree(NULL, de);
261 
262  PASS;
263 }
264 
265 /**
266  * \test RfbSecresultTestParse02 is a test for an invalid secresult value
267  */
268 static int RfbSecresultTestParse02 (void)
269 {
270  DetectRfbSecresultData *de = DetectRfbSecresultParse("invalidopt");
271 
273 
274  PASS;
275 }
276 
277 /**
278  * \brief this function registers unit tests for RfbSecresult
279  */
280 void RfbSecresultRegisterTests(void)
281 {
282  UtRegisterTest("RfbSecresultTestParse01", RfbSecresultTestParse01);
283  UtRegisterTest("RfbSecresultTestParse02", RfbSecresultTestParse02);
284 }
285 #endif /* UNITTESTS */
DETECT_AL_RFB_SECRESULT
@ DETECT_AL_RFB_SECRESULT
Definition: detect-engine-register.h:278
DetectRfbSecresultData_::result
uint32_t result
Definition: detect-rfb-secresult.c:51
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
DetectRfbSecresultRegister
void DetectRfbSecresultRegister(void)
Registration function for rfb.secresult: keyword.
Definition: detect-rfb-secresult.c:57
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-rfb-secresult.c:35
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1296
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectRfbSecresult_::result
const char * result
Definition: detect-rfb-secresult.c:89
Flow_
Flow data structure.
Definition: flow.h:351
results
struct DetectRfbSecresult_ results[]
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
DetectRfbSecresultData_
Definition: detect-rfb-secresult.c:50
RFB_SECRESULT_TOOMANY
@ RFB_SECRESULT_TOOMANY
Definition: detect-rfb-secresult.c:79
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1267
rust.h
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
RFB_SECRESULT_UNKNOWN
@ RFB_SECRESULT_UNKNOWN
Definition: detect-rfb-secresult.c:80
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:267
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
RFB_SECRESULT_OK
@ RFB_SECRESULT_OK
Definition: detect-rfb-secresult.c:77
de
uint8_t de
Definition: app-layer-htp.c:581
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2800
detect.h
DetectRfbSecresult_::code
uint16_t code
Definition: detect-rfb-secresult.c:90
conf.h
DetectRfbSecresultFree
void DetectRfbSecresultFree(DetectEngineCtx *, void *)
Definition: detect-rfb-secresult.c:240
detect-rfb-secresult.h
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
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
ALPROTO_RFB
@ ALPROTO_RFB
Definition: app-layer-protos.h:55
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
DetectRfbSecresultData
struct DetectRfbSecresultData_ DetectRfbSecresultData
RFB_SECRESULT_FAIL
@ RFB_SECRESULT_FAIL
Definition: detect-rfb-secresult.c:78
DetectRfbSecresult_
Definition: detect-rfb-secresult.c:88
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288