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"
34#include "detect-ftp-reply.h"
35
36#include "app-layer.h"
37#include "app-layer-ftp.h"
38
39#include "flow.h"
40
41#include "util-debug.h"
42
43#include "rust.h"
44
45#define KEYWORD_NAME "ftp.reply"
46#define KEYWORD_DOC "ftp-keywords.html#ftp-reply"
47#define BUFFER_NAME "ftp.reply"
48#define BUFFER_DESC "ftp reply"
49
50static int g_ftp_reply_buffer_id = 0;
51
52static int DetectFtpReplySetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
53{
54 if (SCDetectBufferSetActiveList(de_ctx, s, g_ftp_reply_buffer_id) < 0)
55 return -1;
56
58 return -1;
59
60 return 0;
61}
62
63static bool DetectFTPReplyGetData(DetectEngineThreadCtx *_det_ctx, const void *txv,
64 uint8_t _flow_flags, uint32_t index, 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 if (!TAILQ_EMPTY(&tx->response_list)) {
72 uint32_t count = 0;
73 FTPResponseWrapper *wrapper;
74 TAILQ_FOREACH (wrapper, &tx->response_list, next) {
75 DEBUG_VALIDATE_BUG_ON(wrapper->response == NULL);
76 if (index == count) {
77 *buffer = (const uint8_t *)wrapper->response->response;
78 *buffer_len = (uint32_t)wrapper->response->length;
79 return true;
80 }
81 count++;
82 }
83 }
84
85 *buffer = NULL;
86 *buffer_len = 0;
87 return false;
88}
89
91{
92 /* ftp.reply sticky buffer */
94 sigmatch_table[DETECT_FTP_REPLY].desc = "sticky buffer to match on the FTP reply buffer";
96 sigmatch_table[DETECT_FTP_REPLY].Setup = DetectFtpReplySetup;
98
100 BUFFER_NAME, ALPROTO_FTP, SIG_FLAG_TOCLIENT, 1, DetectFTPReplyGetData, 2);
101
103
104 g_ftp_reply_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
105
106 SCLogDebug("registering " BUFFER_NAME " rule option");
107}
struct HtpBodyChunk_ * next
@ ALPROTO_FTP
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
int DetectBufferTypeGetByName(const char *name)
void DetectFtpReplyRegister(void)
#define KEYWORD_DOC
#define BUFFER_DESC
#define BUFFER_NAME
#define KEYWORD_NAME
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
DetectEngineCtx * de_ctx
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:252
#define TAILQ_EMPTY(head)
Definition queue.h:248
main detection engine ctx
Definition detect.h:932
FTPResponseLine * response
FtpCommandInfo command_descriptor
FtpRequestCommand command_code
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCLogDebug(...)
Definition util-debug.h:275
#define DEBUG_VALIDATE_BUG_ON(exp)