suricata
util-file.h
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
25#ifndef SURICATA_UTIL_FILE_H
26#define SURICATA_UTIL_FILE_H
27
28#include "conf.h"
30#include "flow.h"
31
32/* Hack: Pulling rust.h to get the SCSha256 causes all sorts of problems with
33 * header include orders, which is something we'll have to resolve as we provide
34 * more functionality via Rust. But this lets me continue with replacing nss
35 * without fighting the headers at this time. */
36typedef struct SCSha256 SCSha256;
37#define SC_SHA256_LEN 32
38
39typedef struct SCSha1 SCSha1;
40#define SC_SHA1_LEN 20
41
42typedef struct SCMd5 SCMd5;
43#define SC_MD5_LEN 16
44
45#define FILE_TRUNCATED BIT_U16(0)
46#define FILE_NOMAGIC BIT_U16(1)
47#define FILE_NOMD5 BIT_U16(2)
48#define FILE_MD5 BIT_U16(3)
49#define FILE_NOSHA1 BIT_U16(4)
50#define FILE_SHA1 BIT_U16(5)
51#define FILE_NOSHA256 BIT_U16(6)
52#define FILE_SHA256 BIT_U16(7)
53#define FILE_LOGGED BIT_U16(8)
54#define FILE_NOSTORE BIT_U16(9)
55#define FILE_STORE BIT_U16(10)
56#define FILE_STORED BIT_U16(11)
57#define FILE_NOTRACK BIT_U16(12) /**< track size of file */
58#define FILE_USE_DETECT BIT_U16(13) /**< use content_inspected tracker */
59#define FILE_HAS_GAPS BIT_U16(15)
60
61// to be used instead of PATH_MAX which depends on the OS
62#define SC_FILENAME_MAX 4096
63
64#define FILEDATA_CONTENT_LIMIT 100000
65#define FILEDATA_CONTENT_INSPECT_MIN_SIZE 32768
66#define FILEDATA_CONTENT_INSPECT_WINDOW 4096
67
68typedef enum FileState_ {
69 FILE_STATE_NONE = 0, /**< no state */
70 FILE_STATE_OPENED, /**< flow file is opened */
71 FILE_STATE_CLOSED, /**< flow file is completed,
72 there will be no more data. */
73 FILE_STATE_TRUNCATED, /**< flow file is not complete, but
74 there will be no more data. */
75 FILE_STATE_ERROR, /**< file is in an error state */
78
79typedef struct File_ {
80 uint16_t flags;
81 uint16_t name_len;
84 uint32_t file_track_id; /**< id used by protocol parser */
85 uint32_t file_store_id; /**< id used in store file name file.<id> */
86 int fd; /**< file descriptor for filestore, not
87 open if equal to -1 */
88 uint8_t *name;
89#ifdef HAVE_MAGIC
90 char *magic;
91#endif
92 struct File_ *next;
94 uint8_t md5[SC_MD5_LEN];
96 uint8_t sha1[SC_SHA1_LEN];
99 uint64_t content_inspected; /**< used in pruning if FILE_USE_DETECT
100 * flag is set */
102 uint64_t size;
105 uint64_t start;
106 uint64_t end;
107
108 uint32_t *sid; /* signature id of a rule that triggered the filestore event */
109 uint32_t sid_cnt;
110 uint32_t sid_max;
112
117
120
122
124
125/**
126 * \brief Open a new File
127 *
128 * \param ffc flow container
129 * \param sbcfg buffer config
130 * \param name filename character array
131 * \param name_len filename len
132 * \param data initial data
133 * \param data_len initial data len
134 * \param flags open flags
135 *
136 * \retval ff flowfile object
137 *
138 * \note filename is not a string, so it's not nul terminated.
139 *
140 * If flags contains the FILE_USE_DETECT bit, the pruning code will
141 * consider not just the content_stored tracker, but also content_inspected.
142 * It's the responsibility of the API user to make sure this tracker is
143 * properly updated.
144 */
146 uint32_t track_id, const uint8_t *name, uint16_t name_len,
147 const uint8_t *data, uint32_t data_len, uint16_t flags);
148
149/**
150 * \brief Close a File
151 *
152 * \param ffc the container
153 * \param data final data if any
154 * \param data_len data len if any
155 * \param flags flags
156 *
157 * \retval 0 ok
158 * \retval -1 error
159 */
160int FileCloseFile(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data,
161 uint32_t data_len, uint16_t flags);
162int FileCloseFileById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
163 const uint8_t *data, uint32_t data_len, uint16_t flags);
164int FileCloseFilePtr(File *ff, const StreamingBufferConfig *sbcfg, const uint8_t *data,
165 uint32_t data_len, uint16_t flags);
166
167/**
168 * \brief Store a chunk of file data in the flow. The open "flowfile"
169 * will be used.
170 *
171 * \param ffc the container
172 * \param data data chunk
173 * \param data_len data chunk len
174 *
175 * \retval 0 ok
176 * \retval -1 error
177 */
178int FileAppendData(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data,
179 uint32_t data_len);
180int FileAppendDataById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
181 const uint8_t *data, uint32_t data_len);
182int FileAppendGAPById(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id,
183 const uint8_t *data, uint32_t data_len);
184
185void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min);
186
187/**
188 * \brief Sets the offset range for a file.
189 *
190 * \param ffc the container
191 * \param start start offset
192 * \param end end offset
193 *
194 * \retval 0 ok
195 * \retval -1 error
196 */
197int FileSetRange(FileContainer *, uint64_t start, uint64_t end);
198
199/**
200 * \brief Tag a file for storing
201 *
202 * \param ff The file to store
203 */
204int FileStore(File *);
205
206/**
207 * \brief disable file storing for a transaction
208 *
209 * \param f flow
210 * \param direction STREAM_TOSERVER or STREAM_TOCLIENT
211 * \param tx transaction pointer
212 * \param tx_id transaction id
213 */
214void FileDisableStoringForTransaction(Flow *f, const uint8_t direction, void *tx, uint64_t tx_id);
215
216void FileForceFilestoreEnable(void);
217int FileForceFilestore(void);
218void FileReassemblyDepthEnable(uint32_t size);
219uint32_t FileReassemblyDepth(void);
220
221void FileForceMagicEnable(void);
222int FileForceMagic(void);
223
224void FileForceMd5Enable(void);
225int FileForceMd5(void);
226
227void FileForceSha1Enable(void);
228int FileForceSha1(void);
229
230void FileForceSha256Enable(void);
231int FileForceSha256(void);
232
233void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction);
234
236
237void FileForceTrackingEnable(void);
238
239void FileStoreFileById(FileContainer *fc, uint32_t);
240
241uint64_t FileDataSize(const File *file);
242uint64_t FileTrackedSize(const File *file);
243
244uint16_t FileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction);
245uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);
246
247#ifdef DEBUG
248void FilePrintFlags(const File *file);
249#else
250#define FilePrintFlags(file)
251#endif
252
253void FilesPrune(FileContainer *fc, const StreamingBufferConfig *sbcfg, const bool trunc);
254
255#endif /* SURICATA_UTIL_FILE_H */
uint8_t flags
Definition decode-gre.h:0
uint8_t * name
Definition util-file.h:88
uint16_t name_len
Definition util-file.h:81
uint32_t inspect_window
Definition util-file.h:103
uint32_t sid_max
Definition util-file.h:110
StreamingBuffer * sb
Definition util-file.h:83
uint64_t size
Definition util-file.h:102
uint8_t md5[SC_MD5_LEN]
Definition util-file.h:94
uint64_t end
Definition util-file.h:106
uint32_t sid_cnt
Definition util-file.h:109
uint16_t flags
Definition util-file.h:80
FileState state
Definition util-file.h:82
uint8_t sha256[SC_SHA256_LEN]
Definition util-file.h:98
SCMd5 * md5_ctx
Definition util-file.h:93
uint32_t inspect_min_size
Definition util-file.h:104
uint32_t * sid
Definition util-file.h:108
SCSha1 * sha1_ctx
Definition util-file.h:95
int fd
Definition util-file.h:86
struct File_ * next
Definition util-file.h:92
SCSha256 * sha256_ctx
Definition util-file.h:97
uint32_t file_track_id
Definition util-file.h:84
uint8_t sha1[SC_SHA1_LEN]
Definition util-file.h:96
uint64_t content_stored
Definition util-file.h:101
uint32_t file_store_id
Definition util-file.h:85
uint64_t content_inspected
Definition util-file.h:99
uint64_t start
Definition util-file.h:105
Flow data structure.
Definition flow.h:356
const char * name
void FilesPrune(FileContainer *fc, const StreamingBufferConfig *sbcfg, const bool trunc)
Definition util-file.c:1187
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition util-file.c:480
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition util-file.c:309
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition util-file.c:326
#define FilePrintFlags(file)
Definition util-file.h:250
void FileForceSha256Enable(void)
Definition util-file.c:116
struct SCSha1 SCSha1
Definition util-file.h:39
struct File_ File
void FileForceHashParseCfg(SCConfNode *)
Function to parse forced file hashing configuration.
Definition util-file.c:170
int FileForceFilestore(void)
Definition util-file.c:122
uint16_t FileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction)
Definition util-file.c:216
void FileDisableStoringForTransaction(Flow *f, const uint8_t direction, void *tx, uint64_t tx_id)
disable file storing for a transaction
Definition util-file.c:1139
#define SC_SHA1_LEN
Definition util-file.h:40
int FileCloseFilePtr(File *ff, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition util-file.c:980
int FileAppendGAPById(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The file with 'track_id' in the FileContainer...
Definition util-file.c:822
void FileForceMagicEnable(void)
Definition util-file.c:98
int FileCloseFileById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition util-file.c:1078
int FileForceSha256(void)
Definition util-file.c:156
int FileAppendData(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len)
Store a chunk of file data in the flow. The open "flowfile" will be used.
Definition util-file.c:766
int FileCloseFile(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition util-file.c:1062
void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min)
Definition util-file.c:843
void FileContainerRecycle(FileContainer *, const StreamingBufferConfig *cfg)
Recycle a FileContainer.
Definition util-file.c:496
uint32_t FileReassemblyDepth(void)
Definition util-file.c:133
struct FileContainer_ FileContainer
int FileForceMd5(void)
Definition util-file.c:146
struct SCSha256 SCSha256
Definition util-file.h:36
void FileReassemblyDepthEnable(uint32_t size)
Definition util-file.c:127
void FileContainerFree(FileContainer *, const StreamingBufferConfig *cfg)
Free a FileContainer.
Definition util-file.c:516
void FileForceFilestoreEnable(void)
Definition util-file.c:92
int FileAppendDataById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The file with 'track_id' in the FileContainer...
Definition util-file.c:791
enum FileState_ FileState
struct SCMd5 SCMd5
Definition util-file.h:42
int FileForceMagic(void)
Definition util-file.c:141
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition util-file.c:273
void FileStoreFileById(FileContainer *fc, uint32_t)
flag a file with id "file_id" to be stored.
Definition util-file.c:1157
void FileForceTrackingEnable(void)
Definition util-file.c:161
void FileContainerAdd(FileContainer *, File *)
Definition util-file.c:595
int FileForceSha1(void)
Definition util-file.c:151
int FileSetRange(FileContainer *, uint64_t start, uint64_t end)
Sets the offset range for a file.
Definition util-file.c:859
void FileForceSha1Enable(void)
Definition util-file.c:110
#define SC_MD5_LEN
Definition util-file.h:43
FileState_
Definition util-file.h:68
@ FILE_STATE_OPENED
Definition util-file.h:70
@ FILE_STATE_TRUNCATED
Definition util-file.h:73
@ FILE_STATE_MAX
Definition util-file.h:76
@ FILE_STATE_ERROR
Definition util-file.h:75
@ FILE_STATE_NONE
Definition util-file.h:69
@ FILE_STATE_CLOSED
Definition util-file.h:71
int FileOpenFileWithId(FileContainer *, const StreamingBufferConfig *, uint32_t track_id, const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len, uint16_t flags)
Open a new File.
Definition util-file.c:967
void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction)
set a flow's file flags
Definition util-file.c:1103
#define SC_SHA256_LEN
Definition util-file.h:37
int FileStore(File *)
Tag a file for storing.
Definition util-file.c:611
void FileForceMd5Enable(void)
Definition util-file.c:104