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 DetectEngineInspectKRB5Generic(ThreadVars *tv,
54  const Signature *s, const SigMatchData *smd,
55  Flow *f, uint8_t flags, void *alstate,
56  void *txv, uint64_t tx_id);
57 
58 static int g_krb5_msg_type_list_id = 0;
59 
60 /**
61  * \brief Registration function for krb5_msg_type: keyword
62  *
63  * This function is called once in the 'lifetime' of the engine.
64  */
66 {
67  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].name = "krb5_msg_type";
68  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].desc = "match Kerberos 5 message type";
69  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].url = "/rules/kerberos-keywords.html#krb5-msg-type";
71  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].AppLayerTxMatch = DetectKrb5MsgTypeMatch;
72  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Setup = DetectKrb5MsgTypeSetup;
73  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].Free = DetectKrb5MsgTypeFree;
74 #ifdef UNITTESTS
75  sigmatch_table[DETECT_AL_KRB5_MSGTYPE].RegisterTests = DetectKrb5MsgTypeRegisterTests;
76 #endif
79  DetectEngineInspectKRB5Generic);
80 
83  DetectEngineInspectKRB5Generic);
84 
85  /* set up the PCRE for keyword parsing */
86  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
87 
88  g_krb5_msg_type_list_id = DetectBufferTypeRegister("krb5_msg_type");
89  SCLogDebug("g_krb5_msg_type_list_id %d", g_krb5_msg_type_list_id);
90 }
91 
92 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
94  const Signature *s, const SigMatchData *smd,
95  Flow *f, uint8_t flags, void *alstate,
96  void *txv, uint64_t tx_id)
97 {
98  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
99  f, flags, alstate, txv, tx_id);
100 }
101 
102 /**
103  * \brief This function is used to match KRB5 rule option on a packet
104  *
105  * \param t pointer to thread vars
106  * \param det_ctx pointer to the pattern matcher thread
107  * \param p pointer to the current packet
108  * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
109  *
110  * \retval 0 no match
111  * \retval 1 match
112  */
113 static int DetectKrb5MsgTypeMatch (DetectEngineThreadCtx *det_ctx,
114  Flow *f, uint8_t flags, void *state,
115  void *txv, const Signature *s,
116  const SigMatchCtx *ctx)
117 {
118  uint32_t msg_type;
119  const DetectKrb5MsgTypeData *dd = (const DetectKrb5MsgTypeData *)ctx;
120 
121  SCEnter();
122 
123  rs_krb5_tx_get_msgtype(txv, &msg_type);
124 
125  if (dd->msg_type == msg_type)
126  SCReturnInt(1);
127 
128  SCReturnInt(0);
129 }
130 
131 /**
132  * \brief This function is used to parse options passed via krb5_msgtype: keyword
133  *
134  * \param krb5str Pointer to the user provided krb5_msg_type options
135  *
136  * \retval krb5d pointer to DetectKrb5Data on success
137  * \retval NULL on failure
138  */
139 static DetectKrb5MsgTypeData *DetectKrb5MsgTypeParse (const char *krb5str)
140 {
141  DetectKrb5MsgTypeData *krb5d = NULL;
142  char arg1[4] = "";
143  int ret = 0, res = 0;
144  int ov[MAX_SUBSTRINGS];
145 
146  ret = DetectParsePcreExec(&parse_regex, krb5str, 0, 0, ov, MAX_SUBSTRINGS);
147  if (ret != 2) {
148  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
149  goto error;
150  }
151 
152  res = pcre_copy_substring((char *) krb5str, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
153  if (res < 0) {
154  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
155  goto error;
156  }
157 
158  krb5d = SCMalloc(sizeof (DetectKrb5MsgTypeData));
159  if (unlikely(krb5d == NULL))
160  goto error;
161  if (StringParseUint8(&krb5d->msg_type, 10, 0,
162  (const char *)arg1) < 0) {
163  goto error;
164  }
165  return krb5d;
166 
167 error:
168  if (krb5d)
169  SCFree(krb5d);
170  return NULL;
171 }
172 
173 /**
174  * \brief parse the options from the 'krb5_msg_type' keyword in the rule into
175  * the Signature data structure.
176  *
177  * \param de_ctx pointer to the Detection Engine Context
178  * \param s pointer to the Current Signature
179  * \param krb5str pointer to the user provided options
180  *
181  * \retval 0 on Success
182  * \retval -1 on Failure
183  */
184 static int DetectKrb5MsgTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
185 {
186  DetectKrb5MsgTypeData *krb5d = NULL;
187  SigMatch *sm = NULL;
188 
190  return -1;
191 
192  krb5d = DetectKrb5MsgTypeParse(krb5str);
193  if (krb5d == NULL)
194  goto error;
195 
196  sm = SigMatchAlloc();
197  if (sm == NULL)
198  goto error;
199 
201  sm->ctx = (void *)krb5d;
202 
203  SigMatchAppendSMToList(s, sm, g_krb5_msg_type_list_id);
204 
205  return 0;
206 
207 error:
208  if (krb5d != NULL)
209  DetectKrb5MsgTypeFree(de_ctx, krb5d);
210  if (sm != NULL)
211  SCFree(sm);
212  return -1;
213 }
214 
215 /**
216  * \brief this function will free memory associated with DetectKrb5Data
217  *
218  * \param ptr pointer to DetectKrb5Data
219  */
220 static void DetectKrb5MsgTypeFree(DetectEngineCtx *de_ctx, void *ptr) {
222 
223  SCFree(krb5d);
224 }
225 
226 #ifdef UNITTESTS
227 
228 /**
229  * \test description of the test
230  */
231 
232 static int DetectKrb5MsgTypeParseTest01 (void)
233 {
234  DetectKrb5MsgTypeData *krb5d = DetectKrb5MsgTypeParse("10");
235  FAIL_IF_NULL(krb5d);
236  FAIL_IF(!(krb5d->msg_type == 10));
237  DetectKrb5MsgTypeFree(NULL, krb5d);
238  PASS;
239 }
240 
241 static int DetectKrb5MsgTypeSignatureTest01 (void)
242 {
245 
246  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_msg_type:10; sid:1; rev:1;)");
247  FAIL_IF_NULL(sig);
248 
250  PASS;
251 }
252 
253 /**
254  * \brief this function registers unit tests for DetectKrb5MsgType
255  */
256 static void DetectKrb5MsgTypeRegisterTests(void)
257 {
258  UtRegisterTest("DetectKrb5MsgTypeParseTest01", DetectKrb5MsgTypeParseTest01);
259  UtRegisterTest("DetectKrb5MsgTypeSignatureTest01",
260  DetectKrb5MsgTypeSignatureTest01);
261 }
262 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
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:1213
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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:298
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1182
rust.h
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigMatchData_
Data needed for Match()
Definition: detect.h:329
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
DetectEngineInspectGenericList
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.
Definition: detect-engine.c:1596
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
app-layer-krb5.h
detect-krb5-msgtype.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1010
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2493
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
DETECT_AL_KRB5_MSGTYPE
@ DETECT_AL_KRB5_MSGTYPE
Definition: detect-engine-register.h:241
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:1179
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:315
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2361
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:836
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
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:65
DetectKrb5MsgTypeData_::msg_type
uint8_t msg_type
Definition: detect-krb5-msgtype.h:28
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
StringParseUint8
int StringParseUint8(uint8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:359
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
Definition: detect-engine.c:171
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
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203