suricata
detect-krb5-msgtype.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 
27 #include "detect-parse.h"
28 #include "detect-engine.h"
29 
30 #include "detect-krb5-msgtype.h"
31 
32 #include "app-layer-krb5.h"
33 #include "rust-krb-detect-gen.h"
34 
35 /**
36  * \brief Regex for parsing our keyword options
37  */
38 #define PARSE_REGEX "^\\s*([A-z0-9\\.]+|\"[A-z0-9_\\.]+\")\\s*$"
39 static pcre *parse_regex;
40 static pcre_extra *parse_regex_study;
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 (void *);
48 static void DetectKrb5MsgTypeRegisterTests (void);
49 
50 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
51  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
52  const Signature *s, const SigMatchData *smd,
53  Flow *f, uint8_t flags, void *alstate,
54  void *txv, uint64_t tx_id);
55 
56 static int g_krb5_msg_type_list_id = 0;
57 
58 /**
59  * \brief Registration function for krb5_msg_type: keyword
60  *
61  * This function is called once in the 'lifetime' of the engine.
62  */
64  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].name = "krb5_msg_type";
65  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].desc = "match Kerberos 5 message type";
66  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].url = DOC_URL DOC_VERSION "/rules/kerberos-keywords.html#krb5-msg-type";
68  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].AppLayerTxMatch = DetectKrb5MsgTypeMatch;
69  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Setup = DetectKrb5MsgTypeSetup;
70  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Free = DetectKrb5MsgTypeFree;
71  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].RegisterTests = DetectKrb5MsgTypeRegisterTests;
72 
75  DetectEngineInspectKRB5Generic);
76 
79  DetectEngineInspectKRB5Generic);
80 
81  /* set up the PCRE for keyword parsing */
82  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
83 
84  g_krb5_msg_type_list_id = DetectBufferTypeRegister("krb5_msg_type");
85  SCLogDebug("g_krb5_msg_type_list_id %d", g_krb5_msg_type_list_id);
86 }
87 
88 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
89  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
90  const Signature *s, const SigMatchData *smd,
91  Flow *f, uint8_t flags, void *alstate,
92  void *txv, uint64_t tx_id)
93 {
94  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
95  f, flags, alstate, txv, tx_id);
96 }
97 
98 /**
99  * \brief This function is used to match KRB5 rule option on a packet
100  *
101  * \param t pointer to thread vars
102  * \param det_ctx pointer to the pattern matcher thread
103  * \param p pointer to the current packet
104  * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
105  *
106  * \retval 0 no match
107  * \retval 1 match
108  */
109 static int DetectKrb5MsgTypeMatch (DetectEngineThreadCtx *det_ctx,
110  Flow *f, uint8_t flags, void *state,
111  void *txv, const Signature *s,
112  const SigMatchCtx *ctx)
113 {
114  uint32_t msg_type;
115  const DetectKrb5MsgTypeData *dd = (const DetectKrb5MsgTypeData *)ctx;
116 
117  SCEnter();
118 
119  rs_krb5_tx_get_msgtype(txv, &msg_type);
120 
121  if (dd->msg_type == msg_type)
122  SCReturnInt(1);
123 
124  SCReturnInt(0);
125 }
126 
127 /**
128  * \brief This function is used to parse options passed via krb5_msgtype: keyword
129  *
130  * \param krb5str Pointer to the user provided krb5_msg_type options
131  *
132  * \retval krb5d pointer to DetectKrb5Data on success
133  * \retval NULL on failure
134  */
135 static DetectKrb5MsgTypeData *DetectKrb5MsgTypeParse (const char *krb5str)
136 {
137  DetectKrb5MsgTypeData *krb5d = NULL;
138  char arg1[4] = "";
139 #define MAX_SUBSTRINGS 30
140  int ret = 0, res = 0;
141  int ov[MAX_SUBSTRINGS];
142 
143  ret = pcre_exec(parse_regex, parse_regex_study,
144  krb5str, strlen(krb5str),
145  0, 0, ov, MAX_SUBSTRINGS);
146  if (ret != 2) {
147  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
148  goto error;
149  }
150 
151  res = pcre_copy_substring((char *) krb5str, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
152  if (res < 0) {
153  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
154  goto error;
155  }
156 
157  krb5d = SCMalloc(sizeof (DetectKrb5MsgTypeData));
158  if (unlikely(krb5d == NULL))
159  goto error;
160  krb5d->msg_type = (uint8_t)atoi(arg1);
161 
162  return krb5d;
163 
164 error:
165  if (krb5d)
166  SCFree(krb5d);
167  return NULL;
168 }
169 
170 /**
171  * \brief parse the options from the 'krb5_msg_type' keyword in the rule into
172  * the Signature data structure.
173  *
174  * \param de_ctx pointer to the Detection Engine Context
175  * \param s pointer to the Current Signature
176  * \param krb5str pointer to the user provided options
177  *
178  * \retval 0 on Success
179  * \retval -1 on Failure
180  */
181 static int DetectKrb5MsgTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
182 {
183  DetectKrb5MsgTypeData *krb5d = NULL;
184  SigMatch *sm = NULL;
185 
187  return -1;
188 
189  krb5d = DetectKrb5MsgTypeParse(krb5str);
190  if (krb5d == NULL)
191  goto error;
192 
193  sm = SigMatchAlloc();
194  if (sm == NULL)
195  goto error;
196 
198  sm->ctx = (void *)krb5d;
199 
200  SigMatchAppendSMToList(s, sm, g_krb5_msg_type_list_id);
201 
202  return 0;
203 
204 error:
205  if (krb5d != NULL)
206  DetectKrb5MsgTypeFree(krb5d);
207  if (sm != NULL)
208  SCFree(sm);
209  return -1;
210 }
211 
212 /**
213  * \brief this function will free memory associated with DetectKrb5Data
214  *
215  * \param ptr pointer to DetectKrb5Data
216  */
217 static void DetectKrb5MsgTypeFree(void *ptr) {
219 
220  SCFree(krb5d);
221 }
222 
223 #ifdef UNITTESTS
224 
225 /**
226  * \test description of the test
227  */
228 
229 static int DetectKrb5MsgTypeParseTest01 (void)
230 {
231  DetectKrb5MsgTypeData *krb5d = DetectKrb5MsgTypeParse("10");
232  FAIL_IF_NULL(krb5d);
233  FAIL_IF(!(krb5d->msg_type == 10));
234  DetectKrb5MsgTypeFree(krb5d);
235  PASS;
236 }
237 
238 static int DetectKrb5MsgTypeSignatureTest01 (void)
239 {
241  FAIL_IF_NULL(de_ctx);
242 
243  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_msg_type:10; sid:1; rev:1;)");
244  FAIL_IF_NULL(sig);
245 
246  DetectEngineCtxFree(de_ctx);
247  PASS;
248 }
249 
250 #endif /* UNITTESTS */
251 
252 /**
253  * \brief this function registers unit tests for DetectKrb5MsgType
254  */
255 static void DetectKrb5MsgTypeRegisterTests(void) {
256 #ifdef UNITTESTS
257  UtRegisterTest("DetectKrb5MsgTypeParseTest01", DetectKrb5MsgTypeParseTest01);
258  UtRegisterTest("DetectKrb5MsgTypeSignatureTest01",
259  DetectKrb5MsgTypeSignatureTest01);
260 #endif /* UNITTESTS */
261 }
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
#define PASS
Pass the test.
#define unlikely(expr)
Definition: util-optimize.h:35
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
Data needed for Match()
Definition: detect.h:327
const char * name
Definition: detect.h:1200
int DetectEngineInspectGenericList(ThreadVars *tv, 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.
Signature container.
Definition: detect.h:522
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
main detection engine ctx
Definition: detect.h:761
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
void(* Free)(void *)
Definition: detect.h:1191
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
#define SCEnter(...)
Definition: util-debug.h:337
#define PARSE_REGEX
Regex for parsing our keyword options.
#define MAX_SUBSTRINGS
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1170
uint8_t type
Definition: detect.h:319
#define SCReturnInt(x)
Definition: util-debug.h:341
const char * desc
Definition: detect.h:1202
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:346
int DetectBufferTypeRegister(const char *name)
SigMatchCtx * ctx
Definition: detect.h:321
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
PoolThreadReserved res
void DetectKrb5MsgTypeRegister(void)
Registration function for krb5_msg_type: keyword.
uint16_t tx_id
const char * url
Definition: detect.h:1203
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1173
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
#define DOC_URL
Definition: suricata.h:86
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
Per thread variable structure.
Definition: threadvars.h:57
#define DOC_VERSION
Definition: suricata.h:91
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Flow data structure.
Definition: flow.h:325
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
void(* RegisterTests)(void)
Definition: detect.h:1192
a single match condition for a signature
Definition: detect.h:318
DetectEngineCtx * DetectEngineCtxInit(void)