suricata
detect-ftp-command-data.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_data 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-helper.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.command_data"
46 #define KEYWORD_DOC "ftp-keywords.html#ftp-command_data"
47 #define BUFFER_NAME "ftp.command_data"
48 #define BUFFER_DESC "ftp command_data"
49 
50 static int g_ftp_cmd_data_buffer_id = 0;
51 
52 static int DetectFtpCommandDataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
53 {
54  if (DetectBufferSetActiveList(de_ctx, s, g_ftp_cmd_data_buffer_id) < 0)
55  return -1;
56 
58  return -1;
59 
60  return 0;
61 }
62 
63 static bool DetectFTPCommandDataGetData(
64  void *txv, const uint8_t _flow_flags, const uint8_t **buffer, uint32_t *buffer_len)
65 {
66  FTPTransaction *tx = (FTPTransaction *)txv;
67 
68  if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
69  return false;
70 
71  const char *b;
72  uint8_t b_len;
73  if (SCGetFtpCommandInfo(tx->command_descriptor.command_index, &b, NULL, &b_len)) {
74  if ((tx->request_length - b_len - 1) > 0) {
75  // command data starts here: advance past command + 1 space
76  *buffer = tx->request + b_len + 1;
77  *buffer_len = tx->request_length - b_len - 1;
78  SCLogDebug("command data: \"%s\" [bytes %d]", *buffer, *buffer_len);
79  return true;
80  }
81  }
82 
83  *buffer = NULL;
84  *buffer_len = 0;
85  return false;
86 }
87 
88 static InspectionBuffer *GetDataWrapper(DetectEngineThreadCtx *det_ctx,
89  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
90  const int list_id)
91 {
92  return DetectHelperGetData(
93  det_ctx, transforms, _f, _flow_flags, txv, list_id, DetectFTPCommandDataGetData);
94 }
95 
97 {
98  /* ftp.command sticky buffer */
101  "sticky buffer to match on the FTP command data buffer";
103  sigmatch_table[DETECT_FTP_COMMAND_DATA].Setup = DetectFtpCommandDataSetup;
105 
107  BUFFER_NAME, BUFFER_NAME, ALPROTO_FTP, false, true, GetDataWrapper);
108 
110 
111  g_ftp_cmd_data_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
112 
113  SCLogDebug("registering " BUFFER_NAME " rule option");
114 }
DetectFtpCommandDataRegister
void DetectFtpCommandDataRegister(void)
Definition: detect-ftp-command-data.c:96
SigTableElmt_::url
const char * url
Definition: detect.h:1405
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2218
detect-content.h
detect-engine.h
FTPTransaction_::request
uint8_t * request
Definition: app-layer-ftp.h:68
SigTableElmt_::desc
const char * desc
Definition: detect.h:1404
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
SigTableElmt_::name
const char * name
Definition: detect.h:1402
DETECT_FTP_COMMAND_DATA
@ DETECT_FTP_COMMAND_DATA
Definition: detect-engine-register.h:335
DetectEngineTransforms
Definition: detect.h:415
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-command-data.c:46
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-command-data.c:48
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1422
InspectionBuffer
Definition: detect.h:380
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1396
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:920
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-command-data.c:47
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
app-layer-ftp.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1387
DetectHelperBufferMpmRegister
int DetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, bool toclient, bool toserver, InspectionBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:64
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1197
detect-engine-mpm.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-command-data.c:45
detect.h
detect-engine-helper.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
FTPTransaction_::request_length
uint32_t request_length
Definition: app-layer-ftp.h:67
suricata-common.h
DetectHelperGetData
InspectionBuffer * DetectHelperGetData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, bool(*GetBuf)(void *txv, const uint8_t flow_flags, const uint8_t **buf, uint32_t *buf_len))
Definition: detect-engine-helper.c:46
str
#define str(s)
Definition: suricata-common.h:300
detect-parse.h
detect-ftp-command-data.h
Signature_
Signature container.
Definition: detect.h:669
FtpCommandInfo_::command_index
uint8_t command_index
Definition: app-layer-ftp.h:56
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1592
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
flow.h
FTPTransaction_
Definition: app-layer-ftp.h:60
app-layer.h