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 #include "util-unittest.h"
28 
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-state.h"
32 
33 #include "app-layer-ftp.h"
34 
35 #include "detect-ftpdata.h"
36 
37 /**
38  * \brief Regex for parsing our keyword options
39  */
40 #define PARSE_REGEX "^\\s*(stor|appe|stou|retr|nlst|list|mlsd)\\s*$"
41 static DetectParseRegex parse_regex;
42 
43 /* Prototypes of functions registered in DetectFtpdataRegister below */
44 static int DetectFtpdataMatch(DetectEngineThreadCtx *,
45  Flow *, uint8_t, void *, void *,
46  const Signature *, const SigMatchCtx *);
47 static int DetectFtpdataSetup (DetectEngineCtx *, Signature *, const char *);
48 static void DetectFtpdataFree (DetectEngineCtx *, void *);
49 #ifdef UNITTESTS
50 static void DetectFtpdataRegisterTests (void);
51 #endif
52 static int g_ftpdata_buffer_id = 0;
53 
54 /**
55  * \brief Registration function for ftpcommand: keyword
56  *
57  * This function is called once in the 'lifetime' of the engine.
58  */
60  /* keyword name: this is how the keyword is used in a rule */
61  sigmatch_table[DETECT_FTPDATA].name = "ftpdata_command";
62  /* description: listed in "suricata --list-keywords=all" */
63  sigmatch_table[DETECT_FTPDATA].desc = "match FTP command triggering a FTP data channel";
64  sigmatch_table[DETECT_FTPDATA].url = "/rules/ftp-keywords.html#ftpdata-command";
66  sigmatch_table[DETECT_FTPDATA].AppLayerTxMatch = DetectFtpdataMatch;
67  /* setup function is called during signature parsing, when the ftpcommand
68  * keyword is encountered in the rule */
69  sigmatch_table[DETECT_FTPDATA].Setup = DetectFtpdataSetup;
70  /* free function is called when the detect engine is freed. Normally at
71  * shutdown, but also during rule reloads. */
72  sigmatch_table[DETECT_FTPDATA].Free = DetectFtpdataFree;
73  /* registers unittests into the system */
74 #ifdef UNITTESTS
75  sigmatch_table[DETECT_FTPDATA].RegisterTests = DetectFtpdataRegisterTests;
76 #endif
79 
82  g_ftpdata_buffer_id = DetectBufferTypeGetByName("ftpdata_command");
83 
84  /* set up the PCRE for keyword parsing */
85  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
86 }
87 
88 /**
89  * \brief This function is used to check matches from the FTP App Layer Parser
90  *
91  * \param t pointer to thread vars
92  * \param det_ctx pointer to the pattern matcher thread
93  * \param p pointer to the current packet
94  * \param m pointer to the sigmatch
95  * \retval 0 no match
96  * \retval 1 match
97  */
98 static int DetectFtpdataMatch(DetectEngineThreadCtx *det_ctx,
99  Flow *f, uint8_t flags,
100  void *state, void *txv,
101  const Signature *s, const SigMatchCtx *m)
102 {
103  const DetectFtpdataData *ftpcommandd = (const DetectFtpdataData *) m;
104  const FtpDataState *ftp_state = (const FtpDataState *)state;
105 
106  if (ftp_state == NULL)
107  return 0;
108 
109  return ftpcommandd->command == ftp_state->command;
110 }
111 
112 /**
113  * \brief This function is used to parse ftpcommand options passed via ftpcommand: keyword
114  *
115  * \param ftpcommandstr Pointer to the user provided ftpcommand options
116  *
117  * \retval ftpcommandd pointer to DetectFtpdataData on success
118  * \retval NULL on failure
119  */
120 static DetectFtpdataData *DetectFtpdataParse(const char *ftpcommandstr)
121 {
122  DetectFtpdataData *ftpcommandd = NULL;
123  char arg1[5] = "";
124  size_t pcre2len;
125  pcre2_match_data *match = NULL;
126 
127  int ret = DetectParsePcreExec(&parse_regex, &match, ftpcommandstr, 0, 0);
128  if (ret != 2) {
129  SCLogError("parse error, ret %" PRId32 "", ret);
130  goto error;
131  }
132 
133  pcre2len = sizeof(arg1);
134  int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
135  if (res < 0) {
136  SCLogError("pcre2_substring_copy_bynumber failed");
137  goto error;
138  }
139  SCLogDebug("Arg1 \"%s\"", arg1);
140 
141  ftpcommandd = SCMalloc(sizeof (DetectFtpdataData));
142  if (unlikely(ftpcommandd == NULL))
143  goto error;
144  if (!strcmp(arg1, "stor")) {
145  ftpcommandd->command = FTP_COMMAND_STOR;
146  } else if (!strcmp(arg1, "appe")) {
147  ftpcommandd->command = FTP_COMMAND_APPE;
148  } else if (!strcmp(arg1, "stou")) {
149  ftpcommandd->command = FTP_COMMAND_STOU;
150  } else if (!strcmp(arg1, "retr")) {
151  ftpcommandd->command = FTP_COMMAND_RETR;
152  } else if (!strcmp(arg1, "nlst")) {
153  ftpcommandd->command = FTP_COMMAND_NLST;
154  } else if (!strcmp(arg1, "list")) {
155  ftpcommandd->command = FTP_COMMAND_LIST;
156  } else if (!strcmp(arg1, "mlsd")) {
157  ftpcommandd->command = FTP_COMMAND_MLSD;
158  } else {
159  SCLogError("Invalid command value");
160  goto error;
161  }
162 
163  pcre2_match_data_free(match);
164  return ftpcommandd;
165 
166 error:
167  if (match) {
168  pcre2_match_data_free(match);
169  }
170  if (ftpcommandd)
171  SCFree(ftpcommandd);
172  return NULL;
173 }
174 
175 /**
176  * \brief parse the options from the 'ftpcommand' keyword in the rule into
177  * the Signature data structure.
178  *
179  * \param de_ctx pointer to the Detection Engine Context
180  * \param s pointer to the Current Signature
181  * \param str pointer to the user provided ftpcommand options
182  *
183  * \retval 0 on Success
184  * \retval -1 on Failure
185  */
186 static int DetectFtpdataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
187 {
189  return -1;
190 
191  DetectFtpdataData *ftpcommandd = DetectFtpdataParse(str);
192  if (ftpcommandd == NULL)
193  return -1;
194 
196  g_ftpdata_buffer_id) == NULL) {
197  DetectFtpdataFree(de_ctx, ftpcommandd);
198  return -1;
199  }
200  return 0;
201 }
202 
203 /**
204  * \brief this function will free memory associated with DetectFtpdataData
205  *
206  * \param ptr pointer to DetectFtpdataData
207  */
208 static void DetectFtpdataFree(DetectEngineCtx *de_ctx, void *ptr) {
209  DetectFtpdataData *ftpcommandd = (DetectFtpdataData *)ptr;
210 
211  /* do more specific cleanup here, if needed */
212 
213  SCFree(ftpcommandd);
214 }
215 
216 #ifdef UNITTESTS
217 
218 static int DetectFtpdataParseTest01(void)
219 {
220  DetectFtpdataData *ftpcommandd = DetectFtpdataParse("stor");
221  FAIL_IF_NULL(ftpcommandd);
222  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_STOR));
223  DetectFtpdataFree(NULL, ftpcommandd);
224 
225  ftpcommandd = DetectFtpdataParse("appe");
226  FAIL_IF_NULL(ftpcommandd);
227  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_APPE));
228  DetectFtpdataFree(NULL, ftpcommandd);
229 
230  ftpcommandd = DetectFtpdataParse("stou");
231  FAIL_IF_NULL(ftpcommandd);
232  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_STOU));
233  DetectFtpdataFree(NULL, ftpcommandd);
234 
235  ftpcommandd = DetectFtpdataParse("list");
236  FAIL_IF_NULL(ftpcommandd);
237  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_LIST));
238  DetectFtpdataFree(NULL, ftpcommandd);
239 
240  ftpcommandd = DetectFtpdataParse("mlsd");
241  FAIL_IF_NULL(ftpcommandd);
242  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_MLSD));
243  DetectFtpdataFree(NULL, ftpcommandd);
244  PASS;
245 }
246 
247 static int DetectFtpdataSignatureTest01(void)
248 {
251 
252  Signature *sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:stor; sid:1; rev:1;)");
253  FAIL_IF_NULL(sig);
254  sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:retr; sid:2; rev:1;)");
255  FAIL_IF_NULL(sig);
256  sig = DetectEngineAppendSig(
257  de_ctx, "alert ip any any -> any any (ftpdata_command:appe; sid:3; rev:1;)");
258  FAIL_IF_NULL(sig);
259  sig = DetectEngineAppendSig(
260  de_ctx, "alert ip any any -> any any (ftpdata_command:stou; sid:4; rev:1;)");
261  FAIL_IF_NULL(sig);
262  sig = DetectEngineAppendSig(
263  de_ctx, "alert ip any any -> any any (ftpdata_command:list; sid:5; rev:1;)");
264  FAIL_IF_NULL(sig);
265  sig = DetectEngineAppendSig(
266  de_ctx, "alert ip any any -> any any (ftpdata_command:mlsd; sid:6; rev:1;)");
267  FAIL_IF_NULL(sig);
268  sig = DetectEngineAppendSig(
269  de_ctx, "alert ip any any -> any any (ftpdata_command:xxx; sid:7; rev:1;)");
270  FAIL_IF_NOT_NULL(sig);
271 
273  PASS;
274 }
275 
276 /**
277  * \brief this function registers unit tests for DetectFtpdata
278  */
279 static void DetectFtpdataRegisterTests(void)
280 {
281  UtRegisterTest("DetectFtpdataParseTest01", DetectFtpdataParseTest01);
282  UtRegisterTest("DetectFtpdataSignatureTest01",
283  DetectFtpdataSignatureTest01);
284 }
285 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1512
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:273
SigTableElmt_::desc
const char * desc
Definition: detect.h:1511
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-ftpdata.c:40
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1496
DetectParseRegex
Definition: detect-parse.h:94
SigTableElmt_::name
const char * name
Definition: detect.h:1509
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:103
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1500
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectFtpdataData_::command
FtpRequestCommand command
Definition: detect-ftpdata.h:35
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2759
FtpDataState_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:116
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1474
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:3654
m
SCMutex m
Definition: flow-hash.h:6
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2306
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:3600
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
util-unittest.h
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:272
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1383
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
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:1291
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:3780
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:387
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:351
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)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2051
FtpDataState_
Definition: app-layer-ftp.h:110
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:316
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:675
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2720
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:334
DetectFtpdataRegister
void DetectFtpdataRegister(void)
Registration function for ftpcommand: keyword.
Definition: detect-ftpdata.c:59
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1498