suricata
detect-ftp-command.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.command 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-content.h"
34 
35 #include "flow.h"
36 
37 #include "util-debug.h"
38 
39 #include "app-layer.h"
40 #include "app-layer-ftp.h"
41 
42 #include "detect-ftp-command.h"
43 
44 #define KEYWORD_NAME "ftp.command"
45 #define KEYWORD_DOC "ftp-keywords.html#ftp-command"
46 #define BUFFER_NAME "ftp.command"
47 #define BUFFER_DESC "ftp command"
48 
49 static int g_ftp_cmd_buffer_id = 0;
50 
51 static int DetectFtpCommandSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
52 {
53  if (DetectBufferSetActiveList(de_ctx, s, g_ftp_cmd_buffer_id) < 0)
54  return -1;
55 
57  return -1;
58 
59  return 0;
60 }
61 
62 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
63  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
64  const int list_id)
65 {
66  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
67  if (buffer->inspect == NULL) {
68  FTPTransaction *tx = (FTPTransaction *)txv;
69 
70  if (tx->command_descriptor->command_name == NULL ||
72  return NULL;
73 
74  InspectionBufferSetup(det_ctx, list_id, buffer,
75  (const uint8_t *)tx->command_descriptor->command_name,
77  InspectionBufferApplyTransforms(buffer, transforms);
78  }
79 
80  return buffer;
81 }
82 
84 {
85  /* ftp.command sticky buffer */
87  sigmatch_table[DETECT_FTP_COMMAND].desc = "sticky buffer to match on the FTP command buffer";
89  sigmatch_table[DETECT_FTP_COMMAND].Setup = DetectFtpCommandSetup;
91 
94 
96  GetData, ALPROTO_FTP, 1);
97 
99 
100  g_ftp_cmd_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
101 
102  SCLogDebug("registering " BUFFER_NAME " rule option");
103 }
FtpCommand_::command_length
const uint8_t command_length
Definition: app-layer-ftp.h:95
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
FTPTransaction_::command_descriptor
const FtpCommand * command_descriptor
Definition: app-layer-ftp.h:130
detect-content.h
detect-engine.h
DetectFtpCommandRegister
void DetectFtpCommandRegister(void)
Definition: detect-ftp-command.c:83
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const 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:2157
SigTableElmt_::name
const char * name
Definition: detect.h:1319
DetectEngineTransforms
Definition: detect.h:410
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1359
InspectionBuffer
Definition: detect.h:375
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-command.c:47
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
FtpCommand_::command_name
const char * command_name
Definition: app-layer-ftp.h:93
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-command.c:45
app-layer-ftp.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1503
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
detect-engine-mpm.h
detect.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-command.c:44
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1714
detect-ftp-command.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1598
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
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:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
flow.h
FTPTransaction_
Definition: app-layer-ftp.h:118
DETECT_FTP_COMMAND
@ DETECT_FTP_COMMAND
Definition: detect-engine-register.h:336
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-command.c:46
app-layer.h