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 __UTIL_FILE_H__
26 #define __UTIL_FILE_H__
27 
28 #include "conf.h"
29 #include "util-streaming-buffer.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. */
36 typedef struct SCSha256 SCSha256;
37 #define SC_SHA256_LEN 32
38 
39 typedef struct SCSha1 SCSha1;
40 #define SC_SHA1_LEN 20
41 
42 typedef 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 
68 typedef 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 
79 typedef 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 */
101  uint64_t content_stored;
102  uint64_t size;
103  uint32_t inspect_window;
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 
113 typedef struct FileContainer_ {
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  */
160 int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len,
161  uint16_t flags);
162 int FileCloseFileById(FileContainer *, uint32_t track_id,
163  const uint8_t *data, uint32_t data_len, uint16_t flags);
164 int FileCloseFilePtr(File *ff, 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  */
178 int FileAppendData(FileContainer *, const uint8_t *data, uint32_t data_len);
179 int FileAppendDataById(FileContainer *, uint32_t track_id,
180  const uint8_t *data, uint32_t data_len);
181 int FileAppendGAPById(FileContainer *ffc, uint32_t track_id,
182  const uint8_t *data, uint32_t data_len);
183 
184 void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min);
185 
186 /**
187  * \brief Sets the offset range for a file.
188  *
189  * \param ffc the container
190  * \param start start offset
191  * \param end end offset
192  *
193  * \retval 0 ok
194  * \retval -1 error
195  */
196 int FileSetRange(FileContainer *, uint64_t start, uint64_t end);
197 
198 /**
199  * \brief Tag a file for storing
200  *
201  * \param ff The file to store
202  */
203 int FileStore(File *);
204 
205 /**
206  * \brief disable file storing for a transaction
207  *
208  * \param f flow
209  * \param direction STREAM_TOSERVER or STREAM_TOCLIENT
210  * \param tx transaction pointer
211  * \param tx_id transaction id
212  */
213 void FileDisableStoringForTransaction(Flow *f, const uint8_t direction, void *tx, uint64_t tx_id);
214 
215 void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id);
216 void FilePrune(FileContainer *ffc);
217 
218 void FileForceFilestoreEnable(void);
219 int FileForceFilestore(void);
220 void FileReassemblyDepthEnable(uint32_t size);
221 uint32_t FileReassemblyDepth(void);
222 
223 void FileForceMagicEnable(void);
224 int FileForceMagic(void);
225 
226 void FileForceMd5Enable(void);
227 int FileForceMd5(void);
228 
229 void FileForceSha1Enable(void);
230 int FileForceSha1(void);
231 
232 void FileForceSha256Enable(void);
233 int FileForceSha256(void);
234 
235 void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction);
236 
238 
239 void FileForceTrackingEnable(void);
240 
241 void FileStoreFileById(FileContainer *fc, uint32_t);
242 
244 
245 uint64_t FileDataSize(const File *file);
246 uint64_t FileTrackedSize(const File *file);
247 
248 uint16_t FileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction);
249 uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);
250 
251 #ifdef DEBUG
252 void FilePrintFlags(const File *file);
253 #else
254 #define FilePrintFlags(file)
255 #endif
256 
257 #endif /* __UTIL_FILE_H__ */
FileFlowToFlags
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:287
SCSha1
struct SCSha1 SCSha1
Definition: util-file.h:39
FileForceMagicEnable
void FileForceMagicEnable(void)
Definition: util-file.c:98
FileContainer_
Definition: util-file.h:113
FileCloseFile
int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:1074
FileState_
FileState_
Definition: util-file.h:68
FileReassemblyDepth
uint32_t FileReassemblyDepth(void)
Definition: util-file.c:133
File_::inspect_min_size
uint32_t inspect_min_size
Definition: util-file.h:104
File_::size
uint64_t size
Definition: util-file.h:102
FileCloseFileById
int FileCloseFileById(FileContainer *, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:1090
SC_SHA256_LEN
#define SC_SHA256_LEN
Definition: util-file.h:37
FileOpenFileWithId
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:978
FILE_STATE_OPENED
@ FILE_STATE_OPENED
Definition: util-file.h:70
Flow_
Flow data structure.
Definition: flow.h:356
File_::state
FileState state
Definition: util-file.h:82
FileStore
int FileStore(File *)
Tag a file for storing.
Definition: util-file.c:623
SC_SHA1_LEN
#define SC_SHA1_LEN
Definition: util-file.h:40
File_::file_store_id
uint32_t file_store_id
Definition: util-file.h:85
FileTruncateAllOpenFiles
void FileTruncateAllOpenFiles(FileContainer *)
Definition: util-file.c:1186
FileForceSha1Enable
void FileForceSha1Enable(void)
Definition: util-file.c:110
FileAppendData
int FileAppendData(FileContainer *, 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:774
FileReassemblyDepthEnable
void FileReassemblyDepthEnable(uint32_t size)
Definition: util-file.c:127
FileContainer_::tail
File * tail
Definition: util-file.h:115
FileContainer
struct FileContainer_ FileContainer
FILE_STATE_TRUNCATED
@ FILE_STATE_TRUNCATED
Definition: util-file.h:73
FileForceMagic
int FileForceMagic(void)
Definition: util-file.c:141
File_::sha1
uint8_t sha1[SC_SHA1_LEN]
Definition: util-file.h:96
SCSha256
struct SCSha256 SCSha256
Definition: util-file.h:36
FileForceTrackingEnable
void FileForceTrackingEnable(void)
Definition: util-file.c:161
File_::sb
StreamingBuffer * sb
Definition: util-file.h:83
File_::name_len
uint16_t name_len
Definition: util-file.h:81
FileForceSha1
int FileForceSha1(void)
Definition: util-file.c:151
File_::md5
uint8_t md5[SC_MD5_LEN]
Definition: util-file.h:94
File_::file_track_id
uint32_t file_track_id
Definition: util-file.h:84
File_::end
uint64_t end
Definition: util-file.h:106
File_::fd
int fd
Definition: util-file.h:86
FileTrackedSize
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition: util-file.c:340
FileContainerFree
void FileContainerFree(FileContainer *)
Free a FileContainer.
Definition: util-file.c:527
FileForceMd5Enable
void FileForceMd5Enable(void)
Definition: util-file.c:104
FileForceFilestore
int FileForceFilestore(void)
Definition: util-file.c:122
FileSetRange
int FileSetRange(FileContainer *, uint64_t start, uint64_t end)
Sets the offset range for a file.
Definition: util-file.c:866
File_::sid_max
uint32_t sid_max
Definition: util-file.h:110
FileContainer_::head
File * head
Definition: util-file.h:114
FileAppendDataById
int FileAppendDataById(FileContainer *, 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:798
FileAppendGAPById
int FileAppendGAPById(FileContainer *ffc, 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:829
FilePrune
void FilePrune(FileContainer *ffc)
Definition: util-file.c:449
File_::sha256_ctx
SCSha256 * sha256_ctx
Definition: util-file.h:97
conf.h
FILE_STATE_MAX
@ FILE_STATE_MAX
Definition: util-file.h:76
File_::name
uint8_t * name
Definition: util-file.h:88
FileSetInspectSizes
void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min)
Definition: util-file.c:850
File_::sid
uint32_t * sid
Definition: util-file.h:108
File_::sid_cnt
uint32_t sid_cnt
Definition: util-file.h:109
StreamingBuffer_
Definition: util-streaming-buffer.h:93
FileForceFilestoreEnable
void FileForceFilestoreEnable(void)
Definition: util-file.c:92
File_::flags
uint16_t flags
Definition: util-file.h:80
File_::content_inspected
uint64_t content_inspected
Definition: util-file.h:99
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:71
File_
Definition: util-file.h:79
File_::content_stored
uint64_t content_stored
Definition: util-file.h:101
flags
uint8_t flags
Definition: decode-gre.h:0
util-streaming-buffer.h
FileForceMd5
int FileForceMd5(void)
Definition: util-file.c:146
File_::next
struct File_ * next
Definition: util-file.h:92
FileContainerAdd
void FileContainerAdd(FileContainer *, File *)
Definition: util-file.c:607
File_::sha256
uint8_t sha256[SC_SHA256_LEN]
Definition: util-file.h:98
File_::start
uint64_t start
Definition: util-file.h:105
FlowFileDisableStoringForTransaction
void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id)
FilePrintFlags
#define FilePrintFlags(file)
Definition: util-file.h:254
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:323
StreamingBufferConfig_
Definition: util-streaming-buffer.h:64
FileForceSha256Enable
void FileForceSha256Enable(void)
Definition: util-file.c:116
FileDisableStoringForTransaction
void FileDisableStoringForTransaction(Flow *f, const uint8_t direction, void *tx, uint64_t tx_id)
disable file storing for a transaction
Definition: util-file.c:1151
FileCloseFilePtr
int FileCloseFilePtr(File *ff, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:991
FileFlowFlagsToFlags
uint16_t FileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction)
Definition: util-file.c:233
ConfNode_
Definition: conf.h:32
File
struct File_ File
File_::md5_ctx
SCMd5 * md5_ctx
Definition: util-file.h:93
FileContainerRecycle
void FileContainerRecycle(FileContainer *)
Recycle a FileContainer.
Definition: util-file.c:507
FileForceSha256
int FileForceSha256(void)
Definition: util-file.c:156
File_::inspect_window
uint32_t inspect_window
Definition: util-file.h:103
FileForceHashParseCfg
void FileForceHashParseCfg(ConfNode *)
Function to parse forced file hashing configuration.
Definition: util-file.c:170
FileContainerAlloc
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:490
FileState
enum FileState_ FileState
FileStoreFileById
void FileStoreFileById(FileContainer *fc, uint32_t)
flag a file with id "file_id" to be stored.
Definition: util-file.c:1171
SC_MD5_LEN
#define SC_MD5_LEN
Definition: util-file.h:43
flow.h
FileUpdateFlowFileFlags
void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction)
set a flow's file flags
Definition: util-file.c:1115
FILE_STATE_ERROR
@ FILE_STATE_ERROR
Definition: util-file.h:75
SCMd5
struct SCMd5 SCMd5
Definition: util-file.h:42
FILE_STATE_NONE
@ FILE_STATE_NONE
Definition: util-file.h:69
File_::sha1_ctx
SCSha1 * sha1_ctx
Definition: util-file.h:95