suricata
detect-ike-exch-type.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 Frank Honza <frank.honza@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-ike-exch-type.h"
30 #include "app-layer-parser.h"
31 #include "util-byte.h"
32 #include "detect-engine-uint.h"
33 
34 #include "rust-bindings.h"
35 
36 /**
37  * [ike.exchtype]:[<|>|<=|>=]<type>;
38  */
39 
40 static int DetectIkeExchTypeSetup(DetectEngineCtx *, Signature *s, const char *str);
41 static void DetectIkeExchTypeFree(DetectEngineCtx *, void *);
42 static int g_ike_exch_type_buffer_id = 0;
43 
44 static int DetectEngineInspectIkeExchTypeGeneric(DetectEngineCtx *de_ctx,
45  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
46  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
47 
48 static int DetectIkeExchTypeMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *, void *,
49  const Signature *, const SigMatchCtx *);
50 
51 /**
52  * \brief Registration function for ike.exchtype keyword.
53  */
55 {
57  sigmatch_table[DETECT_AL_IKE_EXCH_TYPE].desc = "match IKE exchange type";
58  sigmatch_table[DETECT_AL_IKE_EXCH_TYPE].url = "/rules/ike-keywords.html#ike-exchtype";
60  sigmatch_table[DETECT_AL_IKE_EXCH_TYPE].AppLayerTxMatch = DetectIkeExchTypeMatch;
61  sigmatch_table[DETECT_AL_IKE_EXCH_TYPE].Setup = DetectIkeExchTypeSetup;
62  sigmatch_table[DETECT_AL_IKE_EXCH_TYPE].Free = DetectIkeExchTypeFree;
63 
65  DetectEngineInspectIkeExchTypeGeneric, NULL);
66 
68  DetectEngineInspectIkeExchTypeGeneric, NULL);
69 
70  g_ike_exch_type_buffer_id = DetectBufferTypeGetByName("ike.exchtype");
71 
73 }
74 
75 static int DetectEngineInspectIkeExchTypeGeneric(DetectEngineCtx *de_ctx,
76  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
77  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
78 {
80  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
81 }
82 
83 /**
84  * \internal
85  * \brief Function to match exchange type of a IKE state
86  *
87  * \param det_ctx Pointer to the pattern matcher thread.
88  * \param f Pointer to the current flow.
89  * \param flags Flags.
90  * \param state App layer state.
91  * \param txv Pointer to the Ike Transaction.
92  * \param s Pointer to the Signature.
93  * \param ctx Pointer to the sigmatch that we will cast into DetectU8Data.
94  *
95  * \retval 0 no match.
96  * \retval 1 match.
97  */
98 static int DetectIkeExchTypeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
99  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
100 {
101  SCEnter();
102 
103  uint8_t exch_type;
104  if (!rs_ike_state_get_exch_type(txv, &exch_type))
105  SCReturnInt(0);
106 
107  const DetectU8Data *du8 = (const DetectU8Data *)ctx;
108  SCReturnInt(DetectU8Match(exch_type, du8));
109 }
110 
111 /**
112  * \brief Function to add the parsed IKE exchange type field into the current signature.
113  *
114  * \param de_ctx Pointer to the Detection Engine Context.
115  * \param s Pointer to the Current Signature.
116  * \param rawstr Pointer to the user provided flags options.
117  *
118  * \retval 0 on Success.
119  * \retval -1 on Failure.
120  */
121 static int DetectIkeExchTypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
122 {
124  return -1;
125 
126  DetectU8Data *ike_exch_type = DetectU8Parse(rawstr);
127  if (ike_exch_type == NULL)
128  return -1;
129 
130  /* okay so far so good, lets get this into a SigMatch
131  * and put it in the Signature. */
132  SigMatch *sm = SigMatchAlloc();
133  if (sm == NULL)
134  goto error;
135 
137  sm->ctx = (SigMatchCtx *)ike_exch_type;
138 
139  SigMatchAppendSMToList(s, sm, g_ike_exch_type_buffer_id);
140  return 0;
141 
142 error:
143  DetectIkeExchTypeFree(de_ctx, ike_exch_type);
144  return -1;
145 }
146 
147 /**
148  * \internal
149  * \brief Function to free memory associated with DetectU8Data.
150  *
151  * \param de_ptr Pointer to DetectU8Data.
152  */
153 static void DetectIkeExchTypeFree(DetectEngineCtx *de_ctx, void *ptr)
154 {
155  SCFree(ptr);
156 }
util-byte.h
detect-engine-uint.h
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
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
DetectIkeExchTypeRegister
void DetectIkeExchTypeRegister(void)
Registration function for ike.exchtype keyword.
Definition: detect-ike-exch-type.c:54
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
SigTableElmt_::name
const char * name
Definition: detect.h:1201
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1172
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectU8Data *du8)
Definition: detect-engine-uint.c:294
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
DetectU8Data_
Definition: detect-engine-uint.h:52
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
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
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1169
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
DETECT_AL_IKE_EXCH_TYPE
@ DETECT_AL_IKE_EXCH_TYPE
Definition: detect-engine-register.h:302
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
detect-ike-exch-type.h
str
#define str(s)
Definition: suricata-common.h:272
DetectU8Parse
DetectU8Data * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:370
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
DetectUintRegister
void DetectUintRegister(void)
Definition: detect-engine-uint.c:284
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349