suricata
detect-krb5-errcode.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-errcode.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 DetectKrb5ErrCodeRegister below */
43 static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *, Flow *,
44  uint8_t, void *, void *, const Signature *,
45  const SigMatchCtx *);
46 static int DetectKrb5ErrCodeSetup (DetectEngineCtx *, Signature *, const char *);
47 static void DetectKrb5ErrCodeFree (void *);
48 static void DetectKrb5ErrCodeRegisterTests (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_err_code_list_id = 0;
57 
58 /**
59  * \brief Registration function for krb5_err_code: keyword
60  *
61  * This function is called once in the 'lifetime' of the engine.
62  */
64  sigmatch_table[DETECT_AL_KRB5_ERRCODE].name = "krb5_err_code";
65  sigmatch_table[DETECT_AL_KRB5_ERRCODE].desc = "match Kerberos 5 error code";
66  sigmatch_table[DETECT_AL_KRB5_ERRCODE].url = DOC_URL DOC_VERSION "/rules/kerberos-keywords.html#krb5-err-code";
68  sigmatch_table[DETECT_AL_KRB5_ERRCODE].AppLayerTxMatch = DetectKrb5ErrCodeMatch;
69  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Setup = DetectKrb5ErrCodeSetup;
70  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Free = DetectKrb5ErrCodeFree;
71  sigmatch_table[DETECT_AL_KRB5_ERRCODE].RegisterTests = DetectKrb5ErrCodeRegisterTests;
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_err_code_list_id = DetectBufferTypeRegister("krb5_err_code");
85  SCLogDebug("g_krb5_err_code_list_id %d", g_krb5_err_code_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 DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *det_ctx,
110  Flow *f, uint8_t flags, void *state,
111  void *txv, const Signature *s,
112  const SigMatchCtx *ctx)
113 {
114  int32_t err_code;
115  int ret;
116  const DetectKrb5ErrCodeData *dd = (const DetectKrb5ErrCodeData *)ctx;
117 
118  SCEnter();
119 
120  ret = rs_krb5_tx_get_errcode(txv, &err_code);
121  if (ret != 0)
122  SCReturnInt(0);
123 
124  if (dd->err_code == err_code)
125  SCReturnInt(1);
126 
127  SCReturnInt(0);
128 }
129 
130 /**
131  * \brief This function is used to parse options passed via krb5_errcode: keyword
132  *
133  * \param krb5str Pointer to the user provided krb5_err_code options
134  *
135  * \retval krb5d pointer to DetectKrb5Data on success
136  * \retval NULL on failure
137  */
138 static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
139 {
140  DetectKrb5ErrCodeData *krb5d = NULL;
141  char arg1[4] = "";
142 #define MAX_SUBSTRINGS 30
143  int ret = 0, res = 0;
144  int ov[MAX_SUBSTRINGS];
145 
146  ret = pcre_exec(parse_regex, parse_regex_study,
147  krb5str, strlen(krb5str),
148  0, 0, ov, MAX_SUBSTRINGS);
149  if (ret != 2) {
150  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
151  goto error;
152  }
153 
154  res = pcre_copy_substring((char *) krb5str, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
155  if (res < 0) {
156  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
157  goto error;
158  }
159 
160  krb5d = SCMalloc(sizeof (DetectKrb5ErrCodeData));
161  if (unlikely(krb5d == NULL))
162  goto error;
163  krb5d->err_code = (int32_t)atoi(arg1);
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_err_code' 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 DetectKrb5ErrCodeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
185 {
186  DetectKrb5ErrCodeData *krb5d = NULL;
187  SigMatch *sm = NULL;
188 
190  return -1;
191 
192  krb5d = DetectKrb5ErrCodeParse(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_err_code_list_id);
204 
205  return 0;
206 
207 error:
208  if (krb5d != NULL)
209  DetectKrb5ErrCodeFree(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 DetectKrb5ErrCodeFree(void *ptr) {
222 
223  SCFree(krb5d);
224 }
225 
226 #ifdef UNITTESTS
227 
228 /**
229  * \test description of the test
230  */
231 
232 static int DetectKrb5ErrCodeParseTest01 (void)
233 {
234  DetectKrb5ErrCodeData *krb5d = DetectKrb5ErrCodeParse("10");
235  FAIL_IF_NULL(krb5d);
236  FAIL_IF(!(krb5d->err_code == 10));
237  DetectKrb5ErrCodeFree(krb5d);
238  PASS;
239 }
240 
241 static int DetectKrb5ErrCodeSignatureTest01 (void)
242 {
244  FAIL_IF_NULL(de_ctx);
245 
246  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_err_code:10; sid:1; rev:1;)");
247  FAIL_IF_NULL(sig);
248 
249  DetectEngineCtxFree(de_ctx);
250  PASS;
251 }
252 
253 #endif /* UNITTESTS */
254 
255 /**
256  * \brief this function registers unit tests for DetectKrb5ErrCode
257  */
258 static void DetectKrb5ErrCodeRegisterTests(void) {
259 #ifdef UNITTESTS
260  UtRegisterTest("DetectKrb5ErrCodeParseTest01", DetectKrb5ErrCodeParseTest01);
261  UtRegisterTest("DetectKrb5ErrCodeSignatureTest01",
262  DetectKrb5ErrCodeSignatureTest01);
263 #endif /* UNITTESTS */
264 }
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 MAX_SUBSTRINGS
#define PARSE_REGEX
Regex for parsing our keyword options.
#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 DetectKrb5ErrCodeRegister(void)
Registration function for krb5_err_code: keyword.
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
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
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)