summaryrefslogtreecommitdiffstats
path: root/src/detect-transform-casechange.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
commita0aa2307322cd47bbf416810ac0292925e03be87 (patch)
tree37076262a026c4b48c8a0e84f44ff9187556ca35 /src/detect-transform-casechange.c
parentInitial commit. (diff)
downloadsuricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz
suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/detect-transform-casechange.c')
-rw-r--r--src/detect-transform-casechange.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/detect-transform-casechange.c b/src/detect-transform-casechange.c
new file mode 100644
index 0000000..8510308
--- /dev/null
+++ b/src/detect-transform-casechange.c
@@ -0,0 +1,169 @@
+/* Copyright (C) 2023 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Jeff Lucovsky <jlucovsky@oisf.net>
+ *
+ * Implements case changing transforms
+ */
+
+#include "suricata-common.h"
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-parse.h"
+#include "detect-transform-casechange.h"
+
+/**
+ * \internal
+ * \brief Register the to_lowercase transform
+ * \param det_ctx detection engine ctx
+ * \param s signature
+ * \param optstr options string
+ * \retval 0 ok
+ * \retval -1 failure
+ */
+static int DetectTransformToLowerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
+{
+ SCEnter();
+
+ int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_TOLOWER, NULL);
+
+ SCReturnInt(r);
+}
+
+/**
+ * \internal
+ * \brief Apply the to_lowercase keyword to the last pattern match
+ * \param buffer Inspection buffer
+ * \param optstr options string
+ */
+static void DetectTransformToLower(InspectionBuffer *buffer, void *options)
+{
+ const uint8_t *input = buffer->inspect;
+ const uint32_t input_len = buffer->inspect_len;
+
+ if (input_len == 0) {
+ return;
+ }
+
+ uint8_t output[input_len];
+ for (uint32_t i = 0; i < input_len; i++) {
+ output[i] = u8_tolower(input[i]);
+ }
+
+ InspectionBufferCopy(buffer, output, input_len);
+}
+/**
+ * \internal
+ * \brief Register the to_upperrcase transform
+ * \param det_ctx detection engine ctx
+ * \param s signature
+ * \param optstr options string
+ * \retval 0 ok
+ * \retval -1 failure
+ */
+static int DetectTransformToUpperSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
+{
+ SCEnter();
+
+ int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_TOUPPER, NULL);
+
+ SCReturnInt(r);
+}
+
+/**
+ * \internal
+ * \brief Apply the to_uppercase keyword to the last pattern match
+ * \param buffer Inspection buffer
+ * \param optstr options string
+ */
+static void DetectTransformToUpper(InspectionBuffer *buffer, void *options)
+{
+ const uint8_t *input = buffer->inspect;
+ const uint32_t input_len = buffer->inspect_len;
+
+ if (input_len == 0) {
+ return;
+ }
+
+ uint8_t output[input_len];
+ for (uint32_t i = 0; i < input_len; i++) {
+ output[i] = u8_toupper(input[i]);
+ }
+
+ InspectionBufferCopy(buffer, output, input_len);
+}
+
+/*
+ * \internal
+ * \brief Check if content is compatible with transform
+ *
+ * If the content contains any lowercase characters, than it is not compatible.
+ */
+static bool TransformToUpperValidate(const uint8_t *content, uint16_t content_len, void *options)
+{
+ if (content) {
+ for (uint32_t i = 0; i < content_len; i++) {
+ if (islower(*content++)) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+/*
+ * \internal
+ * \brief Check if content is compatible with transform
+ *
+ * If the content contains any uppercase characters, than it is not compatible.
+ */
+static bool TransformToLowerValidate(const uint8_t *content, uint16_t content_len, void *options)
+{
+ if (content) {
+ for (uint32_t i = 0; i < content_len; i++) {
+ if (isupper(*content++)) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+void DetectTransformToUpperRegister(void)
+{
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].name = "to_uppercase";
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].desc = "convert buffer to uppercase";
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].url = "/rules/transforms.html#to_uppercase";
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].Transform = DetectTransformToUpper;
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].TransformValidate = TransformToUpperValidate;
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].Setup = DetectTransformToUpperSetup;
+ sigmatch_table[DETECT_TRANSFORM_TOUPPER].flags |= SIGMATCH_NOOPT;
+}
+
+void DetectTransformToLowerRegister(void)
+{
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].name = "to_lowercase";
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].desc = "convert buffer to lowercase";
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].url = "/rules/transforms.html#to_lowercase";
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].Transform = DetectTransformToLower;
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].TransformValidate = TransformToLowerValidate;
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].Setup = DetectTransformToLowerSetup;
+ sigmatch_table[DETECT_TRANSFORM_TOLOWER].flags |= SIGMATCH_NOOPT;
+}