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  const htp_headers_t *headers;
83  if (flags & STREAM_TOSERVER) {
84  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
86  return NULL;
87  headers = htp_tx_request_headers(tx);
88  } else {
89  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
91  return NULL;
92  headers = htp_tx_response_headers(tx);
93  }
94  if (headers == NULL)
95  return NULL;
96 
97  size_t i = 0;
98  size_t no_of_headers = htp_headers_size(headers);
99  for (; i < no_of_headers; i++) {
100  const htp_header_t *h = htp_headers_get_index(headers, i);
101  size_t size1 = htp_header_name_len(h);
102  size_t size2 = htp_header_value_len(h);
103 
104  if (flags & STREAM_TOSERVER) {
105  if (size1 == 6 && SCMemcmpLowercase("cookie", htp_header_name_ptr(h), 6) == 0) {
106  continue;
107  }
108  } else {
109  if (size1 == 10 && SCMemcmpLowercase("set-cookie", htp_header_name_ptr(h), 10) == 0) {
110  continue;
111  }
112  }
113 
114  size_t size = size1 + size2 + 4;
115 #if 0
116  if (i + 1 == no_of_headers)
117  size += 2;
118 #endif
119  if (size + buf->len > buf->size) {
120  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
121  return NULL;
122  }
123  }
124 
125  memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
126  buf->len += htp_header_name_len(h);
127  buf->buffer[buf->len++] = ':';
128  buf->buffer[buf->len++] = ' ';
129  memcpy(buf->buffer + buf->len, htp_header_value_ptr(h), htp_header_value_len(h));
130  buf->len += htp_header_value_len(h);
131  buf->buffer[buf->len++] = '\r';
132  buf->buffer[buf->len++] = '\n';
133 #if 0 // looks like this breaks existing rules
134  if (i + 1 == no_of_headers) {
135  buf->buffer[buf->len++] = '\r';
136  buf->buffer[buf->len++] = '\n';
137  }
138 #endif
139  }
140 
141  *buffer_len = buf->len;
142  return buf->buffer;
143 }
144 
145 static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
146  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
147  const int list_id)
148 {
149  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
150  if (buffer->inspect == NULL) {
151  uint32_t b_len = 0;
152  const uint8_t *b = NULL;
153 
154  if (rs_http2_tx_get_headers(txv, flow_flags, &b, &b_len) != 1)
155  return NULL;
156  if (b == NULL || b_len == 0)
157  return NULL;
158 
159  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
160  }
161 
162  return buffer;
163 }
164 
165 /** \internal
166  * \brief custom inspect function to utilize the cached headers
167  */
168 static uint8_t DetectEngineInspectBufferHttpHeader(DetectEngineCtx *de_ctx,
170  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
171 {
172  SCEnter();
173 
174  const int list_id = engine->sm_list;
175  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
176  bool eof =
177  (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress);
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  if (engine->match_on_null && eof) {
193  }
194  goto end;
195  }
196  /* setup buffer and apply transforms */
198  det_ctx, list_id, buffer, rawdata, rawdata_len, transforms);
199  }
200 
201  const uint32_t data_len = buffer->inspect_len;
202  const uint8_t *data = buffer->inspect;
203  const uint64_t offset = buffer->inspect_offset;
204 
205  /* Inspect all the uricontents fetched on each
206  * transaction at the app layer */
207  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
208  (uint8_t *)data, data_len, offset, DETECT_CI_FLAGS_SINGLE,
210  if (match) {
212  }
213 end:
214  if (eof) {
216  }
218 }
219 
221  int list_id;
222  const MpmCtx *mpm_ctx;
225 
226 /** \brief Generic Mpm prefilter callback
227  *
228  * \param det_ctx detection engine thread ctx
229  * \param p packet to inspect
230  * \param f flow to inspect
231  * \param txv tx to inspect
232  * \param pectx inspection context
233  */
234 static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
235  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
236 {
237  SCEnter();
238 
239  const PrefilterMpmHttpHeaderCtx *ctx = pectx;
240  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
241  SCLogDebug("running on list %d", ctx->list_id);
242 
243  const int list_id = ctx->list_id;
244  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
245  if (buffer->inspect == NULL) {
246  uint32_t rawdata_len = 0;
247  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
248  if (rawdata_len == 0)
249  return;
250 
251  /* setup buffer and apply transforms */
253  det_ctx, list_id, buffer, rawdata, rawdata_len, ctx->transforms);
254  }
255 
256  const uint32_t data_len = buffer->inspect_len;
257  const uint8_t *data = buffer->inspect;
258 
259  SCLogDebug("mpm'ing buffer:");
260  //PrintRawDataFp(stdout, data, data_len);
261 
262  if (data != NULL && data_len >= mpm_ctx->minlen) {
263  (void)mpm_table[mpm_ctx->mpm_type].Search(
264  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
265  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
266  }
267 }
268 
269 static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
270  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
271 {
272  SCEnter();
273 
274  htp_tx_t *tx = txv;
275  const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
276  /* if the request wasn't flagged as having a trailer, we skip */
277  if (htud && (
278  ((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
279  ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers))) {
280  SCReturn;
281  }
282  PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
283  SCReturn;
284 }
285 
286 static void PrefilterMpmHttpHeaderFree(void *ptr)
287 {
288  SCFree(ptr);
289 }
290 
291 static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
292  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
293 {
294  SCEnter();
295 
296  /* header */
297  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
298  if (pectx == NULL)
299  return -1;
300  pectx->list_id = list_id;
301  pectx->mpm_ctx = mpm_ctx;
302  pectx->transforms = &mpm_reg->transforms;
303 
304  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
305  HTP_REQUEST_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
306  if (r != 0) {
307  SCFree(pectx);
308  return r;
309  }
310 
311  /* trailer */
312  pectx = SCCalloc(1, sizeof(*pectx));
313  if (pectx == NULL)
314  return -1;
315  pectx->list_id = list_id;
316  pectx->mpm_ctx = mpm_ctx;
317  pectx->transforms = &mpm_reg->transforms;
318 
319  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
320  HTP_REQUEST_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
321  if (r != 0) {
322  SCFree(pectx);
323  }
324  return r;
325 }
326 
327 static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
328  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
329 {
330  SCEnter();
331 
332  /* header */
333  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
334  if (pectx == NULL)
335  return -1;
336  pectx->list_id = list_id;
337  pectx->mpm_ctx = mpm_ctx;
338  pectx->transforms = &mpm_reg->transforms;
339 
340  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
341  HTP_RESPONSE_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
342  if (r != 0) {
343  SCFree(pectx);
344  return r;
345  }
346 
347  /* trailer */
348  pectx = SCCalloc(1, sizeof(*pectx));
349  if (pectx == NULL)
350  return -1;
351  pectx->list_id = list_id;
352  pectx->mpm_ctx = mpm_ctx;
353  pectx->transforms = &mpm_reg->transforms;
354 
355  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
356  HTP_RESPONSE_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
357  if (r != 0) {
358  SCFree(pectx);
359  }
360  return r;
361 }
362 
363 /**
364  * \brief The setup function for the http_header keyword for a signature.
365  *
366  * \param de_ctx Pointer to the detection engine context.
367  * \param s Pointer to signature for the current Signature being parsed
368  * from the rules.
369  * \param m Pointer to the head of the SigMatchs for the current rule
370  * being parsed.
371  * \param arg Pointer to the string holding the keyword value.
372  *
373  * \retval 0 On success.
374  * \retval -1 On failure.
375  */
376 static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
377 {
379  de_ctx, s, arg, DETECT_HTTP_HEADER_CM, g_http_header_buffer_id, ALPROTO_HTTP1);
380 }
381 
382 /**
383  * \brief this function setup the http.header keyword used in the rule
384  *
385  * \param de_ctx Pointer to the Detection Engine Context
386  * \param s Pointer to the Signature to which the current keyword belongs
387  * \param str Should hold an empty string always
388  *
389  * \retval 0 On success
390  */
391 static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
392 {
393  if (DetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
394  return -1;
396  return -1;
397  return 0;
398 }
399 
400 /**
401  * \brief Registers the keyword handlers for the "http_header" keyword.
402  */
404 {
405  /* http_header content modifier */
406  sigmatch_table[DETECT_HTTP_HEADER_CM].name = "http_header";
408  "content modifier to match only on the HTTP header-buffer";
410  "/rules/http-keywords.html#http-header-and-http-raw-header";
411  sigmatch_table[DETECT_HTTP_HEADER_CM].Setup = DetectHttpHeaderSetup;
412 #ifdef UNITTESTS
414 #endif
418 
419  /* http.header sticky buffer */
420  sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
421  sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
422  sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
423  sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
426 
428  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
430  PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
431  0); /* not used, registered twice: HEADERS/TRAILER */
432 
434  HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
436  PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
437  0); /* not used, registered twice: HEADERS/TRAILER */
438 
440  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
442  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
443 
445  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
447  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
448 
450  "http headers");
451 
452  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
453 
454  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
456 }
457 
458 static int g_http_request_header_buffer_id = 0;
459 static int g_http_response_header_buffer_id = 0;
460 static int g_request_header_thread_id = 0;
461 static int g_response_header_thread_id = 0;
462 
463 typedef struct HttpMultiBufItem {
464  uint8_t *buffer;
465  size_t len;
467 
469  // array of items, being defined as a buffer with its length just above
471  // capacity of items (size of allocation)
472  size_t cap;
473  // length of items (number in use)
474  size_t len;
476 
477 static void *HttpMultiBufHeaderThreadDataInit(void *data)
478 {
479  HttpMultiBufHeaderThreadData *td = SCCalloc(1, sizeof(*td));
480 
481  /* This return value check to satisfy our Cocci malloc checks. */
482  if (td == NULL) {
483  SCLogError("failed to allocate %" PRIuMAX " bytes: %s", (uintmax_t)sizeof(*td),
484  strerror(errno));
485  return NULL;
486  }
487  return td;
488 }
489 
490 static void HttpMultiBufHeaderThreadDataFree(void *data)
491 {
492  HttpMultiBufHeaderThreadData *td = data;
493  for (size_t i = 0; i < td->cap; i++) {
494  SCFree(td->items[i].buffer);
495  }
496  SCFree(td->items);
497  SCFree(td);
498 }
499 
500 static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx,
501  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
502  int list_id, uint32_t local_id)
503 {
504  SCEnter();
505 
506  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
507  if (buffer == NULL)
508  return NULL;
509  if (buffer->initialized)
510  return buffer;
511 
512  uint32_t b_len = 0;
513  const uint8_t *b = NULL;
514 
515  if (rs_http2_tx_get_header(txv, flags, local_id, &b, &b_len) != 1) {
517  return NULL;
518  }
519  if (b == NULL || b_len == 0) {
521  return NULL;
522  }
523 
524  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
525  buffer->flags = DETECT_CI_FLAGS_SINGLE;
526 
527  SCReturnPtr(buffer, "InspectionBuffer");
528 }
529 
530 static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
531  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
532  int list_id, uint32_t local_id)
533 {
534  SCEnter();
535  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
536  if (buffer == NULL)
537  return NULL;
538  if (buffer->initialized)
539  return buffer;
540 
541  int kw_thread_id;
542  if (flags & STREAM_TOSERVER) {
543  kw_thread_id = g_request_header_thread_id;
544  } else {
545  kw_thread_id = g_response_header_thread_id;
546  }
548  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id);
549  if (unlikely(hdr_td == NULL)) {
550  return NULL;
551  }
552 
553  htp_tx_t *tx = (htp_tx_t *)txv;
554  const htp_headers_t *headers;
555  if (flags & STREAM_TOSERVER) {
556  headers = htp_tx_request_headers(tx);
557  } else {
558  headers = htp_tx_response_headers(tx);
559  }
560  size_t no_of_headers = htp_headers_size(headers);
561  if (local_id == 0) {
562  // We initialize a big buffer on first item
563  // Then, we will just use parts of it
564  hdr_td->len = 0;
565  if (hdr_td->cap < no_of_headers) {
566  void *new_buffer = SCRealloc(hdr_td->items, no_of_headers * sizeof(HttpMultiBufItem));
567  if (unlikely(new_buffer == NULL)) {
568  return NULL;
569  }
570  hdr_td->items = new_buffer;
571  // zeroes the new part of the items
572  memset(hdr_td->items + hdr_td->cap, 0,
573  (no_of_headers - hdr_td->cap) * sizeof(HttpMultiBufItem));
574  hdr_td->cap = no_of_headers;
575  }
576  for (size_t i = 0; i < no_of_headers; i++) {
577  const htp_header_t *h = htp_headers_get_index(headers, i);
578  size_t size1 = htp_header_name_len(h);
579  size_t size2 = htp_header_value_len(h);
580  size_t size = size1 + size2 + 2;
581  if (hdr_td->items[i].len < size) {
582  // Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
583  void *tmp = SCRealloc(hdr_td->items[i].buffer, size);
584  if (unlikely(tmp == NULL)) {
585  return NULL;
586  }
587  hdr_td->items[i].buffer = tmp;
588  }
589  memcpy(hdr_td->items[i].buffer, htp_header_name_ptr(h), size1);
590  hdr_td->items[i].buffer[size1] = ':';
591  hdr_td->items[i].buffer[size1 + 1] = ' ';
592  memcpy(hdr_td->items[i].buffer + size1 + 2, htp_header_value_ptr(h), size2);
593  hdr_td->items[i].len = size;
594  }
595  hdr_td->len = no_of_headers;
596  }
597 
598  // cbdata->local_id is the index of the requested header buffer
599  // hdr_td->len is the number of header buffers
600  if (local_id < hdr_td->len) {
601  // we have one valid header buffer
603  buffer, transforms, hdr_td->items[local_id].buffer, hdr_td->items[local_id].len);
604  buffer->flags = DETECT_CI_FLAGS_SINGLE;
605  SCReturnPtr(buffer, "InspectionBuffer");
606  } // else there are no more header buffer to get
608  return NULL;
609 }
610 
611 static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
612 {
613  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
614  return -1;
615 
617  return -1;
618 
619  return 0;
620 }
621 
623 {
624  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
626  "sticky buffer to match on only one HTTP header name and value";
627  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
628  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
631 
633  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
636 
637  DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
638  g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
639  DetectBufferTypeSupportsMultiInstance("http_request_header");
640  g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header",
641  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
642 }
643 
644 static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
645 {
646  if (DetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
647  return -1;
648 
650  return -1;
651 
652  return 0;
653 }
654 
656 {
657  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
659  "sticky buffer to match on only one HTTP header name and value";
660  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
661  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
664 
666  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
669 
670  DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
671  g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
672  DetectBufferTypeSupportsMultiInstance("http_response_header");
673  g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header",
674  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
675 }
676 
677 /************************************Unittests*********************************/
678 
679 #ifdef UNITTESTS
681 #endif
682 
683 /**
684  * @}
685  */
htp_header_value_len
#define htp_header_value_len(h)
Definition: app-layer-htp-libhtp.h:191
DetectEngineAppInspectionEngine_
Definition: detect.h:431
SigTableElmt_::url
const char * url
Definition: detect.h:1323
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1782
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-header.c:67
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:435
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:1528
SigTableElmt_::desc
const char * desc
Definition: detect.h:1322
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2195
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1526
SigTableElmt_::name
const char * name
Definition: detect.h:1320
InspectionBuffer::initialized
bool initialized
Definition: detect.h:379
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine.c:1662
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1476
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:410
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectHttpRequestHeaderRegister
void DetectHttpRequestHeaderRegister(void)
Definition: detect-http-header.c:622
HTP_RESPONSE_PROGRESS_HEADERS
#define HTP_RESPONSE_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:94
htp_headers_get_index
#define htp_headers_get_index(headers, index)
Definition: app-layer-htp-libhtp.h:197
Flow_::proto
uint8_t proto
Definition: flow.h:376
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1377
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:1073
InspectionBuffer
Definition: detect.h:375
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1216
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1314
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:855
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1064
htp_tx_response_headers
#define htp_tx_response_headers(tx)
Definition: app-layer-htp-libhtp.h:174
detect-http-header.h
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:698
HttpMultiBufItem::len
size_t len
Definition: detect-http-header.c:465
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:380
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1305
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:711
detect-engine-prefilter.h
HTP_REQUEST_PROGRESS_TRAILER
#define HTP_REQUEST_PROGRESS_TRAILER
Definition: app-layer-htp-libhtp.h:91
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1521
htp_headers_size
#define htp_headers_size(headers)
Definition: app-layer-htp-libhtp.h:196
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1114
htp_headers_t
#define htp_headers_t
Definition: app-layer-htp-libhtp.h:200
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:2245
util-memcmp.h
DetectHttpResponseHeaderRegister
void DetectHttpResponseHeaderRegister(void)
Definition: detect-http-header.c:655
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:439
htp_tx_request_headers
#define htp_tx_request_headers(tx)
Definition: app-layer-htp-libhtp.h:173
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
app-layer-htp.h
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1609
HttpMultiBufHeaderThreadData::len
size_t len
Definition: detect-http-header.c:474
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1110
HttpMultiBufHeaderThreadData::items
HttpMultiBufItem * items
Definition: detect-http-header.c:470
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:218
DETECT_HTTP_REQUEST_HEADER
@ DETECT_HTTP_REQUEST_HEADER
Definition: detect-engine-register.h:213
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
HttpMultiBufHeaderThreadData
Definition: detect-http-header.c:468
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:377
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:1318
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:217
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:220
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:438
htp_header_value_ptr
#define htp_header_value_ptr(h)
Definition: app-layer-htp-libhtp.h:192
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.
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@82::@84 app_v2
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
HttpMultiBufHeaderThreadData::cap
size_t cap
Definition: detect-http-header.c:472
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:167
HttpMultiBufItem
Definition: detect-http-header.c:463
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:1215
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
htp_header_name_len
#define htp_header_name_len(h)
Definition: app-layer-htp-libhtp.h:188
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:453
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1383
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:221
PrefilterMpmHttpHeaderCtx
Definition: detect-http-header.c:220
DetectHttpHeaderRegister
void DetectHttpHeaderRegister(void)
Registers the keyword handlers for the "http_header" keyword.
Definition: detect-http-header.c:403
suricata-common.h
DETECT_HTTP_HEADER
@ DETECT_HTTP_HEADER
Definition: detect-engine-register.h:161
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
HTP_REQUEST_PROGRESS_HEADERS
#define HTP_REQUEST_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:89
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:1622
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:212
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:441
util-validate.h
PrefilterMpmHttpHeaderCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-http-header.c:223
PrefilterMpmHttpHeaderCtx
struct PrefilterMpmHttpHeaderCtx PrefilterMpmHttpHeaderCtx
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:378
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
PrefilterMpmHttpHeaderCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-http-header.c:222
str
#define str(s)
Definition: suricata-common.h:300
DetectThreadCtxGetGlobalKeywordThreadCtx
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3857
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DETECT_HTTP_HEADER_CM
@ DETECT_HTTP_HEADER_CM
Definition: detect-engine-register.h:160
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:450
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
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:1541
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
HTP_RESPONSE_PROGRESS_TRAILER
#define HTP_RESPONSE_PROGRESS_TRAILER
Definition: app-layer-htp-libhtp.h:96
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:1504
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:245
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1211
htp_header_name_ptr
#define htp_header_name_ptr(h)
Definition: app-layer-htp-libhtp.h:189
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:214
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:448
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@77 v2
DetectHttpHeaderRegisterTests
void DetectHttpHeaderRegisterTests(void)
Definition: detect-http-header.c:4557
HttpMultiBufItem::buffer
uint8_t * buffer
Definition: detect-http-header.c:464
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:3813
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1312
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:700