suricata
detect-http-header.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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  * \ingroup httplayer
20  *
21  * @{
22  */
23 
24 
25 /**
26  * \file
27  *
28  * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
29  *
30  * Implements support for http_header keyword.
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
44 #include "detect-content.h"
45 #include "detect-pcre.h"
46 
47 #include "util-debug.h"
48 #include "util-print.h"
49 #include "util-memcmp.h"
50 #include "util-profiling.h"
51 
52 #include "app-layer.h"
53 #include "app-layer-parser.h"
54 
55 #include "app-layer-htp.h"
56 #include "detect-http-header.h"
58 
59 static int DetectHttpHeaderSetup(DetectEngineCtx *, Signature *, const char *);
60 #ifdef UNITTESTS
61 static void DetectHttpHeaderRegisterTests(void);
62 #endif
63 static int g_http_header_buffer_id = 0;
64 static int g_keyword_thread_id = 0;
65 
66 #define BUFFER_SIZE_STEP 1024
68 
69 static uint8_t *GetBufferForTX(
70  htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint32_t *buffer_len)
71 {
72  *buffer_len = 0;
73 
74  HttpHeaderThreadData *hdr_td = NULL;
75  HttpHeaderBuffer *buf =
76  HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
77  if (unlikely(buf == NULL)) {
78  return NULL;
79  }
80 
81  htp_table_t *headers;
82  if (flags & STREAM_TOSERVER) {
83  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
84  HTP_REQUEST_HEADERS)
85  return NULL;
86  headers = tx->request_headers;
87  } else {
88  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
89  HTP_RESPONSE_HEADERS)
90  return NULL;
91  headers = tx->response_headers;
92  }
93  if (headers == NULL)
94  return NULL;
95 
96  size_t i = 0;
97  size_t no_of_headers = htp_table_size(headers);
98  for (; i < no_of_headers; i++) {
99  htp_header_t *h = htp_table_get_index(headers, i, NULL);
100  size_t size1 = bstr_size(h->name);
101  size_t size2 = bstr_size(h->value);
102 
103  if (flags & STREAM_TOSERVER) {
104  if (size1 == 6 &&
105  SCMemcmpLowercase("cookie", bstr_ptr(h->name), 6) == 0) {
106  continue;
107  }
108  } else {
109  if (size1 == 10 &&
110  SCMemcmpLowercase("set-cookie", bstr_ptr(h->name), 10) == 0) {
111  continue;
112  }
113  }
114 
115  size_t size = size1 + size2 + 4;
116 #if 0
117  if (i + 1 == no_of_headers)
118  size += 2;
119 #endif
120  if (size + buf->len > buf->size) {
121  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
122  return NULL;
123  }
124  }
125 
126  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
127  buf->len += bstr_size(h->name);
128  buf->buffer[buf->len++] = ':';
129  buf->buffer[buf->len++] = ' ';
130  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
131  buf->len += bstr_size(h->value);
132  buf->buffer[buf->len++] = '\r';
133  buf->buffer[buf->len++] = '\n';
134 #if 0 // looks like this breaks existing rules
135  if (i + 1 == no_of_headers) {
136  buf->buffer[buf->len++] = '\r';
137  buf->buffer[buf->len++] = '\n';
138  }
139 #endif
140  }
141 
142  *buffer_len = buf->len;
143  return buf->buffer;
144 }
145 
146 static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
147  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
148  const int list_id)
149 {
150  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
151  if (buffer->inspect == NULL) {
152  uint32_t b_len = 0;
153  const uint8_t *b = NULL;
154 
155  if (rs_http2_tx_get_headers(txv, flow_flags, &b, &b_len) != 1)
156  return NULL;
157  if (b == NULL || b_len == 0)
158  return NULL;
159 
160  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
161  InspectionBufferApplyTransforms(buffer, transforms);
162  }
163 
164  return buffer;
165 }
166 
167 /** \internal
168  * \brief custom inspect function to utilize the cached headers
169  */
170 static uint8_t DetectEngineInspectBufferHttpHeader(DetectEngineCtx *de_ctx,
172  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
173 {
174  SCEnter();
175 
176  const int list_id = engine->sm_list;
177  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
178  if (buffer->inspect == NULL) {
179  SCLogDebug("setting up inspect buffer %d", list_id);
180 
181  /* if prefilter didn't already run, we need to consider transformations */
182  const DetectEngineTransforms *transforms = NULL;
183  if (!engine->mpm) {
184  transforms = engine->v2.transforms;
185  }
186 
187  uint32_t rawdata_len = 0;
188  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
189  if (rawdata_len == 0) {
190  SCLogDebug("no data");
191  goto end;
192  }
193  /* setup buffer and apply transforms */
194  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
195  InspectionBufferApplyTransforms(buffer, transforms);
196  }
197 
198  const uint32_t data_len = buffer->inspect_len;
199  const uint8_t *data = buffer->inspect;
200  const uint64_t offset = buffer->inspect_offset;
201 
202  det_ctx->discontinue_matching = 0;
203  det_ctx->buffer_offset = 0;
204  det_ctx->inspection_recursion_counter = 0;
205 
206  /* Inspect all the uricontents fetched on each
207  * transaction at the app layer */
208  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
209  NULL, f, (uint8_t *)data, data_len, offset,
211  SCLogDebug("r = %d", r);
212  if (r == 1) {
214  }
215 end:
216  if (flags & STREAM_TOSERVER) {
217  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, txv, flags) >
218  HTP_REQUEST_HEADERS)
220  } else {
221  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, txv, flags) >
222  HTP_RESPONSE_HEADERS)
224  }
226 }
227 
229  int list_id;
230  const MpmCtx *mpm_ctx;
233 
234 /** \brief Generic Mpm prefilter callback
235  *
236  * \param det_ctx detection engine thread ctx
237  * \param p packet to inspect
238  * \param f flow to inspect
239  * \param txv tx to inspect
240  * \param pectx inspection context
241  */
242 static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
243  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
244 {
245  SCEnter();
246 
247  const PrefilterMpmHttpHeaderCtx *ctx = pectx;
248  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
249  SCLogDebug("running on list %d", ctx->list_id);
250 
251  const int list_id = ctx->list_id;
252  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
253  if (buffer->inspect == NULL) {
254  uint32_t rawdata_len = 0;
255  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
256  if (rawdata_len == 0)
257  return;
258 
259  /* setup buffer and apply transforms */
260  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
262  }
263 
264  const uint32_t data_len = buffer->inspect_len;
265  const uint8_t *data = buffer->inspect;
266 
267  SCLogDebug("mpm'ing buffer:");
268  //PrintRawDataFp(stdout, data, data_len);
269 
270  if (data != NULL && data_len >= mpm_ctx->minlen) {
271  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
272  &det_ctx->mtcu, &det_ctx->pmq, data, data_len);
273  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
274  }
275 }
276 
277 static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
278  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
279 {
280  SCEnter();
281 
282  htp_tx_t *tx = txv;
283  const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
284  /* if the request wasn't flagged as having a trailer, we skip */
285  if (htud && (
286  ((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
287  ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers))) {
288  SCReturn;
289  }
290  PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
291  SCReturn;
292 }
293 
294 static void PrefilterMpmHttpHeaderFree(void *ptr)
295 {
296  SCFree(ptr);
297 }
298 
299 static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
300  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
301 {
302  SCEnter();
303 
304  /* header */
305  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
306  if (pectx == NULL)
307  return -1;
308  pectx->list_id = list_id;
309  pectx->mpm_ctx = mpm_ctx;
310  pectx->transforms = &mpm_reg->transforms;
311 
312  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
313  mpm_reg->app_v2.alproto, HTP_REQUEST_HEADERS,
314  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
315  if (r != 0) {
316  SCFree(pectx);
317  return r;
318  }
319 
320  /* trailer */
321  pectx = SCCalloc(1, sizeof(*pectx));
322  if (pectx == NULL)
323  return -1;
324  pectx->list_id = list_id;
325  pectx->mpm_ctx = mpm_ctx;
326  pectx->transforms = &mpm_reg->transforms;
327 
328  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
329  mpm_reg->app_v2.alproto, HTP_REQUEST_TRAILER,
330  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
331  if (r != 0) {
332  SCFree(pectx);
333  }
334  return r;
335 }
336 
337 static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
338  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
339 {
340  SCEnter();
341 
342  /* header */
343  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
344  if (pectx == NULL)
345  return -1;
346  pectx->list_id = list_id;
347  pectx->mpm_ctx = mpm_ctx;
348  pectx->transforms = &mpm_reg->transforms;
349 
350  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
351  mpm_reg->app_v2.alproto, HTP_RESPONSE_HEADERS,
352  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
353  if (r != 0) {
354  SCFree(pectx);
355  return r;
356  }
357 
358  /* trailer */
359  pectx = SCCalloc(1, sizeof(*pectx));
360  if (pectx == NULL)
361  return -1;
362  pectx->list_id = list_id;
363  pectx->mpm_ctx = mpm_ctx;
364  pectx->transforms = &mpm_reg->transforms;
365 
366  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
367  mpm_reg->app_v2.alproto, HTP_RESPONSE_TRAILER,
368  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
369  if (r != 0) {
370  SCFree(pectx);
371  }
372  return r;
373 }
374 
375 /**
376  * \brief The setup function for the http_header keyword for a signature.
377  *
378  * \param de_ctx Pointer to the detection engine context.
379  * \param s Pointer to signature for the current Signature being parsed
380  * from the rules.
381  * \param m Pointer to the head of the SigMatchs for the current rule
382  * being parsed.
383  * \param arg Pointer to the string holding the keyword value.
384  *
385  * \retval 0 On success.
386  * \retval -1 On failure.
387  */
388 static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
389 {
391  de_ctx, s, arg, DETECT_AL_HTTP_HEADER, g_http_header_buffer_id, ALPROTO_HTTP1);
392 }
393 
394 /**
395  * \brief this function setup the http.header keyword used in the rule
396  *
397  * \param de_ctx Pointer to the Detection Engine Context
398  * \param s Pointer to the Signature to which the current keyword belongs
399  * \param str Should hold an empty string always
400  *
401  * \retval 0 On success
402  */
403 static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
404 {
405  if (DetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
406  return -1;
408  return -1;
409  return 0;
410 }
411 
412 /**
413  * \brief Registers the keyword handlers for the "http_header" keyword.
414  */
416 {
417  /* http_header content modifier */
418  sigmatch_table[DETECT_AL_HTTP_HEADER].name = "http_header";
419  sigmatch_table[DETECT_AL_HTTP_HEADER].desc = "content modifier to match only on the HTTP header-buffer";
420  sigmatch_table[DETECT_AL_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
421  sigmatch_table[DETECT_AL_HTTP_HEADER].Setup = DetectHttpHeaderSetup;
422 #ifdef UNITTESTS
424 #endif
428 
429  /* http.header sticky buffer */
430  sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
431  sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
432  sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
433  sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
436 
438  HTP_REQUEST_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
440  PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
441  0); /* not used, registered twice: HEADERS/TRAILER */
442 
444  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
446  PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
447  0); /* not used, registered twice: HEADERS/TRAILER */
448 
450  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
452  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
453 
455  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
457  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
458 
460  "http headers");
461 
462  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
463 
464  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
466 }
467 
468 static int g_http_request_header_buffer_id = 0;
469 static int g_http_response_header_buffer_id = 0;
470 
471 static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx, const uint8_t flags,
472  const DetectEngineTransforms *transforms, Flow *_f, const struct MpmListIdDataArgs *cbdata,
473  int list_id)
474 {
475  SCEnter();
476 
477  InspectionBuffer *buffer =
478  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
479  if (buffer == NULL)
480  return NULL;
481  if (buffer->initialized)
482  return buffer;
483 
484  uint32_t b_len = 0;
485  const uint8_t *b = NULL;
486 
487  if (rs_http2_tx_get_header(cbdata->txv, flags, cbdata->local_id, &b, &b_len) != 1) {
489  return NULL;
490  }
491  if (b == NULL || b_len == 0) {
493  return NULL;
494  }
495 
496  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
497 
498  SCReturnPtr(buffer, "InspectionBuffer");
499 }
500 
501 static void PrefilterTxHttp2Header(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
502  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
503 {
504  SCEnter();
505 
506  const PrefilterMpmListId *ctx = (const PrefilterMpmListId *)pectx;
507  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
508  const int list_id = ctx->list_id;
509 
510  uint32_t local_id = 0;
511 
512  while (1) {
513  // loop until we get a NULL
514 
515  struct MpmListIdDataArgs cbdata = { local_id, txv };
516  InspectionBuffer *buffer =
517  GetHttp2HeaderData(det_ctx, flags, ctx->transforms, f, &cbdata, list_id);
518  if (buffer == NULL)
519  break;
520 
521  if (buffer->inspect_len >= mpm_ctx->minlen) {
522  (void)mpm_table[mpm_ctx->mpm_type].Search(
523  mpm_ctx, &det_ctx->mtcu, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
524  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
525  }
526 
527  local_id++;
528  }
529 }
530 
531 static uint8_t DetectEngineInspectHttp2Header(DetectEngineCtx *de_ctx,
533  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
534 {
535  uint32_t local_id = 0;
536 
537  const DetectEngineTransforms *transforms = NULL;
538  if (!engine->mpm) {
539  transforms = engine->v2.transforms;
540  }
541 
542  while (1) {
543  struct MpmListIdDataArgs cbdata = {
544  local_id,
545  txv,
546  };
547  InspectionBuffer *buffer =
548  GetHttp2HeaderData(det_ctx, flags, transforms, f, &cbdata, engine->sm_list);
549 
550  if (buffer == NULL || buffer->inspect == NULL)
551  break;
552 
553  det_ctx->buffer_offset = 0;
554  det_ctx->discontinue_matching = 0;
555  det_ctx->inspection_recursion_counter = 0;
556 
557  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
558  (uint8_t *)buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
560  if (match == 1) {
562  }
563  local_id++;
564  }
565 
567 }
568 
569 static int PrefilterMpmHttp2HeaderRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
570  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
571 {
572  PrefilterMpmListId *pectx = SCCalloc(1, sizeof(*pectx));
573  if (pectx == NULL)
574  return -1;
575  pectx->list_id = list_id;
576  pectx->mpm_ctx = mpm_ctx;
577  pectx->transforms = &mpm_reg->transforms;
578 
579  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttp2Header, mpm_reg->app_v2.alproto,
580  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->name);
581 }
582 
583 static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx, const uint8_t flags,
584  const DetectEngineTransforms *transforms, Flow *f, const struct MpmListIdDataArgs *cbdata,
585  int list_id)
586 {
587  SCEnter();
588 
589  InspectionBuffer *buffer =
590  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
591  if (buffer == NULL)
592  return NULL;
593  if (buffer->initialized)
594  return buffer;
595 
596  HttpHeaderThreadData *hdr_td = NULL;
597  HttpHeaderBuffer *buf =
598  HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
599  if (unlikely(buf == NULL)) {
600  return NULL;
601  }
602 
603  htp_tx_t *tx = (htp_tx_t *)cbdata->txv;
604  htp_table_t *headers;
605  if (flags & STREAM_TOSERVER) {
606  headers = tx->request_headers;
607  } else {
608  headers = tx->response_headers;
609  }
610  if (cbdata->local_id < htp_table_size(headers)) {
611  htp_header_t *h = htp_table_get_index(headers, cbdata->local_id, NULL);
612  size_t size1 = bstr_size(h->name);
613  size_t size2 = bstr_size(h->value);
614  size_t b_len = size1 + 2 + size2;
615  if (b_len > buf->size) {
616  if (HttpHeaderExpandBuffer(hdr_td, buf, b_len) != 0) {
617  return NULL;
618  }
619  }
620  memcpy(buf->buffer, bstr_ptr(h->name), bstr_size(h->name));
621  buf->buffer[size1] = ':';
622  buf->buffer[size1 + 1] = ' ';
623  memcpy(buf->buffer + size1 + 2, bstr_ptr(h->value), bstr_size(h->value));
624  buf->len = b_len;
625  } else {
627  return NULL;
628  }
629  if (buf->len == 0) {
631  return NULL;
632  }
633 
634  InspectionBufferSetupMulti(buffer, transforms, buf->buffer, buf->len);
635 
636  SCReturnPtr(buffer, "InspectionBuffer");
637 }
638 
639 static void PrefilterTxHttp1Header(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
640  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
641 {
642  SCEnter();
643 
644  const PrefilterMpmListId *ctx = (const PrefilterMpmListId *)pectx;
645  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
646  const int list_id = ctx->list_id;
647 
648  uint32_t local_id = 0;
649 
650  while (1) {
651  // loop until we get a NULL
652 
653  struct MpmListIdDataArgs cbdata = { local_id, txv };
654  InspectionBuffer *buffer =
655  GetHttp1HeaderData(det_ctx, flags, ctx->transforms, f, &cbdata, list_id);
656  if (buffer == NULL)
657  break;
658 
659  if (buffer->inspect_len >= mpm_ctx->minlen) {
660  (void)mpm_table[mpm_ctx->mpm_type].Search(
661  mpm_ctx, &det_ctx->mtcu, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
662  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
663  }
664 
665  local_id++;
666  }
667 }
668 
669 static int PrefilterMpmHttp1HeaderRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
670  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
671 {
672  PrefilterMpmListId *pectx = SCCalloc(1, sizeof(*pectx));
673  if (pectx == NULL)
674  return -1;
675  pectx->list_id = list_id;
676  pectx->mpm_ctx = mpm_ctx;
677  pectx->transforms = &mpm_reg->transforms;
678 
679  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttp1Header, mpm_reg->app_v2.alproto,
680  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->name);
681 }
682 
683 static uint8_t DetectEngineInspectHttp1Header(DetectEngineCtx *de_ctx,
685  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
686 {
687  uint32_t local_id = 0;
688 
689  const DetectEngineTransforms *transforms = NULL;
690  if (!engine->mpm) {
691  transforms = engine->v2.transforms;
692  }
693 
694  while (1) {
695  struct MpmListIdDataArgs cbdata = {
696  local_id,
697  txv,
698  };
699  InspectionBuffer *buffer =
700  GetHttp1HeaderData(det_ctx, flags, transforms, f, &cbdata, engine->sm_list);
701 
702  if (buffer == NULL || buffer->inspect == NULL)
703  break;
704 
705  det_ctx->buffer_offset = 0;
706  det_ctx->discontinue_matching = 0;
707  det_ctx->inspection_recursion_counter = 0;
708 
709  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
710  (uint8_t *)buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
712  if (match == 1) {
714  }
715  local_id++;
716  }
717 
719 }
720 
721 static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
722 {
723  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
724  return -1;
725 
727  return -1;
728 
729  return 0;
730 }
731 
733 {
734  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
736  "sticky buffer to match on only one HTTP header name and value";
737  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
738  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
741 
742  DetectAppLayerMpmRegister2("http_request_header", SIG_FLAG_TOSERVER, 2,
743  PrefilterMpmHttp2HeaderRegister, NULL, ALPROTO_HTTP2, HTTP2StateOpen);
745  HTTP2StateOpen, DetectEngineInspectHttp2Header, NULL);
746  DetectAppLayerMpmRegister2("http_request_header", SIG_FLAG_TOSERVER, 2,
747  PrefilterMpmHttp1HeaderRegister, NULL, ALPROTO_HTTP1, 0);
749  HTP_REQUEST_HEADERS, DetectEngineInspectHttp1Header, NULL);
750 
751  DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
752  g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
753  DetectBufferTypeSupportsMultiInstance("http_request_header");
754 }
755 
756 static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
757 {
758  if (DetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
759  return -1;
760 
762  return -1;
763 
764  return 0;
765 }
766 
768 {
769  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
771  "sticky buffer to match on only one HTTP header name and value";
772  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
773  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
776 
777  DetectAppLayerMpmRegister2("http_response_header", SIG_FLAG_TOCLIENT, 2,
778  PrefilterMpmHttp2HeaderRegister, NULL, ALPROTO_HTTP2, HTTP2StateOpen);
780  HTTP2StateOpen, DetectEngineInspectHttp2Header, NULL);
781  DetectAppLayerMpmRegister2("http_response_header", SIG_FLAG_TOCLIENT, 2,
782  PrefilterMpmHttp1HeaderRegister, NULL, ALPROTO_HTTP1, 0);
784  HTP_RESPONSE_HEADERS, DetectEngineInspectHttp1Header, NULL);
785 
786  DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
787  g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
788  DetectBufferTypeSupportsMultiInstance("http_response_header");
789 }
790 
791 /************************************Unittests*********************************/
792 
793 #ifdef UNITTESTS
795 #endif
796 
797 /**
798  * @}
799  */
DetectEngineAppInspectionEngine_
Definition: detect.h:418
SigTableElmt_::url
const char * url
Definition: detect.h:1288
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1704
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-header.c:66
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:422
detect-content.h
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:91
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1103
detect-engine.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1491
SigTableElmt_::desc
const char * desc
Definition: detect.h:1287
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
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:2165
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1489
SigTableElmt_::name
const char * name
Definition: detect.h:1285
InspectionBuffer::initialized
bool initialized
Definition: detect.h:369
MpmListIdDataArgs
Definition: detect-engine-mpm.h:131
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1439
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:400
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectHttpRequestHeaderRegister
void DetectHttpRequestHeaderRegister(void)
Definition: detect-http-header.c:732
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1387
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1105
InspectionBuffer
Definition: detect.h:365
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1182
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1279
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:827
MpmListIdDataArgs::local_id
uint32_t local_id
Definition: detect-engine-mpm.h:132
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1074
detect-http-header.h
MpmListIdDataArgs::txv
void * txv
Definition: detect-engine-mpm.h:133
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:669
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:256
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1270
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:682
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1180
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1526
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
util-memcmp.h
DetectHttpResponseHeaderRegister
void DetectHttpResponseHeaderRegister(void)
Definition: detect-http-header.c:767
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:424
DETECT_AL_HTTP_HEADER
@ DETECT_AL_HTTP_HEADER
Definition: detect-engine-register.h:136
PrefilterMpmListId
Definition: detect-engine-mpm.h:125
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
app-layer-htp.h
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1593
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1075
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
HtpTxUserData_::response_has_trailers
uint8_t response_has_trailers
Definition: app-layer-htp.h:212
DETECT_HTTP_REQUEST_HEADER
@ DETECT_HTTP_REQUEST_HEADER
Definition: detect-engine-register.h:190
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:367
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
PrefilterMpmListId::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-mpm.h:128
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1283
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:100
HtpTxUserData_::request_has_trailers
uint8_t request_has_trailers
Definition: app-layer-htp.h:211
util-profiling.h
SCReturn
#define SCReturn
Definition: util-debug.h:273
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:207
PrefilterMpmListId::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-engine-mpm.h:127
detect-http-header.c
Packet_
Definition: decode.h:430
detect-http-header-common.h
HttpHeaderThreadConfig_
Definition: detect-http-header-common.h:33
HttpHeaderBuffer_::buffer
uint8_t * buffer
Definition: detect-http-header-common.h:28
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:216
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.
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:163
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:40
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1140
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:435
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
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1347
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:287
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
PrefilterMpmHttpHeaderCtx::list_id
int list_id
Definition: detect-http-header.c:229
PrefilterMpmHttpHeaderCtx
Definition: detect-http-header.c:228
DetectHttpHeaderRegister
void DetectHttpHeaderRegister(void)
Registers the keyword handlers for the "http_header" keyword.
Definition: detect-http-header.c:415
suricata-common.h
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@86::@88 app_v2
DETECT_HTTP_HEADER
@ DETECT_HTTP_HEADER
Definition: detect-engine-register.h:137
DetectBufferMpmRegistry_::name
const char * name
Definition: detect.h:670
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1725
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1159
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:1606
HttpHeaderBuffer_::size
uint32_t size
Definition: detect-http-header-common.h:29
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
HtpTxUserData_
Definition: app-layer-htp.h:206
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
PrefilterMpmHttpHeaderCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-http-header.c:231
PrefilterMpmHttpHeaderCtx
struct PrefilterMpmHttpHeaderCtx PrefilterMpmHttpHeaderCtx
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:368
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@83 v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:366
PrefilterMpmHttpHeaderCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-http-header.c:230
str
#define str(s)
Definition: suricata-common.h:286
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:1621
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:582
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:432
PrefilterMpmListId::list_id
int list_id
Definition: detect-engine-mpm.h:126
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
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:1546
HttpHeaderGetBufferSpace
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
Definition: detect-http-header-common.c:100
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
Definition: detect-http-header-common.c:81
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1467
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1221
MpmCtx_
Definition: util-mpm.h:89
HttpHeaderBuffer_
Definition: detect-http-header-common.h:27
DETECT_HTTP_RESPONSE_HEADER
@ DETECT_HTTP_RESPONSE_HEADER
Definition: detect-engine-register.h:191
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectHttpHeaderRegisterTests
void DetectHttpHeaderRegisterTests(void)
Definition: detect-http-header.c:4557
HttpHeaderThreadData_
Definition: detect-http-header-common.h:37
HttpHeaderThreadDataFree
void HttpHeaderThreadDataFree(void *data)
Definition: detect-http-header-common.c:74
DetectRegisterThreadCtxGlobalFuncs
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:3676
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1277
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:671