suricata
detect-ftp-reply.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.reply sticky buffer
23  *
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-helper.h"
33 #include "detect-ftp-reply.h"
34 
35 #include "app-layer.h"
36 #include "app-layer-ftp.h"
37 
38 #include "flow.h"
39 
40 #include "util-debug.h"
41 
42 #include "rust.h"
43 
44 #define KEYWORD_NAME "ftp.reply"
45 #define KEYWORD_DOC "ftp-keywords.html#ftp-reply"
46 #define BUFFER_NAME "ftp.reply"
47 #define BUFFER_DESC "ftp reply"
48 
49 static int g_ftp_reply_buffer_id = 0;
50 
51 static int DetectFtpReplySetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
52 {
53  if (DetectBufferSetActiveList(de_ctx, s, g_ftp_reply_buffer_id) < 0)
54  return -1;
55 
57  return -1;
58 
59  return 0;
60 }
61 
62 static bool DetectFTPReplyGetData(void *txv, uint8_t _flow_flags, uint32_t index,
63  const uint8_t **buffer, uint32_t *buffer_len)
64 {
65  FTPTransaction *tx = (FTPTransaction *)txv;
66 
67  if (tx->command_descriptor.command_code == FTP_COMMAND_UNKNOWN)
68  return false;
69 
70  if (!TAILQ_EMPTY(&tx->response_list)) {
71  uint32_t count = 0;
72  FTPResponseWrapper *wrapper;
73  TAILQ_FOREACH (wrapper, &tx->response_list, next) {
74  DEBUG_VALIDATE_BUG_ON(wrapper->response == NULL);
75  if (index == count) {
76  *buffer = (const uint8_t *)wrapper->response->response;
77  *buffer_len = wrapper->response->length;
78  return true;
79  }
80  count++;
81  }
82  }
83 
84  *buffer = NULL;
85  *buffer_len = 0;
86  return false;
87 }
88 
89 static InspectionBuffer *GetDataWrapper(DetectEngineThreadCtx *det_ctx,
90  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
91  const int list_id, uint32_t index)
92 {
94  det_ctx, transforms, _f, _flow_flags, txv, list_id, index, DetectFTPReplyGetData);
95 }
96 
98 {
99  /* ftp.reply sticky buffer */
101  sigmatch_table[DETECT_FTP_REPLY].desc = "sticky buffer to match on the FTP reply buffer";
103  sigmatch_table[DETECT_FTP_REPLY].Setup = DetectFtpReplySetup;
105 
107  BUFFER_NAME, ALPROTO_FTP, SIG_FLAG_TOCLIENT, 0, GetDataWrapper, 2, 1);
108 
110 
111  g_ftp_reply_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
112 
113  SCLogDebug("registering " BUFFER_NAME " rule option");
114 }
SigTableElmt_::url
const char * url
Definition: detect.h:1405
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2218
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1404
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
DetectHelperGetMultiData
InspectionBuffer * DetectHelperGetMultiData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, uint32_t index, MultiGetTxBuffer GetBuf)
Definition: detect-engine-helper.c:158
SigTableElmt_::name
const char * name
Definition: detect.h:1402
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-reply.c:44
DetectEngineTransforms
Definition: detect.h:415
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
detect-ftp-reply.h
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1422
InspectionBuffer
Definition: detect.h:380
DetectFtpReplyRegister
void DetectFtpReplyRegister(void)
Definition: detect-ftp-reply.c:97
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
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
DETECT_FTP_REPLY
@ DETECT_FTP_REPLY
Definition: detect-engine-register.h:336
rust.h
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
FTPResponseWrapper_
Definition: app-layer-ftp.h:44
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:1387
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority, int tx_min_progress)
Definition: detect-engine.c:2302
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1197
detect.h
detect-engine-helper.h
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-reply.c:47
FTPTransaction_::command_descriptor
FtpCommandInfo command_descriptor
Definition: app-layer-ftp.h:72
FtpCommandInfo_::command_code
FtpRequestCommand command_code
Definition: app-layer-ftp.h:57
FTPResponseWrapper_::response
FTPResponseLine * response
Definition: app-layer-ftp.h:45
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-reply.c:45
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ftp-reply.c:46
detect-engine-content-inspection.h
suricata-common.h
str
#define str(s)
Definition: suricata-common.h:300
detect-parse.h
Signature_
Signature container.
Definition: detect.h:669
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
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
app-layer.h