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 #include "util-validate.h"
52 
53 #include "app-layer.h"
54 #include "app-layer-parser.h"
55 
56 #include "app-layer-htp.h"
57 #include "detect-http-header.h"
59 
60 static int DetectHttpHeaderSetup(DetectEngineCtx *, Signature *, const char *);
61 #ifdef UNITTESTS
62 static void DetectHttpHeaderRegisterTests(void);
63 #endif
64 static int g_http_header_buffer_id = 0;
65 static int g_keyword_thread_id = 0;
66 
67 #define BUFFER_SIZE_STEP 1024
69 
70 static uint8_t *GetBufferForTX(
71  htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint32_t *buffer_len)
72 {
73  *buffer_len = 0;
74 
75  HttpHeaderThreadData *hdr_td = NULL;
76  HttpHeaderBuffer *buf =
77  HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
78  if (unlikely(buf == NULL)) {
79  return NULL;
80  }
81 
82  htp_table_t *headers;
83  if (flags & STREAM_TOSERVER) {
84  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
85  HTP_REQUEST_HEADERS)
86  return NULL;
87  headers = tx->request_headers;
88  } else {
89  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
90  HTP_RESPONSE_HEADERS)
91  return NULL;
92  headers = tx->response_headers;
93  }
94  if (headers == NULL)
95  return NULL;
96 
97  size_t i = 0;
98  size_t no_of_headers = htp_table_size(headers);
99  for (; i < no_of_headers; i++) {
100  htp_header_t *h = htp_table_get_index(headers, i, NULL);
101  size_t size1 = bstr_size(h->name);
102  size_t size2 = bstr_size(h->value);
103 
104  if (flags & STREAM_TOSERVER) {
105  if (size1 == 6 &&
106  SCMemcmpLowercase("cookie", bstr_ptr(h->name), 6) == 0) {
107  continue;
108  }
109  } else {
110  if (size1 == 10 &&
111  SCMemcmpLowercase("set-cookie", bstr_ptr(h->name), 10) == 0) {
112  continue;
113  }
114  }
115 
116  size_t size = size1 + size2 + 4;
117 #if 0
118  if (i + 1 == no_of_headers)
119  size += 2;
120 #endif
121  if (size + buf->len > buf->size) {
122  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
123  return NULL;
124  }
125  }
126 
127  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
128  buf->len += bstr_size(h->name);
129  buf->buffer[buf->len++] = ':';
130  buf->buffer[buf->len++] = ' ';
131  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
132  buf->len += bstr_size(h->value);
133  buf->buffer[buf->len++] = '\r';
134  buf->buffer[buf->len++] = '\n';
135 #if 0 // looks like this breaks existing rules
136  if (i + 1 == no_of_headers) {
137  buf->buffer[buf->len++] = '\r';
138  buf->buffer[buf->len++] = '\n';
139  }
140 #endif
141  }
142 
143  *buffer_len = buf->len;
144  return buf->buffer;
145 }
146 
147 static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
148  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
149  const int list_id)
150 {
151  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
152  if (buffer->inspect == NULL) {
153  uint32_t b_len = 0;
154  const uint8_t *b = NULL;
155 
156  if (rs_http2_tx_get_headers(txv, flow_flags, &b, &b_len) != 1)
157  return NULL;
158  if (b == NULL || b_len == 0)
159  return NULL;
160 
161  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
162  InspectionBufferApplyTransforms(buffer, transforms);
163  }
164 
165  return buffer;
166 }
167 
168 /** \internal
169  * \brief custom inspect function to utilize the cached headers
170  */
171 static uint8_t DetectEngineInspectBufferHttpHeader(DetectEngineCtx *de_ctx,
173  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
174 {
175  SCEnter();
176 
177  const int list_id = engine->sm_list;
178  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
179  bool eof =
180  (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress);
181  if (buffer->inspect == NULL) {
182  SCLogDebug("setting up inspect buffer %d", list_id);
183 
184  /* if prefilter didn't already run, we need to consider transformations */
185  const DetectEngineTransforms *transforms = NULL;
186  if (!engine->mpm) {
187  transforms = engine->v2.transforms;
188  }
189 
190  uint32_t rawdata_len = 0;
191  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
192  if (rawdata_len == 0) {
193  SCLogDebug("no data");
194  if (engine->match_on_null && eof) {
196  }
197  goto end;
198  }
199  /* setup buffer and apply transforms */
200  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
201  InspectionBufferApplyTransforms(buffer, transforms);
202  }
203 
204  const uint32_t data_len = buffer->inspect_len;
205  const uint8_t *data = buffer->inspect;
206  const uint64_t offset = buffer->inspect_offset;
207 
208  /* Inspect all the uricontents fetched on each
209  * transaction at the app layer */
210  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
211  (uint8_t *)data, data_len, offset, DETECT_CI_FLAGS_SINGLE,
213  if (match) {
215  }
216 end:
217  if (eof) {
219  }
221 }
222 
224  int list_id;
225  const MpmCtx *mpm_ctx;
228 
229 /** \brief Generic Mpm prefilter callback
230  *
231  * \param det_ctx detection engine thread ctx
232  * \param p packet to inspect
233  * \param f flow to inspect
234  * \param txv tx to inspect
235  * \param pectx inspection context
236  */
237 static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
238  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
239 {
240  SCEnter();
241 
242  const PrefilterMpmHttpHeaderCtx *ctx = pectx;
243  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
244  SCLogDebug("running on list %d", ctx->list_id);
245 
246  const int list_id = ctx->list_id;
247  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
248  if (buffer->inspect == NULL) {
249  uint32_t rawdata_len = 0;
250  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
251  if (rawdata_len == 0)
252  return;
253 
254  /* setup buffer and apply transforms */
255  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
256  InspectionBufferApplyTransforms(buffer, ctx->transforms);
257  }
258 
259  const uint32_t data_len = buffer->inspect_len;
260  const uint8_t *data = buffer->inspect;
261 
262  SCLogDebug("mpm'ing buffer:");
263  //PrintRawDataFp(stdout, data, data_len);
264 
265  if (data != NULL && data_len >= mpm_ctx->minlen) {
266  (void)mpm_table[mpm_ctx->mpm_type].Search(
267  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
268  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
269  }
270 }
271 
272 static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
273  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
274 {
275  SCEnter();
276 
277  htp_tx_t *tx = txv;
278  const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
279  /* if the request wasn't flagged as having a trailer, we skip */
280  if (htud && (
281  ((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
282  ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers))) {
283  SCReturn;
284  }
285  PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
286  SCReturn;
287 }
288 
289 static void PrefilterMpmHttpHeaderFree(void *ptr)
290 {
291  SCFree(ptr);
292 }
293 
294 static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
295  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
296 {
297  SCEnter();
298 
299  /* header */
300  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
301  if (pectx == NULL)
302  return -1;
303  pectx->list_id = list_id;
304  pectx->mpm_ctx = mpm_ctx;
305  pectx->transforms = &mpm_reg->transforms;
306 
307  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
308  mpm_reg->app_v2.alproto, HTP_REQUEST_HEADERS,
309  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
310  if (r != 0) {
311  SCFree(pectx);
312  return r;
313  }
314 
315  /* trailer */
316  pectx = SCCalloc(1, sizeof(*pectx));
317  if (pectx == NULL)
318  return -1;
319  pectx->list_id = list_id;
320  pectx->mpm_ctx = mpm_ctx;
321  pectx->transforms = &mpm_reg->transforms;
322 
323  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
324  mpm_reg->app_v2.alproto, HTP_REQUEST_TRAILER,
325  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
326  if (r != 0) {
327  SCFree(pectx);
328  }
329  return r;
330 }
331 
332 static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
333  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
334 {
335  SCEnter();
336 
337  /* header */
338  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
339  if (pectx == NULL)
340  return -1;
341  pectx->list_id = list_id;
342  pectx->mpm_ctx = mpm_ctx;
343  pectx->transforms = &mpm_reg->transforms;
344 
345  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
346  mpm_reg->app_v2.alproto, HTP_RESPONSE_HEADERS,
347  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
348  if (r != 0) {
349  SCFree(pectx);
350  return r;
351  }
352 
353  /* trailer */
354  pectx = SCCalloc(1, sizeof(*pectx));
355  if (pectx == NULL)
356  return -1;
357  pectx->list_id = list_id;
358  pectx->mpm_ctx = mpm_ctx;
359  pectx->transforms = &mpm_reg->transforms;
360 
361  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
362  mpm_reg->app_v2.alproto, HTP_RESPONSE_TRAILER,
363  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
364  if (r != 0) {
365  SCFree(pectx);
366  }
367  return r;
368 }
369 
370 /**
371  * \brief The setup function for the http_header keyword for a signature.
372  *
373  * \param de_ctx Pointer to the detection engine context.
374  * \param s Pointer to signature for the current Signature being parsed
375  * from the rules.
376  * \param m Pointer to the head of the SigMatchs for the current rule
377  * being parsed.
378  * \param arg Pointer to the string holding the keyword value.
379  *
380  * \retval 0 On success.
381  * \retval -1 On failure.
382  */
383 static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
384 {
386  de_ctx, s, arg, DETECT_AL_HTTP_HEADER, g_http_header_buffer_id, ALPROTO_HTTP1);
387 }
388 
389 /**
390  * \brief this function setup the http.header keyword used in the rule
391  *
392  * \param de_ctx Pointer to the Detection Engine Context
393  * \param s Pointer to the Signature to which the current keyword belongs
394  * \param str Should hold an empty string always
395  *
396  * \retval 0 On success
397  */
398 static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
399 {
400  if (DetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
401  return -1;
403  return -1;
404  return 0;
405 }
406 
407 /**
408  * \brief Registers the keyword handlers for the "http_header" keyword.
409  */
411 {
412  /* http_header content modifier */
413  sigmatch_table[DETECT_AL_HTTP_HEADER].name = "http_header";
414  sigmatch_table[DETECT_AL_HTTP_HEADER].desc = "content modifier to match only on the HTTP header-buffer";
415  sigmatch_table[DETECT_AL_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
416  sigmatch_table[DETECT_AL_HTTP_HEADER].Setup = DetectHttpHeaderSetup;
417 #ifdef UNITTESTS
419 #endif
423 
424  /* http.header sticky buffer */
425  sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
426  sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
427  sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
428  sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
431 
433  HTP_REQUEST_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
435  PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
436  0); /* not used, registered twice: HEADERS/TRAILER */
437 
439  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
441  PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
442  0); /* not used, registered twice: HEADERS/TRAILER */
443 
445  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
447  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
448 
450  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
452  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
453 
455  "http headers");
456 
457  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
458 
459  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
461 }
462 
463 static int g_http_request_header_buffer_id = 0;
464 static int g_http_response_header_buffer_id = 0;
465 static int g_request_header_thread_id = 0;
466 static int g_response_header_thread_id = 0;
467 
468 typedef struct HttpMultiBufItem {
469  uint8_t *buffer;
470  size_t len;
472 
474  // array of items, being defined as a buffer with its length just above
476  // capacity of items (size of allocation)
477  size_t cap;
478  // length of items (number in use)
479  size_t len;
481 
482 static void *HttpMultiBufHeaderThreadDataInit(void *data)
483 {
484  HttpMultiBufHeaderThreadData *td = SCCalloc(1, sizeof(*td));
485 
486  /* This return value check to satisfy our Cocci malloc checks. */
487  if (td == NULL) {
488  SCLogError("failed to allocate %" PRIuMAX " bytes: %s", (uintmax_t)sizeof(*td),
489  strerror(errno));
490  return NULL;
491  }
492  return td;
493 }
494 
495 static void HttpMultiBufHeaderThreadDataFree(void *data)
496 {
497  HttpMultiBufHeaderThreadData *td = data;
498  for (size_t i = 0; i < td->cap; i++) {
499  SCFree(td->items[i].buffer);
500  }
501  SCFree(td->items);
502  SCFree(td);
503 }
504 
505 static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx,
506  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
507  int list_id, uint32_t local_id)
508 {
509  SCEnter();
510 
511  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
512  if (buffer == NULL)
513  return NULL;
514  if (buffer->initialized)
515  return buffer;
516 
517  uint32_t b_len = 0;
518  const uint8_t *b = NULL;
519 
520  if (rs_http2_tx_get_header(txv, flags, local_id, &b, &b_len) != 1) {
522  return NULL;
523  }
524  if (b == NULL || b_len == 0) {
526  return NULL;
527  }
528 
529  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
530  buffer->flags = DETECT_CI_FLAGS_SINGLE;
531 
532  SCReturnPtr(buffer, "InspectionBuffer");
533 }
534 
535 static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
536  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
537  int list_id, uint32_t local_id)
538 {
539  SCEnter();
540  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
541  if (buffer == NULL)
542  return NULL;
543  if (buffer->initialized)
544  return buffer;
545 
546  int kw_thread_id;
547  if (flags & STREAM_TOSERVER) {
548  kw_thread_id = g_request_header_thread_id;
549  } else {
550  kw_thread_id = g_response_header_thread_id;
551  }
553  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id);
554  if (unlikely(hdr_td == NULL)) {
555  return NULL;
556  }
557 
558  htp_tx_t *tx = (htp_tx_t *)txv;
559  htp_table_t *headers;
560  if (flags & STREAM_TOSERVER) {
561  headers = tx->request_headers;
562  } else {
563  headers = tx->response_headers;
564  }
565  size_t no_of_headers = htp_table_size(headers);
566  if (local_id == 0) {
567  // We initialize a big buffer on first item
568  // Then, we will just use parts of it
569  hdr_td->len = 0;
570  if (hdr_td->cap < no_of_headers) {
571  void *new_buffer = SCRealloc(hdr_td->items, no_of_headers * sizeof(HttpMultiBufItem));
572  if (unlikely(new_buffer == NULL)) {
573  return NULL;
574  }
575  hdr_td->items = new_buffer;
576  // zeroes the new part of the items
577  memset(hdr_td->items + hdr_td->cap, 0,
578  (no_of_headers - hdr_td->cap) * sizeof(HttpMultiBufItem));
579  hdr_td->cap = no_of_headers;
580  }
581  for (size_t i = 0; i < no_of_headers; i++) {
582  htp_header_t *h = htp_table_get_index(headers, i, NULL);
583  size_t size1 = bstr_size(h->name);
584  size_t size2 = bstr_size(h->value);
585  size_t size = size1 + size2 + 2;
586  if (hdr_td->items[i].len < size) {
587  // Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
588  void *tmp = SCRealloc(hdr_td->items[i].buffer, size);
589  if (unlikely(tmp == NULL)) {
590  return NULL;
591  }
592  hdr_td->items[i].buffer = tmp;
593  }
594  memcpy(hdr_td->items[i].buffer, bstr_ptr(h->name), size1);
595  hdr_td->items[i].buffer[size1] = ':';
596  hdr_td->items[i].buffer[size1 + 1] = ' ';
597  memcpy(hdr_td->items[i].buffer + size1 + 2, bstr_ptr(h->value), size2);
598  hdr_td->items[i].len = size;
599  }
600  hdr_td->len = no_of_headers;
601  }
602 
603  // cbdata->local_id is the index of the requested header buffer
604  // hdr_td->len is the number of header buffers
605  if (local_id < hdr_td->len) {
606  // we have one valid header buffer
608  buffer, transforms, hdr_td->items[local_id].buffer, hdr_td->items[local_id].len);
609  buffer->flags = DETECT_CI_FLAGS_SINGLE;
610  SCReturnPtr(buffer, "InspectionBuffer");
611  } // else there are no more header buffer to get
613  return NULL;
614 }
615 
616 static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
617 {
618  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
619  return -1;
620 
622  return -1;
623 
624  return 0;
625 }
626 
628 {
629  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
631  "sticky buffer to match on only one HTTP header name and value";
632  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
633  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
636 
638  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
640  HTP_REQUEST_HEADERS, GetHttp1HeaderData, 2, HTP_REQUEST_HEADERS);
641 
642  DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
643  g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
644  DetectBufferTypeSupportsMultiInstance("http_request_header");
645  g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header",
646  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
647 }
648 
649 static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
650 {
651  if (DetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
652  return -1;
653 
655  return -1;
656 
657  return 0;
658 }
659 
661 {
662  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
664  "sticky buffer to match on only one HTTP header name and value";
665  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
666  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
669 
671  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
673  HTP_RESPONSE_HEADERS, GetHttp1HeaderData, 2, HTP_RESPONSE_HEADERS);
674 
675  DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
676  g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
677  DetectBufferTypeSupportsMultiInstance("http_response_header");
678  g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header",
679  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
680 }
681 
682 /************************************Unittests*********************************/
683 
684 #ifdef UNITTESTS
686 #endif
687 
688 /**
689  * @}
690  */
DetectEngineAppInspectionEngine_
Definition: detect.h:429
SigTableElmt_::url
const char * url
Definition: detect.h:1312
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1738
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-header.c:67
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:433
detect-content.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1517
SigTableElmt_::desc
const char * desc
Definition: detect.h:1311
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:128
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:2155
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1515
SigTableElmt_::name
const char * name
Definition: detect.h:1309
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1465
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:408
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectHttpRequestHeaderRegister
void DetectHttpRequestHeaderRegister(void)
Definition: detect-http-header.c:627
Flow_::proto
uint8_t proto
Definition: flow.h:378
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1357
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:1072
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1205
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@84::@86 app_v2
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1303
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1044
detect-http-header.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@79 v2
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:687
HttpMultiBufItem::len
size_t len
Definition: detect-http-header.c:470
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:378
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:700
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1501
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1094
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority, int tx_min_progress)
Definition: detect-engine.c:2205
util-memcmp.h
DetectHttpResponseHeaderRegister
void DetectHttpResponseHeaderRegister(void)
Definition: detect-http-header.c:660
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:437
DETECT_AL_HTTP_HEADER
@ DETECT_AL_HTTP_HEADER
Definition: detect-engine-register.h:159
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
app-layer-htp.h
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1568
HttpMultiBufHeaderThreadData::len
size_t len
Definition: detect-http-header.c:479
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
HttpMultiBufHeaderThreadData::items
HttpMultiBufItem * items
Definition: detect-http-header.c:475
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:212
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
HttpMultiBufHeaderThreadData
Definition: detect-http-header.c:473
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:375
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1307
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
HttpMultiBufItem
struct HttpMultiBufItem HttpMultiBufItem
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:195
detect-http-header.c
Packet_
Definition: decode.h:476
detect-http-header-common.h
HttpMultiBufHeaderThreadData
struct HttpMultiBufHeaderThreadData HttpMultiBufHeaderThreadData
HttpHeaderThreadConfig_
Definition: detect-http-header-common.h:33
HttpHeaderBuffer_::buffer
uint8_t * buffer
Definition: detect-http-header-common.h:28
DetectEngineAppInspectionEngine_::match_on_null
bool match_on_null
Definition: detect.h:436
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:64
HttpMultiBufHeaderThreadData::cap
size_t cap
Definition: detect-http-header.c:477
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:168
HttpMultiBufItem
Definition: detect-http-header.c:468
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:39
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1204
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:451
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1372
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:286
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
flags
uint8_t flags
Definition: decode-gre.h:0
PrefilterMpmHttpHeaderCtx::list_id
int list_id
Definition: detect-http-header.c:224
PrefilterMpmHttpHeaderCtx
Definition: detect-http-header.c:223
DetectHttpHeaderRegister
void DetectHttpHeaderRegister(void)
Registers the keyword handlers for the "http_header" keyword.
Definition: detect-http-header.c:410
suricata-common.h
DETECT_HTTP_HEADER
@ DETECT_HTTP_HEADER
Definition: detect-engine-register.h:160
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1712
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
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:1581
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:273
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:37
DetectEngineAppInspectionEngine_::progress
int16_t progress
Definition: detect.h:439
util-validate.h
PrefilterMpmHttpHeaderCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-http-header.c:226
PrefilterMpmHttpHeaderCtx
struct PrefilterMpmHttpHeaderCtx PrefilterMpmHttpHeaderCtx
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:376
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
PrefilterMpmHttpHeaderCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-http-header.c:225
str
#define str(s)
Definition: suricata-common.h:291
DetectThreadCtxGetGlobalKeywordThreadCtx
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3783
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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:1596
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:448
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:70
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
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:1521
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
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1493
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1191
MpmCtx_
Definition: util-mpm.h:88
HttpHeaderBuffer_
Definition: detect-http-header-common.h:27
DETECT_HTTP_RESPONSE_HEADER
@ DETECT_HTTP_RESPONSE_HEADER
Definition: detect-engine-register.h:213
DetectEngineContentInspection
bool DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len, const uint32_t stream_start_offset, const uint8_t flags, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:723
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:455
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectHttpHeaderRegisterTests
void DetectHttpHeaderRegisterTests(void)
Definition: detect-http-header.c:4557
HttpMultiBufItem::buffer
uint8_t * buffer
Definition: detect-http-header.c:469
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:3739
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1301
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:689