suricata
detect-ftpdata.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2020 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  * \file
20  *
21  * \author Eric Leblond <eric@regit.org>
22  *
23  * Match on ftp command used to trigger a ftp data transfer
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect-parse.h"
29 #include "detect-engine.h"
30 #include "detect-engine-state.h"
31 
32 #include "app-layer-ftp.h"
33 
34 #include "detect-ftpdata.h"
35 
36 /**
37  * \brief Regex for parsing our keyword options
38  */
39 #define PARSE_REGEX "^\\s*(stor|appe|stou|retr|nlst|list|mlsd)\\s*$"
40 static DetectParseRegex parse_regex;
41 
42 /* Prototypes of functions registered in DetectFtpdataRegister below */
43 static int DetectFtpdataMatch(DetectEngineThreadCtx *,
44  Flow *, uint8_t, void *, void *,
45  const Signature *, const SigMatchCtx *);
46 static int DetectFtpdataSetup (DetectEngineCtx *, Signature *, const char *);
47 static void DetectFtpdataFree (DetectEngineCtx *, void *);
48 #ifdef UNITTESTS
49 static void DetectFtpdataRegisterTests (void);
50 #endif
51 static int g_ftpdata_buffer_id = 0;
52 
53 /**
54  * \brief Registration function for ftpcommand: keyword
55  *
56  * This function is called once in the 'lifetime' of the engine.
57  */
59  /* keyword name: this is how the keyword is used in a rule */
60  sigmatch_table[DETECT_FTPDATA].name = "ftpdata_command";
61  /* description: listed in "suricata --list-keywords=all" */
62  sigmatch_table[DETECT_FTPDATA].desc = "match FTP command triggering a FTP data channel";
63  sigmatch_table[DETECT_FTPDATA].url = "/rules/ftp-keywords.html#ftpdata-command";
65  sigmatch_table[DETECT_FTPDATA].AppLayerTxMatch = DetectFtpdataMatch;
66  /* setup function is called during signature parsing, when the ftpcommand
67  * keyword is encountered in the rule */
68  sigmatch_table[DETECT_FTPDATA].Setup = DetectFtpdataSetup;
69  /* free function is called when the detect engine is freed. Normally at
70  * shutdown, but also during rule reloads. */
71  sigmatch_table[DETECT_FTPDATA].Free = DetectFtpdataFree;
72  /* registers unittests into the system */
73 #ifdef UNITTESTS
74  sigmatch_table[DETECT_FTPDATA].RegisterTests = DetectFtpdataRegisterTests;
75 #endif
78 
81  g_ftpdata_buffer_id = DetectBufferTypeGetByName("ftpdata_command");
82 
83  /* set up the PCRE for keyword parsing */
84  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
85 }
86 
87 /**
88  * \brief This function is used to check matches from the FTP App Layer Parser
89  *
90  * \param t pointer to thread vars
91  * \param det_ctx pointer to the pattern matcher thread
92  * \param p pointer to the current packet
93  * \param m pointer to the sigmatch
94  * \retval 0 no match
95  * \retval 1 match
96  */
97 static int DetectFtpdataMatch(DetectEngineThreadCtx *det_ctx,
98  Flow *f, uint8_t flags,
99  void *state, void *txv,
100  const Signature *s, const SigMatchCtx *m)
101 {
102  const DetectFtpdataData *ftpcommandd = (const DetectFtpdataData *) m;
103  const FtpDataState *ftp_state = (const FtpDataState *)state;
104 
105  if (ftp_state == NULL)
106  return 0;
107 
108  return ftpcommandd->command == ftp_state->command;
109 }
110 
111 /**
112  * \brief This function is used to parse ftpcommand options passed via ftpcommand: keyword
113  *
114  * \param ftpcommandstr Pointer to the user provided ftpcommand options
115  *
116  * \retval ftpcommandd pointer to DetectFtpdataData on success
117  * \retval NULL on failure
118  */
119 static DetectFtpdataData *DetectFtpdataParse(const char *ftpcommandstr)
120 {
121  DetectFtpdataData *ftpcommandd = NULL;
122  char arg1[5] = "";
123  size_t pcre2len;
124  pcre2_match_data *match = NULL;
125 
126  int ret = DetectParsePcreExec(&parse_regex, &match, ftpcommandstr, 0, 0);
127  if (ret != 2) {
128  SCLogError("parse error, ret %" PRId32 "", ret);
129  goto error;
130  }
131 
132  pcre2len = sizeof(arg1);
133  int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
134  if (res < 0) {
135  SCLogError("pcre2_substring_copy_bynumber failed");
136  goto error;
137  }
138  SCLogDebug("Arg1 \"%s\"", arg1);
139 
140  ftpcommandd = SCMalloc(sizeof (DetectFtpdataData));
141  if (unlikely(ftpcommandd == NULL))
142  goto error;
143  if (!strcmp(arg1, "stor")) {
144  ftpcommandd->command = FTP_COMMAND_STOR;
145  } else if (!strcmp(arg1, "appe")) {
146  ftpcommandd->command = FTP_COMMAND_APPE;
147  } else if (!strcmp(arg1, "stou")) {
148  ftpcommandd->command = FTP_COMMAND_STOU;
149  } else if (!strcmp(arg1, "retr")) {
150  ftpcommandd->command = FTP_COMMAND_RETR;
151  } else if (!strcmp(arg1, "nlst")) {
152  ftpcommandd->command = FTP_COMMAND_NLST;
153  } else if (!strcmp(arg1, "list")) {
154  ftpcommandd->command = FTP_COMMAND_LIST;
155  } else if (!strcmp(arg1, "mlsd")) {
156  ftpcommandd->command = FTP_COMMAND_MLSD;
157  } else {
158  SCLogError("Invalid command value");
159  goto error;
160  }
161 
162  pcre2_match_data_free(match);
163  return ftpcommandd;
164 
165 error:
166  if (match) {
167  pcre2_match_data_free(match);
168  }
169  if (ftpcommandd)
170  SCFree(ftpcommandd);
171  return NULL;
172 }
173 
174 /**
175  * \brief parse the options from the 'ftpcommand' keyword in the rule into
176  * the Signature data structure.
177  *
178  * \param de_ctx pointer to the Detection Engine Context
179  * \param s pointer to the Current Signature
180  * \param str pointer to the user provided ftpcommand options
181  *
182  * \retval 0 on Success
183  * \retval -1 on Failure
184  */
185 static int DetectFtpdataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
186 {
188  return -1;
189 
190  DetectFtpdataData *ftpcommandd = DetectFtpdataParse(str);
191  if (ftpcommandd == NULL)
192  return -1;
193 
195  g_ftpdata_buffer_id) == NULL) {
196  DetectFtpdataFree(de_ctx, ftpcommandd);
197  return -1;
198  }
199  return 0;
200 }
201 
202 /**
203  * \brief this function will free memory associated with DetectFtpdataData
204  *
205  * \param ptr pointer to DetectFtpdataData
206  */
207 static void DetectFtpdataFree(DetectEngineCtx *de_ctx, void *ptr) {
208  DetectFtpdataData *ftpcommandd = (DetectFtpdataData *)ptr;
209 
210  /* do more specific cleanup here, if needed */
211 
212  SCFree(ftpcommandd);
213 }
214 
215 #ifdef UNITTESTS
216 
217 static int DetectFtpdataParseTest01(void)
218 {
219  DetectFtpdataData *ftpcommandd = DetectFtpdataParse("stor");
220  FAIL_IF_NULL(ftpcommandd);
221  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_STOR));
222  DetectFtpdataFree(NULL, ftpcommandd);
223 
224  ftpcommandd = DetectFtpdataParse("appe");
225  FAIL_IF_NULL(ftpcommandd);
226  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_APPE));
227  DetectFtpdataFree(NULL, ftpcommandd);
228 
229  ftpcommandd = DetectFtpdataParse("stou");
230  FAIL_IF_NULL(ftpcommandd);
231  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_STOU));
232  DetectFtpdataFree(NULL, ftpcommandd);
233 
234  ftpcommandd = DetectFtpdataParse("list");
235  FAIL_IF_NULL(ftpcommandd);
236  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_LIST));
237  DetectFtpdataFree(NULL, ftpcommandd);
238 
239  ftpcommandd = DetectFtpdataParse("mlsd");
240  FAIL_IF_NULL(ftpcommandd);
241  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_MLSD));
242  DetectFtpdataFree(NULL, ftpcommandd);
243  PASS;
244 }
245 
246 static int DetectFtpdataSignatureTest01(void)
247 {
250 
251  Signature *sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:stor; sid:1; rev:1;)");
252  FAIL_IF_NULL(sig);
253  sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:retr; sid:2; rev:1;)");
254  FAIL_IF_NULL(sig);
255  sig = DetectEngineAppendSig(
256  de_ctx, "alert ip any any -> any any (ftpdata_command:appe; sid:3; rev:1;)");
257  FAIL_IF_NULL(sig);
258  sig = DetectEngineAppendSig(
259  de_ctx, "alert ip any any -> any any (ftpdata_command:stou; sid:4; rev:1;)");
260  FAIL_IF_NULL(sig);
261  sig = DetectEngineAppendSig(
262  de_ctx, "alert ip any any -> any any (ftpdata_command:list; sid:5; rev:1;)");
263  FAIL_IF_NULL(sig);
264  sig = DetectEngineAppendSig(
265  de_ctx, "alert ip any any -> any any (ftpdata_command:mlsd; sid:6; rev:1;)");
266  FAIL_IF_NULL(sig);
267  sig = DetectEngineAppendSig(
268  de_ctx, "alert ip any any -> any any (ftpdata_command:xxx; sid:7; rev:1;)");
269  FAIL_IF_NOT_NULL(sig);
270 
272  PASS;
273 }
274 
275 /**
276  * \brief this function registers unit tests for DetectFtpdata
277  */
278 static void DetectFtpdataRegisterTests(void)
279 {
280  UtRegisterTest("DetectFtpdataParseTest01", DetectFtpdataParseTest01);
281  UtRegisterTest("DetectFtpdataSignatureTest01",
282  DetectFtpdataSignatureTest01);
283 }
284 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DETECT_FTPDATA
@ DETECT_FTPDATA
Definition: detect-engine-register.h:275
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-ftpdata.c:39
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
DetectParseRegex
Definition: detect-parse.h:94
SigTableElmt_::name
const char * name
Definition: detect.h:1542
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectFtpdataData_::command
FtpRequestCommand command
Definition: detect-ftpdata.h:35
Flow_
Flow data structure.
Definition: flow.h:359
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
FtpDataState_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:118
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1507
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:4025
m
SCMutex m
Definition: flow-hash.h:6
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2619
app-layer-ftp.h
detect-ftpdata.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3971
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:275
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:275
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1452
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:274
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:4151
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:424
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
ALPROTO_FTPDATA
@ ALPROTO_FTPDATA
Definition: app-layer-protos.h:53
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Definition: detect-engine.c:2155
FtpDataState_
Definition: app-layer-ftp.h:112
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:313
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFtpdataData_
Definition: detect-ftpdata.h:34
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:336
DetectFtpdataRegister
void DetectFtpdataRegister(void)
Registration function for ftpcommand: keyword.
Definition: detect-ftpdata.c:58
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
f
Flow f
Definition: fuzz_dataset.c:32