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|retr)\\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 DetectEngineInspectFtpdataGeneric(ThreadVars *tv,
54  const Signature *s, const SigMatchData *smd,
55  Flow *f, uint8_t flags, void *alstate,
56  void *txv, uint64_t tx_id);
57 static int g_ftpdata_buffer_id = 0;
58 
59 /**
60  * \brief Registration function for ftpcommand: keyword
61  *
62  * This function is called once in the 'lifetime' of the engine.
63  */
65  /* keyword name: this is how the keyword is used in a rule */
66  sigmatch_table[DETECT_FTPDATA].name = "ftpdata_command";
67  /* description: listed in "suricata --list-keywords=all" */
68  sigmatch_table[DETECT_FTPDATA].desc = "match FTP command triggering a FTP data channel";
69  sigmatch_table[DETECT_FTPDATA].url = "/rules/ftp-keywords.html#ftpdata-command";
70  sigmatch_table[DETECT_FTPDATA].AppLayerTxMatch = DetectFtpdataMatch;
71  /* setup function is called during signature parsing, when the ftpcommand
72  * keyword is encountered in the rule */
73  sigmatch_table[DETECT_FTPDATA].Setup = DetectFtpdataSetup;
74  /* free function is called when the detect engine is freed. Normally at
75  * shutdown, but also during rule reloads. */
76  sigmatch_table[DETECT_FTPDATA].Free = DetectFtpdataFree;
77  /* registers unittests into the system */
78 #ifdef UNITTESTS
79  sigmatch_table[DETECT_FTPDATA].RegisterTests = DetectFtpdataRegisterTests;
80 #endif
81  DetectAppLayerInspectEngineRegister("ftpdata_command",
83  DetectEngineInspectFtpdataGeneric);
84 
85  DetectAppLayerInspectEngineRegister("ftpdata_command",
87  DetectEngineInspectFtpdataGeneric);
88  g_ftpdata_buffer_id = DetectBufferTypeGetByName("ftpdata_command");
89 
90  /* set up the PCRE for keyword parsing */
91  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
92 }
93 
94 static int DetectEngineInspectFtpdataGeneric(ThreadVars *tv,
96  const Signature *s, const SigMatchData *smd,
97  Flow *f, uint8_t flags, void *alstate,
98  void *txv, uint64_t tx_id)
99 {
100  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
101  f, flags, alstate, txv, tx_id);
102 }
103 
104 /**
105  * \brief This function is used to check matches from the FTP App Layer Parser
106  *
107  * \param t pointer to thread vars
108  * \param det_ctx pointer to the pattern matcher thread
109  * \param p pointer to the current packet
110  * \param m pointer to the sigmatch
111  * \retval 0 no match
112  * \retval 1 match
113  */
114 static int DetectFtpdataMatch(DetectEngineThreadCtx *det_ctx,
115  Flow *f, uint8_t flags,
116  void *state, void *txv,
117  const Signature *s, const SigMatchCtx *m)
118 {
119  const DetectFtpdataData *ftpcommandd = (const DetectFtpdataData *) m;
120  const FtpDataState *ftp_state = (const FtpDataState *)state;
121 
122  if (ftp_state == NULL)
123  return 0;
124 
125  if (ftpcommandd->command == ftp_state->command) {
126  /* Only match if the flow is in the good direction */
127  if ((flags & STREAM_TOSERVER) && (ftpcommandd->command == FTP_COMMAND_RETR)) {
128  return 0;
129  } else if ((flags & STREAM_TOCLIENT) && (ftpcommandd->command == FTP_COMMAND_STOR)) {
130  return 0;
131  }
132  return 1;
133  }
134 
135  return 0;
136 }
137 
138 /**
139  * \brief This function is used to parse ftpcommand options passed via ftpcommand: keyword
140  *
141  * \param ftpcommandstr Pointer to the user provided ftpcommand options
142  *
143  * \retval ftpcommandd pointer to DetectFtpdataData on success
144  * \retval NULL on failure
145  */
146 static DetectFtpdataData *DetectFtpdataParse(const char *ftpcommandstr)
147 {
148  DetectFtpdataData *ftpcommandd = NULL;
149  char arg1[5] = "";
150  int ov[MAX_SUBSTRINGS];
151 
152  int ret = DetectParsePcreExec(&parse_regex, ftpcommandstr, 0, 0, ov, MAX_SUBSTRINGS);
153  if (ret != 2) {
154  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
155  goto error;
156  }
157 
158  int res = pcre_copy_substring((char *) ftpcommandstr, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
159  if (res < 0) {
160  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
161  goto error;
162  }
163  SCLogDebug("Arg1 \"%s\"", arg1);
164 
165  ftpcommandd = SCMalloc(sizeof (DetectFtpdataData));
166  if (unlikely(ftpcommandd == NULL))
167  goto error;
168  if (!strcmp(arg1, "stor")) {
169  ftpcommandd->command = FTP_COMMAND_STOR;
170  } else if (!strcmp(arg1, "retr")) {
171  ftpcommandd->command = FTP_COMMAND_RETR;
172  } else {
173  SCLogError(SC_ERR_NOT_SUPPORTED, "Invalid command value");
174  goto error;
175  }
176 
177  return ftpcommandd;
178 
179 error:
180  if (ftpcommandd)
181  SCFree(ftpcommandd);
182  return NULL;
183 }
184 
185 /**
186  * \brief parse the options from the 'ftpcommand' keyword in the rule into
187  * the Signature data structure.
188  *
189  * \param de_ctx pointer to the Detection Engine Context
190  * \param s pointer to the Current Signature
191  * \param str pointer to the user provided ftpcommand options
192  *
193  * \retval 0 on Success
194  * \retval -1 on Failure
195  */
196 static int DetectFtpdataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
197 {
199  return -1;
200 
201  DetectFtpdataData *ftpcommandd = DetectFtpdataParse(str);
202  if (ftpcommandd == NULL)
203  return -1;
204 
205  SigMatch *sm = SigMatchAlloc();
206  if (sm == NULL) {
207  DetectFtpdataFree(de_ctx, ftpcommandd);
208  return -1;
209  }
210  sm->type = DETECT_FTPDATA;
211  sm->ctx = (void *)ftpcommandd;
212 
213  SigMatchAppendSMToList(s, sm, g_ftpdata_buffer_id);
214  return 0;
215 }
216 
217 /**
218  * \brief this function will free memory associated with DetectFtpdataData
219  *
220  * \param ptr pointer to DetectFtpdataData
221  */
222 static void DetectFtpdataFree(DetectEngineCtx *de_ctx, void *ptr) {
223  DetectFtpdataData *ftpcommandd = (DetectFtpdataData *)ptr;
224 
225  /* do more specific cleanup here, if needed */
226 
227  SCFree(ftpcommandd);
228 }
229 
230 #ifdef UNITTESTS
231 
232 static int DetectFtpdataParseTest01(void)
233 {
234  DetectFtpdataData *ftpcommandd = DetectFtpdataParse("stor");
235  FAIL_IF_NULL(ftpcommandd);
236  FAIL_IF(!(ftpcommandd->command == FTP_COMMAND_STOR));
237  DetectFtpdataFree(NULL, ftpcommandd);
238  PASS;
239 }
240 
241 static int DetectFtpdataSignatureTest01(void)
242 {
245 
246  Signature *sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:stor; sid:1; rev:1;)");
247  FAIL_IF_NULL(sig);
248  sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:retr; sid:2; rev:1;)");
249  FAIL_IF_NULL(sig);
250  sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (ftpdata_command:xxx; sid:3; rev:1;)");
251  FAIL_IF_NOT_NULL(sig);
252 
254  PASS;
255 }
256 
257 /**
258  * \brief this function registers unit tests for DetectFtpdata
259  */
260 static void DetectFtpdataRegisterTests(void)
261 {
262  UtRegisterTest("DetectFtpdataParseTest01", DetectFtpdataParseTest01);
263  UtRegisterTest("DetectFtpdataSignatureTest01",
264  DetectFtpdataSignatureTest01);
265 }
266 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
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:264
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
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:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectFtpdataData_::command
FtpRequestCommand command
Definition: detect-ftpdata.h:36
SC_ERR_NOT_SUPPORTED
@ SC_ERR_NOT_SUPPORTED
Definition: util-error.h:257
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
FtpDataState_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:207
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1182
m
SCMutex m
Definition: flow-hash.h:6
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
app-layer-ftp.h
detect-ftpdata.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigMatchData_
Data needed for Match()
Definition: detect.h:329
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(ThreadVars *tv, const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1596
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2493
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:315
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2361
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
ALPROTO_FTPDATA
@ ALPROTO_FTPDATA
Definition: app-layer-protos.h:47
FtpDataState_
Definition: app-layer-ftp.h:200
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:273
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFtpdataData_
Definition: detect-ftpdata.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
FTP_COMMAND_RETR
@ FTP_COMMAND_RETR
Definition: app-layer-ftp.h:70
FTP_COMMAND_STOR
@ FTP_COMMAND_STOR
Definition: app-layer-ftp.h:78
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
Definition: detect-engine.c:171
DetectFtpdataRegister
void DetectFtpdataRegister(void)
Registration function for ftpcommand: keyword.
Definition: detect-ftpdata.c:64
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203