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"
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"
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 */
65static 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, 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}
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
struct AppLayerGetFileState AppLayerGetFileState
uint8_t flags
Definition decode-gre.h:0
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.
Data structures and function prototypes for keeping state for the detection engine.
#define DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
SigTableElmt * sigmatch_table
#define FILE_SIG_NEED_SIZE
Definition detect.h:327
#define FILE_SIG_NEED_FILENAME
Definition detect.h:321
#define FILE_SIG_NEED_MAGIC
Definition detect.h:322
#define FILE_SIG_NEED_MD5
Definition detect.h:324
#define FILE_SIG_NEED_SHA1
Definition detect.h:325
#define FILE_SIG_NEED_SHA256
Definition detect.h:326
#define FILE_SIG_NEED_FILECONTENT
Definition detect.h:323
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
void * alstate
Definition flow.h:479
Data needed for Match()
Definition detect.h:365
bool is_last
Definition detect.h:367
SigMatchCtx * ctx
Definition detect.h:368
uint16_t type
Definition detect.h:366
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition detect.h:1429
Signature container.
Definition detect.h:668
uint8_t file_flags
Definition detect.h:684
uint32_t id
Definition detect.h:713
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition util-file.c:309
#define FILE_SHA256
Definition util-file.h:52
#define FILE_MD5
Definition util-file.h:48
@ FILE_STATE_NONE
Definition util-file.h:69
@ FILE_STATE_CLOSED
Definition util-file.h:71
#define FILE_SHA1
Definition util-file.h:50
#define KEYWORD_PROFILING_END(ctx, type, m)
#define KEYWORD_PROFILING_START
#define DEBUG_VALIDATE_BUG_ON(exp)