suricata
detect-krb5-msgtype.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-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  * \file
20  *
21  * \author Pierre Chifflier <chifflier@wzdftpd.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "util-unittest.h"
26 #include "util-byte.h"
27 
28 #include "detect-parse.h"
29 #include "detect-engine.h"
30 
31 #include "detect-krb5-msgtype.h"
32 
33 #include "app-layer-krb5.h"
34 #include "rust.h"
35 
36 /**
37  * \brief Regex for parsing our keyword options
38  */
39 #define PARSE_REGEX "^\\s*([A-z0-9\\.]+|\"[A-z0-9_\\.]+\")\\s*$"
40 static DetectParseRegex parse_regex;
41 
42 /* Prototypes of functions registered in DetectKrb5MsgTypeRegister below */
43 static int DetectKrb5MsgTypeMatch (DetectEngineThreadCtx *, Flow *,
44  uint8_t, void *, void *, const Signature *,
45  const SigMatchCtx *);
46 static int DetectKrb5MsgTypeSetup (DetectEngineCtx *, Signature *, const char *);
47 static void DetectKrb5MsgTypeFree (DetectEngineCtx *, void *);
48 #ifdef UNITTESTS
49 static void DetectKrb5MsgTypeRegisterTests (void);
50 #endif
51 
52 static int g_krb5_msg_type_list_id = 0;
53 
54 /**
55  * \brief Registration function for krb5_msg_type: keyword
56  *
57  * This function is called once in the 'lifetime' of the engine.
58  */
60 {
61  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].name = "krb5_msg_type";
62  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].desc = "match Kerberos 5 message type";
63  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].url = "/rules/kerberos-keywords.html#krb5-msg-type";
65  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].AppLayerTxMatch = DetectKrb5MsgTypeMatch;
66  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Setup = DetectKrb5MsgTypeSetup;
67  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Free = DetectKrb5MsgTypeFree;
68 #ifdef UNITTESTS
69  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].RegisterTests = DetectKrb5MsgTypeRegisterTests;
70 #endif
71 
74 
77 
78  /* set up the PCRE for keyword parsing */
79  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
80 
81  g_krb5_msg_type_list_id = DetectBufferTypeRegister("krb5_msg_type");
82  SCLogDebug("g_krb5_msg_type_list_id %d", g_krb5_msg_type_list_id);
83 }
84 
85 /**
86  * \brief This function is used to match KRB5 rule option on a packet
87  *
88  * \param t pointer to thread vars
89  * \param det_ctx pointer to the pattern matcher thread
90  * \param p pointer to the current packet
91  * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
92  *
93  * \retval 0 no match
94  * \retval 1 match
95  */
96 static int DetectKrb5MsgTypeMatch (DetectEngineThreadCtx *det_ctx,
97  Flow *f, uint8_t flags, void *state,
98  void *txv, const Signature *s,
99  const SigMatchCtx *ctx)
100 {
101  uint32_t msg_type;
102  const DetectKrb5MsgTypeData *dd = (const DetectKrb5MsgTypeData *)ctx;
103 
104  SCEnter();
105 
106  rs_krb5_tx_get_msgtype(txv, &msg_type);
107 
108  if (dd->msg_type == msg_type)
109  SCReturnInt(1);
110 
111  SCReturnInt(0);
112 }
113 
114 /**
115  * \brief This function is used to parse options passed via krb5_msgtype: keyword
116  *
117  * \param krb5str Pointer to the user provided krb5_msg_type options
118  *
119  * \retval krb5d pointer to DetectKrb5Data on success
120  * \retval NULL on failure
121  */
122 static DetectKrb5MsgTypeData *DetectKrb5MsgTypeParse (const char *krb5str)
123 {
124  DetectKrb5MsgTypeData *krb5d = NULL;
125  char arg1[4] = "";
126  int res = 0;
127  size_t pcre2len;
128 
129  pcre2_match_data *match = NULL;
130  int ret = DetectParsePcreExec(&parse_regex, &match, krb5str, 0, 0);
131  if (ret != 2) {
132  SCLogError("parse error, ret %" PRId32 "", ret);
133  goto error;
134  }
135 
136  pcre2len = sizeof(arg1);
137  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
138  if (res < 0) {
139  SCLogError("pcre2_substring_copy_bynumber failed");
140  goto error;
141  }
142 
143  krb5d = SCMalloc(sizeof (DetectKrb5MsgTypeData));
144  if (unlikely(krb5d == NULL))
145  goto error;
146  if (StringParseUint8(&krb5d->msg_type, 10, 0,
147  (const char *)arg1) < 0) {
148  goto error;
149  }
150  pcre2_match_data_free(match);
151  return krb5d;
152 
153 error:
154  if (match) {
155  pcre2_match_data_free(match);
156  }
157  if (krb5d)
158  SCFree(krb5d);
159  return NULL;
160 }
161 
162 /**
163  * \brief parse the options from the 'krb5_msg_type' keyword in the rule into
164  * the Signature data structure.
165  *
166  * \param de_ctx pointer to the Detection Engine Context
167  * \param s pointer to the Current Signature
168  * \param krb5str pointer to the user provided options
169  *
170  * \retval 0 on Success
171  * \retval -1 on Failure
172  */
173 static int DetectKrb5MsgTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
174 {
175  DetectKrb5MsgTypeData *krb5d = NULL;
176  SigMatch *sm = NULL;
177 
179  return -1;
180 
181  krb5d = DetectKrb5MsgTypeParse(krb5str);
182  if (krb5d == NULL)
183  goto error;
184 
185  sm = SigMatchAlloc();
186  if (sm == NULL)
187  goto error;
188 
190  sm->ctx = (void *)krb5d;
191 
192  SigMatchAppendSMToList(s, sm, g_krb5_msg_type_list_id);
193 
194  return 0;
195 
196 error:
197  if (krb5d != NULL)
198  DetectKrb5MsgTypeFree(de_ctx, krb5d);
199  if (sm != NULL)
200  SCFree(sm);
201  return -1;
202 }
203 
204 /**
205  * \brief this function will free memory associated with DetectKrb5Data
206  *
207  * \param ptr pointer to DetectKrb5Data
208  */
209 static void DetectKrb5MsgTypeFree(DetectEngineCtx *de_ctx, void *ptr) {
211 
212  SCFree(krb5d);
213 }
214 
215 #ifdef UNITTESTS
216 
217 /**
218  * \test description of the test
219  */
220 
221 static int DetectKrb5MsgTypeParseTest01 (void)
222 {
223  DetectKrb5MsgTypeData *krb5d = DetectKrb5MsgTypeParse("10");
224  FAIL_IF_NULL(krb5d);
225  FAIL_IF(!(krb5d->msg_type == 10));
226  DetectKrb5MsgTypeFree(NULL, krb5d);
227  PASS;
228 }
229 
230 static int DetectKrb5MsgTypeSignatureTest01 (void)
231 {
234 
235  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_msg_type:10; sid:1; rev:1;)");
236  FAIL_IF_NULL(sig);
237 
239  PASS;
240 }
241 
242 /**
243  * \brief this function registers unit tests for DetectKrb5MsgType
244  */
245 static void DetectKrb5MsgTypeRegisterTests(void)
246 {
247  UtRegisterTest("DetectKrb5MsgTypeParseTest01", DetectKrb5MsgTypeParseTest01);
248  UtRegisterTest("DetectKrb5MsgTypeSignatureTest01",
249  DetectKrb5MsgTypeSignatureTest01);
250 }
251 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1284
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
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:826
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2592
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1255
rust.h
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2623
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2569
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:256
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
util-unittest.h
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
app-layer-krb5.h
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:361
detect-krb5-msgtype.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
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:1074
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2747
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
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:216
DETECT_AL_KRB5_MSGTYPE
@ DETECT_AL_KRB5_MSGTYPE
Definition: detect-engine-register.h:255
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-krb5-msgtype.c:39
DetectKrb5MsgTypeData_
Definition: detect-krb5-msgtype.h:27
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1252
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:335
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1060
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
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:2122
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DetectKrb5MsgTypeRegister
void DetectKrb5MsgTypeRegister(void)
Registration function for krb5_msg_type: keyword.
Definition: detect-krb5-msgtype.c:59
DetectKrb5MsgTypeData_::msg_type
uint8_t msg_type
Definition: detect-krb5-msgtype.h:28
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:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2553
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276