suricata
detect-http-start.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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  * \ingroup httplayer
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Victor Julien <victor@inliniac.net>
28  *
29  * Implements http_start
30  */
31 
32 #include "suricata-common.h"
33 #include "threads.h"
34 #include "decode.h"
35 
36 #include "detect.h"
37 #include "detect-parse.h"
38 #include "detect-engine.h"
39 #include "detect-engine-mpm.h"
40 #include "detect-engine-state.h"
43 #include "detect-content.h"
44 #include "detect-pcre.h"
46 #include "detect-http-start.h"
47 
48 #include "flow.h"
49 #include "flow-var.h"
50 #include "flow-util.h"
51 
52 #include "util-debug.h"
53 #include "util-unittest.h"
54 #include "util-unittest-helper.h"
55 #include "util-spm.h"
56 #include "util-print.h"
57 
58 #include "app-layer.h"
59 #include "app-layer-parser.h"
60 
61 #include "app-layer-htp.h"
62 #include "detect-http-header.h"
63 #include "stream-tcp.h"
64 
65 #include "util-print.h"
66 
67 #define KEYWORD_NAME "http.start"
68 #define KEYWORD_NAME_LEGACY "http_start"
69 #define KEYWORD_DOC "http-keywords.html#http-start"
70 #define BUFFER_NAME "http_start"
71 #define BUFFER_DESC "http start: request/response line + headers"
72 static int g_buffer_id = 0;
73 static int g_keyword_thread_id = 0;
74 
75 #define BUFFER_TX_STEP 4
76 #define BUFFER_SIZE_STEP 2048
78 
79 static uint8_t *GetBufferForTX(htp_tx_t *tx, uint64_t tx_id,
80  DetectEngineThreadCtx *det_ctx,
81  Flow *f, uint8_t flags, uint32_t *buffer_len)
82 {
83  *buffer_len = 0;
84 
85  HttpHeaderThreadData *hdr_td = NULL;
87  tx_id, g_keyword_thread_id, &hdr_td);
88  if (unlikely(buf == NULL)) {
89  return NULL;
90  } else if (buf->len > 0) {
91  /* already filled buf, reuse */
92  *buffer_len = buf->len;
93  return buf->buffer;
94  }
95 
96  bstr *line = NULL;
97  htp_table_t *headers;
98  if (flags & STREAM_TOSERVER) {
99  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
100  HTP_REQUEST_HEADERS)
101  return NULL;
102  line = tx->request_line;
103  headers = tx->request_headers;
104  } else {
105  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
106  HTP_RESPONSE_HEADERS)
107  return NULL;
108  headers = tx->response_headers;
109  line = tx->response_line;
110  }
111  if (line == NULL || headers == NULL)
112  return NULL;
113 
114  size_t line_size = bstr_len(line) + 2;
115  if (line_size + buf->len > buf->size) {
116  if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
117  return NULL;
118  }
119  }
120  memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
121  buf->len += bstr_size(line);
122  buf->buffer[buf->len++] = '\r';
123  buf->buffer[buf->len++] = '\n';
124 
125  size_t i = 0;
126  size_t no_of_headers = htp_table_size(headers);
127  for (; i < no_of_headers; i++) {
128  htp_header_t *h = htp_table_get_index(headers, i, NULL);
129  size_t size1 = bstr_size(h->name);
130  size_t size2 = bstr_size(h->value);
131  size_t size = size1 + size2 + 4;
132  if (i + 1 == no_of_headers)
133  size += 2;
134  if (size + buf->len > buf->size) {
135  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
136  return NULL;
137  }
138  }
139 
140  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
141  buf->len += bstr_size(h->name);
142  buf->buffer[buf->len++] = ':';
143  buf->buffer[buf->len++] = ' ';
144  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
145  buf->len += bstr_size(h->value);
146  buf->buffer[buf->len++] = '\r';
147  buf->buffer[buf->len++] = '\n';
148  if (i + 1 == no_of_headers) {
149  buf->buffer[buf->len++] = '\r';
150  buf->buffer[buf->len++] = '\n';
151  }
152  }
153 
154  *buffer_len = buf->len;
155  return buf->buffer;
156 }
157 
158 typedef struct PrefilterMpmHttpStartCtx {
159  int list_id;
160  const MpmCtx *mpm_ctx;
163 
164 /** \brief HTTP Headers Mpm prefilter callback
165  *
166  * \param det_ctx detection engine thread ctx
167  * \param p packet to inspect
168  * \param f flow to inspect
169  * \param txv tx to inspect
170  * \param pectx inspection context
171  */
172 static void PrefilterTxHttpRequestStart(DetectEngineThreadCtx *det_ctx,
173  const void *pectx,
174  Packet *p, Flow *f, void *txv,
175  const uint64_t idx, const uint8_t flags)
176 {
177  SCEnter();
178 
179  const PrefilterMpmHttpStartCtx *ctx = pectx;
180  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
181  SCLogDebug("running on list %d", ctx->list_id);
182 
183  const int list_id = ctx->list_id;
184  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
185  if (buffer->inspect == NULL) {
186  uint32_t rawdata_len = 0;
187  uint8_t *rawdata = GetBufferForTX(txv, idx, det_ctx,
188  f, flags, &rawdata_len);
189  if (rawdata_len == 0)
190  return;
191 
192  /* setup buffer and apply transforms */
193  InspectionBufferSetup(buffer, rawdata, rawdata_len);
195  }
196 
197  const uint32_t data_len = buffer->inspect_len;
198  const uint8_t *data = buffer->inspect;
199 
200  SCLogDebug("mpm'ing buffer:");
201  //PrintRawDataFp(stdout, data, data_len);
202 
203  if (data != NULL && data_len >= mpm_ctx->minlen) {
204  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
205  &det_ctx->mtcu, &det_ctx->pmq, data, data_len);
206  }
207 }
208 
209 static void PrefilterMpmHttpHeaderFree(void *ptr)
210 {
211  SCFree(ptr);
212 }
213 
214 static int PrefilterTxHttpRequestStartRegister(DetectEngineCtx *de_ctx,
215  SigGroupHead *sgh, MpmCtx *mpm_ctx,
216  const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
217 {
218  SCEnter();
219 
220  PrefilterMpmHttpStartCtx *pectx = SCCalloc(1, sizeof(*pectx));
221  if (pectx == NULL)
222  return -1;
223  pectx->list_id = list_id;
224  pectx->mpm_ctx = mpm_ctx;
225  pectx->transforms = &mpm_reg->v2.transforms;
226 
227  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpRequestStart,
228  mpm_reg->v2.alproto, HTP_REQUEST_HEADERS,
229  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
230  if (r != 0) {
231  SCFree(pectx);
232  return r;
233  }
234 
235  return r;
236 }
237 
238 /** \brief HTTP Headers Mpm prefilter callback
239  *
240  * \param det_ctx detection engine thread ctx
241  * \param p packet to inspect
242  * \param f flow to inspect
243  * \param txv tx to inspect
244  * \param pectx inspection context
245  */
246 static void PrefilterTxHttpResponseStart(DetectEngineThreadCtx *det_ctx,
247  const void *pectx,
248  Packet *p, Flow *f, void *txv,
249  const uint64_t idx, const uint8_t flags)
250 {
251  SCEnter();
252 
253  const PrefilterMpmHttpStartCtx *ctx = pectx;
254  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
255  SCLogDebug("running on list %d", ctx->list_id);
256 
257  const int list_id = ctx->list_id;
258  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
259  if (buffer->inspect == NULL) {
260  uint32_t rawdata_len = 0;
261  uint8_t *rawdata = GetBufferForTX(txv, idx, det_ctx,
262  f, flags, &rawdata_len);
263  if (rawdata_len == 0)
264  return;
265 
266  /* setup buffer and apply transforms */
267  InspectionBufferSetup(buffer, rawdata, rawdata_len);
269  }
270 
271  const uint32_t data_len = buffer->inspect_len;
272  const uint8_t *data = buffer->inspect;
273 
274  SCLogDebug("mpm'ing buffer:");
275  //PrintRawDataFp(stdout, data, data_len);
276 
277  if (data != NULL && data_len >= mpm_ctx->minlen) {
278  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
279  &det_ctx->mtcu, &det_ctx->pmq, data, data_len);
280  }
281 }
282 
283 static int PrefilterTxHttpResponseStartRegister(DetectEngineCtx *de_ctx,
284  SigGroupHead *sgh, MpmCtx *mpm_ctx,
285  const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
286 {
287  SCEnter();
288 
289  PrefilterMpmHttpStartCtx *pectx = SCCalloc(1, sizeof(*pectx));
290  if (pectx == NULL)
291  return -1;
292  pectx->list_id = list_id;
293  pectx->mpm_ctx = mpm_ctx;
294  pectx->transforms = &mpm_reg->v2.transforms;
295 
296  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpResponseStart,
297  mpm_reg->v2.alproto, HTP_RESPONSE_HEADERS,
298  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
299  if (r != 0) {
300  SCFree(pectx);
301  return r;
302  }
303 
304  return r;
305 }
306 
307 static int InspectEngineHttpStart(
308  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
309  const DetectEngineAppInspectionEngine *engine,
310  const Signature *s,
311  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
312 {
313  const int list_id = engine->sm_list;
314  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
315  if (buffer->inspect == NULL) {
316  SCLogDebug("setting up inspect buffer %d", list_id);
317 
318  /* if prefilter didn't already run, we need to consider transformations */
319  const DetectEngineTransforms *transforms = NULL;
320  if (!engine->mpm) {
321  transforms = engine->v2.transforms;
322  }
323 
324  uint32_t rawdata_len = 0;
325  uint8_t *rawdata = GetBufferForTX(txv, tx_id, det_ctx,
326  f, flags, &rawdata_len);
327  if (rawdata_len == 0) {
328  SCLogDebug("no data");
329  goto end;
330  }
331  /* setup buffer and apply transforms */
332  InspectionBufferSetup(buffer, rawdata, rawdata_len);
333  InspectionBufferApplyTransforms(buffer, transforms);
334  }
335 
336  const uint32_t data_len = buffer->inspect_len;
337  const uint8_t *data = buffer->inspect;
338  const uint64_t offset = buffer->inspect_offset;
339 
340  det_ctx->buffer_offset = 0;
341  det_ctx->discontinue_matching = 0;
342  det_ctx->inspection_recursion_counter = 0;
343  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
344  NULL, f, (uint8_t *)data, data_len, offset,
347  if (r == 1)
349 
350  end:
351  if (flags & STREAM_TOSERVER) {
352  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, txv, flags) > HTP_REQUEST_HEADERS)
354  } else {
355  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, txv, flags) > HTP_RESPONSE_HEADERS)
357  }
359 }
360 
361 static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
362 {
363  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
364  return -1;
365 
367  return -1;
368 
369  return 0;
370 }
371 
372 /**
373  * \brief Registers the keyword handlers for the "http_header" keyword.
374  */
376 {
381  sigmatch_table[DETECT_AL_HTTP_START].Setup = DetectHttpStartSetup;
383 
385  PrefilterTxHttpRequestStartRegister, NULL, ALPROTO_HTTP,
386  HTP_REQUEST_HEADERS);
388  PrefilterTxHttpResponseStartRegister, NULL, ALPROTO_HTTP,
389  HTP_RESPONSE_HEADERS);
390 
392  ALPROTO_HTTP, SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
393  InspectEngineHttpStart, NULL);
395  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_HEADERS,
396  InspectEngineHttpStart, NULL);
397 
399  BUFFER_DESC);
400 
401  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
402 
403  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
405 
406  SCLogDebug("keyword %s registered. Thread id %d. "
407  "Buffer %s registered. Buffer id %d",
408  KEYWORD_NAME, g_keyword_thread_id,
409  BUFFER_NAME, g_buffer_id);
410 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1409
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1150
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint16_t minlen
Definition: util-mpm.h:99
uint16_t discontinue_matching
Definition: detect.h:1030
void HttpHeaderThreadDataFree(void *data)
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
#define unlikely(expr)
Definition: util-optimize.h:35
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.
#define KEYWORD_NAME
one time registration of keywords at start up
Definition: detect.h:571
uint64_t offset
Container for matching data for a signature group.
Definition: detect.h:1299
uint32_t buffer_offset
Definition: detect.h:995
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
const DetectEngineTransforms * transforms
int DetectBufferTypeGetByName(const char *name)
#define SCCalloc(nm, a)
Definition: util-mem.h:197
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1356
#define BUFFER_SIZE_STEP
#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
HttpHeaderBuffer * HttpHeaderGetBufferSpaceForTXID(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint64_t tx_id, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
#define SIG_FLAG_TOSERVER
Definition: detect.h:241
#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
#define BUFFER_NAME
#define KEYWORD_NAME_LEGACY
struct DetectMpmAppLayerRegistery_::@93 v2
void * HttpHeaderThreadDataInit(void *data)
#define KEYWORD_DOC
const char * desc
Definition: detect.h:1166
const char * alias
Definition: detect.h:1165
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#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 BUFFER_TX_STEP
#define SIGMATCH_NOOPT
Definition: detect.h:1332
const char * url
Definition: detect.h:1167
#define STREAM_TOSERVER
Definition: stream.h:31
int inspection_recursion_counter
Definition: detect.h:1042
#define DETECT_CI_FLAGS_SINGLE
struct PrefilterMpmHttpStartCtx PrefilterMpmHttpStartCtx
uint32_t inspect_len
Definition: detect.h:349
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
#define BUFFER_DESC
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_header" keyword.
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)
#define DOC_VERSION
Definition: suricata.h:91
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
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