suricata
detect-krb5-errcode.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-errcode.h"
32
33#include "rust.h"
34
35/**
36 * \brief Regex for parsing our keyword options
37 */
38#define PARSE_REGEX "^\\s*([A-z0-9\\.]+|\"[A-z0-9_\\.]+\")\\s*$"
39static DetectParseRegex parse_regex;
40
41/* Prototypes of functions registered in DetectKrb5ErrCodeRegister below */
42static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *, Flow *,
43 uint8_t, void *, void *, const Signature *,
44 const SigMatchCtx *);
45static int DetectKrb5ErrCodeSetup (DetectEngineCtx *, Signature *, const char *);
46static void DetectKrb5ErrCodeFree (DetectEngineCtx *, void *);
47#ifdef UNITTESTS
48static void DetectKrb5ErrCodeRegisterTests (void);
49#endif
50
51static int g_krb5_err_code_list_id = 0;
52
53/**
54 * \brief Registration function for krb5_err_code: keyword
55 *
56 * This function is called once in the 'lifetime' of the engine.
57 */
59{
60 sigmatch_table[DETECT_KRB5_ERRCODE].name = "krb5_err_code";
61 sigmatch_table[DETECT_KRB5_ERRCODE].desc = "match Kerberos 5 error code";
62 sigmatch_table[DETECT_KRB5_ERRCODE].url = "/rules/kerberos-keywords.html#krb5-err-code";
64 sigmatch_table[DETECT_KRB5_ERRCODE].AppLayerTxMatch = DetectKrb5ErrCodeMatch;
65 sigmatch_table[DETECT_KRB5_ERRCODE].Setup = DetectKrb5ErrCodeSetup;
66 sigmatch_table[DETECT_KRB5_ERRCODE].Free = DetectKrb5ErrCodeFree;
67#ifdef UNITTESTS
68 sigmatch_table[DETECT_KRB5_ERRCODE].RegisterTests = DetectKrb5ErrCodeRegisterTests;
69#endif
70
73
76
77 /* set up the PCRE for keyword parsing */
79
80 g_krb5_err_code_list_id = DetectBufferTypeRegister("krb5_err_code");
81 SCLogDebug("g_krb5_err_code_list_id %d", g_krb5_err_code_list_id);
82}
83
84/**
85 * \brief This function is used to match KRB5 rule option on a packet
86 *
87 * \param t pointer to thread vars
88 * \param det_ctx pointer to the pattern matcher thread
89 * \param p pointer to the current packet
90 * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
91 *
92 * \retval 0 no match
93 * \retval 1 match
94 */
95static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *det_ctx,
96 Flow *f, uint8_t flags, void *state,
97 void *txv, const Signature *s,
98 const SigMatchCtx *ctx)
99{
100 int32_t err_code;
101 int ret;
103
104 SCEnter();
105
106 ret = SCKrb5TxGetErrorCode(txv, &err_code);
107 if (ret != 0)
108 SCReturnInt(0);
109
110 if (dd->err_code == err_code)
111 SCReturnInt(1);
112
113 SCReturnInt(0);
114}
115
116/**
117 * \brief This function is used to parse options passed via krb5_errcode: keyword
118 *
119 * \param krb5str Pointer to the user provided krb5_err_code options
120 *
121 * \retval krb5d pointer to DetectKrb5Data on success
122 * \retval NULL on failure
123 */
124static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
125{
126 DetectKrb5ErrCodeData *krb5d = NULL;
127 char arg1[4] = "";
128 int res = 0;
129 size_t pcre2len;
130
131 pcre2_match_data *match = NULL;
132 int ret = DetectParsePcreExec(&parse_regex, &match, krb5str, 0, 0);
133 if (ret != 2) {
134 SCLogError("parse error, ret %" PRId32 "", ret);
135 goto error;
136 }
137
138 pcre2len = sizeof(arg1);
139 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
140 if (res < 0) {
141 SCLogError("pcre2_substring_copy_bynumber failed");
142 goto error;
143 }
144
145 krb5d = SCMalloc(sizeof (DetectKrb5ErrCodeData));
146 if (unlikely(krb5d == NULL))
147 goto error;
148 if (StringParseInt32(&krb5d->err_code, 10, 0,
149 (const char *)arg1) < 0) {
150 goto error;
151 }
152 pcre2_match_data_free(match);
153 return krb5d;
154
155error:
156 if (match) {
157 pcre2_match_data_free(match);
158 }
159 if (krb5d)
160 SCFree(krb5d);
161 return NULL;
162}
163
164/**
165 * \brief parse the options from the 'krb5_err_code' keyword in the rule into
166 * the Signature data structure.
167 *
168 * \param de_ctx pointer to the Detection Engine Context
169 * \param s pointer to the Current Signature
170 * \param krb5str pointer to the user provided options
171 *
172 * \retval 0 on Success
173 * \retval -1 on Failure
174 */
175static int DetectKrb5ErrCodeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
176{
177 DetectKrb5ErrCodeData *krb5d = NULL;
178
180 return -1;
181
182 krb5d = DetectKrb5ErrCodeParse(krb5str);
183 if (krb5d == NULL)
184 goto error;
185
187 g_krb5_err_code_list_id) == NULL) {
188 goto error;
189 }
190
191 return 0;
192
193error:
194 if (krb5d != NULL)
195 DetectKrb5ErrCodeFree(de_ctx, krb5d);
196 return -1;
197}
198
199/**
200 * \brief this function will free memory associated with DetectKrb5Data
201 *
202 * \param ptr pointer to DetectKrb5Data
203 */
204static void DetectKrb5ErrCodeFree(DetectEngineCtx *de_ctx, void *ptr) {
206
207 SCFree(krb5d);
208}
209
210#ifdef UNITTESTS
211/**
212 * \test description of the test
213 */
214
215static int DetectKrb5ErrCodeParseTest01 (void)
216{
217 DetectKrb5ErrCodeData *krb5d = DetectKrb5ErrCodeParse("10");
218 FAIL_IF_NULL(krb5d);
219 FAIL_IF(!(krb5d->err_code == 10));
220 DetectKrb5ErrCodeFree(NULL, krb5d);
221 PASS;
222}
223
224static int DetectKrb5ErrCodeSignatureTest01 (void)
225{
228
229 Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_err_code:10; sid:1; rev:1;)");
230 FAIL_IF_NULL(sig);
231
233 PASS;
234}
235
236/**
237 * \brief this function registers unit tests for DetectKrb5ErrCode
238 */
239static void DetectKrb5ErrCodeRegisterTests(void)
240{
241 UtRegisterTest("DetectKrb5ErrCodeParseTest01", DetectKrb5ErrCodeParseTest01);
242 UtRegisterTest("DetectKrb5ErrCodeSignatureTest01",
243 DetectKrb5ErrCodeSignatureTest01);
244}
245#endif /* UNITTESTS */
@ ALPROTO_KRB5
uint8_t flags
Definition decode-gre.h:0
@ DETECT_KRB5_ERRCODE
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
int DetectBufferTypeRegister(const char *name)
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
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.
void DetectKrb5ErrCodeRegister(void)
Registration function for krb5_err_code: keyword.
#define PARSE_REGEX
Regex for parsing our keyword options.
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition detect.h:1424
void(* RegisterTests)(void)
Definition detect.h:1448
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition util-byte.c:622
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCMalloc(sz)
Definition util-mem.h:47
#define SCFree(p)
Definition util-mem.h:61
#define unlikely(expr)