suricata
detect-template-buffer.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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  * TODO: Update the \author in this file and detect-template-buffer.h.
20  * TODO: Update description in the \file section below.
21  * TODO: Remove SCLogNotice statements or convert to debug.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Set up of the "template_buffer" keyword to allow content
30  * inspections on the decoded template application layer buffers.
31  */
32 
33 #include "suricata-common.h"
34 #include "conf.h"
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.h"
38 #include "detect-engine-mpm.h"
40 #include "app-layer-template.h"
41 #include "detect-template-buffer.h"
42 
43 static int DetectTemplateBufferSetup(DetectEngineCtx *, Signature *, const char *);
44 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms,
46  Flow *_f, const uint8_t flow_flags,
47  void *txv, const int list_id);
48 #ifdef UNITTESTS
49 static void DetectTemplateBufferRegisterTests(void);
50 #endif
51 static int g_template_buffer_id = 0;
52 
54 {
55  /* TEMPLATE_START_REMOVE */
56  /* Prevent registration of this buffer unless explicitly enabled or when
57  * running unittests. This will be removed during code generation from this
58  * template. */
59  if (!RunmodeIsUnittests()) {
60  if (ConfGetNode("app-layer.protocols.template") == NULL) {
61  return;
62  }
63  }
64  /* TEMPLATE_END_REMOVE */
65  sigmatch_table[DETECT_AL_TEMPLATE_BUFFER].name = "template_buffer";
67  "Template content modifier to match on the template buffers";
68  sigmatch_table[DETECT_AL_TEMPLATE_BUFFER].Setup = DetectTemplateBufferSetup;
69 #ifdef UNITTESTS
71  DetectTemplateBufferRegisterTests;
72 #endif
73 
75 
76  /* register inspect engines - these are called per signature */
77  DetectAppLayerInspectEngineRegister2("template_buffer",
80  DetectAppLayerInspectEngineRegister2("template_buffer",
83 
84  /* register mpm engines - these are called in the prefilter stage */
85  DetectAppLayerMpmRegister2("template_buffer", SIG_FLAG_TOSERVER, 0,
87  ALPROTO_TEMPLATE, 0);
88  DetectAppLayerMpmRegister2("template_buffer", SIG_FLAG_TOCLIENT, 0,
90  ALPROTO_TEMPLATE, 0);
91 
92 
93  g_template_buffer_id = DetectBufferTypeGetByName("template_buffer");
94 
95  /* NOTE: You may want to change this to SCLogNotice during development. */
96  SCLogDebug("Template application layer detect registered.");
97 }
98 
99 static int DetectTemplateBufferSetup(DetectEngineCtx *de_ctx, Signature *s,
100  const char *str)
101 {
102  /* store list id. Content, pcre, etc will be added to the list at this
103  * id. */
104  s->init_data->list = g_template_buffer_id;
105 
106  /* set the app proto for this signature. This means it will only be
107  * evaluated against flows that are ALPROTO_TEMPLATE */
109  return -1;
110 
111  return 0;
112 }
113 
114 /** \internal
115  * \brief get the data to inspect from the transaction.
116  * This function gets the data, sets up the InspectionBuffer object
117  * and applies transformations (if any).
118  *
119  * \retval buffer or NULL in case of error
120  */
121 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
122  const DetectEngineTransforms *transforms,
123  Flow *_f, const uint8_t flow_flags,
124  void *txv, const int list_id)
125 {
126  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
127  if (buffer->inspect == NULL) {
128  const TemplateTransaction *tx = (TemplateTransaction *)txv;
129  const uint8_t *data = NULL;
130  uint32_t data_len = 0;
131 
132  if (flow_flags & STREAM_TOSERVER) {
133  data = tx->request_buffer;
134  data_len = tx->request_buffer_len;
135  } else if (flow_flags & STREAM_TOCLIENT) {
136  data = tx->response_buffer;
137  data_len = tx->response_buffer_len;
138  } else {
139  return NULL; /* no buffer */
140  }
141 
142  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
143  InspectionBufferApplyTransforms(buffer, transforms);
144  }
145 
146  return buffer;
147 }
148 
149 #ifdef UNITTESTS
151 #endif
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1247
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const 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.
Definition: detect-engine.c:1994
SigTableElmt_::name
const char * name
Definition: detect.h:1245
DetectEngineTransforms
Definition: detect.h:372
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
InspectionBuffer
Definition: detect.h:338
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
DetectTemplateBufferRegister
void DetectTemplateBufferRegister(void)
Definition: detect-template-buffer.c:53
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:755
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1239
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DETECT_AL_TEMPLATE_BUFFER
@ DETECT_AL_TEMPLATE_BUFFER
Definition: detect-engine-register.h:305
detect-template-buffer.c
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1369
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1034
TemplateTransaction::response_buffer_len
uint32_t response_buffer_len
Definition: app-layer-template.h:43
SignatureInitData_::list
int list
Definition: detect.h:517
detect-engine-mpm.h
detect.h
TemplateTransaction::request_buffer
uint8_t * request_buffer
Definition: app-layer-template.h:39
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:227
conf.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:611
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:279
TemplateTransaction
Definition: app-layer-template.h:35
TemplateTransaction::response_buffer
uint8_t * response_buffer
Definition: app-layer-template.h:42
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
TemplateTransaction::request_buffer_len
uint32_t request_buffer_len
Definition: app-layer-template.h:40
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1552
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
str
#define str(s)
Definition: suricata-common.h:272
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1450
app-layer-template.h
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
ALPROTO_TEMPLATE
@ ALPROTO_TEMPLATE
Definition: app-layer-protos.h:59
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1431
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237
detect-template-buffer.h