suricata
detect-ftp-dynamic-port.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.dynamic_port sticky buffer
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
33 #include "detect-engine-uint.h"
34 #include "detect-content.h"
35 
36 #include "flow.h"
37 
38 #include "util-debug.h"
39 
40 #include "app-layer.h"
41 #include "app-layer-ftp.h"
42 
44 
45 #define KEYWORD_NAME "ftp.dynamic_port"
46 #define KEYWORD_DOC "ftp-keywords.html#ftp-dynamic_port"
47 #define BUFFER_NAME "ftp.dynamic_port"
48 #define BUFFER_DESC "ftp dynamic_port"
49 
50 static int g_ftp_dynport_buffer_id = 0;
51 
52 static DetectU16Data *DetectFtpDynamicPortParse(const char *rawstr)
53 {
54  return SCDetectU16Parse(rawstr);
55 }
56 
57 static void DetectFtpDynamicPortFree(DetectEngineCtx *de_ctx, void *ptr)
58 {
59  SCDetectU16Free(ptr);
60 }
61 
62 static int DetectFtpDynamicPortSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
63 {
65  return -1;
66 
67  DetectU16Data *fdp = DetectFtpDynamicPortParse(str);
68  if (fdp == NULL) {
69  SCLogError("parsing dynamic port from \"%s\" failed", str);
70  return -1;
71  }
72 
73  SCLogDebug("low %u hi %u", fdp->arg1, fdp->arg2);
75  g_ftp_dynport_buffer_id) == NULL) {
76  DetectFtpDynamicPortFree(de_ctx, fdp);
77  return -1;
78  }
79  return 0;
80 }
81 
82 static int DetectFtpDynamicPortMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
83  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
84 {
85  SCEnter();
86 
87  FTPTransaction *tx = (FTPTransaction *)txv;
88  if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
89  return 0;
90 
91  const DetectU16Data *ftpd = (const DetectU16Data *)ctx;
92 
93  SCLogDebug("Checking for match between rule value(s) %u, %u with actual value %d", ftpd->arg1,
94  ftpd->arg2, tx->dyn_port);
95  return DetectU16Match(tx->dyn_port, ftpd);
96 }
97 
99 {
100  /* ftp.dynamic_port sticky buffer */
102  sigmatch_table[DETECT_FTP_DYNPORT].desc = "match on the FTP dynamic_port buffer";
104  sigmatch_table[DETECT_FTP_DYNPORT].Setup = DetectFtpDynamicPortSetup;
105  sigmatch_table[DETECT_FTP_DYNPORT].Free = DetectFtpDynamicPortFree;
106  sigmatch_table[DETECT_FTP_DYNPORT].AppLayerTxMatch = DetectFtpDynamicPortMatch;
107 
110 
113 
114  g_ftp_dynport_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
115 
116  SCLogDebug("registering " BUFFER_NAME " rule option");
117 }
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-dynamic-port.c:46
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
detect-content.h
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1418
SigTableElmt_::name
const char * name
Definition: detect.h:1428
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_
Flow data structure.
Definition: flow.h:356
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-dynamic-port.c:45
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1399
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
app-layer-ftp.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
detect-ftp-dynamic-port.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
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:351
DetectU16Match
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
Definition: detect-engine-uint.c:107
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DetectFtpDynamicPortRegister
void DetectFtpDynamicPortRegister(void)
Definition: detect-ftp-dynamic-port.c:98
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:2060
str
#define str(s)
Definition: suricata-common.h:308
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:74
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:245
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:464
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-dynamic-port.c:47
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
flow.h
FTPTransaction_
Definition: app-layer-ftp.h:60
DETECT_FTP_DYNPORT
@ DETECT_FTP_DYNPORT
Definition: detect-engine-register.h:107
app-layer.h