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"
46 #include "app-layer-smtp.h"
47 
48 #include "util-unittest.h"
49 #include "util-unittest-helper.h"
50 #include "util-profiling.h"
51 #include "util-validate.h"
52 
53 
54 /**
55  * \brief Inspect the file inspecting keywords.
56  *
57  * \param tv thread vars
58  * \param det_ctx detection engine thread ctx
59  * \param f flow
60  * \param s signature to inspect
61  *
62  * \retval 0 no match
63  * \retval 1 match
64  * \retval 2 can't match
65  * \retval 3 can't match filestore signature
66  */
67 static int DetectFileInspect(DetectEngineThreadCtx *det_ctx, Flow *f, const Signature *s,
68  const SigMatchData *smd, uint8_t flags, FileContainer *ffc)
69 {
70  int r = 0;
71  int match = 0;
72  int store_r = 0;
73 
74  SCLogDebug("file inspection... %p", ffc);
75 
76  if (ffc != NULL) {
77  File *file = ffc->head;
78  for (; file != NULL; file = file->next) {
79  SCLogDebug("file");
80 
81  if (file->state == FILE_STATE_NONE) {
82  SCLogDebug("file state FILE_STATE_NONE");
83  continue;
84  }
85 
86  if (file->txid < det_ctx->tx_id) {
87  SCLogDebug("file->txid < det_ctx->tx_id == %"PRIu64" < %"PRIu64, file->txid, det_ctx->tx_id);
88  continue;
89  }
90 
91  if (file->txid > det_ctx->tx_id) {
92  SCLogDebug("file->txid > det_ctx->tx_id == %"PRIu64" > %"PRIu64, file->txid, det_ctx->tx_id);
93  break;
94  }
95 
96  if ((s->file_flags & FILE_SIG_NEED_FILENAME) && file->name == NULL) {
97  SCLogDebug("sig needs filename, but we don't have any");
99  continue;
100  }
101 
102  uint64_t file_size = FileDataSize(file);
103  if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
104  SCLogDebug("sig needs file content, but we don't have any");
106  continue;
107  }
108 
109  if ((s->file_flags & FILE_SIG_NEED_FILECONTENT) && file_size == 0) {
110  SCLogDebug("sig needs file content, but we don't have any");
112  continue;
113  }
114 
115  if ((s->file_flags & FILE_SIG_NEED_MD5) && (!(file->flags & FILE_MD5))) {
116  SCLogDebug("sig needs file md5, but we don't have any");
118  continue;
119  }
120 
121  if ((s->file_flags & FILE_SIG_NEED_SHA1) && (!(file->flags & FILE_SHA1))) {
122  SCLogDebug("sig needs file sha1, but we don't have any");
124  continue;
125  }
126 
127  if ((s->file_flags & FILE_SIG_NEED_SHA256) && (!(file->flags & FILE_SHA256))) {
128  SCLogDebug("sig needs file sha256, but we don't have any");
130  continue;
131  }
132 
133  if ((s->file_flags & FILE_SIG_NEED_SIZE) && file->state < FILE_STATE_CLOSED) {
134  SCLogDebug("sig needs filesize, but state < FILE_STATE_CLOSED");
136  continue;
137  }
138 
139  /* run the file match functions. */
140  while (1) {
141  SCLogDebug("smd %p", smd);
142 
143  if (sigmatch_table[smd->type].FileMatch != NULL) {
145  match = sigmatch_table[smd->type].
146  FileMatch(det_ctx, f, flags, file, s, smd->ctx);
147  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
148  if (match == 0) {
150  break;
151  } else if (smd->is_last) {
153  break;
154  }
155  }
156  if (smd->is_last)
157  break;
158  smd++;
159  }
160 
161  /* continue inspection for other files as we may want to store
162  * those as well. We'll return 1 (match) regardless of their
163  * results though */
166 
167  /* continue, this file may (or may not) be unable to match
168  * maybe we have more that can :) */
169  }
170  } else {
171  /* if we have a filestore sm with a scope > file (so tx, ssn) we
172  * run it here */
173  if (smd != NULL && smd->is_last && smd->type == DETECT_FILESTORE &&
174  smd->ctx != NULL)
175  {
177  if (fd->scope > FILESTORE_SCOPE_DEFAULT) {
179  match = sigmatch_table[smd->type].
180  FileMatch(det_ctx, f, flags, /* no file */NULL, s, smd->ctx);
181  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
182 
183  if (match == 1) {
185  }
186  }
187  }
188  }
189 
191  SCLogDebug("stored MATCH, current file NOMATCH");
193  }
194 
195  if (store_r == DETECT_ENGINE_INSPECT_SIG_MATCH)
197  SCReturnInt(r);
198 }
199 
200 /**
201  * \brief Inspect the file inspecting keywords against the state
202  *
203  * \param det_ctx detection engine thread ctx
204  * \param f flow
205  * \param s signature to inspect
206  * \param alstate state
207  * \param flags direction flag
208  *
209  * \retval 0 no match
210  * \retval 1 match
211  * \retval 2 can't match
212  * \retval 3 can't match filestore signature
213  *
214  * \note flow is not locked at this time
215  */
217  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
218  uint8_t flags, void *_alstate, void *tx, uint64_t tx_id)
219 {
220  SCEnter();
221  DEBUG_VALIDATE_BUG_ON(f->alstate != _alstate);
222 
223  const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
224  FileContainer *ffc = AppLayerParserGetFiles(f, direction);
225  if (ffc == NULL || ffc->head == NULL) {
227  }
228 
230  int match = DetectFileInspect(det_ctx, f, s, engine->smd, flags, ffc);
231  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
233  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
234  SCLogDebug("sid %u can't match on this transaction", s->id);
236  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES) {
237  SCLogDebug("sid %u can't match on this transaction (file sig)", s->id);
239  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
240  SCLogDebug("match with more files ahead");
241  r = match;
242  }
243 
244  SCReturnInt(r);
245 }
DetectEngineAppInspectionEngine_
Definition: detect.h:397
FileContainer_
Definition: util-file.h:107
detect-engine.h
FILE_SIG_NEED_SHA1
#define FILE_SIG_NEED_SHA1
Definition: detect.h:289
DetectFilestoreData_::scope
int16_t scope
Definition: detect-filestore.h:38
stream-tcp.h
FILE_SHA256
#define FILE_SHA256
Definition: util-file.h:52
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1072
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:332
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:1177
File_::state
FileState state
Definition: util-file.h:75
DETECT_FILESTORE
@ DETECT_FILESTORE
Definition: detect-engine-register.h:201
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
AppLayerParserGetFiles
FileContainer * AppLayerParserGetFiles(const Flow *f, const uint8_t direction)
Definition: app-layer-parser.c:861
stream-tcp-reassemble.h
FILE_SIG_NEED_FILENAME
#define FILE_SIG_NEED_FILENAME
Definition: detect.h:285
SigMatchData_
Data needed for Match()
Definition: detect.h:329
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:69
util-unittest.h
util-unittest-helper.h
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:83
FILE_SIG_NEED_MD5
#define FILE_SIG_NEED_MD5
Definition: detect.h:288
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:44
app-layer-htp.h
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:286
detect-engine-file.h
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
FileContainer_::head
File * head
Definition: util-file.h:108
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
SigMatchData_::type
uint8_t type
Definition: detect.h:330
app-layer-parser.h
util-profiling.h
FILE_SIG_NEED_SHA256
#define FILE_SIG_NEED_SHA256
Definition: detect.h:290
app-layer-dcerpc-common.h
stream-tcp-private.h
File_::name
uint8_t * name
Definition: util-file.h:82
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-filestore.h
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:40
DetectFileInspectGeneric
int DetectFileInspectGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *_alstate, void *tx, uint64_t tx_id)
Inspect the file inspecting keywords against the state.
Definition: detect-engine-file.c:216
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:294
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:414
File_::flags
uint16_t flags
Definition: util-file.h:73
DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
Definition: detect-engine-state.h:50
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:64
File_
Definition: util-file.h:72
flags
uint8_t flags
Definition: decode-gre.h:0
SigMatchData_::is_last
uint8_t is_last
Definition: detect.h:331
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:532
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
File_::next
struct File_ * next
Definition: util-file.h:86
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
FILE_MD5
#define FILE_MD5
Definition: util-file.h:48
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
util-validate.h
DetectFilestoreData_
Definition: detect-filestore.h:36
Flow_::alstate
void * alstate
Definition: flow.h:486
Signature_::id
uint32_t id
Definition: detect.h:551
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
app-layer-protos.h
FILE_SHA1
#define FILE_SHA1
Definition: util-file.h:50
File_::txid
uint64_t txid
Definition: util-file.h:77
FILE_SIG_NEED_FILECONTENT
#define FILE_SIG_NEED_FILECONTENT
Definition: detect.h:287
FILESTORE_SCOPE_DEFAULT
#define FILESTORE_SCOPE_DEFAULT
Definition: detect-filestore.h:32
app-layer-smtp.h
detect-engine-dcepayload.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
FILE_SIG_NEED_SIZE
#define FILE_SIG_NEED_SIZE
Definition: detect.h:291
FILE_STATE_NONE
@ FILE_STATE_NONE
Definition: util-file.h:62