suricata
detect-fileext.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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 Victor Julien <victor@inliniac.net>
22  * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "debug.h"
29 #include "decode.h"
30 
31 #include "detect.h"
32 #include "detect-parse.h"
33 
34 #include "detect-engine.h"
35 #include "detect-engine-mpm.h"
36 #include "detect-engine-state.h"
37 
38 #include "flow.h"
39 #include "flow-var.h"
40 #include "flow-util.h"
41 
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 #include "util-spm-bm.h"
46 #include "util-print.h"
47 #include "util-memcmp.h"
48 
49 #include "app-layer.h"
50 
51 #include "stream-tcp.h"
52 #include "detect-fileext.h"
53 
54 static int DetectFileextMatch (DetectEngineThreadCtx *, Flow *,
55  uint8_t, File *, const Signature *, const SigMatchCtx *);
56 static int DetectFileextSetup (DetectEngineCtx *, Signature *, const char *);
57 #ifdef UNITTESTS
58 static void DetectFileextRegisterTests(void);
59 #endif
60 static void DetectFileextFree(DetectEngineCtx *, void *);
61 static int g_file_match_list_id = 0;
62 
63 /**
64  * \brief Registration function for keyword: fileext
65  */
67 {
68  sigmatch_table[DETECT_FILEEXT].name = "fileext";
69  sigmatch_table[DETECT_FILEEXT].desc = "match on the extension of a file name";
70  sigmatch_table[DETECT_FILEEXT].url = "/rules/file-keywords.html#fileext";
71  sigmatch_table[DETECT_FILEEXT].FileMatch = DetectFileextMatch;
72  sigmatch_table[DETECT_FILEEXT].Setup = DetectFileextSetup;
73  sigmatch_table[DETECT_FILEEXT].Free = DetectFileextFree;
74 #ifdef UNITTESTS
75  sigmatch_table[DETECT_FILEEXT].RegisterTests = DetectFileextRegisterTests;
76 #endif
79 
80  g_file_match_list_id = DetectBufferTypeRegister("files");
81 
82  SCLogDebug("registering fileext rule option");
83  return;
84 }
85 
86 /**
87  * \brief match the specified file extension
88  *
89  * \param t thread local vars
90  * \param det_ctx pattern matcher thread local data
91  * \param f *LOCKED* flow
92  * \param flags direction flags
93  * \param file file being inspected
94  * \param s signature being inspected
95  * \param m sigmatch that we will cast into DetectFileextData
96  *
97  * \retval 0 no match
98  * \retval 1 match
99  */
100 static int DetectFileextMatch (DetectEngineThreadCtx *det_ctx,
101  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
102 {
103  SCEnter();
104  int ret = 0;
105 
106  DetectFileextData *fileext = (DetectFileextData *)m;
107 
108  if (file->name == NULL)
109  SCReturnInt(0);
110 
111  if (file->name_len <= fileext->len)
112  SCReturnInt(0);
113 
114  int offset = file->name_len - fileext->len;
115 
116  /* fileext->ext is already in lowercase, as SCMemcmpLowercase requires */
117  if (file->name[offset - 1] == '.' &&
118  SCMemcmpLowercase(fileext->ext, file->name + offset, fileext->len) == 0)
119  {
120  if (!(fileext->flags & DETECT_CONTENT_NEGATED)) {
121  ret = 1;
122  SCLogDebug("File ext found");
123  }
124  } else if (fileext->flags & DETECT_CONTENT_NEGATED) {
125  SCLogDebug("negated match");
126  ret = 1;
127  }
128 
129  SCReturnInt(ret);
130 }
131 
132 /**
133  * \brief This function is used to parse fileet
134  *
135  * \param de_ctx Pointer to the detection engine context
136  * \param str Pointer to the fileext value string
137  *
138  * \retval pointer to DetectFileextData on success
139  * \retval NULL on failure
140  */
141 static DetectFileextData *DetectFileextParse (DetectEngineCtx *de_ctx, const char *str, bool negate)
142 {
143  DetectFileextData *fileext = NULL;
144 
145  /* We have a correct filename option */
146  fileext = SCMalloc(sizeof(DetectFileextData));
147  if (unlikely(fileext == NULL))
148  goto error;
149 
150  memset(fileext, 0x00, sizeof(DetectFileextData));
151 
152  if (DetectContentDataParse("fileext", str, &fileext->ext, &fileext->len) == -1) {
153  goto error;
154  }
155  uint16_t u;
156  for (u = 0; u < fileext->len; u++)
157  fileext->ext[u] = tolower(fileext->ext[u]);
158 
159  if (negate) {
160  fileext->flags |= DETECT_CONTENT_NEGATED;
161  }
162 
163  SCLogDebug("flags %02X", fileext->flags);
164  if (fileext->flags & DETECT_CONTENT_NEGATED) {
165  SCLogDebug("negated fileext");
166  }
167 
168 #ifdef DEBUG
169  if (SCLogDebugEnabled()) {
170  char *ext = SCMalloc(fileext->len + 1);
171  if (ext != NULL) {
172  memcpy(ext, fileext->ext, fileext->len);
173  ext[fileext->len] = '\0';
174  SCLogDebug("will look for fileext %s", ext);
175  SCFree(ext);
176  }
177  }
178 #endif
179 
180  return fileext;
181 
182 error:
183  if (fileext != NULL)
184  DetectFileextFree(de_ctx, fileext);
185  return NULL;
186 
187 }
188 
189 /**
190  * \brief this function is used to add the parsed "id" option
191  * into the current signature
192  *
193  * \param de_ctx pointer to the Detection Engine Context
194  * \param s pointer to the Current Signature
195  * \param idstr pointer to the user provided "id" option
196  *
197  * \retval 0 on Success
198  * \retval -1 on Failure
199  */
200 static int DetectFileextSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
201 {
202  DetectFileextData *fileext= NULL;
203  SigMatch *sm = NULL;
204 
205  fileext = DetectFileextParse(de_ctx, str, s->init_data->negated);
206  if (fileext == NULL)
207  goto error;
208 
209  /* Okay so far so good, lets get this into a SigMatch
210  * and put it in the Signature. */
211  sm = SigMatchAlloc();
212  if (sm == NULL)
213  goto error;
214 
215  sm->type = DETECT_FILEEXT;
216  sm->ctx = (void *)fileext;
217 
218  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
219 
221  return 0;
222 
223 error:
224  if (fileext != NULL)
225  DetectFileextFree(de_ctx, fileext);
226  if (sm != NULL)
227  SCFree(sm);
228  return -1;
229 
230 }
231 
232 /**
233  * \brief this function will free memory associated with DetectFileextData
234  *
235  * \param fileext pointer to DetectFileextData
236  */
237 static void DetectFileextFree(DetectEngineCtx *de_ctx, void *ptr)
238 {
239  if (ptr != NULL) {
240  DetectFileextData *fileext = (DetectFileextData *)ptr;
241  if (fileext->ext != NULL)
242  SCFree(fileext->ext);
243  SCFree(fileext);
244  }
245 }
246 
247 #ifdef UNITTESTS /* UNITTESTS */
248 
249 /**
250  * \test DetectFileextTestParse01
251  */
252 static int DetectFileextTestParse01 (void)
253 {
254  DetectFileextData *dfd = DetectFileextParse(NULL, "doc", false);
255  if (dfd != NULL) {
256  DetectFileextFree(NULL, dfd);
257  return 1;
258  }
259  return 0;
260 }
261 
262 /**
263  * \test DetectFileextTestParse02
264  */
265 static int DetectFileextTestParse02 (void)
266 {
267  int result = 0;
268 
269  DetectFileextData *dfd = DetectFileextParse(NULL, "tar.gz", false);
270  if (dfd != NULL) {
271  if (dfd->len == 6 && memcmp(dfd->ext, "tar.gz", 6) == 0) {
272  result = 1;
273  }
274 
275  DetectFileextFree(NULL, dfd);
276  return result;
277  }
278  return 0;
279 }
280 
281 /**
282  * \test DetectFileextTestParse03
283  */
284 static int DetectFileextTestParse03 (void)
285 {
286  int result = 0;
287 
288  DetectFileextData *dfd = DetectFileextParse(NULL, "pdf", false);
289  if (dfd != NULL) {
290  if (dfd->len == 3 && memcmp(dfd->ext, "pdf", 3) == 0) {
291  result = 1;
292  }
293 
294  DetectFileextFree(NULL, dfd);
295  return result;
296  }
297  return 0;
298 }
299 
300 /**
301  * \brief this function registers unit tests for DetectFileext
302  */
303 void DetectFileextRegisterTests(void)
304 {
305  UtRegisterTest("DetectFileextTestParse01", DetectFileextTestParse01);
306  UtRegisterTest("DetectFileextTestParse02", DetectFileextTestParse02);
307  UtRegisterTest("DetectFileextTestParse03", DetectFileextTestParse03);
308 }
309 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
detect-engine.h
DetectFileextData_
Definition: detect-fileext.h:29
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1211
stream-tcp.h
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
SIGMATCH_QUOTES_OPTIONAL
#define SIGMATCH_QUOTES_OPTIONAL
Definition: detect.h:1392
DetectFileextRegister
void DetectFileextRegister(void)
Registration function for keyword: fileext.
Definition: detect-fileext.c:66
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1187
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
DetectFileextData_::ext
uint8_t * ext
Definition: detect-fileext.h:30
detect-fileext.h
FILE_SIG_NEED_FILENAME
#define FILE_SIG_NEED_FILENAME
Definition: detect.h:285
m
SCMutex m
Definition: flow-hash.h:6
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
util-unittest-helper.h
util-memcmp.h
File_::name_len
uint16_t name_len
Definition: util-file.h:65
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1209
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1400
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:597
File_::name
uint8_t * name
Definition: util-file.h:73
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SignatureInitData_::negated
bool negated
Definition: detect.h:485
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
DetectContentDataParse
int DetectContentDataParse(const char *keyword, const char *contentstr, uint8_t **pstr, uint16_t *plen)
Parse a content string, ie "abc|DE|fgh".
Definition: detect-content.c:82
DetectFileextData_::len
uint16_t len
Definition: detect-fileext.h:31
File_
Definition: util-file.h:63
DETECT_FILE_NAME
@ DETECT_FILE_NAME
Definition: detect-engine-register.h:198
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:836
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:542
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm-bm.h
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
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
DetectFileextData_::flags
uint32_t flags
Definition: detect-fileext.h:32
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:284
DETECT_FILEEXT
@ DETECT_FILEEXT
Definition: detect-engine-register.h:199
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
SCLogDebugEnabled
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:662
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
app-layer.h