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