suricata
detect-ftp-mode.c
Go to the documentation of this file.
1/* Copyright (C) 2025 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 *
20 * \author Jeff Lucovsky <jlucovsky@oisf.net>
21 *
22 * Implements the ftp.mode sticky buffer
23 *
24 */
25
26#include "suricata-common.h"
27
28#include "detect.h"
29#include "detect-parse.h"
30#include "detect-engine.h"
31
32#include "rust.h"
33#include "flow.h"
34
35#include "util-debug.h"
36
37#include "app-layer.h"
38#include "app-layer-ftp.h"
39
40#include "detect-ftp-mode.h"
41
42#define KEYWORD_NAME "ftp.mode"
43#define KEYWORD_DOC "ftp-keywords.html#ftp-mode"
44#define BUFFER_NAME "ftp.mode"
45#define BUFFER_DESC "ftp mode"
46
47static int g_ftp_mode_buffer_id = 0;
48
49/**
50 * \brief This function is used to check matches from the FTP App Layer Parser
51 *
52 * \param t pointer to thread vars
53 * \param det_ctx pointer to the pattern matcher thread
54 * \param p pointer to the current packet
55 * \param m pointer to the sigmatch
56 * \retval 0 no match
57 * \retval 1 match
58 */
59static int DetectFtpModeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, void *state,
60 void *txv, const Signature *s, const SigMatchCtx *m)
61{
62 FTPTransaction *tx = (FTPTransaction *)txv;
63 if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN) {
64 return 0;
65 }
66 if (!tx->dyn_port) {
67 return 0;
68 }
69
70 const DetectFtpModeData *ftpmoded = (const DetectFtpModeData *)m;
71 return ftpmoded->active == tx->active;
72}
73
74/**
75 * \brief this function will free memory associated with DetectFtpModeData
76 *
77 * \param ptr pointer to DetectFtpModeData
78 */
79static void DetectFtpModeFree(DetectEngineCtx *de_ctx, void *ptr)
80{
81 SCFTPFreeModeData(ptr);
82}
83
84/**
85 * \brief This function is used to parse ftp.mode options passed via ftp.mode keyword
86 *
87 * \param str Pointer to the user provided ftp.mode options
88 *
89 * \retval pointer to DetectFtpModeData on success
90 * \retval NULL on failure
91 */
92static DetectFtpModeData *DetectFtpModeParse(const char *optstr)
93{
94 DetectFtpModeData *ftpmoded = SCFTPParseMode(optstr);
95 if (unlikely(ftpmoded == NULL)) {
96 SCLogError("Invalid command value");
97 return NULL;
98 }
99
100 return ftpmoded;
101}
102
103static int DetectFtpModeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
104{
106 return -1;
107
108 DetectFtpModeData *ftpmoded = DetectFtpModeParse(str);
109 if (ftpmoded == NULL)
110 return -1;
111
113 g_ftp_mode_buffer_id) == NULL) {
114 DetectFtpModeFree(de_ctx, ftpmoded);
115 return -1;
116 }
117
118 return 0;
119}
120
122{
123 /* ftp.mode sticky buffer */
125 sigmatch_table[DETECT_FTP_MODE].desc = "sticky buffer to match on the FTP mode buffer";
127 sigmatch_table[DETECT_FTP_MODE].Setup = DetectFtpModeSetup;
128 sigmatch_table[DETECT_FTP_MODE].AppLayerTxMatch = DetectFtpModeMatch;
129 sigmatch_table[DETECT_FTP_MODE].Free = DetectFtpModeFree;
130
133
136
138
139 g_ftp_mode_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
140
141 SCLogDebug("registering " BUFFER_NAME " rule option");
142}
@ ALPROTO_FTP
uint8_t flags
Definition decode-gre.h:0
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
#define KEYWORD_DOC
#define BUFFER_DESC
void DetectFtpModeRegister(void)
#define BUFFER_NAME
#define KEYWORD_NAME
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIG_FLAG_TOSERVER
Definition detect.h:271
SCMutex m
Definition flow-hash.h:6
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
FtpCommandInfo command_descriptor
Flow data structure.
Definition flow.h:356
FtpRequestCommand command_code
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition detect.h:1424
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define unlikely(expr)