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  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
160  InspectionBufferApplyTransforms(buffer, transforms);
161  }
162 
163  return buffer;
164 }
165 
166 /** \internal
167  * \brief custom inspect function to utilize the cached headers
168  */
169 static uint8_t DetectEngineInspectBufferHttpHeader(DetectEngineCtx *de_ctx,
171  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
172 {
173  SCEnter();
174 
175  const int list_id = engine->sm_list;
176  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
177  bool eof =
178  (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress);
179  if (buffer->inspect == NULL) {
180  SCLogDebug("setting up inspect buffer %d", list_id);
181 
182  /* if prefilter didn't already run, we need to consider transformations */
183  const DetectEngineTransforms *transforms = NULL;
184  if (!engine->mpm) {
185  transforms = engine->v2.transforms;
186  }
187 
188  uint32_t rawdata_len = 0;
189  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
190  if (rawdata_len == 0) {
191  SCLogDebug("no data");
192  if (engine->match_on_null && eof) {
194  }
195  goto end;
196  }
197  /* setup buffer and apply transforms */
198  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
199  InspectionBufferApplyTransforms(buffer, transforms);
200  }
201 
202  const uint32_t data_len = buffer->inspect_len;
203  const uint8_t *data = buffer->inspect;
204  const uint64_t offset = buffer->inspect_offset;
205 
206  /* Inspect all the uricontents fetched on each
207  * transaction at the app layer */
208  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
209  (uint8_t *)data, data_len, offset, DETECT_CI_FLAGS_SINGLE,
211  if (match) {
213  }
214 end:
215  if (eof) {
217  }
219 }
220 
222  int list_id;
223  const MpmCtx *mpm_ctx;
226 
227 /** \brief Generic Mpm prefilter callback
228  *
229  * \param det_ctx detection engine thread ctx
230  * \param p packet to inspect
231  * \param f flow to inspect
232  * \param txv tx to inspect
233  * \param pectx inspection context
234  */
235 static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
236  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
237 {
238  SCEnter();
239 
240  const PrefilterMpmHttpHeaderCtx *ctx = pectx;
241  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
242  SCLogDebug("running on list %d", ctx->list_id);
243 
244  const int list_id = ctx->list_id;
245  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
246  if (buffer->inspect == NULL) {
247  uint32_t rawdata_len = 0;
248  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
249  if (rawdata_len == 0)
250  return;
251 
252  /* setup buffer and apply transforms */
253  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
254  InspectionBufferApplyTransforms(buffer, ctx->transforms);
255  }
256 
257  const uint32_t data_len = buffer->inspect_len;
258  const uint8_t *data = buffer->inspect;
259 
260  SCLogDebug("mpm'ing buffer:");
261  //PrintRawDataFp(stdout, data, data_len);
262 
263  if (data != NULL && data_len >= mpm_ctx->minlen) {
264  (void)mpm_table[mpm_ctx->mpm_type].Search(
265  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
266  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
267  }
268 }
269 
270 static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
271  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
272 {
273  SCEnter();
274 
275  htp_tx_t *tx = txv;
276  const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
277  /* if the request wasn't flagged as having a trailer, we skip */
278  if (htud && (
279  ((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
280  ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers))) {
281  SCReturn;
282  }
283  PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
284  SCReturn;
285 }
286 
287 static void PrefilterMpmHttpHeaderFree(void *ptr)
288 {
289  SCFree(ptr);
290 }
291 
292 static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
293  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
294 {
295  SCEnter();
296 
297  /* header */
298  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
299  if (pectx == NULL)
300  return -1;
301  pectx->list_id = list_id;
302  pectx->mpm_ctx = mpm_ctx;
303  pectx->transforms = &mpm_reg->transforms;
304 
305  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
306  HTP_REQUEST_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
307  if (r != 0) {
308  SCFree(pectx);
309  return r;
310  }
311 
312  /* trailer */
313  pectx = SCCalloc(1, sizeof(*pectx));
314  if (pectx == NULL)
315  return -1;
316  pectx->list_id = list_id;
317  pectx->mpm_ctx = mpm_ctx;
318  pectx->transforms = &mpm_reg->transforms;
319 
320  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
321  HTP_REQUEST_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
322  if (r != 0) {
323  SCFree(pectx);
324  }
325  return r;
326 }
327 
328 static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
329  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
330 {
331  SCEnter();
332 
333  /* header */
334  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
335  if (pectx == NULL)
336  return -1;
337  pectx->list_id = list_id;
338  pectx->mpm_ctx = mpm_ctx;
339  pectx->transforms = &mpm_reg->transforms;
340 
341  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
342  HTP_RESPONSE_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
343  if (r != 0) {
344  SCFree(pectx);
345  return r;
346  }
347 
348  /* trailer */
349  pectx = SCCalloc(1, sizeof(*pectx));
350  if (pectx == NULL)
351  return -1;
352  pectx->list_id = list_id;
353  pectx->mpm_ctx = mpm_ctx;
354  pectx->transforms = &mpm_reg->transforms;
355 
356  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
357  HTP_RESPONSE_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
358  if (r != 0) {
359  SCFree(pectx);
360  }
361  return r;
362 }
363 
364 /**
365  * \brief The setup function for the http_header keyword for a signature.
366  *
367  * \param de_ctx Pointer to the detection engine context.
368  * \param s Pointer to signature for the current Signature being parsed
369  * from the rules.
370  * \param m Pointer to the head of the SigMatchs for the current rule
371  * being parsed.
372  * \param arg Pointer to the string holding the keyword value.
373  *
374  * \retval 0 On success.
375  * \retval -1 On failure.
376  */
377 static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
378 {
380  de_ctx, s, arg, DETECT_HTTP_HEADER_CM, g_http_header_buffer_id, ALPROTO_HTTP1);
381 }
382 
383 /**
384  * \brief this function setup the http.header keyword used in the rule
385  *
386  * \param de_ctx Pointer to the Detection Engine Context
387  * \param s Pointer to the Signature to which the current keyword belongs
388  * \param str Should hold an empty string always
389  *
390  * \retval 0 On success
391  */
392 static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
393 {
394  if (DetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
395  return -1;
397  return -1;
398  return 0;
399 }
400 
401 /**
402  * \brief Registers the keyword handlers for the "http_header" keyword.
403  */
405 {
406  /* http_header content modifier */
407  sigmatch_table[DETECT_HTTP_HEADER_CM].name = "http_header";
409  "content modifier to match only on the HTTP header-buffer";
411  "/rules/http-keywords.html#http-header-and-http-raw-header";
412  sigmatch_table[DETECT_HTTP_HEADER_CM].Setup = DetectHttpHeaderSetup;
413 #ifdef UNITTESTS
415 #endif
419 
420  /* http.header sticky buffer */
421  sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
422  sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
423  sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
424  sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
427 
429  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
431  PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
432  0); /* not used, registered twice: HEADERS/TRAILER */
433 
435  HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
437  PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
438  0); /* not used, registered twice: HEADERS/TRAILER */
439 
441  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
443  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
444 
446  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
448  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
449 
451  "http headers");
452 
453  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
454 
455  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
457 }
458 
459 static int g_http_request_header_buffer_id = 0;
460 static int g_http_response_header_buffer_id = 0;
461 static int g_request_header_thread_id = 0;
462 static int g_response_header_thread_id = 0;
463 
464 typedef struct HttpMultiBufItem {
465  uint8_t *buffer;
466  size_t len;
468 
470  // array of items, being defined as a buffer with its length just above
472  // capacity of items (size of allocation)
473  size_t cap;
474  // length of items (number in use)
475  size_t len;
477 
478 static void *HttpMultiBufHeaderThreadDataInit(void *data)
479 {
480  HttpMultiBufHeaderThreadData *td = SCCalloc(1, sizeof(*td));
481 
482  /* This return value check to satisfy our Cocci malloc checks. */
483  if (td == NULL) {
484  SCLogError("failed to allocate %" PRIuMAX " bytes: %s", (uintmax_t)sizeof(*td),
485  strerror(errno));
486  return NULL;
487  }
488  return td;
489 }
490 
491 static void HttpMultiBufHeaderThreadDataFree(void *data)
492 {
493  HttpMultiBufHeaderThreadData *td = data;
494  for (size_t i = 0; i < td->cap; i++) {
495  SCFree(td->items[i].buffer);
496  }
497  SCFree(td->items);
498  SCFree(td);
499 }
500 
501 static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx,
502  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
503  int list_id, uint32_t local_id)
504 {
505  SCEnter();
506 
507  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
508  if (buffer == NULL)
509  return NULL;
510  if (buffer->initialized)
511  return buffer;
512 
513  uint32_t b_len = 0;
514  const uint8_t *b = NULL;
515 
516  if (rs_http2_tx_get_header(txv, flags, local_id, &b, &b_len) != 1) {
518  return NULL;
519  }
520  if (b == NULL || b_len == 0) {
522  return NULL;
523  }
524 
525  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
526  buffer->flags = DETECT_CI_FLAGS_SINGLE;
527 
528  SCReturnPtr(buffer, "InspectionBuffer");
529 }
530 
531 static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
532  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
533  int list_id, uint32_t local_id)
534 {
535  SCEnter();
536  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
537  if (buffer == NULL)
538  return NULL;
539  if (buffer->initialized)
540  return buffer;
541 
542  int kw_thread_id;
543  if (flags & STREAM_TOSERVER) {
544  kw_thread_id = g_request_header_thread_id;
545  } else {
546  kw_thread_id = g_response_header_thread_id;
547  }
549  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id);
550  if (unlikely(hdr_td == NULL)) {
551  return NULL;
552  }
553 
554  htp_tx_t *tx = (htp_tx_t *)txv;
555  const htp_headers_t *headers;
556  if (flags & STREAM_TOSERVER) {
557  headers = htp_tx_request_headers(tx);
558  } else {
559  headers = htp_tx_response_headers(tx);
560  }
561  size_t no_of_headers = htp_headers_size(headers);
562  if (local_id == 0) {
563  // We initialize a big buffer on first item
564  // Then, we will just use parts of it
565  hdr_td->len = 0;
566  if (hdr_td->cap < no_of_headers) {
567  void *new_buffer = SCRealloc(hdr_td->items, no_of_headers * sizeof(HttpMultiBufItem));
568  if (unlikely(new_buffer == NULL)) {
569  return NULL;
570  }
571  hdr_td->items = new_buffer;
572  // zeroes the new part of the items
573  memset(hdr_td->items + hdr_td->cap, 0,
574  (no_of_headers - hdr_td->cap) * sizeof(HttpMultiBufItem));
575  hdr_td->cap = no_of_headers;
576  }
577  for (size_t i = 0; i < no_of_headers; i++) {
578  const htp_header_t *h = htp_headers_get_index(headers, i);
579  size_t size1 = htp_header_name_len(h);
580  size_t size2 = htp_header_value_len(h);
581  size_t size = size1 + size2 + 2;
582  if (hdr_td->items[i].len < size) {
583  // Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
584  void *tmp = SCRealloc(hdr_td->items[i].buffer, size);
585  if (unlikely(tmp == NULL)) {
586  return NULL;
587  }
588  hdr_td->items[i].buffer = tmp;
589  }
590  memcpy(hdr_td->items[i].buffer, htp_header_name_ptr(h), size1);
591  hdr_td->items[i].buffer[size1] = ':';
592  hdr_td->items[i].buffer[size1 + 1] = ' ';
593  memcpy(hdr_td->items[i].buffer + size1 + 2, htp_header_value_ptr(h), size2);
594  hdr_td->items[i].len = size;
595  }
596  hdr_td->len = no_of_headers;
597  }
598 
599  // cbdata->local_id is the index of the requested header buffer
600  // hdr_td->len is the number of header buffers
601  if (local_id < hdr_td->len) {
602  // we have one valid header buffer
604  buffer, transforms, hdr_td->items[local_id].buffer, hdr_td->items[local_id].len);
605  buffer->flags = DETECT_CI_FLAGS_SINGLE;
606  SCReturnPtr(buffer, "InspectionBuffer");
607  } // else there are no more header buffer to get
609  return NULL;
610 }
611 
612 static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
613 {
614  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
615  return -1;
616 
618  return -1;
619 
620  return 0;
621 }
622 
624 {
625  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
627  "sticky buffer to match on only one HTTP header name and value";
628  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
629  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
632 
634  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
637 
638  DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
639  g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
640  DetectBufferTypeSupportsMultiInstance("http_request_header");
641  g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header",
642  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
643 }
644 
645 static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
646 {
647  if (DetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
648  return -1;
649 
651  return -1;
652 
653  return 0;
654 }
655 
657 {
658  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
660  "sticky buffer to match on only one HTTP header name and value";
661  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
662  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
665 
667  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
670 
671  DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
672  g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
673  DetectBufferTypeSupportsMultiInstance("http_response_header");
674  g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header",
675  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
676 }
677 
678 /************************************Unittests*********************************/
679 
680 #ifdef UNITTESTS
682 #endif
683 
684 /**
685  * @}
686  */
htp_header_value_len
#define htp_header_value_len(h)
Definition: app-layer-htp-libhtp.h:131
DetectEngineAppInspectionEngine_
Definition: detect.h:431
SigTableElmt_::url
const char * url
Definition: detect.h:1314
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1765
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:1519
SigTableElmt_::desc
const char * desc
Definition: detect.h:1313
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:2157
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1517
SigTableElmt_::name
const char * name
Definition: detect.h:1311
InspectionBuffer::initialized
bool initialized
Definition: detect.h:379
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1467
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:623
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:137
Flow_::proto
uint8_t proto
Definition: flow.h:376
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1359
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:1070
InspectionBuffer
Definition: detect.h:375
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1207
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@84::@86 app_v2
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1305
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:846
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1046
htp_tx_response_headers
#define htp_tx_response_headers(tx)
Definition: app-layer-htp-libhtp.h:114
detect-http-header.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@79 v2
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:690
HttpMultiBufItem::len
size_t len
Definition: detect-http-header.c:466
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:1296
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:703
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:1503
htp_headers_size
#define htp_headers_size(headers)
Definition: app-layer-htp-libhtp.h:136
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
htp_headers_t
#define htp_headers_t
Definition: app-layer-htp-libhtp.h:140
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:2207
util-memcmp.h
DetectHttpResponseHeaderRegister
void DetectHttpResponseHeaderRegister(void)
Definition: detect-http-header.c:656
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:113
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:1570
HttpMultiBufHeaderThreadData::len
size_t len
Definition: detect-http-header.c:475
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1101
HttpMultiBufHeaderThreadData::items
HttpMultiBufItem * items
Definition: detect-http-header.c:471
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:216
DETECT_HTTP_REQUEST_HEADER
@ DETECT_HTTP_REQUEST_HEADER
Definition: detect-engine-register.h:214
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
HttpMultiBufHeaderThreadData
Definition: detect-http-header.c:469
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:1309
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:215
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:132
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:70
HttpMultiBufHeaderThreadData::cap
size_t cap
Definition: detect-http-header.c:473
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:464
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:1206
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:128
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:1374
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:222
PrefilterMpmHttpHeaderCtx
Definition: detect-http-header.c:221
DetectHttpHeaderRegister
void DetectHttpHeaderRegister(void)
Registers the keyword handlers for the "http_header" keyword.
Definition: detect-http-header.c:404
suricata-common.h
DETECT_HTTP_HEADER
@ DETECT_HTTP_HEADER
Definition: detect-engine-register.h:162
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1714
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:1583
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:210
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:224
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:223
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:3784
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:1598
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DETECT_HTTP_HEADER_CM
@ DETECT_HTTP_HEADER_CM
Definition: detect-engine-register.h:161
detect-parse.h
Signature_
Signature container.
Definition: detect.h:606
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:1523
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:1495
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:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
htp_header_name_ptr
#define htp_header_name_ptr(h)
Definition: app-layer-htp-libhtp.h:129
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:215
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
DetectHttpHeaderRegisterTests
void DetectHttpHeaderRegisterTests(void)
Definition: detect-http-header.c:4557
HttpMultiBufItem::buffer
uint8_t * buffer
Definition: detect-http-header.c:465
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:3740
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1303
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:692