suricata
detect-transform-header-lowercase.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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  * \file
20  *
21  * \author Philippe Antoine <contact@catenacyber.fr>
22  *
23  * Implements the header_lowercase transform keyword with option support
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "detect-engine.h"
29 #include "detect-parse.h"
31 
32 /**
33  * \internal
34  * \brief Apply the header_lowercase keyword to the last pattern match
35  * \param det_ctx detection engine ctx
36  * \param s signature
37  * \param optstr options string
38  * \retval 0 ok
39  * \retval -1 failure
40  */
41 static int DetectTransformHeaderLowercaseSetup(
42  DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
43 {
44  SCEnter();
46  SCReturnInt(r);
47 }
48 
49 static void DetectTransformHeaderLowercase(InspectionBuffer *buffer, void *options)
50 {
51  const uint8_t *input = buffer->inspect;
52  const uint32_t input_len = buffer->inspect_len;
53  if (input_len == 0) {
54  return;
55  }
56  uint8_t output[input_len];
57 
58  // state 0 is header name, 1 is header value
59  int state = 0;
60  for (uint32_t i = 0; i < input_len; i++) {
61  if (state == 0) {
62  if (input[i] == ':') {
63  output[i] = input[i];
64  state = 1;
65  } else {
66  output[i] = u8_tolower(input[i]);
67  }
68  } else {
69  output[i] = input[i];
70  if (input[i] == '\n') {
71  state = 0;
72  }
73  }
74  }
75  InspectionBufferCopy(buffer, output, input_len);
76 }
77 
79 {
82  "modify buffer via lowercaseing header names";
84  "/rules/transforms.html#header_lowercase";
85  sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Transform = DetectTransformHeaderLowercase;
86  sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Setup = DetectTransformHeaderLowercaseSetup;
88 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
SigTableElmt_::name
const char * name
Definition: detect.h:1296
DETECT_TRANSFORM_HEADER_LOWERCASE
@ DETECT_TRANSFORM_HEADER_LOWERCASE
Definition: detect-engine-register.h:343
InspectionBuffer
Definition: detect.h:374
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
u8_tolower
#define u8_tolower(c)
Definition: suricata-common.h:436
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1622
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1277
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:377
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1728
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
detect-transform-header-lowercase.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DetectTransformHeaderLowercaseRegister
void DetectTransformHeaderLowercaseRegister(void)
Definition: detect-transform-header-lowercase.c:78