From: Alice Akaki Date: Wed, 26 Mar 2025 08:36:29 +0000 (-0400) Subject: detect: add email.to keyword X-Git-Tag: suricata-8.0.0-beta1~238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F12836%2Fhead;p=thirdparty%2Fsuricata.git detect: add email.to keyword 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 --- diff --git a/doc/userguide/rules/email-keywords.rst b/doc/userguide/rules/email-keywords.rst index d2ecc4c385..0971f53e47 100644 --- a/doc/userguide/rules/email-keywords.rst +++ b/doc/userguide/rules/email-keywords.rst @@ -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:""; + +``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;) diff --git a/src/detect-email.c b/src/detect-email.c index 131a25f06b..74b151142c 100644 --- a/src/detect-email.c +++ b/src/detect-email.c @@ -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); }