From: Alice Akaki Date: Tue, 1 Apr 2025 22:35:57 +0000 (-0400) Subject: detect: add email.x_mailer keyword X-Git-Tag: suricata-8.0.0-beta1~80 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=341369f20340ef74495726a84720373547d096c8;p=thirdparty%2Fsuricata.git detect: add email.x_mailer keyword 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 --- diff --git a/doc/userguide/rules/email-keywords.rst b/doc/userguide/rules/email-keywords.rst index fe2a615f4f..8c9ec97477 100644 --- a/doc/userguide/rules/email-keywords.rst +++ b/doc/userguide/rules/email-keywords.rst @@ -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:"";` sid:1;) + +email.x_mailer +-------------- + +Matches the MIME ``X-Mailer`` field of an email. + +Comparison is case-sensitive. + +Syntax:: + + email.x_mailer; content:""; + +``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;) diff --git a/src/detect-email.c b/src/detect-email.c index 7e30abf782..9640df309b 100644 --- a/src/detect-email.c +++ b/src/detect-email.c @@ -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); }