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 
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-smtp.h"
46 
47 #include "util-unittest.h"
48 #include "util-unittest-helper.h"
49 #include "util-profiling.h"
50 #include "util-validate.h"
51 
52 /**
53  * \brief Inspect the file inspecting keywords.
54  *
55  * \param tv thread vars
56  * \param det_ctx detection engine thread ctx
57  * \param f flow
58  * \param s signature to inspect
59  *
60  * \retval 0 no match
61  * \retval 1 match
62  * \retval 2 can't match
63  * \retval 3 can't match filestore signature
64  */
65 static uint8_t DetectFileInspect(DetectEngineThreadCtx *det_ctx, Flow *f, const Signature *s,
66  const SigMatchData *smd, uint8_t flags, FileContainer *ffc)
67 {
68  uint8_t r = 0;
69  int match = 0;
70  int store_r = 0;
71 
72  SCLogDebug("file inspection... %p", ffc);
73 
74  for (File *file = ffc->head; file != NULL; file = file->next) {
75  SCLogDebug("file");
76 
77  if (file->state == FILE_STATE_NONE) {
78  SCLogDebug("file state FILE_STATE_NONE");
79  continue;
80  }
81 
82  if ((s->file_flags & FILE_SIG_NEED_FILENAME) && file->name == NULL) {
83  SCLogDebug("sig needs filename, but we don't have any");
85  continue;
86  }
87 
88  uint64_t file_size = FileDataSize(file);
89  if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
90  SCLogDebug("sig needs file content, but we don't have any");
92  continue;
93  }
94 
95  if ((s->file_flags & FILE_SIG_NEED_FILECONTENT) && file_size == 0) {
96  SCLogDebug("sig needs file content, but we don't have any");
98  continue;
99  }
100 
101  if ((s->file_flags & FILE_SIG_NEED_MD5) && (!(file->flags & FILE_MD5))) {
102  SCLogDebug("sig needs file md5, but we don't have any");
104  continue;
105  }
106 
107  if ((s->file_flags & FILE_SIG_NEED_SHA1) && (!(file->flags & FILE_SHA1))) {
108  SCLogDebug("sig needs file sha1, but we don't have any");
110  continue;
111  }
112 
113  if ((s->file_flags & FILE_SIG_NEED_SHA256) && (!(file->flags & FILE_SHA256))) {
114  SCLogDebug("sig needs file sha256, but we don't have any");
116  continue;
117  }
118 
119  if ((s->file_flags & FILE_SIG_NEED_SIZE) && file->state < FILE_STATE_CLOSED) {
120  SCLogDebug("sig needs filesize, but state < FILE_STATE_CLOSED");
122  continue;
123  }
124 
125  /* run the file match functions. */
126  while (1) {
127  SCLogDebug("smd %p", smd);
128 
129  if (sigmatch_table[smd->type].FileMatch != NULL) {
131  match = sigmatch_table[smd->type].FileMatch(det_ctx, f, flags, file, s, smd->ctx);
132  KEYWORD_PROFILING_END(det_ctx, smd->type, (match > 0));
133  if (match == 0) {
135  break;
136  } else if (smd->is_last) {
138  break;
139  }
140  }
141  if (smd->is_last)
142  break;
143  smd++;
144  }
145 
146  /* continue inspection for other files as we may want to store
147  * those as well. We'll return 1 (match) regardless of their
148  * results though */
151 
152  /* continue, this file may (or may not) be unable to match
153  * maybe we have more that can :) */
154  }
155 
157  SCLogDebug("stored MATCH, current file NOMATCH");
159  }
160 
161  if (store_r == DETECT_ENGINE_INSPECT_SIG_MATCH)
163  SCReturnInt(r);
164 }
165 
166 /**
167  * \brief Inspect the file inspecting keywords against the state
168  *
169  * \param det_ctx detection engine thread ctx
170  * \param f flow
171  * \param s signature to inspect
172  * \param alstate state
173  * \param flags direction flag
174  *
175  * \retval 0 no match
176  * \retval 1 match
177  * \retval 2 can't match
178  * \retval 3 can't match filestore signature
179  *
180  * \note flow is not locked at this time
181  */
183  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
184  uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
185 {
186  SCEnter();
187  DEBUG_VALIDATE_BUG_ON(f->alstate != alstate);
188 
189  const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
190  AppLayerGetFileState files = AppLayerParserGetTxFiles(f, alstate, tx, direction);
191  FileContainer *ffc = files.fc;
192  SCLogDebug("tx %p tx_id %" PRIu64 " ffc %p ffc->head %p sid %u", tx, tx_id, ffc,
193  ffc ? ffc->head : NULL, s->id);
194  if (ffc == NULL) {
196  } else if (ffc->head == NULL) {
198  }
199 
201  uint8_t match = DetectFileInspect(det_ctx, f, s, engine->smd, flags, ffc);
202  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
204  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
205  SCLogDebug("sid %u can't match on this transaction", s->id);
207  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES) {
208  SCLogDebug("sid %u can't match on this transaction (file sig)", s->id);
210  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
211  SCLogDebug("match with more files ahead");
212  r = match;
213  }
214 
215  SCReturnInt(r);
216 }
DetectEngineAppInspectionEngine_
Definition: detect.h:427
FileContainer_
Definition: util-file.h:113
detect-engine.h
FILE_SIG_NEED_SHA1
#define FILE_SIG_NEED_SHA1
Definition: detect.h:319
stream-tcp.h
FILE_SHA256
#define FILE_SHA256
Definition: util-file.h:52
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SigMatchData_::is_last
bool is_last
Definition: detect.h:361
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:362
Flow_
Flow data structure.
Definition: flow.h:351
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1272
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
stream-tcp-reassemble.h
FILE_SIG_NEED_FILENAME
#define FILE_SIG_NEED_FILENAME
Definition: detect.h:315
SigMatchData_
Data needed for Match()
Definition: detect.h:359
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
SigMatchData_::type
uint16_t type
Definition: detect.h:360
util-unittest.h
util-unittest-helper.h
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:318
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:43
app-layer-htp.h
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:316
detect-engine-file.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
FileContainer_::head
File * head
Definition: util-file.h:114
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
app-layer-parser.h
util-profiling.h
FILE_SIG_NEED_SHA256
#define FILE_SIG_NEED_SHA256
Definition: detect.h:320
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:39
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:326
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:444
DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
Definition: detect-engine-state.h:49
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:71
File_
Definition: util-file.h:79
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:612
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
File_::next
struct File_ * next
Definition: util-file.h:92
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:37
util-validate.h
Flow_::alstate
void * alstate
Definition: flow.h:476
Signature_::id
uint32_t id
Definition: detect.h:631
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:182
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
app-layer-protos.h
FILE_SHA1
#define FILE_SHA1
Definition: util-file.h:50
FILE_SIG_NEED_FILECONTENT
#define FILE_SIG_NEED_FILECONTENT
Definition: detect.h:317
AppLayerParserGetTxFiles
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *state, void *tx, const uint8_t direction)
Definition: app-layer-parser.c:877
app-layer-smtp.h
detect-engine-dcepayload.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:103
FILE_SIG_NEED_SIZE
#define FILE_SIG_NEED_SIZE
Definition: detect.h:321
FILE_STATE_NONE
@ FILE_STATE_NONE
Definition: util-file.h:69