]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
domain: add new transformation
authorEric Leblond <el@stamus-networks.com>
Tue, 23 Aug 2022 14:44:23 +0000 (16:44 +0200)
committerJason Ish <jason.ish@oisf.net>
Mon, 7 Apr 2025 21:25:04 +0000 (15:25 -0600)
Ticket: #5639

rust/Cargo.lock.in
rust/Cargo.toml.in
rust/src/domain.rs [new file with mode: 0644]
rust/src/lib.rs
src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-transform-domain.c [new file with mode: 0644]
src/detect-transform-domain.h [new file with mode: 0644]

index 7e20af651934b1ecb849f54305c73febc16779bb..437867cc2685faddb340c7d434c78b751dca2ab2 100644 (file)
@@ -1153,6 +1153,21 @@ dependencies = [
  "unicode-ident",
 ]
 
+[[package]]
+name = "psl"
+version = "2.1.98"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1496f247a7781d739a03e54e2d4829931d44c9733c96b971e07f2d24cfb59221"
+dependencies = [
+ "psl-types",
+]
+
+[[package]]
+name = "psl-types"
+version = "2.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "33cb294fe86a74cbcf50d4445b37da762029549ebeea341421c7c70370f86cac"
+
 [[package]]
 name = "quote"
 version = "1.0.38"
