]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/smtp: smtp.helo keyword
authorPhilippe Antoine <pantoine@oisf.net>
Fri, 24 Jan 2025 12:17:38 +0000 (13:17 +0100)
committerVictor Julien <victor@inliniac.net>
Sun, 26 Jan 2025 06:09:59 +0000 (07:09 +0100)
Ticket: 7515

It is a sticky buffer mapping to the smtp.helo log field

doc/userguide/rules/smtp-keywords.rst
src/Makefile.am
src/detect-engine-register.c
src/detect-smtp.c [new file with mode: 0644]
src/detect-smtp.h [new file with mode: 0644]

index 227a3a25fa301fe9b9560447ecca55ab9586b8bf..bd5e9488ed6f5e650b7f1dc11110dc5659e007f0 100644 (file)
@@ -18,6 +18,29 @@ 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.
+This keyword matches per transaction, so it can match more than once per flow,
+even if the helo occured only once at the beginning of the flow.
+
+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``
+
+
 Frames
 ------
 
index 72d0fe6276d017288a50202a38d44fcbb0a834cf..869cc1a4c66777fe6de5ba88a9a7b70dee547d95 100755 (executable)
@@ -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 \
@@ -833,6 +834,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 \
index 146618c690d1f85ee48c5a9d518a9c5513613585..b796e7fb72c2896653e001c833d806b03f24acea 100644 (file)
@@ -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"
@@ -730,6 +731,7 @@ void SigTableSetup(void)
     DetectVlanIdRegister();
     DetectVlanLayersRegister();
 
+    SCDetectSMTPRegister();
     ScDetectSNMPRegister();
     SCDetectDHCPRegister();
     ScDetectWebsocketRegister();
diff --git a/src/detect-smtp.c b/src/detect-smtp.c
new file mode 100644 (file)
index 0000000..3c8e5bc
--- /dev/null
@@ -0,0 +1,76 @@
+/* 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-helper.h"
+#include "detect-parse.h"
+#include "app-layer-smtp.h"
+#include "rust.h"
+
+static int g_smtp_helo_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;
+}
+
+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);
+}
diff --git a/src/detect-smtp.h b/src/detect-smtp.h
new file mode 100644 (file)
index 0000000..937b68d
--- /dev/null
@@ -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 */