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 "detect-engine-uint.h"
31 #include "app-layer-parser.h"
32 #include "util-byte.h"
33 
34 #include "rust-bindings.h"
35 
36 
37 static int DetectRfbSectypeSetup (DetectEngineCtx *, Signature *s, const char *str);
38 static void DetectRfbSectypeFree(DetectEngineCtx *, void *);
39 static int g_rfb_sectype_buffer_id = 0;
40 
41 static int DetectRfbSectypeMatch (DetectEngineThreadCtx *, Flow *,
42  uint8_t, void *, void *, const Signature *,
43  const SigMatchCtx *);
44 
45 /**
46  * \brief Registration function for rfb.sectype keyword.
47  */
49 {
50  sigmatch_table[DETECT_AL_RFB_SECTYPE].name = "rfb.sectype";
51  sigmatch_table[DETECT_AL_RFB_SECTYPE].desc = "match RFB security type";
52  sigmatch_table[DETECT_AL_RFB_SECTYPE].url = "/rules/rfb-keywords.html#rfb-sectype";
53  sigmatch_table[DETECT_AL_RFB_SECTYPE].AppLayerTxMatch = DetectRfbSectypeMatch;
54  sigmatch_table[DETECT_AL_RFB_SECTYPE].Setup = DetectRfbSectypeSetup;
55  sigmatch_table[DETECT_AL_RFB_SECTYPE].Free = DetectRfbSectypeFree;
56 
59 
60  g_rfb_sectype_buffer_id = DetectBufferTypeGetByName("rfb.sectype");
61 }
62 
63 /**
64  * \internal
65  * \brief Function to match security type of a RFB TX
66  *
67  * \param det_ctx Pointer to the pattern matcher thread.
68  * \param f Pointer to the current flow.
69  * \param flags Flags.
70  * \param state App layer state.
71  * \param txv Pointer to the RFBTransaction.
72  * \param s Pointer to the Signature.
73  * \param ctx Pointer to the sigmatch that we will cast into DetectU32Data.
74  *
75  * \retval 0 no match.
76  * \retval 1 match.
77  */
78 static int DetectRfbSectypeMatch (DetectEngineThreadCtx *det_ctx,
79  Flow *f, uint8_t flags, void *state,
80  void *txv, const Signature *s,
81  const SigMatchCtx *ctx)
82 {
83  SCEnter();
84 
85  const DetectU32Data *dd = (const DetectU32Data *)ctx;
86  uint32_t version;
87  rs_rfb_tx_get_sectype(txv, &version);
88  if (DetectU32Match(version, dd))
89  SCReturnInt(1);
90  SCReturnInt(0);
91 }
92 
93 /**
94  * \internal
95  * \brief Function to parse options passed via rfb.sectype keywords.
96  *
97  * \param rawstr Pointer to the user provided options.
98  *
99  * \retval dd pointer to DetectU32Data on success.
100  * \retval NULL on failure.
101  */
102 static DetectU32Data *DetectRfbSectypeParse(const char *rawstr)
103 {
104  return DetectU32Parse(rawstr);
105 }
106 
107 /**
108  * \brief Function to add the parsed RFB security type field into the current signature.
109  *
110  * \param de_ctx Pointer to the Detection Engine Context.
111  * \param s Pointer to the Current Signature.
112  * \param rawstr Pointer to the user provided flags options.
113  *
114  * \retval 0 on Success.
115  * \retval -1 on Failure.
116  */
117 static int DetectRfbSectypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
118 {
120  return -1;
121 
122  DetectU32Data *dd = DetectRfbSectypeParse(rawstr);
123  if (dd == NULL) {
124  SCLogError("Parsing \'%s\' failed", rawstr);
125  goto error;
126  }
127 
128  /* okay so far so good, lets get this into a SigMatch
129  * and put it in the Signature. */
130 
132  g_rfb_sectype_buffer_id) == NULL) {
133  goto error;
134  }
135  return 0;
136 
137 error:
138  DetectRfbSectypeFree(de_ctx, dd);
139  return -1;
140 }
141 
142 /**
143  * \internal
144  * \brief Function to free memory associated with DetectU32Data.
145  *
146  * \param de_ptr Pointer to DetectU32Data.
147  */
148 static void DetectRfbSectypeFree(DetectEngineCtx *de_ctx, void *ptr)
149 {
150  rs_detect_u32_free(ptr);
151 }
util-byte.h
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
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
SigTableElmt_::name
const char * name
Definition: detect.h:1296
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
Flow_
Flow data structure.
Definition: flow.h:351
detect-rfb-sectype.h
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
DetectRfbSectypeRegister
void DetectRfbSectypeRegister(void)
Registration function for rfb.sectype keyword.
Definition: detect-rfb-sectype.c:48
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
app-layer-parser.h
conf.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
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
version
uint8_t version
Definition: decode-gre.h:1
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
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DETECT_AL_RFB_SECTYPE
@ DETECT_AL_RFB_SECTYPE
Definition: detect-engine-register.h:279