]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/ftp: Add ftp.reply rule keyword
authorJeff Lucovsky <jlucovsky@oisf.net>
Thu, 27 Mar 2025 14:30:47 +0000 (10:30 -0400)
committerJason Ish <jason.ish@oisf.net>
Mon, 7 Apr 2025 21:25:04 +0000 (15:25 -0600)
Issue: 7508

Add the rule detection keyword "ftp.reply". This is a multi-buffer
command.

src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-ftp-reply.c [new file with mode: 0644]
src/detect-ftp-reply.h [new file with mode: 0644]

index e436335629e12b154826776c1553ea1466611f51..dadd0cf4057f999283d149538d61c6e33b5cf370 100755 (executable)
@@ -172,6 +172,7 @@ noinst_HEADERS = \
        detect-ftpdata.h \
        detect-ftp-command.h \
        detect-ftp-command-data.h \
+       detect-ftp-reply.h \
        detect-geoip.h \
        detect-gid.h \
        detect.h \
@@ -761,6 +762,7 @@ libsuricata_c_a_SOURCES = \
        detect-ftpdata.c \
        detect-ftp-command.c \
        detect-ftp-command-data.c \
+       detect-ftp-reply.c \
        detect-geoip.c \
        detect-gid.c \
        detect-hostbits.c \
index 2ec99aad85f80b3aafe98de2c4da90257b66f8d9..97ef5075d3938ca75e6f1d2d725d8d5a282f8393 100644 (file)
 #include "detect-ftp-command.h"
 #include "detect-entropy.h"
 #include "detect-ftp-command-data.h"
+#include "detect-ftp-reply.h"
 
 #include "detect-bypass.h"
 #include "detect-ftpdata.h"
@@ -719,6 +720,7 @@ void SigTableSetup(void)
     DetectJa4HashRegister();
     DetectFtpCommandRegister();
     DetectFtpCommandDataRegister();
+    DetectFtpReplyRegister();
 
     DetectBypassRegister();
     DetectConfigRegister();
index 08246a4f19a44c99e9ec53cc07ada56d353af630..dbadd4d20631c525ca186953affa9bdc71a4fc15 100644 (file)
@@ -335,6 +335,7 @@ enum DetectKeywordId {
 
     DETECT_FTP_COMMAND,
     DETECT_FTP_COMMAND_DATA,
+    DETECT_FTP_REPLY,
 
     DETECT_VLAN_ID,
     DETECT_VLAN_LAYERS,
diff --git a/src/detect-ftp-reply.c b/src/detect-ftp-reply.c
new file mode 100644 (file)
index 0000000..f90e7d5
--- /dev/null
@@ -0,0 +1,114 @@
+/* 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.
+ */
+
+/**
+ *
+ * \author Jeff Lucovsky <jlucovsky@oisf.net>
+ *
+ * Implements the ftp.reply sticky buffer
+ *
+ */
+
+#include "suricata-common.h"
+
+#include "detect.h"
+#include "detect-parse.h"
+#include "detect-engine.h"
+#include "detect-engine-helper.h"
+#include "detect-engine-content-inspection.h"
+#include "detect-ftp-reply.h"
+
+#include "app-layer.h"
+#include "app-layer-ftp.h"
+
+#include "flow.h"
+
+#include "util-debug.h"
+
+#include "rust.h"
+
+#define KEYWORD_NAME "ftp.reply"
+#define KEYWORD_DOC  "ftp-keywords.html#ftp-reply"
+#define BUFFER_NAME  "ftp.reply"
+#define BUFFER_DESC  "ftp reply"
+
+static int g_ftp_reply_buffer_id = 0;
+
+static int DetectFtpReplySetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_ftp_reply_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_FTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static bool DetectFTPReplyGetData(void *txv, uint8_t _flow_flags, uint32_t index,
+        const uint8_t **buffer, uint32_t *buffer_len)
+{
+    FTPTransaction *tx = (FTPTransaction *)txv;
+
+    if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
+        return false;
+
+    if (!TAILQ_EMPTY(&tx->response_list)) {
+        uint32_t count = 0;
+        FTPResponseWrapper *wrapper;
+        TAILQ_FOREACH (wrapper, &tx->response_list, next) {
+            DEBUG_VALIDATE_BUG_ON(wrapper->response == NULL);
+            if (index == count) {
+                *buffer = (const uint8_t *)wrapper->response->response;
+                *buffer_len = wrapper->response->length;
+                return true;
+            }
+            count++;
+        }
+    }
+
+    *buffer = NULL;
+    *buffer_len = 0;
+    return false;
+}
+
+static InspectionBuffer *GetDataWrapper(DetectEngineThreadCtx *det_ctx,
+        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
+        const int list_id, uint32_t index)
+{
+    return DetectHelperGetMultiData(
+            det_ctx, transforms, _f, _flow_flags, txv, list_id, index, DetectFTPReplyGetData);
+}
+
+void DetectFtpReplyRegister(void)
+{
+    /* ftp.reply sticky buffer */
+    sigmatch_table[DETECT_FTP_REPLY].name = KEYWORD_NAME;
+    sigmatch_table[DETECT_FTP_REPLY].desc = "sticky buffer to match on the FTP reply buffer";
+    sigmatch_table[DETECT_FTP_REPLY].url = "/rules/" KEYWORD_DOC;
+    sigmatch_table[DETECT_FTP_REPLY].Setup = DetectFtpReplySetup;
+    sigmatch_table[DETECT_FTP_REPLY].flags |= SIGMATCH_NOOPT;
+
+    DetectAppLayerMultiRegister(
+            BUFFER_NAME, ALPROTO_FTP, SIG_FLAG_TOCLIENT, 0, GetDataWrapper, 2, 1);
+
+    DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC);
+
+    g_ftp_reply_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
+
+    SCLogDebug("registering " BUFFER_NAME " rule option");
+}
diff --git a/src/detect-ftp-reply.h b/src/detect-ftp-reply.h
new file mode 100644 (file)
index 0000000..e8efb6d
--- /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 Jeff Lucovsky  <jlucovsky@oisf.net>
+ */
+
+#ifndef SURICATA_DETECT_FTP_REPLY_H
+#define SURICATA_DETECT_FTP_REPLY_H
+
+void DetectFtpReplyRegister(void);
+
+#endif /* SURICATA_DETECT_FTP_REPLY_H */