suricata
detect-engine-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 
35 #include "detect-engine-file.h"
36 
37 #include "stream-tcp.h"
38 #include "stream-tcp-private.h"
39 #include "stream-tcp-reassemble.h"
40 
41 #include "app-layer-parser.h"
42 #include "app-layer-protos.h"
43 #include "app-layer-htp.h"
44 #include "app-layer-smtp.h"
45 
46 #include "util-unittest.h"
47 #include "util-unittest-helper.h"
48 #include "util-profiling.h"
49 #include "util-validate.h"
50 
51 /**
52  * \brief Inspect the file inspecting keywords.
53  *
54  * \param tv thread vars
55  * \param det_ctx detection engine thread ctx
56  * \param f flow
57  * \param s signature to inspect
58  *
59  * \retval 0 no match
60  * \retval 1 match
61  * \retval 2 can't match
62  * \retval 3 can't match filestore signature
63  */
64 static uint8_t DetectFileInspect(DetectEngineThreadCtx *det_ctx, Flow *f, const Signature *s,
65  const SigMatchData *smd, uint8_t flags, FileContainer *ffc)
66 {
67  uint8_t r = 0;
68  int match = 0;
69  int store_r = 0;
70 
71  SCLogDebug("file inspection... %p", ffc);
72 
73  for (File *file = ffc->head; file != NULL; file = file->next) {
74  SCLogDebug("file");
75 
76  if (file->state == FILE_STATE_NONE) {
77  SCLogDebug("file state FILE_STATE_NONE");
78  continue;
79  }
80 
81  if ((s->file_flags & FILE_SIG_NEED_FILENAME) && file->name == NULL) {
82  SCLogDebug("sig needs filename, but we don't have any");
84  continue;
85  }
86 
87  uint64_t file_size = FileDataSize(file);
88  if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
89  SCLogDebug("sig needs file content, but we don't have any");
91  continue;
92  }
93 
94  if ((s->file_flags & FILE_SIG_NEED_FILECONTENT) && file_size == 0) {
95  SCLogDebug("sig needs file content, but we don't have any");
97  continue;
98  }
99 
100  if ((s->file_flags & FILE_SIG_NEED_MD5) && (!(file->flags & FILE_MD5))) {
101  SCLogDebug("sig needs file md5, but we don't have any");
103  continue;
104  }
105 
106  if ((s->file_flags & FILE_SIG_NEED_SHA1) && (!(file->flags & FILE_SHA1))) {
107  SCLogDebug("sig needs file sha1, but we don't have any");
109  continue;
110  }
111 
112  if ((s->file_flags & FILE_SIG_NEED_SHA256) && (!(file->flags & FILE_SHA256))) {
113  SCLogDebug("sig needs file sha256, but we don't have any");
115  continue;
116  }
117 
118  if ((s->file_flags & FILE_SIG_NEED_SIZE) && file->state < FILE_STATE_CLOSED) {
119  SCLogDebug("sig needs filesize, but state < FILE_STATE_CLOSED");
121  continue;
122  }
123 
124  /* run the file match functions. */
125  while (1) {
126  SCLogDebug("smd %p", smd);
127 
128  if (sigmatch_table[smd->type].FileMatch != NULL) {
130  match = sigmatch_table[smd->type].FileMatch(det_ctx, f, flags, file, s, smd->ctx);
131  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
132  if (match == 0) {
134  break;
135  } else if (smd->is_last) {
137  break;
138  }
139  }
140  if (smd->is_last)
141  break;
142  smd++;
143  }
144 
145  /* continue inspection for other files as we may want to store
146  * those as well. We'll return 1 (match) regardless of their
147  * results though */
150 
151  /* continue, this file may (or may not) be unable to match
152  * maybe we have more that can :) */
153  }
154 
156  SCLogDebug("stored MATCH, current file NOMATCH");
158  }
159 
160  if (store_r == DETECT_ENGINE_INSPECT_SIG_MATCH)
162  SCReturnInt(r);
163 }
164 
165 /**
166  * \brief Inspect the file inspecting keywords against the state
167  *
168  * \param det_ctx detection engine thread ctx
169  * \param f flow
170  * \param s signature to inspect
171  * \param alstate state
172  * \param flags direction flag
173  *
174  * \retval 0 no match
175  * \retval 1 match
176  * \retval 2 can't match
177  * \retval 3 can't match filestore signature
178  *
179  * \note flow is not locked at this time
180  */
182  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
183  uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
184 {
185  SCEnter();
186  DEBUG_VALIDATE_BUG_ON(f->alstate != alstate);
187 
188  const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
189  AppLayerGetFileState files = AppLayerParserGetTxFiles(f, tx, direction);
190  FileContainer *ffc = files.fc;
191  SCLogDebug("tx %p tx_id %" PRIu64 " ffc %p ffc->head %p sid %u", tx, tx_id, ffc,
192  ffc ? ffc->head : NULL, s->id);
193  if (ffc == NULL) {
195  } else if (ffc->head == NULL) {
197  }
198 
200  uint8_t match = DetectFileInspect(det_ctx, f, s, engine->smd, flags, ffc);
201  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
203  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
204  SCLogDebug("sid %u can't match on this transaction", s->id);
206  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES) {
207  SCLogDebug("sid %u can't match on this transaction (file sig)", s->id);
209  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
210  SCLogDebug("match with more files ahead");
211  r = match;
212  }
213 
214  SCReturnInt(r);
215 }
DetectEngineAppInspectionEngine_
Definition: detect.h:416
FileContainer_
Definition: util-file.h:37
detect-engine.h
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
FILE_SIG_NEED_SHA1
#define FILE_SIG_NEED_SHA1
Definition: detect.h:325
stream-tcp.h
FILE_SHA256
#define FILE_SHA256
Definition: util-file.h:119
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SigMatchData_::is_last
bool is_last
Definition: detect.h:367
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:368
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:1438
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:937
stream-tcp-reassemble.h
FILE_SIG_NEED_FILENAME
#define FILE_SIG_NEED_FILENAME
Definition: detect.h:321
SigMatchData_
Data needed for Match()
Definition: detect.h:365
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
SigMatchData_::type
uint16_t type
Definition: detect.h:366
util-unittest.h
util-unittest-helper.h
AppLayerParserGetTxFiles
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
Definition: app-layer-parser.c:897
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:64
FILE_SIG_NEED_MD5
#define FILE_SIG_NEED_MD5
Definition: detect.h:324
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:46
app-layer-htp.h
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1252
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:322
detect-engine-file.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
FileContainer_::head
File * head
Definition: util-file.h:38
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:41
app-layer-parser.h
util-profiling.h
FILE_SIG_NEED_SHA256
#define FILE_SIG_NEED_SHA256
Definition: detect.h:326
stream-tcp-private.h
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:42
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:308
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:439
DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
Definition: detect-engine-state.h:52
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:138
File_
Definition: util-file.h:146
flags
uint8_t flags
Definition: decode-gre.h:0
AppLayerGetFileState
Definition: util-file.h:44
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:688
File_::next
struct File_ * next
Definition: util-file.h:159
FILE_MD5
#define FILE_MD5
Definition: util-file.h:115
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:40
util-validate.h
Flow_::alstate
void * alstate
Definition: flow.h:472
Signature_::id
uint32_t id
Definition: detect.h:717
DetectFileInspectGeneric
uint8_t 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:181
detect-parse.h
Signature_
Signature container.
Definition: detect.h:672
app-layer-protos.h
FILE_SHA1
#define FILE_SHA1
Definition: util-file.h:117
FILE_SIG_NEED_FILECONTENT
#define FILE_SIG_NEED_FILECONTENT
Definition: detect.h:323
app-layer-smtp.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
FILE_SIG_NEED_SIZE
#define FILE_SIG_NEED_SIZE
Definition: detect.h:327
FILE_STATE_NONE
@ FILE_STATE_NONE
Definition: util-file.h:136