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-buffer.h"
32 #include "detect-engine-helper.h"
33 
34 #include "flow.h"
35 
36 #include "app-layer.h"
37 #include "app-layer-ftp.h"
38 
39 #include "detect-ftp-command.h"
40 
41 #define KEYWORD_NAME "ftp.command"
42 #define KEYWORD_DOC "ftp-keywords.html#ftp-command"
43 #define BUFFER_NAME "ftp.command"
44 #define BUFFER_DESC "ftp command"
45 
46 static int g_ftp_cmd_buffer_id = 0;
47 
48 static int DetectFtpCommandSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
49 {
50  if (SCDetectBufferSetActiveList(de_ctx, s, g_ftp_cmd_buffer_id) < 0)
51  return -1;
52 
54  return -1;
55 
56  return 0;
57 }
58 
59 static InspectionBuffer *DetectFTPCommandGetData(DetectEngineThreadCtx *det_ctx,
60  const DetectEngineTransforms *transforms, Flow *f, const uint8_t _flow_flags, void *txv,
61  const int list_id)
62 {
63  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
64  if (buffer->inspect == NULL) {
65  FTPTransaction *tx = (FTPTransaction *)txv;
66 
67  if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
68  return NULL;
69 
70  const char *b = NULL;
71  uint8_t b_len = 0;
72  if (SCGetFtpCommandInfo(tx->command_descriptor.command_index, &b, NULL, &b_len)) {
74  det_ctx, list_id, buffer, (const uint8_t *)b, b_len, transforms);
75  } else {
76  return NULL;
77  }
78  }
79  return buffer;
80 }
81 
83 {
84  /* ftp.command sticky buffer */
86  sigmatch_table[DETECT_FTP_COMMAND].desc = "sticky buffer to match on the FTP command buffer";
88  sigmatch_table[DETECT_FTP_COMMAND].Setup = DetectFtpCommandSetup;
90 
91  g_ftp_cmd_buffer_id = DetectHelperBufferMpmRegister(
92  BUFFER_NAME, BUFFER_DESC, ALPROTO_FTP, STREAM_TOSERVER, DetectFTPCommandGetData);
93 
94  SCLogDebug("registering " BUFFER_NAME " rule option");
95 }
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
detect-engine.h
DetectFtpCommandRegister
void DetectFtpCommandRegister(void)
Definition: detect-ftp-command.c:82
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
SigTableElmt_::name
const char * name
Definition: detect.h:1428
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine.c:1570
DetectEngineTransforms
Definition: detect.h:415
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
InspectionBuffer
Definition: detect.h:380
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-command.c:44
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-command.c:42
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
app-layer-ftp.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1429
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
detect.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-command.c:41
DetectHelperBufferMpmRegister
int DetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:64
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
suricata-common.h
detect-ftp-command.h
detect-engine-buffer.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:308
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
FtpCommandInfo_::command_index
uint8_t command_index
Definition: app-layer-ftp.h:56
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
flow.h
FTPTransaction_
Definition: app-layer-ftp.h:60
DETECT_FTP_COMMAND
@ DETECT_FTP_COMMAND
Definition: detect-engine-register.h:331
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-command.c:43
app-layer.h