Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect smtp keywords 7515 v1.1 #12468

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions doc/userguide/rules/smtp-keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,67 @@ Signature Example:

For additional information on the ``file.name`` keyword, see :doc:`file-keywords`.


smtp.helo
---------

SMTP helo is the parameter passed to the first HELO command from the client.
catenacyber marked this conversation as resolved.
Show resolved Hide resolved

Syntax::

smtp.helo; content:"localhost";

Signature example::

alert smtp any any -> any any (msg:"SMTP helo localhost"; smtp.helo; content:"localhost"; sid:2; rev:1;)

``smtp.helo`` is a 'sticky buffer'.

``smtp.helo`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.helo``

smtp.mail_from
--------------

SMTP mail from is the parameter passed to the first MAIL FROM command from the client.

Syntax::

smtp.mail_from; content:"spam";

Signature example::

alert smtp any any -> any any (msg:"SMTP mail from spam"; smtp.mail_from; content:"spam"; sid:2; rev:1;)

``smtp.mail_from`` is a 'sticky buffer'.

``smtp.mail_from`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.mail_from``

smtp.rcpt_to
------------

SMTP rcpt to is the one of the parameters passed to one RCPT TO command from the client.

Syntax::

smtp.rcpt_to; content:"sensitive@target";

Signature example::

alert smtp any any -> any any (msg:"SMTP rcpt to sensitive"; smtp.rcpt_to; content:"sensitive@target"; sid:2; rev:1;)

``smtp.rcpt_to`` is a 'sticky buffer'.

``smtp.rcpt_to`` is a 'multi buffer'.

