diff --git a/doc/userguide/rules/email-keywords.rst b/doc/userguide/rules/email-keywords.rst index b6d3a7a208..fe2a615f4f 100644 --- a/doc/userguide/rules/email-keywords.rst +++ b/doc/userguide/rules/email-keywords.rst @@ -122,3 +122,27 @@ Example of a signature that would alert if a packet contains the MIME field ``da .. 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;) + +email.message_id +---------------- + +Matches the MIME ``Message-Id`` field of an email. + +Comparison is case-sensitive. + +Syntax:: + + email.message_id; content:""; + +``email.message_id`` is a 'sticky buffer' and can be used as a ``fast_pattern``. + +This keyword maps to the EVE field ``email.message_id`` + +Example +^^^^^^^ + +Example of a signature that would alert if a packet contains the MIME field ``message id`` with the value ```` + +.. container:: example-rule + + alert smtp any any -> any any (msg:"Test mime email message id"; :example-rule-emphasis:`email.message_id; content:"";` sid:1;) diff --git a/src/detect-email.c b/src/detect-email.c index 770f3f1679..7e30abf782 100644 --- a/src/detect-email.c +++ b/src/detect-email.c @@ -27,6 +27,7 @@ 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 g_mime_email_message_id_buffer_id = 0; static int DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) { @@ -201,6 +202,41 @@ static InspectionBuffer *GetMimeEmailDateData(DetectEngineThreadCtx *det_ctx, return buffer; } +static int DetectMimeEmailMessageIdSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) +{ + if (DetectBufferSetActiveList(de_ctx, s, g_mime_email_message_id_buffer_id) < 0) + return -1; + + if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0) + return -1; + + return 0; +} + +static InspectionBuffer *GetMimeEmailMessageIdData(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_msg_id = NULL; + uint32_t b_email_msg_id_len = 0; + + if (tx->mime_state == NULL) + return NULL; + + if (SCDetectMimeEmailGetData( + tx->mime_state, &b_email_msg_id, &b_email_msg_id_len, "message-id") != 1) + return NULL; + + InspectionBufferSetup(det_ctx, list_id, buffer, b_email_msg_id, b_email_msg_id_len); + InspectionBufferApplyTransforms(buffer, transforms); + } + return buffer; +} + void DetectEmailRegister(void) { SCSigTableElmt kw = { 0 }; @@ -259,4 +295,15 @@ void DetectEmailRegister(void) DetectHelperBufferMpmRegister("email.date", "MIME EMAIL DATE", ALPROTO_SMTP, false, true, // to server GetMimeEmailDateData); + + kw.name = "email.message_id"; + kw.desc = "'Message-Id' field from an email"; + kw.url = "/rules/email-keywords.html#email.message_id"; + kw.Setup = (int (*)(void *, void *, const char *))DetectMimeEmailMessageIdSetup; + kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; + DetectHelperKeywordRegister(&kw); + g_mime_email_message_id_buffer_id = DetectHelperBufferMpmRegister("email.message_id", + "MIME EMAIL Message-Id", ALPROTO_SMTP, false, + true, // to server + GetMimeEmailMessageIdData); }