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
50static int g_ftp_dynport_buffer_id = 0;
51
52static DetectU16Data *DetectFtpDynamicPortParse(const char *rawstr)
53{
54 return SCDetectU16Parse(rawstr);
55}
56
57static void DetectFtpDynamicPortFree(DetectEngineCtx *de_ctx, void *ptr)
58{
59 SCDetectU16Free(ptr);
60}
61
62static 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
82static 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
115
116 g_ftp_dynport_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
117
118 SCLogDebug("registering " BUFFER_NAME " rule option");
119}
@ ALPROTO_FTP
uint8_t flags
Definition decode-gre.h:0
@ DETECT_FTP_DYNPORT
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
DetectUintData_u16 DetectU16Data
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.
void DetectFtpDynamicPortRegister(void)
#define KEYWORD_DOC
#define BUFFER_DESC
#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
DetectEngineCtx * de_ctx
struct Thresholds 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 SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267