]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: add email.x_mailer keyword
authorAlice Akaki <akakialice@gmail.com>
Tue, 1 Apr 2025 22:35:57 +0000 (18:35 -0400)
committerVictor Julien <victor@inliniac.net>
Thu, 3 Apr 2025 08:05:48 +0000 (10:05 +0200)
email.x_mailer matches on MIME EMAIL X-Mailer
This keyword maps to the EVE field email.x_mailer
It is a sticky buffer
Supports prefiltering

Ticket: #7598

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

index fe2a615f4fab21908ea7e15e9024742c2740a7cc..8c9ec97477fefde9ea95ec976f7c837c18b4c6b1 100644 (file)
@@ -146,3 +146,27 @@ Example of a signature that would alert if a packet contains the MIME field ``me
 .. container:: example-rule
 
   alert smtp any any -> any any (msg:"Test mime email message id"; :example-rule-emphasis:`email.message_id; content:"<alpine.DEB.2.00.1311261630120.9535@sd-26634.dedibox.fr>";` sid:1;)
+
+email.x_mailer
+--------------
+
+Matches the MIME ``X-Mailer`` field of an email.
+
+Comparison is case-sensitive.
+
+Syntax::
+
+ email.x_mailer; content:"<content to match against>";
+
+``email.x_mailer`` is a 'sticky buffer' and can be used as a ``fast_pattern``.
+
+This keyword maps to the EVE field ``email.x_mailer``
+
+Example
+^^^^^^^
+
+Example of a signature that would alert if a packet contains the MIME field ``x-mailer`` with the value ``Microsoft Office Outlook, Build 11.0.5510``
+
+.. container:: example-rule
+
+  alert smtp any any -> any any (msg:"Test mime email x-mailer"; :example-rule-emphasis:`email.x_mailer; content:"Microsoft Office Outlook, Build 11.0.5510";` sid:1;)
index 7e30abf7829596fd9391ebbf3cb3c8470656e803..9640df309be818adfb1b63786d9276b3b5a3f4de 100644 (file)
@@ -28,6 +28,7 @@ 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 g_mime_email_message_id_buffer_id = 0;
+static int g_mime_email_x_mailer_buffer_id = 0;
 
 static int DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
 {
@@ -237,6 +238,41 @@ static InspectionBuffer *GetMimeEmailMessageIdData(DetectEngineThreadCtx *det_ct
     return buffer;
 }
 
+static int DetectMimeEmailXMailerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_mime_email_x_mailer_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static InspectionBuffer *GetMimeEmailXMailerData(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_x_mailer = NULL;
+        uint32_t b_email_x_mailer_len = 0;
+
+        if (tx->mime_state == NULL)
+            return NULL;
+
+        if (SCDetectMimeEmailGetData(
+                    tx->mime_state, &b_email_x_mailer, &b_email_x_mailer_len, "x-mailer") != 1)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b_email_x_mailer, b_email_x_mailer_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+    return buffer;
+}
+
 void DetectEmailRegister(void)
 {
     SCSigTableElmt kw = { 0 };
@@ -306,4 +342,15 @@ void DetectEmailRegister(void)
             "MIME EMAIL Message-Id", ALPROTO_SMTP, false,
             true, // to server
             GetMimeEmailMessageIdData);
+
+    kw.name = "email.x_mailer";
+    kw.desc = "'X-Mailer' field from an email";
+    kw.url = "/rules/email-keywords.html#email.x_mailer";
+    kw.Setup = (int (*)(void *, void *, const char *))DetectMimeEmailXMailerSetup;
+    kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
+    DetectHelperKeywordRegister(&kw);
+    g_mime_email_x_mailer_buffer_id = DetectHelperBufferMpmRegister("email.x_mailer",
+            "MIME EMAIL X-Mailer", ALPROTO_SMTP, false,
+            true, // to server
+            GetMimeEmailXMailerData);
 }