suricata
detect-krb5-sname.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-sname.h"
35 
36 #include "rust.h"
37 #include "app-layer-krb5.h"
38 #include "rust-krb-detect-gen.h"
39 
40 static int g_krb5_sname_buffer_id = 0;
41 
43  int 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,
60  Flow *_f, const struct Krb5PrincipalNameDataArgs *cbdata,
61  int list_id, bool first)
62 {
63  SCEnter();
64 
67  if (buffer == NULL)
68  return NULL;
69  if (!first && buffer->inspect != NULL)
70  return buffer;
71 
72  uint32_t b_len = 0;
73  uint8_t *b = NULL;
74 
75  if (rs_krb5_tx_get_sname(cbdata->txv, (uint16_t)cbdata->local_id, &b, &b_len) != 1)
76  return NULL;
77  if (b == NULL || b_len == 0)
78  return NULL;
79 
80  InspectionBufferSetup(buffer, b, b_len);
81  InspectionBufferApplyTransforms(buffer, transforms);
82 
83  SCReturnPtr(buffer, "InspectionBuffer");
84 }
85 
86 static int DetectEngineInspectKrb5SName(
87  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
88  const DetectEngineAppInspectionEngine *engine,
89  const Signature *s,
90  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
91 {
92  int local_id = 0;
93 
94  const DetectEngineTransforms *transforms = NULL;
95  if (!engine->mpm) {
96  transforms = engine->v2.transforms;
97  }
98 
99  while (1) {
100  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv, };
101  InspectionBuffer *buffer = GetKrb5SNameData(det_ctx,
102  transforms, f, &cbdata, engine->sm_list, false);
103 
104  if (buffer == NULL || buffer->inspect == NULL)
105  break;
106 
107  det_ctx->buffer_offset = 0;
108  det_ctx->discontinue_matching = 0;
109  det_ctx->inspection_recursion_counter = 0;
110 
111  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
112  NULL, f,
113  (uint8_t *)buffer->inspect,
114  buffer->inspect_len,
117  if (match == 1) {
119  }
120  local_id++;
121  }
122 
124 }
125 
126 typedef struct PrefilterMpmKrb5Name {
127  int list_id;
128  const MpmCtx *mpm_ctx;
129  const DetectEngineTransforms *transforms;
131 
132 /** \brief Krb5SName Krb5SName Mpm prefilter callback
133  *
134  * \param det_ctx detection engine thread ctx
135  * \param p packet to inspect
136  * \param f flow to inspect
137  * \param txv tx to inspect
138  * \param pectx inspection context
139  */
140 static void PrefilterTxKrb5SName(DetectEngineThreadCtx *det_ctx,
141  const void *pectx,
142  Packet *p, Flow *f, void *txv,
143  const uint64_t idx, const uint8_t flags)
144 {
145  SCEnter();
146 
147  const PrefilterMpmKrb5Name *ctx = (const PrefilterMpmKrb5Name *)pectx;
148  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
149  const int list_id = ctx->list_id;
150 
151  int local_id = 0;
152 
153  while(1) {
154  // loop until we get a NULL
155 
156  struct Krb5PrincipalNameDataArgs cbdata = { local_id, txv };
157  InspectionBuffer *buffer = GetKrb5SNameData(det_ctx, ctx->transforms,
158  f, &cbdata, list_id, true);
159  if (buffer == NULL)
160  break;
161 
162  if (buffer->inspect_len >= mpm_ctx->minlen) {
163  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
164  &det_ctx->mtcu, &det_ctx->pmq,
165  buffer->inspect, buffer->inspect_len);
166  }
167 
168  local_id++;
169  }
170 }
171 
172 static void PrefilterMpmKrb5NameFree(void *ptr)
173 {
174  SCFree(ptr);
175 }
176 
177 static int PrefilterMpmKrb5SNameRegister(DetectEngineCtx *de_ctx,
178  SigGroupHead *sgh, MpmCtx *mpm_ctx,
179  const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
180 {
181  PrefilterMpmKrb5Name *pectx = SCCalloc(1, sizeof(*pectx));
182  if (pectx == NULL)
183  return -1;
184  pectx->list_id = list_id;
185  pectx->mpm_ctx = mpm_ctx;
186  pectx->transforms = &mpm_reg->v2.transforms;
187 
188  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxKrb5SName,
189  mpm_reg->v2.alproto, mpm_reg->v2.tx_min_progress,
190  pectx, PrefilterMpmKrb5NameFree, mpm_reg->name);
191 }
192 
194 {
195  sigmatch_table[DETECT_AL_KRB5_SNAME].name = "krb5.sname";
196  sigmatch_table[DETECT_AL_KRB5_SNAME].alias = "krb5_sname";
197  sigmatch_table[DETECT_AL_KRB5_SNAME].Setup = DetectKrb5SNameSetup;
199  sigmatch_table[DETECT_AL_KRB5_SNAME].desc = "sticky buffer to match on Kerberos 5 server name";
200 
202  PrefilterMpmKrb5SNameRegister, NULL,
203  ALPROTO_KRB5, 1);
204 
207  DetectEngineInspectKrb5SName, NULL);
208 
210  "Kerberos 5 ticket server name");
211 
212  g_krb5_sname_buffer_id = DetectBufferTypeGetByName("krb5_sname");
213 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1409
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1150
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint16_t minlen
Definition: util-mpm.h:99
uint16_t discontinue_matching
Definition: detect.h:1030
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, 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.
one time registration of keywords at start up
Definition: detect.h:571
Container for matching data for a signature group.
Definition: detect.h:1299
uint32_t buffer_offset
Definition: detect.h:995
const char * name
Definition: detect.h:1164
Signature container.
Definition: detect.h:496
struct DetectEngineAppInspectionEngine_::@92 v2
main detection engine ctx
Definition: detect.h:724
struct PrefilterMpmKrb5Name PrefilterMpmKrb5Name
int DetectBufferTypeGetByName(const char *name)
#define SCCalloc(nm, a)
Definition: util-mem.h:197
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
void DetectKrb5SNameRegister(void)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1356
#define DETECT_ENGINE_INSPECT_SIG_MATCH
uint64_t inspect_offset
Definition: detect.h:348
Data structures and function prototypes for keeping state for the detection engine.
uint8_t mpm_type
Definition: util-mpm.h:90
#define SCEnter(...)
Definition: util-debug.h:337
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
PrefilterRuleStore pmq
Definition: detect.h:1065
struct DetectMpmAppLayerRegistery_::@93 v2
const char * desc
Definition: detect.h:1166
const char * alias
Definition: detect.h:1165
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:169
#define SCFree(a)
Definition: util-mem.h:228
uint16_t tx_id
DetectEngineTransforms transforms
Definition: detect.h:589
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
MpmThreadCtx mtcu
Definition: detect.h:1063
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1332
#define SCReturnPtr(x, type)
Definition: util-debug.h:353
InspectionBufferMultipleForList * InspectionBufferGetMulti(DetectEngineThreadCtx *det_ctx, const int list_id)
int inspection_recursion_counter
Definition: detect.h:1042
#define DETECT_CI_FLAGS_SINGLE
uint32_t inspect_len
Definition: detect.h:349
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
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)
const DetectEngineTransforms * transforms
InspectionBuffer * InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
uint16_t flags
Definition: detect.h:1158
Flow data structure.
Definition: flow.h:325
const DetectEngineTransforms * transforms
Definition: detect.h:420
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine