Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform: Add case-changing transforms #9761

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/userguide/rules/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,23 @@ Example::

alert http any any -> any any (msg:"HTTP with xor"; http.uri; \
xor:"0d0ac8ff"; content:"password="; sid:1;)

to_lowercase
------------

Converts the buffer to lowercase.

Example::

alert http any any -> any any (http.uri; to_lowercase; \
content:"|this text has been converted to lowercase|"; sid:1;)

to_uppercase
------------

Converts the buffer to uppercase.

Example::

alert http any any -> any any (http.uri; to_uppercase; \
content:"|THIS TEXT HAS BEEN CONVERTED TO UPPERCASE|"; sid:1;)
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ noinst_HEADERS = \
detect-tls-version.h \
detect-tls-random.h \
detect-tos.h \
detect-transform-casechange.h \
detect-transform-compress-whitespace.h \
detect-transform-dotprefix.h \
detect-transform-md5.h \
Expand Down Expand Up @@ -946,6 +947,7 @@ libsuricata_c_a_SOURCES = \
detect-tls-version.c \
detect-tls-random.c \
detect-tos.c \
detect-transform-casechange.c \
detect-transform-compress-whitespace.c \
detect-transform-dotprefix.c \
detect-transform-md5.c \
Expand Down
3 changes: 3 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
#include "detect-transform-pcrexform.h"
#include "detect-transform-urldecode.h"
#include "detect-transform-xor.h"
#include "detect-transform-casechange.h"

#include "util-rule-vars.h"

Expand Down Expand Up @@ -696,6 +697,8 @@ void SigTableSetup(void)
DetectTransformPcrexformRegister();
DetectTransformUrlDecodeRegister();
DetectTransformXorRegister();
DetectTransformToLowerRegister();
DetectTransformToUpperRegister();

DetectFileHandlerRegister();

Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ enum DetectKeywordId {
DETECT_TRANSFORM_PCREXFORM,
DETECT_TRANSFORM_URL_DECODE,
DETECT_TRANSFORM_XOR,
DETECT_TRANSFORM_TOLOWER,
DETECT_TRANSFORM_TOUPPER,

DETECT_AL_IKE_EXCH_TYPE,
DETECT_AL_IKE_SPI_INITIATOR,
Expand Down
118 changes: 118 additions & 0 deletions src/detect-transform-casechange.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* 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 Apply the to_lowercase keyword to the last pattern match
* \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);
}

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 Apply the to_uppercase keyword to the last pattern match
* \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);
}

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);
}

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].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].Setup = DetectTransformToLowerSetup;
sigmatch_table[DETECT_TRANSFORM_TOLOWER].flags |= SIGMATCH_NOOPT;
}
31 changes: 31 additions & 0 deletions src/detect-transform-casechange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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>
*/

#ifndef __DETECT_TRANSFORM_CASECHANGE_H
#define __DETECT_TRANSFORM_CASECHANGE_H

/* prototypes */
void DetectTransformToLowerRegister(void);
void DetectTransformToUpperRegister(void);

#endif /* __DETECT_TRANSFORM_CASECHANGE_H */
Loading