``smtp.rcpt_to`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.rcpt_to[]``


Frames
------

Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ noinst_HEADERS = \
detect-smb-ntlmssp.h \
detect-smb-share.h \
detect-smb-version.h \
detect-smtp.h \
detect-ssh-hassh.h \
detect-ssh-hassh-server.h \
detect-ssh-hassh-server-string.h \
Expand Down Expand Up @@ -831,6 +832,7 @@ libsuricata_c_a_SOURCES = \
detect-smb-ntlmssp.c \
detect-smb-share.c \
detect-smb-version.c \
detect-smtp.c \
detect-ssh-hassh.c \
detect-ssh-hassh-server.c \
detect-ssh-hassh-server-string.c \
Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

#include "detect-smb-share.h"
#include "detect-smb-version.h"
#include "detect-smtp.h"

#include "detect-base64-decode.h"
#include "detect-base64-data.h"
Expand Down Expand Up @@ -730,6 +731,7 @@ void SigTableSetup(void)
DetectVlanIdRegister();
DetectVlanLayersRegister();

SCDetectSMTPRegister();
ScDetectSNMPRegister();
SCDetectDHCPRegister();
ScDetectWebsocketRegister();
Expand Down
172 changes: 172 additions & 0 deletions src/detect-smtp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* 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.
*/

/**
* \file
*
* \author Philippe Antoine <pantoine@oisf.net>
*
*/

#include "suricata-common.h"
#include "detect-smtp.h"
#include "detect-engine.h"
#include "detect-engine-content-inspection.h"
#include "detect-engine-helper.h"
#include "detect-parse.h"
#include "app-layer-smtp.h"
#include "rust.h"

static int g_smtp_helo_buffer_id = 0;
static int g_smtp_mail_from_buffer_id = 0;
static int g_smtp_rcpt_to_buffer_id = 0;

static int DetectSmtpHeloSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (DetectBufferSetActiveList(de_ctx, s, g_smtp_helo_buffer_id) < 0)
return -1;

if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;

return 0;
}

static InspectionBuffer *GetSmtpHeloData(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) {
SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
if (smtp_state) {
if (smtp_state->helo == NULL || smtp_state->helo_len == 0)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, smtp_state->helo, smtp_state->helo_len);
InspectionBufferApplyTransforms(buffer, transforms);
}
}
return buffer;
}

static int DetectSmtpMailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (DetectBufferSetActiveList(de_ctx, s, g_smtp_mail_from_buffer_id) < 0)
return -1;

if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;

return 0;
}

static InspectionBuffer *GetSmtpMailFromData(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;
if (tx->mail_from == NULL || tx->mail_from_len == 0)
return NULL;
InspectionBufferSetup(det_ctx, list_id, buffer, tx->mail_from, tx->mail_from_len);
InspectionBufferApplyTransforms(buffer, transforms);
}
return buffer;
}

static int DetectSmtpRcptToSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (DetectBufferSetActiveList(de_ctx, s, g_smtp_rcpt_to_buffer_id) < 0)
return -1;

if (DetectSignatureSetAppProto(s, ALPROTO_SMTP) < 0)
return -1;

return 0;
}

static InspectionBuffer *GetSmtpRcptToData(DetectEngineThreadCtx *det_ctx,
const DetectEngineTransforms *transforms, Flow *f, const uint8_t _flow_flags, void *txv,
const int list_id, uint32_t idx)
{
InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, idx);
if (buffer == NULL || buffer->initialized)
return buffer;

SMTPTransaction *tx = (SMTPTransaction *)txv;
if (TAILQ_EMPTY(&tx->rcpt_to_list)) {
InspectionBufferSetupMultiEmpty(buffer);
return NULL;
}

SMTPString *s;
if (idx == 0) {
s = TAILQ_FIRST(&tx->rcpt_to_list);
} else {
// TODO optimize ?
s = TAILQ_FIRST(&tx->rcpt_to_list);
for (uint32_t i = 0; i < idx; i++) {
s = TAILQ_NEXT(s, next);
}
}
if (s == NULL) {
InspectionBufferSetupMultiEmpty(buffer);
return NULL;
}

InspectionBufferSetupMulti(buffer, transforms, s->str, s->len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;
return buffer;
}

void SCDetectSMTPRegister(void)
{
SCSigTableElmt kw = { 0 };
kw.name = "smtp.helo";
kw.desc = "SMTP helo buffer";
kw.url = "/rules/smtp-keywords.html#smtp-helo";
kw.Setup = (int (*)(void *, void *, const char *))DetectSmtpHeloSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
DetectHelperKeywordRegister(&kw);
g_smtp_helo_buffer_id =
DetectHelperBufferMpmRegister("smtp.helo", "SMTP helo", ALPROTO_SMTP, false,
true, // to server
GetSmtpHeloData);

kw.name = "smtp.mail_from";
kw.desc = "SMTP mail from buffer";
kw.url = "/rules/smtp-keywords.html#smtp-mail-from";
kw.Setup = (int (*)(void *, void *, const char *))DetectSmtpMailFromSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
DetectHelperKeywordRegister(&kw);
g_smtp_mail_from_buffer_id =
DetectHelperBufferMpmRegister("smtp.mail_from", "SMTP MAIL FROM", ALPROTO_SMTP, false,
true, // to server
GetSmtpMailFromData);

kw.name = "smtp.rcpt_to";
kw.desc = "SMTP rcpt to buffer";
kw.url = "/rules/smtp-keywords.html#smtp-rcpt-to";
kw.Setup = (int (*)(void *, void *, const char *))DetectSmtpRcptToSetup;
kw.flags = SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
DetectHelperKeywordRegister(&kw);
g_smtp_rcpt_to_buffer_id =
DetectHelperMultiBufferMpmRegister("smtp.rcpt_to", "SMTP RCPT TO", ALPROTO_SMTP, false,
true, // to server
GetSmtpRcptToData);
}
29 changes: 29 additions & 0 deletions src/detect-smtp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* 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.
*/

/**
* \file
*
* \author Philippe Antoine <pantoine@oisf.net>
*/

#ifndef SURICATA_DETECT_SMTP_H
#define SURICATA_DETECT_SMTP_H

void SCDetectSMTPRegister(void);

#endif /* SURICATA_DETECT_SMTP_H */
Loading