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 
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.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 DetectParseRegex parse_regex;
40 
41 /* Prototypes of functions registered in DetectKrb5ErrCodeRegister below */
42 static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *, Flow *,
43  uint8_t, void *, void *, const Signature *,
44  const SigMatchCtx *);
45 static int DetectKrb5ErrCodeSetup (DetectEngineCtx *, Signature *, const char *);
46 static void DetectKrb5ErrCodeFree (DetectEngineCtx *, void *);
47 static void DetectKrb5ErrCodeRegisterTests (void);
48 
49 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
51  const Signature *s, const SigMatchData *smd,
52  Flow *f, uint8_t flags, void *alstate,
53  void *txv, uint64_t tx_id);
54 
55 static int g_krb5_err_code_list_id = 0;
56 
57 /**
58  * \brief Registration function for krb5_err_code: keyword
59  *
60  * This function is called once in the 'lifetime' of the engine.
61  */
63  sigmatch_table[DETECT_AL_KRB5_ERRCODE].name = "krb5_err_code";
64  sigmatch_table[DETECT_AL_KRB5_ERRCODE].desc = "match Kerberos 5 error code";
65  sigmatch_table[DETECT_AL_KRB5_ERRCODE].url = "/rules/kerberos-keywords.html#krb5-err-code";
67  sigmatch_table[DETECT_AL_KRB5_ERRCODE].AppLayerTxMatch = DetectKrb5ErrCodeMatch;
68  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Setup = DetectKrb5ErrCodeSetup;
69  sigmatch_table[DETECT_AL_KRB5_ERRCODE].Free = DetectKrb5ErrCodeFree;
70  sigmatch_table[DETECT_AL_KRB5_ERRCODE].RegisterTests = DetectKrb5ErrCodeRegisterTests;
71 
74  DetectEngineInspectKRB5Generic);
75 
78  DetectEngineInspectKRB5Generic);
79 
80  /* set up the PCRE for keyword parsing */
81  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
82 
83  g_krb5_err_code_list_id = DetectBufferTypeRegister("krb5_err_code");
84  SCLogDebug("g_krb5_err_code_list_id %d", g_krb5_err_code_list_id);
85 }
86 
87 static int DetectEngineInspectKRB5Generic(ThreadVars *tv,
89  const Signature *s, const SigMatchData *smd,
90  Flow *f, uint8_t flags, void *alstate,
91  void *txv, uint64_t tx_id)
92 {
93  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
94  f, flags, alstate, txv, tx_id);
95 }
96 
97 /**
98  * \brief This function is used to match KRB5 rule option on a packet
99  *
100  * \param t pointer to thread vars
101  * \param det_ctx pointer to the pattern matcher thread
102  * \param p pointer to the current packet
103  * \param m pointer to the sigmatch with context that we will cast into DetectKrb5Data
104  *
105  * \retval 0 no match
106  * \retval 1 match
107  */
108 static int DetectKrb5ErrCodeMatch (DetectEngineThreadCtx *det_ctx,
109  Flow *f, uint8_t flags, void *state,
110  void *txv, const Signature *s,
111  const SigMatchCtx *ctx)
112 {
113  int32_t err_code;
114  int ret;
115  const DetectKrb5ErrCodeData *dd = (const DetectKrb5ErrCodeData *)ctx;
116 
117  SCEnter();
118 
119  ret = rs_krb5_tx_get_errcode(txv, &err_code);
120  if (ret != 0)
121  SCReturnInt(0);
122 
123  if (dd->err_code == err_code)
124  SCReturnInt(1);
125 
126  SCReturnInt(0);
127 }
128 
129 /**
130  * \brief This function is used to parse options passed via krb5_errcode: keyword
131  *
132  * \param krb5str Pointer to the user provided krb5_err_code options
133  *
134  * \retval krb5d pointer to DetectKrb5Data on success
135  * \retval NULL on failure
136  */
137 static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
138 {
139  DetectKrb5ErrCodeData *krb5d = NULL;
140  char arg1[4] = "";
141  int ret = 0, res = 0;
142  int ov[MAX_SUBSTRINGS];
143 
144  ret = DetectParsePcreExec(&parse_regex, krb5str, 0, 0, ov, MAX_SUBSTRINGS);
145  if (ret != 2) {
146  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
147  goto error;
148  }
149 
150  res = pcre_copy_substring((char *) krb5str, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
151  if (res < 0) {
152  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
153  goto error;
154  }
155 
156  krb5d = SCMalloc(sizeof (DetectKrb5ErrCodeData));
157  if (unlikely(krb5d == NULL))
158  goto error;
159  krb5d->err_code = (int32_t)atoi(arg1);
160 
161  return krb5d;
162 
163 error:
164  if (krb5d)
165  SCFree(krb5d);
166  return NULL;
167 }
168 
169 /**
170  * \brief parse the options from the 'krb5_err_code' keyword in the rule into
171  * the Signature data structure.
172  *
173  * \param de_ctx pointer to the Detection Engine Context
174  * \param s pointer to the Current Signature
175  * \param krb5str pointer to the user provided options
176  *
177  * \retval 0 on Success
178  * \retval -1 on Failure
179  */
180 static int DetectKrb5ErrCodeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *krb5str)
181 {
182  DetectKrb5ErrCodeData *krb5d = NULL;
183  SigMatch *sm = NULL;
184 
186  return -1;
187 
188  krb5d = DetectKrb5ErrCodeParse(krb5str);
189  if (krb5d == NULL)
190  goto error;
191 
192  sm = SigMatchAlloc();
193  if (sm == NULL)
194  goto error;
195 
197  sm->ctx = (void *)krb5d;
198 
199  SigMatchAppendSMToList(s, sm, g_krb5_err_code_list_id);
200 
201  return 0;
202 
203 error:
204  if (krb5d != NULL)
205  DetectKrb5ErrCodeFree(de_ctx, krb5d);
206  if (sm != NULL)
207  SCFree(sm);
208  return -1;
209 }
210 
211 /**
212  * \brief this function will free memory associated with DetectKrb5Data
213  *
214  * \param ptr pointer to DetectKrb5Data
215  */
216 static void DetectKrb5ErrCodeFree(DetectEngineCtx *de_ctx, void *ptr) {
218 
219  SCFree(krb5d);
220 }
221 
222 #ifdef UNITTESTS
223 
224 /**
225  * \test description of the test
226  */
227 
228 static int DetectKrb5ErrCodeParseTest01 (void)
229 {
230  DetectKrb5ErrCodeData *krb5d = DetectKrb5ErrCodeParse("10");
231  FAIL_IF_NULL(krb5d);
232  FAIL_IF(!(krb5d->err_code == 10));
233  DetectKrb5ErrCodeFree(NULL, krb5d);
234  PASS;
235 }
236 
237 static int DetectKrb5ErrCodeSignatureTest01 (void)
238 {
241 
242  Signature *sig = DetectEngineAppendSig(de_ctx, "alert krb5 any any -> any any (krb5_err_code:10; sid:1; rev:1;)");
243  FAIL_IF_NULL(sig);
244 
246  PASS;
247 }
248 
249 #endif /* UNITTESTS */
250 
251 /**
252  * \brief this function registers unit tests for DetectKrb5ErrCode
253  */
254 static void DetectKrb5ErrCodeRegisterTests(void) {
255 #ifdef UNITTESTS
256  UtRegisterTest("DetectKrb5ErrCodeParseTest01", DetectKrb5ErrCodeParseTest01);
257  UtRegisterTest("DetectKrb5ErrCodeSignatureTest01",
258  DetectKrb5ErrCodeSignatureTest01);
259 #endif /* UNITTESTS */
260 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1468
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:1211
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1209
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
DetectKrb5ErrCodeData_::err_code
int32_t err_code
Definition: detect-krb5-errcode.h:28
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_
Flow data structure.
Definition: flow.h:343
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
Krb5PrincipalNameDataArgs::txv
void * txv
Definition: detect-krb5-cname.c:43
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2089
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1181
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:328
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
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:1592
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
DetectKrb5ErrCodeData_
Definition: detect-krb5-errcode.h:27
detect-krb5-errcode.h
app-layer-krb5.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:1009
DetectKrb5ErrCodeRegister
void DetectKrb5ErrCodeRegister(void)
Registration function for krb5_err_code: keyword.
Definition: detect-krb5-errcode.c:62
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2470
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-krb5-errcode.c:38
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2400
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1178
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:320
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:314
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:2338
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:832
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
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
DETECT_AL_KRB5_ERRCODE
@ DETECT_AL_KRB5_ERRCODE
Definition: detect-engine-register.h:228
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2044
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:170
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:1201