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  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  goto end;
193  }
194  /* setup buffer and apply transforms */
195  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
196  InspectionBufferApplyTransforms(buffer, transforms);
197  }
198 
199  const uint32_t data_len = buffer->inspect_len;
200  const uint8_t *data = buffer->inspect;
201  const uint64_t offset = buffer->inspect_offset;
202 
203  /* Inspect all the uricontents fetched on each
204  * transaction at the app layer */
205  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
206  (uint8_t *)data, data_len, offset, DETECT_CI_FLAGS_SINGLE,
208  if (match) {
210  }
211 end:
212  if (flags & STREAM_TOSERVER) {
213  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, txv, flags) >
214  HTP_REQUEST_HEADERS)
216  } else {
217  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, txv, flags) >
218  HTP_RESPONSE_HEADERS)
220  }
222 }
223 
225  int list_id;
226  const MpmCtx *mpm_ctx;
229 
230 /** \brief Generic Mpm prefilter callback
231  *
232  * \param det_ctx detection engine thread ctx
233  * \param p packet to inspect
234  * \param f flow to inspect
235  * \param txv tx to inspect
236  * \param pectx inspection context
237  */
238 static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
239  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
240 {
241  SCEnter();
242 
243  const PrefilterMpmHttpHeaderCtx *ctx = pectx;
244  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
245  SCLogDebug("running on list %d", ctx->list_id);
246 
247  const int list_id = ctx->list_id;
248  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
249  if (buffer->inspect == NULL) {
250  uint32_t rawdata_len = 0;
251  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flags, &rawdata_len);
252  if (rawdata_len == 0)
253  return;
254 
255  /* setup buffer and apply transforms */
256  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
257  InspectionBufferApplyTransforms(buffer, ctx->transforms);
258  }
259 
260  const uint32_t data_len = buffer->inspect_len;
261  const uint8_t *data = buffer->inspect;
262 
263  SCLogDebug("mpm'ing buffer:");
264  //PrintRawDataFp(stdout, data, data_len);
265 
266  if (data != NULL && data_len >= mpm_ctx->minlen) {
267  (void)mpm_table[mpm_ctx->mpm_type].Search(
268  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
269  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
270  }
271 }
272 
273 static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
274  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
275 {
276  SCEnter();
277 
278  htp_tx_t *tx = txv;
279  const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
280  /* if the request wasn't flagged as having a trailer, we skip */
281  if (htud && (
282  ((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
283  ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers))) {
284  SCReturn;
285  }
286  PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
287  SCReturn;
288 }
289 
290 static void PrefilterMpmHttpHeaderFree(void *ptr)
291 {
292  SCFree(ptr);
293 }
294 
295 static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
296  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
297 {
298  SCEnter();
299 
300  /* header */
301  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
302  if (pectx == NULL)
303  return -1;
304  pectx->list_id = list_id;
305  pectx->mpm_ctx = mpm_ctx;
306  pectx->transforms = &mpm_reg->transforms;
307 
308  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
309  mpm_reg->app_v2.alproto, HTP_REQUEST_HEADERS,
310  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
311  if (r != 0) {
312  SCFree(pectx);
313  return r;
314  }
315 
316  /* trailer */
317  pectx = SCCalloc(1, sizeof(*pectx));
318  if (pectx == NULL)
319  return -1;
320  pectx->list_id = list_id;
321  pectx->mpm_ctx = mpm_ctx;
322  pectx->transforms = &mpm_reg->transforms;
323 
324  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
325  mpm_reg->app_v2.alproto, HTP_REQUEST_TRAILER,
326  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
327  if (r != 0) {
328  SCFree(pectx);
329  }
330  return r;
331 }
332 
333 static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
334  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
335 {
336  SCEnter();
337 
338  /* header */
339  PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
340  if (pectx == NULL)
341  return -1;
342  pectx->list_id = list_id;
343  pectx->mpm_ctx = mpm_ctx;
344  pectx->transforms = &mpm_reg->transforms;
345 
346  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader,
347  mpm_reg->app_v2.alproto, HTP_RESPONSE_HEADERS,
348  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
349  if (r != 0) {
350  SCFree(pectx);
351  return r;
352  }
353 
354  /* trailer */
355  pectx = SCCalloc(1, sizeof(*pectx));
356  if (pectx == NULL)
357  return -1;
358  pectx->list_id = list_id;
359  pectx->mpm_ctx = mpm_ctx;
360  pectx->transforms = &mpm_reg->transforms;
361 
362  r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer,
363  mpm_reg->app_v2.alproto, HTP_RESPONSE_TRAILER,
364  pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
365  if (r != 0) {
366  SCFree(pectx);
367  }
368  return r;
369 }
370 
371 /**
372  * \brief The setup function for the http_header keyword for a signature.
373  *
374  * \param de_ctx Pointer to the detection engine context.
375  * \param s Pointer to signature for the current Signature being parsed
376  * from the rules.
377  * \param m Pointer to the head of the SigMatchs for the current rule
378  * being parsed.
379  * \param arg Pointer to the string holding the keyword value.
380  *
381  * \retval 0 On success.
382  * \retval -1 On failure.
383  */
384 static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
385 {
387  de_ctx, s, arg, DETECT_AL_HTTP_HEADER, g_http_header_buffer_id, ALPROTO_HTTP1);
388 }
389 
390 /**
391  * \brief this function setup the http.header keyword used in the rule
392  *
393  * \param de_ctx Pointer to the Detection Engine Context
394  * \param s Pointer to the Signature to which the current keyword belongs
395  * \param str Should hold an empty string always
396  *
397  * \retval 0 On success
398  */
399 static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
400 {
401  if (DetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
402  return -1;
404  return -1;
405  return 0;
406 }
407 
408 /**
409  * \brief Registers the keyword handlers for the "http_header" keyword.
410  */
412 {
413  /* http_header content modifier */
414  sigmatch_table[DETECT_AL_HTTP_HEADER].name = "http_header";
415  sigmatch_table[DETECT_AL_HTTP_HEADER].desc = "content modifier to match only on the HTTP header-buffer";
416  sigmatch_table[DETECT_AL_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
417  sigmatch_table[DETECT_AL_HTTP_HEADER].Setup = DetectHttpHeaderSetup;
418 #ifdef UNITTESTS
420 #endif
424 
425  /* http.header sticky buffer */
426  sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
427  sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
428  sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
429  sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
432 
434  HTP_REQUEST_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
436  PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
437  0); /* not used, registered twice: HEADERS/TRAILER */
438 
440  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferHttpHeader, NULL);
442  PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
443  0); /* not used, registered twice: HEADERS/TRAILER */
444 
446  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
448  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
449 
451  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
453  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
454 
456  "http headers");
457 
458  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
459 
460  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
462 }
463 
464 static int g_http_request_header_buffer_id = 0;
465 static int g_http_response_header_buffer_id = 0;
466 static int g_request_header_thread_id = 0;
467 static int g_response_header_thread_id = 0;
468 
469 typedef struct HttpMultiBufItem {
470  uint8_t *buffer;
471  size_t len;
473 
475  // array of items, being defined as a buffer with its length just above
477  // capacity of items (size of allocation)
478  size_t cap;
479  // length of items (number in use)
480  size_t len;
482 
483 static void *HttpMultiBufHeaderThreadDataInit(void *data)
484 {
485  HttpMultiBufHeaderThreadData *td = SCCalloc(1, sizeof(*td));
486 
487  /* This return value check to satisfy our Cocci malloc checks. */
488  if (td == NULL) {
489  SCLogError("failed to allocate %" PRIuMAX " bytes: %s", (uintmax_t)sizeof(*td),
490  strerror(errno));
491  return NULL;
492  }
493  return td;
494 }
495 
496 static void HttpMultiBufHeaderThreadDataFree(void *data)
497 {
498  HttpMultiBufHeaderThreadData *td = data;
499  for (size_t i = 0; i < td->cap; i++) {
500  SCFree(td->items[i].buffer);
501  }
502  SCFree(td->items);
503  SCFree(td);
504 }
505 
506 static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx,
507  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
508  int list_id, uint32_t local_id)
509 {
510  SCEnter();
511 
512  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
513  if (buffer == NULL)
514  return NULL;
515  if (buffer->initialized)
516  return buffer;
517 
518  uint32_t b_len = 0;
519  const uint8_t *b = NULL;
520 
521  if (rs_http2_tx_get_header(txv, flags, local_id, &b, &b_len) != 1) {
523  return NULL;
524  }
525  if (b == NULL || b_len == 0) {
527  return NULL;
528  }
529 
530  InspectionBufferSetupMulti(buffer, transforms, b, b_len);
531  buffer->flags = DETECT_CI_FLAGS_SINGLE;
532 
533  SCReturnPtr(buffer, "InspectionBuffer");
534 }
535 
536 static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
537  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
538  int list_id, uint32_t local_id)
539 {
540  SCEnter();
541  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
542  if (buffer == NULL)
543  return NULL;
544  if (buffer->initialized)
545  return buffer;
546 
547  int kw_thread_id;
548  if (flags & STREAM_TOSERVER) {
549  kw_thread_id = g_request_header_thread_id;
550  } else {
551  kw_thread_id = g_response_header_thread_id;
552  }
554  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id);
555  if (unlikely(hdr_td == NULL)) {
556  return NULL;
557  }
558 
559  htp_tx_t *tx = (htp_tx_t *)txv;
560  htp_table_t *headers;
561  if (flags & STREAM_TOSERVER) {
562  headers = tx->request_headers;
563  } else {
564  headers = tx->response_headers;
565  }
566  size_t no_of_headers = htp_table_size(headers);
567  if (local_id == 0) {
568  // We initialize a big buffer on first item
569  // Then, we will just use parts of it
570  hdr_td->len = 0;
571  if (hdr_td->cap < no_of_headers) {
572  void *new_buffer = SCRealloc(hdr_td->items, no_of_headers * sizeof(HttpMultiBufItem));
573  if (unlikely(new_buffer == NULL)) {
574  return NULL;
575  }
576  hdr_td->items = new_buffer;
577  // zeroes the new part of the items
578  memset(hdr_td->items + hdr_td->cap, 0,
579  (no_of_headers - hdr_td->cap) * sizeof(HttpMultiBufItem));
580  hdr_td->cap = no_of_headers;
581  }
582  for (size_t i = 0; i < no_of_headers; i++) {
583  htp_header_t *h = htp_table_get_index(headers, i, NULL);
584  size_t size1 = bstr_size(h->name);
585  size_t size2 = bstr_size(h->value);
586  size_t size = size1 + size2 + 2;
587  if (hdr_td->items[i].len < size) {
588  // Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
589  void *tmp = SCRealloc(hdr_td->items[i].buffer, size);
590  if (unlikely(tmp == NULL)) {
591  return NULL;
592  }
593  hdr_td->items[i].buffer = tmp;
594  }
595  memcpy(hdr_td->items[i].buffer, bstr_ptr(h->name), size1);
596  hdr_td->items[i].buffer[size1] = ':';
597  hdr_td->items[i].buffer[size1 + 1] = ' ';
598  memcpy(hdr_td->items[i].buffer + size1 + 2, bstr_ptr(h->value), size2);
599  hdr_td->items[i].len = size;
600  }
601  hdr_td->len = no_of_headers;
602  }
603 
604  // cbdata->local_id is the index of the requested header buffer
605  // hdr_td->len is the number of header buffers
606  if (local_id < hdr_td->len) {
607  // we have one valid header buffer
609  buffer, transforms, hdr_td->items[local_id].buffer, hdr_td->items[local_id].len);
610  buffer->flags = DETECT_CI_FLAGS_SINGLE;
611  SCReturnPtr(buffer, "InspectionBuffer");
612  } // else there are no more header buffer to get
614  return NULL;
615 }
616 
617 static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
618 {
619  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
620  return -1;
621 
623  return -1;
624 
625  return 0;
626 }
627 
629 {
630  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
632  "sticky buffer to match on only one HTTP header name and value";
633  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
634  sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
637 
639  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
641  HTP_REQUEST_HEADERS, GetHttp1HeaderData, 2, HTP_REQUEST_HEADERS);
642 
643  DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
644  g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
645  DetectBufferTypeSupportsMultiInstance("http_request_header");
646  g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header",
647  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
648 }
649 
650 static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
651 {
652  if (DetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
653  return -1;
654 
656  return -1;
657 
658  return 0;
659 }
660 
662 {
663  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
665  "sticky buffer to match on only one HTTP header name and value";
666  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
667  sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
670 
672  HTTP2StateOpen, GetHttp2HeaderData, 2, HTTP2StateOpen);
674  HTP_RESPONSE_HEADERS, GetHttp1HeaderData, 2, HTP_RESPONSE_HEADERS);
675 
676  DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
677  g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
678  DetectBufferTypeSupportsMultiInstance("http_response_header");
679  g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header",
680  HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
681 }
682 
683 /************************************Unittests*********************************/
684 
685 #ifdef UNITTESTS
687 #endif
688 
689 /**
690  * @}
691  */
DetectEngineAppInspectionEngine_
Definition: detect.h:429
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
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:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
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:2154
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1510
SigTableElmt_::name
const char * name
Definition: detect.h:1304
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1460
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:628
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
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:1065
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1200
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@84::@86 app_v2
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1043
detect-http-header.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@79 v2
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:685
HttpMultiBufItem::len
size_t len
Definition: detect-http-header.c:471
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:1289
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:698
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
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:2201
util-memcmp.h
DetectHttpResponseHeaderRegister
void DetectHttpResponseHeaderRegister(void)
Definition: detect-http-header.c:661
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:435
DETECT_AL_HTTP_HEADER
@ DETECT_AL_HTTP_HEADER
Definition: detect-engine-register.h:156
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:1567
HttpMultiBufHeaderThreadData::len
size_t len
Definition: detect-http-header.c:480
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
HttpMultiBufHeaderThreadData::items
HttpMultiBufItem * items
Definition: detect-http-header.c:476
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:209
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
HttpMultiBufHeaderThreadData
Definition: detect-http-header.c:474
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:1302
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:194
detect-http-header.c
Packet_
Definition: decode.h:473
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
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:478
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:469
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:1199
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:449
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1367
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:225
PrefilterMpmHttpHeaderCtx
Definition: detect-http-header.c:224
DetectHttpHeaderRegister
void DetectHttpHeaderRegister(void)
Registers the keyword handlers for the "http_header" keyword.
Definition: detect-http-header.c:411
suricata-common.h
DETECT_HTTP_HEADER
@ DETECT_HTTP_HEADER
Definition: detect-engine-register.h:157
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
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:1580
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
util-validate.h
PrefilterMpmHttpHeaderCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-http-header.c:227
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:226
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:3765
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:1595
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:446
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:1520
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:1488
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:1190
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:210
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:717
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:470
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:3721
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:687