suricata
detect-krb5-sname.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2022 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 #include "detect-engine-mpm.h"
30 #include "detect-engine-state.h"
33 
34 #include "detect-krb5-sname.h"
35 
36 #include "rust.h"
37 #include "app-layer-krb5.h"
38 #include "util-profiling.h"
39 
40 static int g_krb5_sname_buffer_id = 0;
41 
43  uint32_t local_id; /**< used as index into thread inspect array */
44  void *txv;
45 };
46 
47 static int DetectKrb5SNameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
48 {
49  if (DetectBufferSetActiveList(s, g_krb5_sname_buffer_id) < 0)
50  return -1;
51 
53  return -1;
54 
55  return 0;
56 }
57 
58 static InspectionBuffer *GetKrb5SNameData(DetectEngineThreadCtx *det_ctx,
59  const DetectEngineTransforms *transforms, Flow *_f,
60  const struct Krb5PrincipalNameDataArgs *cbdata, int list_id)
61 {
62  SCEnter();
63 
64  InspectionBuffer *buffer =
65  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
66  if (buffer == NULL)
67  return NULL;
68  if (buffer->initialized)
69  return buffer;
70 
71  uint32_t b_len = 0;
72  const uint8_t *b = NULL;
73 
74  if (rs_krb5_tx_get_sname(cbdata->txv, cbdata->local_id, &b, &b_len) != 1)
75  return NULL;
76  if (b == NULL || b_len == 0)
77  return NULL;
78 
79  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
80 
81  SCReturnPtr(buffer, "InspectionBuffer");
82 }
83 
84 static uint8_t DetectEngineInspectKrb5SName(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
85  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
86  void *alstate, void *txv, uint64_t tx_id)
87 {
88  uint32_t local_id = 0;
89 
90  const DetectEngineTransforms *transforms = NULL;
91  if (!engine->mpm) {
92  transforms = engine->v2.transforms;
93  }
94 
95  while (1) {
96  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv, };
97  InspectionBuffer *buffer =
98  GetKrb5SNameData(det_ctx, transforms, f, &cbdata, engine->sm_list);
99 
100  if (buffer == NULL || buffer->inspect == NULL)
101  break;
102 
103  det_ctx->buffer_offset = 0;
104  det_ctx->discontinue_matching = 0;
105  det_ctx->inspection_recursion_counter = 0;
106 
107  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
108  NULL, f,
109  (uint8_t *)buffer->inspect,
110  buffer->inspect_len,
113  if (match == 1) {
115  }
116  local_id++;
117  }
118 
120 }
121 
122 typedef struct PrefilterMpmKrb5Name {
123  int list_id;
124  const MpmCtx *mpm_ctx;
127 
128 /** \brief Krb5SName Krb5SName Mpm prefilter callback
129  *
130  * \param det_ctx detection engine thread ctx
131  * \param p packet to inspect
132  * \param f flow to inspect
133  * \param txv tx to inspect
134  * \param pectx inspection context
135  */
136 static void PrefilterTxKrb5SName(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
137  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
138 {
139  SCEnter();
140 
141  const PrefilterMpmKrb5Name *ctx = (const PrefilterMpmKrb5Name *)pectx;
142  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
143  const int list_id = ctx->list_id;
144 
145  uint32_t local_id = 0;
146 
147  while(1) {
148  // loop until we get a NULL
149 
150  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv };
151  InspectionBuffer *buffer = GetKrb5SNameData(det_ctx, ctx->transforms, f, &cbdata, list_id);
152  if (buffer == NULL)
153  break;
154 
155  if (buffer->inspect_len >= mpm_ctx->minlen) {
156  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
157  &det_ctx->mtcu, &det_ctx->pmq,
158  buffer->inspect, buffer->inspect_len);
159  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
160  }
161 
162  local_id++;
163  }
164 }
165 
166 static void PrefilterMpmKrb5NameFree(void *ptr)
167 {
168  SCFree(ptr);
169 }
170 
171 static int PrefilterMpmKrb5SNameRegister(DetectEngineCtx *de_ctx,
172  SigGroupHead *sgh, MpmCtx *mpm_ctx,
173  const DetectBufferMpmRegistery *mpm_reg, int list_id)
174 {
175  PrefilterMpmKrb5Name *pectx = SCCalloc(1, sizeof(*pectx));
176  if (pectx == NULL)
177  return -1;
178  pectx->list_id = list_id;
179  pectx->mpm_ctx = mpm_ctx;
180  pectx->transforms = &mpm_reg->transforms;
181 
182  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxKrb5SName,
183  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
184  pectx, PrefilterMpmKrb5NameFree, mpm_reg->name);
185 }
186 
188 {
189  sigmatch_table[DETECT_AL_KRB5_SNAME].name = "krb5.sname";
190  sigmatch_table[DETECT_AL_KRB5_SNAME].alias = "krb5_sname";
191  sigmatch_table[DETECT_AL_KRB5_SNAME].url = "/rules/kerberos-keywords.html#krb5-sname";
192  sigmatch_table[DETECT_AL_KRB5_SNAME].Setup = DetectKrb5SNameSetup;
194  sigmatch_table[DETECT_AL_KRB5_SNAME].desc = "sticky buffer to match on Kerberos 5 server name";
195 
197  PrefilterMpmKrb5SNameRegister, NULL,
198  ALPROTO_KRB5, 1);
199 
202  DetectEngineInspectKrb5SName, NULL);
203 
205  "Kerberos 5 ticket server name");
206 
207  g_krb5_sname_buffer_id = DetectBufferTypeGetByName("krb5_sname");
208 }
PrefilterMpmKrb5Name
Definition: detect-krb5-cname.c:122
DetectEngineAppInspectionEngine_
Definition: detect.h:390
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:394
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1053
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
SigTableElmt_::name
const char * name
Definition: detect.h:1238
InspectionBuffer::initialized
bool initialized
Definition: detect.h:341
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1395
DetectEngineTransforms
Definition: detect.h:372
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:640
InspectionBuffer
Definition: detect.h:337
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1134
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
Krb5PrincipalNameDataArgs::txv
void * txv
Definition: detect-krb5-cname.c:44
rust.h
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:626
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:229
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1132
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
PrefilterMpmKrb5Name::list_id
int list_id
Definition: detect-krb5-cname.c:123
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
app-layer-krb5.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:339
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
util-profiling.h
DetectBufferMpmRegistery_::name
const char * name
Definition: detect.h:627
Packet_
Definition: decode.h:428
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:224
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-krb5-sname.h
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1092
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:407
PrefilterMpmKrb5Name::transforms
const DetectEngineTransforms * transforms
Definition: detect-krb5-cname.c:125
DetectEngineContentInspection
uint8_t DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:106
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1303
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:307
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1239
suricata-common.h
DetectKrb5SNameRegister
void DetectKrb5SNameRegister(void)
Definition: detect-krb5-sname.c:187
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1111
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1431
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:340
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
Krb5PrincipalNameDataArgs::local_id
uint32_t local_id
Definition: detect-krb5-cname.c:43
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Krb5PrincipalNameDataArgs
Definition: detect-krb5-cname.c:42
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:404
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
DETECT_AL_KRB5_SNAME
@ DETECT_AL_KRB5_SNAME
Definition: detect-engine-register.h:256
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1384
PrefilterMpmKrb5Name
struct PrefilterMpmKrb5Name PrefilterMpmKrb5Name
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1176
MpmCtx_
Definition: util-mpm.h:88
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterMpmKrb5Name::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-krb5-cname.c:124