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 #ifdef HAVE_RUST
33 
34 #include "app-layer-krb5.h"
35 #include "rust-krb-detect-gen.h"
36 
37 /**
38  * \brief Regex for parsing our keyword options
39  */
40 #define PARSE_REGEX "^\\s*([A-z0-9\\.]+|\"[A-z0-9_\\.]+\")\\s*$"
41 static pcre *parse_regex;
42 static pcre_extra *parse_regex_study;
43 
44 /* Prototypes of functions registered in DetectKrb5ErrCodeRegister below */
45 static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *, Flow *,
46  uint8_t, void *, void *, const Signature *,
47  const SigMatchCtx *);
48 static int DetectKrb5ErrCodeSetup (DetectEngineCtx *, Signature *, const char *);
49 static void DetectKrb5ErrCodeFree (void *);
50 static void DetectKrb5ErrCodeRegisterTests (void);
51 
52 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
53  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
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_err_code_list_id = 0;
59 
60 /**
61  * \brief Registration function for krb5_err_code: keyword
62  *
63  * This function is called once in the 'lifetime' of the engine.
64  */
65 void DetectKrb5ErrCodeRegister(void) {
66  sigmatch_table[DETECT_AL_KRB5_ERRCODE].name = "krb5_err_code";
67  sigmatch_table[DETECT_AL_KRB5_ERRCODE].desc = "match Kerberos 5 message type";
68  sigmatch_table[DETECT_AL_KRB5_ERRCODE].url = DOC_URL DOC_VERSION "/rules/kerberos-keywords.html#krb5_err_code";
70  sigmatch_table[DETECT_AL_KRB5_ERRCODE].AppLayerTxMatch = DetectKrb5ErrCodeMatch;
71  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Setup = DetectKrb5ErrCodeSetup;
72  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Free = DetectKrb5ErrCodeFree;
73  sigmatch_table[DETECT_AL_KRB5_ERRCODE].RegisterTests = DetectKrb5ErrCodeRegisterTests;
74 
77  DetectEngineInspectKRB5Generic);
78 
81  DetectEngineInspectKRB5Generic);
82 
83  /* set up the PCRE for keyword parsing */
84  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
85 
86  g_krb5_err_code_list_id = DetectBufferTypeRegister("krb5_err_code");
87  SCLogDebug("g_krb5_err_code_list_id %d", g_krb5_err_code_list_id);
88 }
89 
90 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
91  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
92  const Signature *s, const SigMatchData *smd,
93  Flow *f, uint8_t flags, void *alstate,
94  void *txv, uint64_t tx_id)
95 {
96  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
97  f, flags, alstate, txv, tx_id);
98 }
99 
100 /**
101  * \brief This function is used to match KRB5 rule option on a packet
102  *
103  * \param t pointer to thread vars
104  * \param det_ctx pointer to the pattern matcher thread
105  * \param p pointer to the current packet
106  * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
107  *
108  * \retval 0 no match
109  * \retval 1 match
110  */
111 static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *det_ctx,
112  Flow *f, uint8_t flags, void *state,
113  void *txv, const Signature *s,
114  const SigMatchCtx *ctx)
115 {
116  int32_t err_code;
117  int ret;
118  const DetectKrb5ErrCodeData *dd = (const DetectKrb5ErrCodeData *)ctx;
119 
120  SCEnter();
121 
122  ret = rs_krb5_tx_get_errcode(txv, &err_code);
123  if (ret != 0)
124  SCReturnInt(0);
125 
126  if (dd->err_code == err_code)
127  SCReturnInt(1);
128 
129  SCReturnInt(0);
130 }
131 
132 /**
133  * \brief This function is used to parse options passed via krb5_errcode: keyword
134  *
135  * \param krb5str Pointer to the user provided krb5_err_code options
136  *
137  * \retval krb5d pointer to DetectKrb5Data on success
138  * \retval NULL on failure
139  */
140 static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
141 {
142  DetectKrb5ErrCodeData *krb5d = NULL;
143  char arg1[4] = "";
144 #define MAX_SUBSTRINGS 30
145  int ret = 0, res = 0;
146  int ov[MAX_SUBSTRINGS];
147 
148  ret = pcre_exec(parse_regex, parse_regex_study,
149  krb5str, strlen(krb5str),
150  0, 0, ov, MAX_SUBSTRINGS);
151  if (ret != 2) {
152  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
153  goto error;
154  }
155 
156  res = pcre_copy_substring((char *) krb5str, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
157  if (res < 0) {
158  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
159  goto error;
160  }
161 
162  krb5d = SCMalloc(sizeof (DetectKrb5ErrCodeData));
163  if (unlikely(krb5d == NULL))
164  goto error;
165  krb5d->err_code = (int32_t)atoi(arg1);
166 
167  return krb5d;
168 
169 error:
170  if (krb5d)
171  SCFree(krb5d);
172  return NULL;
173 }
174 
175 /**
176  * \brief parse the options from the 'krb5_err_code' keyword in the rule into
177  * the Signature data structure.
178  *
179  * \param de_ctx pointer to the Detection Engine Context
180  * \param s pointer to the Current Signature
181  * \param krb5str pointer to the user provided options
182  *
183  * \retval 0 on Success
184  * \retval -1 on Failure
185  */
186 static int DetectKrb5ErrCodeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
187 {
188  DetectKrb5ErrCodeData *krb5d = NULL;
189  SigMatch *sm = NULL;
190 
192  return -1;
193 
194  krb5d = DetectKrb5ErrCodeParse(krb5str);
195  if (krb5d == NULL)
196  goto error;
197 
198  sm = SigMatchAlloc();
199  if (sm == NULL)
200  goto error;
201 
203  sm->ctx = (void *)krb5d;
204 
205  SigMatchAppendSMToList(s, sm, g_krb5_err_code_list_id);
206 
207  return 0;
208 
209 error:
210  if (krb5d != NULL)
211  DetectKrb5ErrCodeFree(krb5d);
212  if (sm != NULL)
213  SCFree(sm);
214  return -1;
215 }
216 
217 /**
218  * \brief this function will free memory associated with DetectKrb5Data
219  *
220  * \param ptr pointer to DetectKrb5Data
221  */
222 static void DetectKrb5ErrCodeFree(void *ptr) {
224 
225  SCFree(krb5d);
226 }
227 
228 #ifdef UNITTESTS
229 
230 /**
231  * \test description of the test
232  */
233 
234 static int DetectKrb5ErrCodeParseTest01 (void)
235 {
236  DetectKrb5ErrCodeData *krb5d = DetectKrb5ErrCodeParse("10");
237  FAIL_IF_NULL(krb5d);
238  FAIL_IF(!(krb5d->err_code == 10));
239  DetectKrb5ErrCodeFree(krb5d);
240  PASS;
241 }
242 
243 static int DetectKrb5ErrCodeSignatureTest01 (void)
244 {
246  FAIL_IF_NULL(de_ctx);
247 
248  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_err_code:10; sid:1; rev:1;)");
249  FAIL_IF_NULL(sig);
250 
251  DetectEngineCtxFree(de_ctx);
252  PASS;
253 }
254 
255 #endif /* UNITTESTS */
256 
257 /**
258  * \brief this function registers unit tests for DetectKrb5ErrCode
259  */
260 static void DetectKrb5ErrCodeRegisterTests(void) {
261 #ifdef UNITTESTS
262  UtRegisterTest("DetectKrb5ErrCodeParseTest01", DetectKrb5ErrCodeParseTest01);
263  UtRegisterTest("DetectKrb5ErrCodeSignatureTest01",
264  DetectKrb5ErrCodeSignatureTest01);
265 #endif /* UNITTESTS */
266 }
267 
268 #else /* HAVE_RUST */
269 
271 {
272 }
273 
274 #endif /* HAVE_RUST */
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:1439
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define SCLogDebug(...)
Definition: util-debug.h:335
#define MAX_SUBSTRINGS
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:322
const char * name
Definition: detect.h:1193
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:517
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
main detection engine ctx
Definition: detect.h:756
#define SIG_FLAG_TOCLIENT
Definition: detect.h:233
void(* Free)(void *)
Definition: detect.h:1184
#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)
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
#define SIG_FLAG_TOSERVER
Definition: detect.h:232
#define SCEnter(...)
Definition: util-debug.h:337
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1163
uint8_t type
Definition: detect.h:314
#define SCReturnInt(x)
Definition: util-debug.h:341
const char * desc
Definition: detect.h:1195
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:288
int DetectBufferTypeRegister(const char *name)
SigMatchCtx * ctx
Definition: detect.h:316
#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:1196
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1166
#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 PARSE_REGEX
#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:1185
a single match condition for a signature
Definition: detect.h:313
DetectEngineCtx * DetectEngineCtxInit(void)