suricata
detect-krb5-cname.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 #include "detect-engine-mpm.h"
30 #include "detect-engine-state.h"
33 
34 #include "detect-krb5-cname.h"
35 
36 #include "rust.h"
37 #include "app-layer-krb5.h"
38 
39 static int g_krb5_cname_buffer_id = 0;
40 
42  int local_id; /**< used as index into thread inspect array */
43  void *txv;
44 };
45 
46 static int DetectKrb5CNameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
47 {
48  if (DetectBufferSetActiveList(s, g_krb5_cname_buffer_id) < 0)
49  return -1;
50 
52  return -1;
53 
54  return 0;
55 }
56 
57 static InspectionBuffer *GetKrb5CNameData(DetectEngineThreadCtx *det_ctx,
58  const DetectEngineTransforms *transforms,
59  Flow *_f, const struct Krb5PrincipalNameDataArgs *cbdata,
60  int list_id, bool first)
61 {
62  SCEnter();
63 
66  if (buffer == NULL)
67  return NULL;
68  if (!first && buffer->inspect != NULL)
69  return buffer;
70 
71  uint32_t b_len = 0;
72  const uint8_t *b = NULL;
73 
74  if (rs_krb5_tx_get_cname(cbdata->txv, (uint16_t)cbdata->local_id, &b, &b_len) != 1)
75  return NULL;
76  if (b == NULL || b_len == 0)
77  return NULL;
78 
79  InspectionBufferSetup(buffer, b, b_len);
80  InspectionBufferApplyTransforms(buffer, transforms);
81 
82  SCReturnPtr(buffer, "InspectionBuffer");
83 }
84 
85 static int DetectEngineInspectKrb5CName(
87  const DetectEngineAppInspectionEngine *engine,
88  const Signature *s,
89  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
90 {
91  int local_id = 0;
92 
93  const DetectEngineTransforms *transforms = NULL;
94  if (!engine->mpm) {
95  transforms = engine->v2.transforms;
96  }
97 
98  while (1) {
99  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv, };
100  InspectionBuffer *buffer = GetKrb5CNameData(det_ctx,
101  transforms, f, &cbdata, engine->sm_list, false);
102 
103  if (buffer == NULL || buffer->inspect == NULL)
104  break;
105 
106  det_ctx->buffer_offset = 0;
107  det_ctx->discontinue_matching = 0;
108  det_ctx->inspection_recursion_counter = 0;
109 
110  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
111  NULL, f,
112  (uint8_t *)buffer->inspect,
113  buffer->inspect_len,
116  if (match == 1) {
118  }
119  local_id++;
120  }
121 
123 }
124 
125 typedef struct PrefilterMpmKrb5Name {
126  int list_id;
127  const MpmCtx *mpm_ctx;
130 
131 /** \brief Krb5CName Krb5CName Mpm prefilter callback
132  *
133  * \param det_ctx detection engine thread ctx
134  * \param p packet to inspect
135  * \param f flow to inspect
136  * \param txv tx to inspect
137  * \param pectx inspection context
138  */
139 static void PrefilterTxKrb5CName(DetectEngineThreadCtx *det_ctx,
140  const void *pectx,
141  Packet *p, Flow *f, void *txv,
142  const uint64_t idx, const uint8_t flags)
143 {
144  SCEnter();
145 
146  const PrefilterMpmKrb5Name *ctx = (const PrefilterMpmKrb5Name *)pectx;
147  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
148  const int list_id = ctx->list_id;
149 
150  int local_id = 0;
151 
152  while(1) {
153  // loop until we get a NULL
154 
155  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv };
156  InspectionBuffer *buffer = GetKrb5CNameData(det_ctx, ctx->transforms,
157  f, &cbdata, list_id, true);
158  if (buffer == NULL)
159  break;
160 
161  if (buffer->inspect_len >= mpm_ctx->minlen) {
162  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
163  &det_ctx->mtcu, &det_ctx->pmq,
164  buffer->inspect, buffer->inspect_len);
165  }
166 
167  local_id++;
168  }
169 }
170 
171 static void PrefilterMpmKrb5NameFree(void *ptr)
172 {
173  SCFree(ptr);
174 }
175 
176 static int PrefilterMpmKrb5CNameRegister(DetectEngineCtx *de_ctx,
177  SigGroupHead *sgh, MpmCtx *mpm_ctx,
178  const DetectBufferMpmRegistery *mpm_reg, int list_id)
179 {
180  PrefilterMpmKrb5Name *pectx = SCCalloc(1, sizeof(*pectx));
181  if (pectx == NULL)
182  return -1;
183  pectx->list_id = list_id;
184  pectx->mpm_ctx = mpm_ctx;
185  pectx->transforms = &mpm_reg->transforms;
186 
187  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxKrb5CName,
188  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
189  pectx, PrefilterMpmKrb5NameFree, mpm_reg->name);
190 }
191 
193 {
194  sigmatch_table[DETECT_AL_KRB5_CNAME].name = "krb5.cname";
195  sigmatch_table[DETECT_AL_KRB5_CNAME].alias = "krb5_cname";
196  sigmatch_table[DETECT_AL_KRB5_CNAME].url = "/rules/kerberos-keywords.html#krb5-cname";
197  sigmatch_table[DETECT_AL_KRB5_CNAME].Setup = DetectKrb5CNameSetup;
199  sigmatch_table[DETECT_AL_KRB5_CNAME].desc = "sticky buffer to match on Kerberos 5 client name";
200 
202  PrefilterMpmKrb5CNameRegister, NULL,
203  ALPROTO_KRB5, 1);
204 
207  DetectEngineInspectKrb5CName, NULL);
208 
210  "Kerberos 5 ticket client name");
211 
212  g_krb5_cname_buffer_id = DetectBufferTypeGetByName("krb5_cname");
213 }
PrefilterMpmKrb5Name
Definition: detect-krb5-cname.c:125
DetectEngineAppInspectionEngine_
Definition: detect.h:402
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1041
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1404
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
SigTableElmt_::name
const char * name
Definition: detect.h:1211
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1347
DetectEngineTransforms
Definition: detect.h:376
Krb5PrincipalNameDataArgs::local_id
int local_id
Definition: detect-krb5-cname.c:42
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:624
InspectionBuffer
Definition: detect.h:344
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1111
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
Krb5PrincipalNameDataArgs::txv
void * txv
Definition: detect-krb5-cname.c:43
rust.h
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:611
DetectEngineContentInspection
int 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:104
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1109
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
PrefilterMpmKrb5Name::list_id
int list_id
Definition: detect-krb5-cname.c:126
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:408
app-layer-krb5.h
InspectionBufferMultipleForList
Definition: detect.h:364
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
DetectEngineAppInspectionEngine_::mpm
uint16_t mpm
Definition: detect.h:406
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:346
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
DetectBufferMpmRegistery_::name
const char * name
Definition: detect.h:612
Packet_
Definition: decode.h:414
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:232
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-krb5-cname.h
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1076
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:426
PrefilterMpmKrb5Name::transforms
const DetectEngineTransforms * transforms
Definition: detect-krb5-cname.c:128
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
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:46
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1212
DetectKrb5CNameRegister
void DetectKrb5CNameRegister(void)
Definition: detect-krb5-cname.c:192
suricata-common.h
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1120
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1211
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1088
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:347
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@93::@95 app_v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Krb5PrincipalNameDataArgs
Definition: detect-krb5-cname.c:41
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:257
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:423
PrefilterMpmKrb5Name
struct PrefilterMpmKrb5Name PrefilterMpmKrb5Name
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
InspectionBufferGetMulti
InspectionBufferMultipleForList * InspectionBufferGetMulti(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1100
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:992
DETECT_AL_KRB5_CNAME
@ DETECT_AL_KRB5_CNAME
Definition: detect-engine-register.h:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:908
MpmCtx_
Definition: util-mpm.h:88
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@91 v2
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1074
PrefilterMpmKrb5Name::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-krb5-cname.c:127