@@ -1541,6 +1556,7 @@ dependencies = [
  "num",
  "num-derive",
  "num-traits 0.2.19",
+ "psl",
  "regex",
  "sawp",
  "sawp-modbus",
index c8d52f8a0b56d912f2afe24e864b100380fd1c13..79603374a440743858ab1abd2b328bfbfbe37082 100644 (file)
@@ -77,6 +77,7 @@ bendy = { version = "~0.3.3", default-features = false }
 asn1-rs = { version = "~0.6.1" }
 ldap-parser = { version = "~0.4.1" }
 hex = "~0.4.3"
+psl = "2"
 
 time = "~0.3.36"
 
diff --git a/rust/src/domain.rs b/rust/src/domain.rs
new file mode 100644 (file)
index 0000000..f55f070
--- /dev/null
@@ -0,0 +1,33 @@
+/* Copyright (C) 2022 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.
+ */
+
+use std::ptr;
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_get_domain(input: *const u8, len: u32, output: *mut u8, olen: *mut u64) -> bool {
+    let slice: &[u8] = std::slice::from_raw_parts(input as *mut u8, len as usize);
+    let result = psl::domain(slice);
+    match result {
+        Some(x) => {
+            let domain = x.as_bytes();
+            ptr::copy(domain.as_ptr(), output, domain.len());
+            *olen = domain.len() as u64;
+            true
+        },
+        None    => false
+    }
+}
index ff4b099ea7423badd0c64de0ff6072b74e089df9..ba2cdb9c8e5e1fa9cf15f80380da7bf94184c5f9 100644 (file)
@@ -137,6 +137,7 @@ pub mod sdp;
 pub mod ldap;
 pub mod flow;
 pub mod direction;
+pub mod domain;
 
 #[allow(unused_imports)]
 pub use suricata_lua_sys;
index 2a98ebbeb3984d4e48ac19946c2d65ba5e0b374b..e436335629e12b154826776c1553ea1466611f51 100755 (executable)
@@ -311,6 +311,7 @@ noinst_HEADERS = \
        detect-tls-random.h \
        detect-tos.h \
        detect-transform-base64.h \
+       detect-transform-domain.h \
        detect-transform-pcrexform.h \
        detect-ttl.h \
        detect-udphdr.h \
@@ -897,6 +898,7 @@ libsuricata_c_a_SOURCES = \
        detect-tls-random.c \
        detect-tos.c \
        detect-transform-base64.c \
+       detect-transform-domain.c\
        detect-transform-pcrexform.c \
        detect-ttl.c \
        detect-udphdr.c \
index 51ea64403b5ba54d273243c280142cb2e9bc0691..2ec99aad85f80b3aafe98de2c4da90257b66f8d9 100644 (file)
 
 #include "detect-transform-pcrexform.h"
 #include "detect-transform-base64.h"
+#include "detect-transform-domain.h"
 
 #include "util-rule-vars.h"
 
@@ -736,6 +737,7 @@ void SigTableSetup(void)
     DetectTransformToUpperRegister();
     DetectTransformHeaderLowercaseRegister();
     DetectTransformFromBase64DecodeRegister();
+    DetectTransformDomainRegister();
 
     DetectFileHandlerRegister();
 
index dd7fefb58b79eba28bb8f0afb0c0ffc5e2429c63..97e2485728995bd66c2936e21f56ae0ad1b2374c 100644 (file)
@@ -318,6 +318,7 @@ enum DetectKeywordId {
     DETECT_TRANSFORM_TOUPPER,
     DETECT_TRANSFORM_HEADER_LOWERCASE,
     DETECT_TRANSFORM_FROM_BASE64,
+    DETECT_TRANSFORM_DOMAIN,
 
     DETECT_IKE_EXCH_TYPE,
     DETECT_IKE_SPI_INITIATOR,
diff --git a/src/detect-transform-domain.c b/src/detect-transform-domain.c
new file mode 100644 (file)
index 0000000..a66773b
--- /dev/null
@@ -0,0 +1,161 @@
+/* Copyright (C) 2022 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 Eric Leblond <el@stamus-networks.com>
+ *
+ * Implements the domain extraction transformation
+ */
+
+#include "suricata-common.h"
+
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-engine-prefilter.h"
+#include "detect-parse.h"
+#include "detect-transform-domain.h"
+#include "detect-engine-build.h"
+
+#include "util-unittest.h"
+#include "util-print.h"
+#include "util-memrchr.h"
+#include "util-memcpy.h"
+#include "rust.h"
+
+static int DetectTransformDomainSetup(DetectEngineCtx *, Signature *, const char *);
+#ifdef UNITTESTS
+static void DetectTransformDomainRegisterTests(void);
+#endif
+static void TransformDomain(DetectEngineThreadCtx *ctx, InspectionBuffer *buffer, void *options);
+
+void DetectTransformDomainRegister(void)
+{
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].name = "domain";
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].desc = "modify buffer to extract the domain";
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].url = "/rules/transforms.html#domain";
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].Transform = TransformDomain;
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].Setup = DetectTransformDomainSetup;
+#ifdef UNITTESTS
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].RegisterTests = DetectTransformDomainRegisterTests;
+#endif
+    sigmatch_table[DETECT_TRANSFORM_DOMAIN].flags |= SIGMATCH_NOOPT;
+}
+
+/**
+ *  \internal
+ *  \brief Extract the dotprefix, if any, the last pattern match, either content or uricontent
+ *  \param det_ctx detection engine ctx
+ *  \param s signature
+ *  \param nullstr should be null
+ *  \retval 0 ok
+ *  \retval -1 failure
+ */
+static int DetectTransformDomainSetup(DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
+{
+    SCEnter();
+    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_DOMAIN, NULL);
+    SCReturnInt(r);
+}
+
+/**
+ * \brief Return the domain, if any, in the last pattern match.
+ *
+ */
+static void TransformDomain(DetectEngineThreadCtx *ctx, InspectionBuffer *buffer, void *options)
+{
+    const size_t input_len = buffer->inspect_len;
+    uint64_t output_len = 0;
+
+    if (input_len) {
+        uint8_t output[input_len];
+
+        bool res = rs_get_domain(buffer->inspect, input_len, output, &output_len);
+        if (res == true) {
+            InspectionBufferCopy(buffer, output, output_len);
+        }
+    }
+}
+
+#ifdef UNITTESTS
+static int DetectTransformDomainTest01(void)
+{
+    const uint8_t *input = (const uint8_t *)"www.example.com";
+    uint32_t input_len = strlen((char *)input);
+
+    const char *result = "example.com";
+    uint32_t result_len = strlen((char *)result);
+
+    InspectionBuffer buffer;
+    InspectionBufferInit(&buffer, input_len);
+    InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    TransformDomain(NULL, &buffer, NULL);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    FAIL_IF_NOT(buffer.inspect_len == result_len);
+    FAIL_IF_NOT(strncmp(result, (const char *)buffer.inspect, result_len) == 0);
+    InspectionBufferFree(&buffer);
+    PASS;
+}
+
+static int DetectTransformDomainTest02(void)
+{
+    const uint8_t *input = (const uint8_t *)"hello.example.co.uk";
+    uint32_t input_len = strlen((char *)input);
+
+    const char *result = "example.co.uk";
+    uint32_t result_len = strlen((char *)result);
+
+    InspectionBuffer buffer;
+    InspectionBufferInit(&buffer, input_len);
+    InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    TransformDomain(NULL, &buffer, NULL);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    FAIL_IF_NOT(buffer.inspect_len == result_len);
+    FAIL_IF_NOT(strncmp(result, (const char *)buffer.inspect, result_len) == 0);
+    InspectionBufferFree(&buffer);
+    PASS;
+}
+
+static int DetectTransformDomainTest03(void)
+{
+    const char rule[] =
+            "alert dns any any -> any any (dns.query; domain; content:\"google.com\"; sid:1;)";
+    ThreadVars th_v;
+    DetectEngineThreadCtx *det_ctx = NULL;
+    memset(&th_v, 0, sizeof(th_v));
+
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
+    Signature *s = DetectEngineAppendSig(de_ctx, rule);
+    FAIL_IF_NULL(s);
+    SigGroupBuild(de_ctx);
+    DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
+    DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
+    DetectEngineCtxFree(de_ctx);
+    PASS;
+}
+
+static void DetectTransformDomainRegisterTests(void)
+{
+    UtRegisterTest("DetectTransformDomainTest01", DetectTransformDomainTest01);
+    UtRegisterTest("DetectTransformDomainTest02", DetectTransformDomainTest02);
+    UtRegisterTest("DetectTransformDomainTest03", DetectTransformDomainTest03);
+}
+#endif
diff --git a/src/detect-transform-domain.h b/src/detect-transform-domain.h
new file mode 100644 (file)
index 0000000..4b2d7ea
--- /dev/null
@@ -0,0 +1,30 @@
+/* Copyright (C) 2022 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 Eric Leblond <el@stamus-networks.com>
+ */
+
+#ifndef __DETECT_TRANSFORM_DOMAIN_H__
+#define __DETECT_TRANSFORM_DOMAIN_H__
+
+/* prototypes */
+void DetectTransformDomainRegister(void);
+
+#endif /* __DETECT_TRANSFORM_DOMAIN_H__ */