]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: add email.date keyword
authorAlice Akaki <akakialice@gmail.com>
Fri, 28 Mar 2025 20:06:01 +0000 (16:06 -0400)
committerVictor Julien <victor@inliniac.net>
Sat, 29 Mar 2025 21:13:59 +0000 (22:13 +0100)
email.date matches on MIME EMAIL DATE
This keyword maps to the EVE field email.date
It is a sticky buffer
Supports prefiltering

Ticket: #7591

doc/userguide/rules/email-keywords.rst
src/detect-email.c

index c5e76840b88065897d8a9fde10a63f9329426497..b6d3a7a2088399ba5087c0a5fbe84270b4c1179f 100644 (file)
@@ -98,3 +98,27 @@ Example of a signature that would alert if a packet contains the MIME field ``cc
 .. container:: example-rule
 
   alert smtp any any -> any any (msg:"Test mime email cc"; :example-rule-emphasis:`email.cc; content:"Emily <emily.roberts@example.com>, Ava <ava.johnson@example.com>, Sophia Wilson <sophia.wilson@example.com>";` sid:1;)
+
+email.date
+----------
+
+Matches the MIME ``Date`` field of an email.
+
+Comparison is case-sensitive.
+
+Syntax::
+
+ email.date; content:"<content to match against>";
+
+``email.date`` is a 'sticky buffer' and can be used as a ``fast_pattern``.
+
+This keyword maps to the EVE field ``email.date``
+
+Example
+^^^^^^^
+
+Example of a signature that would alert if a packet contains the MIME field ``date`` with the value ``Fri, 21 Apr 2023 05:10:36 +0000``
+
+.. container:: example-rule
+
+  alert smtp any any -> any any (msg:"Test mime email date"; :example-rule-emphasis:`email.date; content:"Fri, 21 Apr 2023 05:10:36 +0000";` sid:1;)
index 543689c0058786d3961583ccb19bc0388b85eb8c..770f3f167918339c3ad588fa252e138951a32e78 100644 (file)
@@ -26,6 +26,7 @@ static int g_mime_email_from_buffer_id = 0;
 static int g_mime_email_subject_buffer_id = 0;
 static int g_mime_email_to_buffer_id = 0;
 static int g_mime_email_cc_buffer_id = 0;
+static int g_mime_email_date_buffer_id = 0;
 
 static int DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
 {
@@ -166,6 +167,40 @@ static InspectionBuffer *GetMimeEmailCcData(DetectEngineThreadCtx *det_ctx,
     return buffer;
 }
 
+static int DetectMimeEmailDateSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_mime_email_date_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static InspectionBuffer *GetMimeEmailDateData(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_date = NULL;
+        uint32_t b_email_date_len = 0;
+
+        if (tx->mime_state == NULL)
+            return NULL;
+
+        if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_date, &b_email_date_len, "date") != 1)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b_email_date, b_email_date_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+    return buffer;
+}
+
 void DetectEmailRegister(void)
 {
     SCSigTableElmt kw = { 0 };
@@ -213,4 +248,15 @@ void DetectEmailRegister(void)
             DetectHelperBufferMpmRegister("email.cc", "MIME EMAIL CC", ALPROTO_SMTP, false,
                     true, // to server
                     GetMimeEmailCcData);
+
+    kw.name = "email.date";
+    kw.desc = "'Date' field from an email";
+    kw.url = "/rules/email-keywords.html#email.date";
+    kw.Setup = (int (*)(void *, void *, const char *))DetectMimeEmailDateSetup;
+    kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
+    DetectHelperKeywordRegister(&kw);
+    g_mime_email_date_buffer_id =
+            DetectHelperBufferMpmRegister("email.date", "MIME EMAIL DATE", ALPROTO_SMTP, false,
+                    true, // to server
+                    GetMimeEmailDateData);
 }