]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/ftp: ftp.command_data keyword
authorJeff Lucovsky <jlucovsky@oisf.net>
Fri, 28 Mar 2025 15:01:15 +0000 (11:01 -0400)
committerJeff Lucovsky <jlucovsky@oisf.net>
Fri, 4 Apr 2025 13:19:01 +0000 (09:19 -0400)
Issue: 7503

Add the rule keyword ftp.command_data; a sticky buffer that matches on
the values send with FTP commands, e.g., USER someuser

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

index 8746f25b8bcc7d4c992cbf80bcd8f83f5956e3f6..3591e110188d403243eea759e4bcc4fcc8410f7b 100755 (executable)
@@ -170,6 +170,7 @@ noinst_HEADERS = \
        detect-ftpbounce.h \
        detect-ftpdata.h \
        detect-ftp-command.h \
+       detect-ftp-command-data.h \
        detect-geoip.h \
        detect-gid.h \
        detect.h \
@@ -754,6 +755,7 @@ libsuricata_c_a_SOURCES = \
        detect-ftpbounce.c \
        detect-ftpdata.c \
        detect-ftp-command.c \
+       detect-ftp-command-data.c \
        detect-geoip.c \
        detect-gid.c \
        detect-hostbits.c \
index a42cbdd2e2868423ac555f40b2a6f6e477cc0e0d..6c1880a7b40b9e5a686752f1446b074cadd4f3e8 100644 (file)
 #include "detect-ja4-hash.h"
 #include "detect-ftp-command.h"
 #include "detect-entropy.h"
+#include "detect-ftp-command-data.h"
 
 #include "detect-bypass.h"
 #include "detect-ftpdata.h"
@@ -712,6 +713,7 @@ void SigTableSetup(void)
     DetectQuicCyuStringRegister();
     DetectJa4HashRegister();
     DetectFtpCommandRegister();
+    DetectFtpCommandDataRegister();
 
     DetectBypassRegister();
     DetectConfigRegister();
index 5ae44b186f18f869cf44464e37302354d8cbb2b8..64355e04e48770e2457fcbe9ff1a418a62902e20 100644 (file)
@@ -333,6 +333,7 @@ enum DetectKeywordId {
     DETECT_JA4_HASH,
 
     DETECT_FTP_COMMAND,
+    DETECT_FTP_COMMAND_DATA,
 
     DETECT_VLAN_ID,
     DETECT_VLAN_LAYERS,
diff --git a/src/detect-ftp-command-data.c b/src/detect-ftp-command-data.c
new file mode 100644 (file)
index 0000000..4bed3d7
--- /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.command_data sticky buffer
+ *
+ */
+
+#include "suricata-common.h"
+#include "detect.h"
+
+#include "detect-parse.h"
+#include "detect-engine.h"
+#include "detect-engine-mpm.h"
+#include "detect-engine-prefilter.h"
+#include "detect-engine-helper.h"
+#include "detect-content.h"
+
+#include "flow.h"
+
+#include "util-debug.h"
+
+#include "app-layer.h"
+#include "app-layer-ftp.h"
+
+#include "detect-ftp-command-data.h"
+
+#define KEYWORD_NAME "ftp.command_data"
+#define KEYWORD_DOC  "ftp-keywords.html#ftp-command_data"
+#define BUFFER_NAME  "ftp.command_data"
+#define BUFFER_DESC  "ftp command_data"
+
+static int g_ftp_cmd_data_buffer_id = 0;
+
+static int DetectFtpCommandDataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
+{
+    if (DetectBufferSetActiveList(de_ctx, s, g_ftp_cmd_data_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_FTP) < 0)
+        return -1;
+
+    return 0;
+}
+
+static bool DetectFTPCommandDataGetData(
+        void *txv, const uint8_t _flow_flags, const uint8_t **buffer, uint32_t *buffer_len)
+{
+    FTPTransaction *tx = (FTPTransaction *)txv;
+
+    if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
+        return false;
+
+    const char *b;
+    uint8_t b_len;
+    if (SCGetFtpCommandInfo(tx->command_descriptor.command_index, &b, NULL, &b_len)) {
+        if ((tx->request_length - b_len - 1) > 0) {
+            // command data starts here: advance past command + 1 space
+            *buffer = tx->request + b_len + 1;
+            *buffer_len = tx->request_length - b_len - 1;
+            SCLogDebug("command data: \"%s\" [bytes %d]", *buffer, *buffer_len);
+            return true;
+        }
+    }
+
+    *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)
+{
+    return DetectHelperGetData(
+            det_ctx, transforms, _f, _flow_flags, txv, list_id, DetectFTPCommandDataGetData);
+}
+
+void DetectFtpCommandDataRegister(void)
+{
+    /* ftp.command sticky buffer */
+    sigmatch_table[DETECT_FTP_COMMAND_DATA].name = KEYWORD_NAME;
+    sigmatch_table[DETECT_FTP_COMMAND_DATA].desc =
+            "sticky buffer to match on the FTP command data buffer";
+    sigmatch_table[DETECT_FTP_COMMAND_DATA].url = "/rules/" KEYWORD_DOC;
+    sigmatch_table[DETECT_FTP_COMMAND_DATA].Setup = DetectFtpCommandDataSetup;
+    sigmatch_table[DETECT_FTP_COMMAND_DATA].flags |= SIGMATCH_NOOPT;
+
+    DetectHelperBufferMpmRegister(
+            BUFFER_NAME, BUFFER_NAME, ALPROTO_FTP, false, true, GetDataWrapper);
+
+    DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC);
+
+    g_ftp_cmd_data_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
+
+    SCLogDebug("registering " BUFFER_NAME " rule option");
+}
diff --git a/src/detect-ftp-command-data.h b/src/detect-ftp-command-data.h
new file mode 100644 (file)
index 0000000..d28b5a9
--- /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_COMMAND_DATA_H
+#define SURICATA_DETECT_FTP_COMMAND_DATA_H
+
+void DetectFtpCommandDataRegister(void);
+
+#endif /* SURICATA_DETECT_FTP_COMMAND_DATA_H */