]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: add email.from
authorAlice Akaki <akakialice@gmail.com>
Sat, 15 Mar 2025 02:32:54 +0000 (22:32 -0400)
committerVictor Julien <victor@inliniac.net>
Thu, 20 Mar 2025 12:12:01 +0000 (13:12 +0100)
email.from matches on MIME EMAIL FROM
This keyword maps to the EVE field email.from
It is a sticky buffer
Supports prefiltering

Ticket: #7592

doc/userguide/rules/email-keywords.rst [new file with mode: 0644]
doc/userguide/rules/index.rst
rust/src/mime/detect.rs [new file with mode: 0644]
rust/src/mime/mod.rs
src/Makefile.am
src/detect-email.c [new file with mode: 0644]
src/detect-email.h [new file with mode: 0644]
src/detect-engine-register.c

diff --git a/doc/userguide/rules/email-keywords.rst b/doc/userguide/rules/email-keywords.rst
new file mode 100644 (file)
index 0000000..b7924b2
--- /dev/null
@@ -0,0 +1,28 @@
+Email Keywords
+==============
+
+.. role:: example-rule-emphasis
+
+email.from
+----------
+
+Matches the MIME ``From`` field of an email.
+
+Comparison is case-sensitive.
+
+Syntax::
+
+ email.from; content:"<content to match against>";
+
+``email.from`` is a 'sticky buffer' and can be used as a ``fast_pattern``.
+
+This keyword maps to the EVE field ``email.from``
+
+Example
+^^^^^^^
+
+Example of a signature that would alert if a packet contains the MIME field ``from`` with the value ``toto <toto@gmail.com>``
+
+.. container:: example-rule
+
+  alert smtp any any -> any any (msg:"Test mime email from"; :example-rule-emphasis:`email.from; content:"toto <toto@gmail.com>";` sid:1;)
index b475fba0f83f09f3796e9f0d0aaefe453b1e2002..70b801311617a358ce2132f72b65d0951e038310 100644 (file)
@@ -53,3 +53,4 @@ Suricata Rules
    vlan-keywords
    ldap-keywords
    rule-types
+   email-keywords
diff --git a/rust/src/mime/detect.rs b/rust/src/mime/detect.rs
new file mode 100644 (file)
index 0000000..802b081
--- /dev/null
@@ -0,0 +1,43 @@
+/* Copyright (C) 2025 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 super::mime;
+use super::smtp::MimeStateSMTP;
+use std::ffi::CStr;
+use std::ptr;
+
+#[no_mangle]
+pub unsafe extern "C" fn SCDetectMimeEmailGetData(
+    ctx: &MimeStateSMTP, buffer: *mut *const u8, buffer_len: *mut u32,
+    hname: *const std::os::raw::c_char,
+) -> u8 {
+    let c_str = CStr::from_ptr(hname); //unsafe
+    let str = c_str.to_str().unwrap_or("");
+
+    for h in &ctx.headers[..ctx.main_headers_nb] {
+        if mime::slice_equals_lowercase(&h.name, str.as_bytes()) {
+            *buffer = h.value.as_ptr();
+            *buffer_len = h.value.len() as u32;
+            return 1;
+        }
+    }
+
+    *buffer = ptr::null();
+    *buffer_len = 0;
+
+    return 0;
+}
index 2eac4d1bd4422e96fea53e62c80a0fb6a9a3567d..a126b79d4a2e0db901b197e4e52d6fd39ab716a7 100644 (file)
@@ -17,6 +17,7 @@
 
 //! MIME protocol parser module.
 
+pub mod detect;
 pub mod mime;
 pub mod smtp;
 pub mod smtp_log;
index 4ebd1a640781fb2c72aed93ceaca6c0f505401bb..8fd7e20172071b5a4feba3f368ae552988d8a43c 100755 (executable)
@@ -317,6 +317,7 @@ noinst_HEADERS = \
        detect-within.h \
        detect-xbits.h \
        detect-vlan.h \
+       detect-email.h \
        device-storage.h \
        feature.h \
        flow-bit.h \
@@ -891,6 +892,7 @@ libsuricata_c_a_SOURCES = \
        detect-within.c \
        detect-xbits.c \
        detect-vlan.c \
+       detect-email.c \
        device-storage.c \
        feature.c \
        flow-bit.c \
diff --git a/src/detect-email.c b/src/detect-email.c
new file mode 100644 (file)
index 0000000..23a69d6
--- /dev/null
@@ -0,0 +1,78 @@
+/* Copyright (C) 2025 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.
+ */
+
+#include "detect-engine.h"
+#include "detect-engine-helper.h"
+#include "detect-parse.h"
+#include "app-layer-smtp.h"
+#include "detect-email.h"
+#include "rust.h"
+
+static int g_mime_email_from_buffer_id = 0;
+
+static int DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_mime_email_from_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static InspectionBuffer *GetMimeEmailFromData(DetectEngineThreadCtx *det_ctx,
+        const DetectEngineTransforms *transforms, Flow *f, const uint8_t _flow_flags, void *txv,
+        const int list_id)
+{
+    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
+    if (buffer->inspect == NULL) {
+        SMTPTransaction *tx = (SMTPTransaction *)txv;
+
+        const uint8_t *b_email_from = NULL;
+        uint32_t b_email_from_len = 0;
+
+        if ((tx->mime_state != NULL)) {
+            if (SCDetectMimeEmailGetData(
+                        tx->mime_state, &b_email_from, &b_email_from_len, "from") != 1)
+                return NULL;
+        }
+
+        if (b_email_from == NULL || b_email_from_len == 0)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b_email_from, b_email_from_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+    return buffer;
+}
+
+void DetectEmailRegister(void)
+{
+    SCSigTableElmt kw = { 0 };
+
+    kw.name = "email.from";
+    kw.desc = "'From' field from an email";
+    kw.url = "/rules/email-keywords.html#email.from";
+    kw.Setup = (int (*)(void *, void *, const char *))DetectMimeEmailFromSetup;
+    kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
+    DetectHelperKeywordRegister(&kw);
+    g_mime_email_from_buffer_id =
+            DetectHelperBufferMpmRegister("email.from", "MIME EMAIL FROM", ALPROTO_SMTP, false,
+                    true, // to server
+                    GetMimeEmailFromData);
+}
diff --git a/src/detect-email.h b/src/detect-email.h
new file mode 100644 (file)
index 0000000..107c6d1
--- /dev/null
@@ -0,0 +1,23 @@
+/* Copyright (C) 2025 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.
+ */
+
+#ifndef SURICATA_DETECT_EMAIL_H
+#define SURICATA_DETECT_EMAIL_H
+
+void DetectEmailRegister(void);
+
+#endif /* SURICATA_DETECT_EMAIL_H */
index 10af70b1d0f7c53a640452a7fba4c4bdec3db854..8aa70757d66fdeed285fee2ccf77380fee0b330c 100644 (file)
 #include "detect-ike-nonce-payload.h"
 #include "detect-ike-key-exchange-payload.h"
 #include "detect-vlan.h"
+#include "detect-email.h"
 
 #include "action-globals.h"
 #include "tm-threads.h"
@@ -733,6 +734,8 @@ void SigTableSetup(void)
     DetectVlanIdRegister();
     DetectVlanLayersRegister();
 
+    DetectEmailRegister();
+
     SCDetectSMTPRegister();
     SCDetectSNMPRegister();
     SCDetectDHCPRegister();