/src/suricata7/src/detect-transform-header-lowercase.c
Line | Count | Source |
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" |
30 | | #include "detect-transform-header-lowercase.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 | 127 | { |
44 | 127 | SCEnter(); |
45 | 127 | int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_HEADER_LOWERCASE, NULL); |
46 | 127 | SCReturnInt(r); |
47 | 127 | } |
48 | | |
49 | | static void DetectTransformHeaderLowercase(InspectionBuffer *buffer, void *options) |
50 | 0 | { |
51 | 0 | const uint8_t *input = buffer->inspect; |
52 | 0 | const uint32_t input_len = buffer->inspect_len; |
53 | 0 | if (input_len == 0) { |
54 | 0 | return; |
55 | 0 | } |
56 | 0 | uint8_t *output = InspectionBufferCheckAndExpand(buffer, input_len); |
57 | 0 | if (output == NULL) { |
58 | 0 | return; |
59 | 0 | } |
60 | | |
61 | | // state 0 is header name, 1 is header value |
62 | 0 | int state = 0; |
63 | 0 | for (uint32_t i = 0; i < input_len; i++) { |
64 | 0 | if (state == 0) { |
65 | 0 | if (input[i] == ':') { |
66 | 0 | output[i] = input[i]; |
67 | 0 | state = 1; |
68 | 0 | } else { |
69 | 0 | output[i] = u8_tolower(input[i]); |
70 | 0 | } |
71 | 0 | } else { |
72 | 0 | output[i] = input[i]; |
73 | 0 | if (input[i] == '\n') { |
74 | 0 | state = 0; |
75 | 0 | } |
76 | 0 | } |
77 | 0 | } |
78 | 0 | InspectionBufferTruncate(buffer, input_len); |
79 | 0 | } |
80 | | |
81 | | void DetectTransformHeaderLowercaseRegister(void) |
82 | 34 | { |
83 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].name = "header_lowercase"; |
84 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].desc = |
85 | 34 | "modify buffer via lowercaseing header names"; |
86 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].url = |
87 | 34 | "/rules/transforms.html#header_lowercase"; |
88 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Transform = DetectTransformHeaderLowercase; |
89 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Setup = DetectTransformHeaderLowercaseSetup; |
90 | 34 | sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].flags |= SIGMATCH_NOOPT; |
91 | 34 | } |