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 
47 static 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  */
59 static 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  */
79 static 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  */
92 static 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 
103 static 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 }
SigTableElmt_::url
const char * url
Definition: detect.h:1422
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-mode.c:44
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-mode.c:42
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1421
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1409
SigTableElmt_::name
const char * name
Definition: detect.h:1419
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:270
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:919
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1387
rust.h
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-mode.c:43
m
SCMutex m
Definition: flow-hash.h:6
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2212
DETECT_FTP_MODE
@ DETECT_FTP_MODE
Definition: detect-engine-register.h:335
app-layer-ftp.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:272
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1404
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1256
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:271
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1211
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:388
detect.h
FTPTransaction_::command_descriptor
FtpCommandInfo command_descriptor
Definition: app-layer-ftp.h:72
FtpCommandInfo_::command_code
FtpRequestCommand command_code
Definition: app-layer-ftp.h:57
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:352
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DetectEngineInspectGenericList
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.
Definition: detect-engine.c:1933
str
#define str(s)
Definition: suricata-common.h:308
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:262
detect-parse.h
Signature_
Signature container.
Definition: detect.h:657
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-mode.c:45
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:74
FTPTransaction_::active
bool active
Definition: app-layer-ftp.h:76
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:249
DetectFtpModeRegister
void DetectFtpModeRegister(void)
Definition: detect-ftp-mode.c:121
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1353
flow.h
FTPTransaction_
Definition: app-layer-ftp.h:60
app-layer.h
detect-ftp-mode.h