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 static void DetectFileextRegisterTests(void);
58 static void DetectFileextFree(DetectEngineCtx *, void *);
59 static int g_file_match_list_id = 0;
60 
61 /**
62  * \brief Registration function for keyword: fileext
63  */
65 {
66  sigmatch_table[DETECT_FILEEXT].name = "fileext";
67  sigmatch_table[DETECT_FILEEXT].desc = "match on the extension of a file name";
68  sigmatch_table[DETECT_FILEEXT].url = "/rules/file-keywords.html#fileext";
69  sigmatch_table[DETECT_FILEEXT].FileMatch = DetectFileextMatch;
70  sigmatch_table[DETECT_FILEEXT].Setup = DetectFileextSetup;
71  sigmatch_table[DETECT_FILEEXT].Free = DetectFileextFree;
72  sigmatch_table[DETECT_FILEEXT].RegisterTests = DetectFileextRegisterTests;
75 
76  g_file_match_list_id = DetectBufferTypeRegister("files");
77 
78  SCLogDebug("registering fileext rule option");
79  return;
80 }
81 
82 /**
83  * \brief match the specified file extension
84  *
85  * \param t thread local vars
86  * \param det_ctx pattern matcher thread local data
87  * \param f *LOCKED* flow
88  * \param flags direction flags
89  * \param file file being inspected
90  * \param s signature being inspected
91  * \param m sigmatch that we will cast into DetectFileextData
92  *
93  * \retval 0 no match
94  * \retval 1 match
95  */
96 static int DetectFileextMatch (DetectEngineThreadCtx *det_ctx,
97  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
98 {
99  SCEnter();
100  int ret = 0;
101 
102  DetectFileextData *fileext = (DetectFileextData *)m;
103 
104  if (file->name == NULL)
105  SCReturnInt(0);
106 
107  if (file->name_len <= fileext->len)
108  SCReturnInt(0);
109 
110  int offset = file->name_len - fileext->len;
111 
112  /* fileext->ext is already in lowercase, as SCMemcmpLowercase requires */
113  if (file->name[offset - 1] == '.' &&
114  SCMemcmpLowercase(fileext->ext, file->name + offset, fileext->len) == 0)
115  {
116  if (!(fileext->flags & DETECT_CONTENT_NEGATED)) {
117  ret = 1;
118  SCLogDebug("File ext found");
119  }
120  } else if (fileext->flags & DETECT_CONTENT_NEGATED) {
121  SCLogDebug("negated match");
122  ret = 1;
123  }
124 
125  SCReturnInt(ret);
126 }
127 
128 /**
129  * \brief This function is used to parse fileet
130  *
131  * \param de_ctx Pointer to the detection engine context
132  * \param str Pointer to the fileext value string
133  *
134  * \retval pointer to DetectFileextData on success
135  * \retval NULL on failure
136  */
137 static DetectFileextData *DetectFileextParse (DetectEngineCtx *de_ctx, const char *str, bool negate)
138 {
139  DetectFileextData *fileext = NULL;
140 
141  /* We have a correct filename option */
142  fileext = SCMalloc(sizeof(DetectFileextData));
143  if (unlikely(fileext == NULL))
144  goto error;
145 
146  memset(fileext, 0x00, sizeof(DetectFileextData));
147 
148  if (DetectContentDataParse("fileext", str, &fileext->ext, &fileext->len) == -1) {
149  goto error;
150  }
151  uint16_t u;
152  for (u = 0; u < fileext->len; u++)
153  fileext->ext[u] = tolower(fileext->ext[u]);
154 
155  if (negate) {
156  fileext->flags |= DETECT_CONTENT_NEGATED;
157  }
158 
159  SCLogDebug("flags %02X", fileext->flags);
160  if (fileext->flags & DETECT_CONTENT_NEGATED) {
161  SCLogDebug("negated fileext");
162  }
163 
164 #ifdef DEBUG
165  if (SCLogDebugEnabled()) {
166  char *ext = SCMalloc(fileext->len + 1);
167  if (ext != NULL) {
168  memcpy(ext, fileext->ext, fileext->len);
169  ext[fileext->len] = '\0';
170  SCLogDebug("will look for fileext %s", ext);
171  SCFree(ext);
172  }
173  }
174 #endif
175 
176  return fileext;
177 
178 error:
179  if (fileext != NULL)
180  DetectFileextFree(de_ctx, fileext);
181  return NULL;
182 
183 }
184 
185 /**
186  * \brief this function is used to add the parsed "id" option
187  * into the current signature
188  *
189  * \param de_ctx pointer to the Detection Engine Context
190  * \param s pointer to the Current Signature
191  * \param idstr pointer to the user provided "id" option
192  *
193  * \retval 0 on Success
194  * \retval -1 on Failure
195  */
196 static int DetectFileextSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
197 {
198  DetectFileextData *fileext= NULL;
199  SigMatch *sm = NULL;
200 
201  fileext = DetectFileextParse(de_ctx, str, s->init_data->negated);
202  if (fileext == NULL)
203  goto error;
204 
205  /* Okay so far so good, lets get this into a SigMatch
206  * and put it in the Signature. */
207  sm = SigMatchAlloc();
208  if (sm == NULL)
209  goto error;
210 
211  sm->type = DETECT_FILEEXT;
212  sm->ctx = (void *)fileext;
213 
214  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
215 
217  return 0;
218 
219 error:
220  if (fileext != NULL)
221  DetectFileextFree(de_ctx, fileext);
222  if (sm != NULL)
223  SCFree(sm);
224  return -1;
225 
226 }
227 
228 /**
229  * \brief this function will free memory associated with DetectFileextData
230  *
231  * \param fileext pointer to DetectFileextData
232  */
233 static void DetectFileextFree(DetectEngineCtx *de_ctx, void *ptr)
234 {
235  if (ptr != NULL) {
236  DetectFileextData *fileext = (DetectFileextData *)ptr;
237  if (fileext->ext != NULL)
238  SCFree(fileext->ext);
239  SCFree(fileext);
240  }
241 }
242 
243 #ifdef UNITTESTS /* UNITTESTS */
244 
245 /**
246  * \test DetectFileextTestParse01
247  */
248 static int DetectFileextTestParse01 (void)
249 {
250  DetectFileextData *dfd = DetectFileextParse(NULL, "doc", false);
251  if (dfd != NULL) {
252  DetectFileextFree(NULL, dfd);
253  return 1;
254  }
255  return 0;
256 }
257 
258 /**
259  * \test DetectFileextTestParse02
260  */
261 static int DetectFileextTestParse02 (void)
262 {
263  int result = 0;
264 
265  DetectFileextData *dfd = DetectFileextParse(NULL, "tar.gz", false);
266  if (dfd != NULL) {
267  if (dfd->len == 6 && memcmp(dfd->ext, "tar.gz", 6) == 0) {
268  result = 1;
269  }
270 
271  DetectFileextFree(NULL, dfd);
272  return result;
273  }
274  return 0;
275 }
276 
277 /**
278  * \test DetectFileextTestParse03
279  */
280 static int DetectFileextTestParse03 (void)
281 {
282  int result = 0;
283 
284  DetectFileextData *dfd = DetectFileextParse(NULL, "pdf", false);
285  if (dfd != NULL) {
286  if (dfd->len == 3 && memcmp(dfd->ext, "pdf", 3) == 0) {
287  result = 1;
288  }
289 
290  DetectFileextFree(NULL, dfd);
291  return result;
292  }
293  return 0;
294 }
295 
296 #endif /* UNITTESTS */
297 
298 /**
299  * \brief this function registers unit tests for DetectFileext
300  */
301 void DetectFileextRegisterTests(void)
302 {
303 #ifdef UNITTESTS /* UNITTESTS */
304  UtRegisterTest("DetectFileextTestParse01", DetectFileextTestParse01);
305  UtRegisterTest("DetectFileextTestParse02", DetectFileextTestParse02);
306  UtRegisterTest("DetectFileextTestParse03", DetectFileextTestParse03);
307 #endif /* UNITTESTS */
308 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
detect-engine.h
DetectFileextData_
Definition: detect-fileext.h:29
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1209
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:1390
DetectFileextRegister
void DetectFileextRegister(void)
Registration function for keyword: fileext.
Definition: detect-fileext.c:64
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1186
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1203
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
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:284
m
SCMutex m
Definition: flow-hash.h:3
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
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:1009
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:1207
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1398
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
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:484
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:320
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:314
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:78
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:182
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:832
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:541
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:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
DetectFileextData_::flags
uint32_t flags
Definition: detect-fileext.h:32
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:283
DETECT_FILEEXT
@ DETECT_FILEEXT
Definition: detect-engine-register.h:183
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:1201
app-layer.h