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 ret = 0, found = 0;
162 
163  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
164  if (ret < 1) {
165  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
166  goto error;
167  }
168 
169  for(i = 0; results[i].result != NULL; i++) {
170  if((strcasecmp(results[i].result,rawstr)) == 0) {
171  found = 1;
172  break;
173  }
174  }
175 
176  if(found == 0) {
177  SCLogError(SC_ERR_UNKNOWN_VALUE, "unknown secresult value %s", rawstr);
178  goto error;
179  }
180 
182  if (unlikely(de == NULL))
183  goto error;
184 
185  de->result = results[i].code;
186 
187  return de;
188 
189 error:
190  if (de) SCFree(de);
191  return NULL;
192 }
193 
194 /**
195  * \internal
196  * \brief this function is used to add the parsed secresult into the current signature
197  *
198  * \param de_ctx pointer to the Detection Engine Context
199  * \param s pointer to the Current Signature
200  * \param rawstr pointer to the user provided secresult options
201  *
202  * \retval 0 on Success
203  * \retval -1 on Failure
204  */
205 static int DetectRfbSecresultSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
206 {
207  DetectRfbSecresultData *de = NULL;
208  SigMatch *sm = NULL;
209 
211  return -1;
212 
213  de = DetectRfbSecresultParse(rawstr);
214  if (de == NULL)
215  goto error;
216 
217  sm = SigMatchAlloc();
218  if (sm == NULL)
219  goto error;
220 
222  sm->ctx = (SigMatchCtx *)de;
223 
224  SigMatchAppendSMToList(s, sm, rfb_secresult_id);
225 
226  return 0;
227 
228 error:
229  if (de) SCFree(de);
230  if (sm) SCFree(sm);
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:262
RFB_SECRESULT_UNKNOWN
@ RFB_SECRESULT_UNKNOWN
Definition: detect-rfb-secresult.c:80
DetectRfbSecresultData_::result
uint32_t result
Definition: detect-rfb-secresult.c:51
SigTableElmt_::url
const char * url
Definition: detect.h:1248
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
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:1247
RFB_SECRESULT_TOOMANY
@ RFB_SECRESULT_TOOMANY
Definition: detect-rfb-secresult.c:79
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2475
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1235
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:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
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:353
results
struct DetectRfbSecresult_ results[]
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DetectRfbSecresultData_
Definition: detect-rfb-secresult.c:50
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1216
rust.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
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:1034
de
uint8_t de
Definition: app-layer-htp.c:568
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
detect.h
DetectRfbSecresult_::code
uint16_t code
Definition: detect-rfb-secresult.c:90
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
RFB_SECRESULT_FAIL
@ RFB_SECRESULT_FAIL
Definition: detect-rfb-secresult.c:78
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:227
conf.h
DetectRfbSecresultFree
void DetectRfbSecresultFree(DetectEngineCtx *, void *)
Definition: detect-rfb-secresult.c:240
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
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:309
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:315
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:255
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:1951
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
RFB_SECRESULT_OK
@ RFB_SECRESULT_OK
Definition: detect-rfb-secresult.c:77
ALPROTO_RFB
@ ALPROTO_RFB
Definition: app-layer-protos.h:55
DetectRfbSecresultData
struct DetectRfbSecresultData_ DetectRfbSecresultData
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
DetectRfbSecresult_
Definition: detect-rfb-secresult.c:88
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237