You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/detect-email.c

416 lines
14 KiB
C

/* Copyright (C) 2025 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "detect-engine.h"
#include "detect-engine-buffer.h"
#include "detect-engine-helper.h"
#include "detect-parse.h"
#include "app-layer-smtp.h"
#include "detect-email.h"
#include "rust.h"
#include "detect-engine-content-inspection.h"
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 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 g_mime_email_url_buffer_id = 0;
static int g_mime_email_received_buffer_id = 0;
static int DetectMimeEmailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_from_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static InspectionBuffer *GetMimeEmailFromData(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_from = NULL;
uint32_t b_email_from_len = 0;
if (tx->mime_state == NULL)
return NULL;
if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_from, &b_email_from_len, "from") != 1)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, b_email_from, b_email_from_len);
InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailSubjectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_subject_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static InspectionBuffer *GetMimeEmailSubjectData(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_sub = NULL;
uint32_t b_email_sub_len = 0;
if (tx->mime_state == NULL)
return NULL;
if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_sub, &b_email_sub_len, "subject") !=
1)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, b_email_sub, b_email_sub_len);
InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailToSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(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(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailCcSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_cc_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static InspectionBuffer *GetMimeEmailCcData(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_cc = NULL;
uint32_t b_email_cc_len = 0;
if (tx->mime_state == NULL)
return NULL;
if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_cc, &b_email_cc_len, "cc") != 1)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, b_email_cc, b_email_cc_len);
InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailDateSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_date_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static InspectionBuffer *GetMimeEmailDateData(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_date = NULL;
uint32_t b_email_date_len = 0;
if (tx->mime_state == NULL)
return NULL;
if (SCDetectMimeEmailGetData(tx->mime_state, &b_email_date, &b_email_date_len, "date") != 1)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, b_email_date, b_email_date_len);
InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailMessageIdSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(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(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailXMailerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(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(det_ctx, buffer, transforms);
}
return buffer;
}
static int DetectMimeEmailUrlSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_url_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static bool GetMimeEmailUrlData(DetectEngineThreadCtx *det_ctx, const void *txv,
const uint8_t flags, uint32_t idx, const uint8_t **buf, uint32_t *buf_len)
{
SMTPTransaction *tx = (SMTPTransaction *)txv;
if (tx->mime_state == NULL) {
return false;
}
if (SCDetectMimeEmailGetUrl(tx->mime_state, buf, buf_len, idx) != 1) {
return false;
}
return true;
}
static int DetectMimeEmailReceivedSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_mime_email_received_buffer_id) < 0)
return -1;
if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;
return 0;
}
static bool GetMimeEmailReceivedData(DetectEngineThreadCtx *det_ctx, const void *txv,
const uint8_t flags, uint32_t idx, const uint8_t **buf, uint32_t *buf_len)
{
SMTPTransaction *tx = (SMTPTransaction *)txv;
if (tx->mime_state == NULL) {
return false;
}
if (SCDetectMimeEmailGetDataArray(tx->mime_state, buf, buf_len, "received", idx) != 1) {
return false;
}
return true;
}
void DetectEmailRegister(void)
{
SCSigTableAppLiteElmt kw = { 0 };
kw.name = "email.from";
kw.desc = "'From' field from an email";
kw.url = "/rules/email-keywords.html#email.from";
kw.Setup = DetectMimeEmailFromSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_from_buffer_id = DetectHelperBufferMpmRegister(
"email.from", "MIME EMAIL FROM", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailFromData);
kw.name = "email.subject";
kw.desc = "'Subject' field from an email";
kw.url = "/rules/email-keywords.html#email.subject";
kw.Setup = DetectMimeEmailSubjectSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_subject_buffer_id = DetectHelperBufferMpmRegister("email.subject",
"MIME EMAIL SUBJECT", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailSubjectData);
kw.name = "email.to";
kw.desc = "'To' field from an email";
kw.url = "/rules/email-keywords.html#email.to";
kw.Setup = DetectMimeEmailToSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_to_buffer_id = DetectHelperBufferMpmRegister(
"email.to", "MIME EMAIL TO", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailToData);
kw.name = "email.cc";
kw.desc = "'Cc' field from an email";
kw.url = "/rules/email-keywords.html#email.cc";
kw.Setup = DetectMimeEmailCcSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_cc_buffer_id = DetectHelperBufferMpmRegister(
"email.cc", "MIME EMAIL CC", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailCcData);
kw.name = "email.date";
kw.desc = "'Date' field from an email";
kw.url = "/rules/email-keywords.html#email.date";
kw.Setup = DetectMimeEmailDateSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_date_buffer_id = DetectHelperBufferMpmRegister(
"email.date", "MIME EMAIL DATE", ALPROTO_SMTP, STREAM_TOSERVER, 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 = DetectMimeEmailMessageIdSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_message_id_buffer_id = DetectHelperBufferMpmRegister("email.message_id",
"MIME EMAIL Message-Id", ALPROTO_SMTP, STREAM_TOSERVER, 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 = DetectMimeEmailXMailerSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_x_mailer_buffer_id = DetectHelperBufferMpmRegister("email.x_mailer",
"MIME EMAIL X-Mailer", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailXMailerData);
kw.name = "email.url";
kw.desc = "'Url' extracted from an email";
kw.url = "/rules/email-keywords.html#email.url";
kw.Setup = DetectMimeEmailUrlSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_url_buffer_id = SCDetectHelperMultiBufferMpmRegister(
"email.url", "MIME EMAIL URL", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailUrlData);
kw.name = "email.received";
kw.desc = "'Received' field from an email";
kw.url = "/rules/email-keywords.html#email.received";
kw.Setup = DetectMimeEmailReceivedSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
SCDetectHelperKeywordRegister(&kw);
g_mime_email_received_buffer_id = SCDetectHelperMultiBufferMpmRegister("email.received",
"MIME EMAIL RECEIVED", ALPROTO_SMTP, STREAM_TOSERVER, GetMimeEmailReceivedData);
}