]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: add email.to keyword 12836/head
authorAlice Akaki <akakialice@gmail.com>
Wed, 26 Mar 2025 08:36:29 +0000 (04:36 -0400)
committerVictor Julien <victor@inliniac.net>
Wed, 26 Mar 2025 13:28:22 +0000 (14:28 +0100)
email.to matches on MIME EMAIL TO
This keyword maps to the EVE field email.to[]
It is a sticky buffer
Supports prefiltering

Ticket: #7596

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

index d2ecc4c385abe2db743638b3108b39f34bab098b..0971f53e47dc99dca36139403b05f85fff3f5dfb 100644 (file)
@@ -50,3 +50,27 @@ Example of a signature that would alert if a packet contains the MIME field ``su
 .. container:: example-rule
 
   alert smtp any any -> any any (msg:"Test mime email subject"; :example-rule-emphasis:`email.subject; content:"This is a test email";` sid:1;)
+
+email.to
+--------
+
+Matches the MIME ``To`` field of an email.
+
+Comparison is case-sensitive.
+
+Syntax::
+
+ email.to; content:"<content to match against>";
+
+``email.to`` is a 'sticky buffer' and can be used as a ``fast_pattern``.
+
+This keyword maps to the EVE field ``email.to``
+
+Example
+^^^^^^^
+
+Example of a signature that would alert if a packet contains the MIME field ``to`` with the value ``172.16.92.2@linuxbox``
+
+.. container:: example-rule
+
+  alert smtp any any -> any any (msg:"Test mime email to"; :example-rule-emphasis:`email.to; content:"172.16.92.2@linuxbox";` sid:1;)
index 131a25f06bbc9885034746f7281c5b4fd716a5b4..74b151142c3597ade2c04e069404a6314eed79eb 100644 (file)
@@ -24,6 +24,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 DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
 {
@@ -99,6 +100,42 @@ static InspectionBuffer *GetMimeEmailSubjectData(DetectEngineThreadCtx *det_ctx,
     return buffer;
 }
 
+static int DetectMimeEmailToSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_mime_email_to_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static InspectionBuffer *GetMimeEmailToData(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_to = NULL;
+        uint32_t b_email_to_len = 0;
+
+        if ((tx->mime_state != NULL)) {
+            if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_to, &b_email_to_len, "to") != 1)
+                return NULL;
+        }
+
+        if (b_email_to == NULL || b_email_to_len == 0)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b_email_to, b_email_to_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+    return buffer;
+}
+
 void DetectEmailRegister(void)
 {
     SCSigTableElmt kw = { 0 };
@@ -124,4 +161,15 @@ void DetectEmailRegister(void)
             "MIME EMAIL SUBJECT", ALPROTO_SMTP, false,
             true, // to server
             GetMimeEmailSubjectData);
+
+    kw.name = "email.to";
+    kw.desc = "'To' field from an email";
+    kw.url = "/rules/email-keywords.html#email.to";
+    kw.Setup = (int (*)(void *, void *, const char *))DetectMimeEmailToSetup;
+    kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
+    DetectHelperKeywordRegister(&kw);
+    g_mime_email_to_buffer_id =
+            DetectHelperBufferMpmRegister("email.to", "MIME EMAIL TO", ALPROTO_SMTP, false,
+                    true, // to server
+                    GetMimeEmailToData);
 }