suricata
detect-engine-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2011 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  */
23 
24 #include "suricata-common.h"
25 
26 #include "decode.h"
27 
28 #include "detect.h"
29 #include "detect-engine.h"
30 #include "detect-parse.h"
31 #include "detect-engine-state.h"
32 
33 #include "detect-filestore.h"
34 
36 #include "detect-engine-file.h"
37 
38 #include "stream-tcp.h"
39 #include "stream-tcp-private.h"
40 #include "stream-tcp-reassemble.h"
41 
42 #include "app-layer-parser.h"
43 #include "app-layer-protos.h"
44 #include "app-layer-htp.h"
45 #include "app-layer-smb.h"
47 #include "app-layer-dcerpc.h"
48 #include "app-layer-smtp.h"
49 
50 #include "util-unittest.h"
51 #include "util-unittest-helper.h"
52 #include "util-profiling.h"
53 
54 
55 /**
56  * \brief Inspect the file inspecting keywords.
57  *
58  * \param tv thread vars
59  * \param det_ctx detection engine thread ctx
60  * \param f flow
61  * \param s signature to inspect
62  *
63  * \retval 0 no match
64  * \retval 1 match
65  * \retval 2 can't match
66  * \retval 3 can't match filestore signature
67  */
68 static int DetectFileInspect(ThreadVars *tv, DetectEngineThreadCtx *det_ctx,
69  Flow *f, const Signature *s, const SigMatchData *smd,
70  uint8_t flags, FileContainer *ffc)
71 {
72  int r = 0;
73  int match = 0;
74  int store_r = 0;
75 
76  SCLogDebug("file inspection... %p", ffc);
77 
78  if (ffc != NULL) {
79  File *file = ffc->head;
80  for (; file != NULL; file = file->next) {
81  SCLogDebug("file");
82 
83  if (file->state == FILE_STATE_NONE) {
84  SCLogDebug("file state FILE_STATE_NONE");
85  continue;
86  }
87 
88  if (file->txid < det_ctx->tx_id) {
89  SCLogDebug("file->txid < det_ctx->tx_id == %"PRIu64" < %"PRIu64, file->txid, det_ctx->tx_id);
90  continue;
91  }
92 
93  if (file->txid > det_ctx->tx_id) {
94  SCLogDebug("file->txid > det_ctx->tx_id == %"PRIu64" > %"PRIu64, file->txid, det_ctx->tx_id);
95  break;
96  }
97 
98  if ((s->file_flags & FILE_SIG_NEED_FILENAME) && file->name == NULL) {
99  SCLogDebug("sig needs filename, but we don't have any");
101  continue;
102  }
103 
104  uint64_t file_size = FileDataSize(file);
105  if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
106  SCLogDebug("sig needs file content, but we don't have any");
108  continue;
109  }
110 
111  if ((s->file_flags & FILE_SIG_NEED_FILECONTENT) && file_size == 0) {
112  SCLogDebug("sig needs file content, but we don't have any");
114  continue;
115  }
116 
117  if ((s->file_flags & FILE_SIG_NEED_MD5) && (!(file->flags & FILE_MD5))) {
118  SCLogDebug("sig needs file md5, but we don't have any");
120  continue;
121  }
122 
123  if ((s->file_flags & FILE_SIG_NEED_SHA1) && (!(file->flags & FILE_SHA1))) {
124  SCLogDebug("sig needs file sha1, but we don't have any");
126  continue;
127  }
128 
129  if ((s->file_flags & FILE_SIG_NEED_SHA256) && (!(file->flags & FILE_SHA256))) {
130  SCLogDebug("sig needs file sha256, but we don't have any");
132  continue;
133  }
134 
135  if ((s->file_flags & FILE_SIG_NEED_SIZE) && file->state < FILE_STATE_CLOSED) {
136  SCLogDebug("sig needs filesize, but state < FILE_STATE_CLOSED");
138  continue;
139  }
140 
141  /* run the file match functions. */
142  while (1) {
143  SCLogDebug("smd %p", smd);
144 
145  if (sigmatch_table[smd->type].FileMatch != NULL) {
147  match = sigmatch_table[smd->type].
148  FileMatch(tv, det_ctx, f, flags, file, s, smd->ctx);
149  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
150  if (match == 0) {
152  break;
153  } else if (smd->is_last) {
155  break;
156  }
157  }
158  if (smd->is_last)
159  break;
160  smd++;
161  }
162 
163  /* continue inspection for other files as we may want to store
164  * those as well. We'll return 1 (match) regardless of their
165  * results though */
168 
169  /* if this is a filestore sig, and the sig can't match
170  * return 3 so we can distinguish */
173 
174  /* continue, this file may (or may not) be unable to match
175  * maybe we have more that can :) */
176  }
177  } else {
178  /* if we have a filestore sm with a scope > file (so tx, ssn) we
179  * run it here */
180  if (smd != NULL && smd->is_last && smd->type == DETECT_FILESTORE &&
181  smd->ctx != NULL)
182  {
184  if (fd->scope > FILESTORE_SCOPE_DEFAULT) {
186  match = sigmatch_table[smd->type].
187  FileMatch(tv, det_ctx, f, flags, /* no file */NULL, s, smd->ctx);
188  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
189 
190  if (match == 1) {
192  }
193  }
194  }
195  }
196 
198  SCLogDebug("stored MATCH, current file NOMATCH");
200  }
201 
202  if (store_r == DETECT_ENGINE_INSPECT_SIG_MATCH)
204  SCReturnInt(r);
205 }
206 
207 /**
208  * \brief Inspect the file inspecting keywords against the state
209  *
210  * \param tv thread vars
211  * \param det_ctx detection engine thread ctx
212  * \param f flow
213  * \param s signature to inspect
214  * \param alstate state
215  * \param flags direction flag
216  *
217  * \retval 0 no match
218  * \retval 1 match
219  * \retval 2 can't match
220  * \retval 3 can't match filestore signature
221  *
222  * \note flow is not locked at this time
223  */
225  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
226  const Signature *s, const SigMatchData *smd,
227  Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
228 {
229  SCEnter();
230 
231  if (alstate == NULL) {
233  }
234 
235  const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
236  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, alstate, direction);
237  if (ffc == NULL || ffc->head == NULL) {
239  }
240 
242  int match = DetectFileInspect(tv, det_ctx, f, s, smd, flags, ffc);
243  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
245  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
246  SCLogDebug("sid %u can't match on this transaction", s->id);
249  SCLogDebug("sid %u can't match on this transaction (filestore sig)", s->id);
251  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
252  SCLogDebug("match with more files ahead");
253  r = match;
254  }
255 
256  SCReturnInt(r);
257 }
#define SIG_FLAG_FILESTORE
Definition: detect.h:241
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
#define KEYWORD_PROFILING_END(ctx, type, m)
uint32_t flags
Definition: detect.h:493
uint8_t proto
Definition: flow.h:346
uint32_t id
Definition: detect.h:525
uint8_t is_last
Definition: detect.h:335
#define FILE_SHA256
Definition: util-file.h:43
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:277
#define FILE_SIG_NEED_FILENAME
Definition: detect.h:289
Data needed for Match()
Definition: detect.h:333
Signature container.
Definition: detect.h:492
struct File_ * next
Definition: util-file.h:79
int DetectFileInspectGeneric(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
Inspect the file inspecting keywords against the state.
main detection engine ctx
Definition: detect.h:720
#define KEYWORD_PROFILING_START
int(* FileMatch)(ThreadVars *, DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1137
uint16_t flags
Definition: util-file.h:65
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Data structures and function prototypes for keeping state for the detection engine.
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:290
uint64_t txid
Definition: util-file.h:69
#define SCEnter(...)
Definition: util-debug.h:337
File * head
Definition: util-file.h:95
#define STREAM_TOCLIENT
Definition: stream.h:32
#define SCReturnInt(x)
Definition: util-debug.h:341
#define FILE_MD5
Definition: util-file.h:39
#define FILE_SIG_NEED_MD5
Definition: detect.h:292
#define FILE_SHA1
Definition: util-file.h:41
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
uint8_t type
Definition: detect.h:334
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
uint16_t tx_id
#define FILE_SIG_NEED_SHA1
Definition: detect.h:293
#define FILE_SIG_NEED_FILECONTENT
Definition: detect.h:291
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILESTORE
#define STREAM_TOSERVER
Definition: stream.h:31
#define FILE_SIG_NEED_SHA256
Definition: detect.h:294
FileState state
Definition: util-file.h:67
#define FILESTORE_SCOPE_DEFAULT
Per thread variable structure.
Definition: threadvars.h:57
AppProto alproto
application level protocol
Definition: flow.h:407
uint8_t file_flags
Definition: detect.h:506
FileContainer * AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t direction)
Flow data structure.
Definition: flow.h:327
uint8_t * name
Definition: util-file.h:75
SigMatchCtx * ctx
Definition: detect.h:336
#define FILE_SIG_NEED_SIZE
Definition: detect.h:295