suricata
util-file.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2011 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 #ifdef HAVE_NSS
29 #include <sechash.h>
30 #endif
31 
32 #include "conf.h"
33 
34 #include "util-streaming-buffer.h"
35 
36 #define FILE_TRUNCATED BIT_U16(0)
37 #define FILE_NOMAGIC BIT_U16(1)
38 #define FILE_NOMD5 BIT_U16(2)
39 #define FILE_MD5 BIT_U16(3)
40 #define FILE_NOSHA1 BIT_U16(4)
41 #define FILE_SHA1 BIT_U16(5)
42 #define FILE_NOSHA256 BIT_U16(6)
43 #define FILE_SHA256 BIT_U16(7)
44 #define FILE_LOGGED BIT_U16(8)
45 #define FILE_NOSTORE BIT_U16(9)
46 #define FILE_STORE BIT_U16(10)
47 #define FILE_STORED BIT_U16(11)
48 #define FILE_NOTRACK BIT_U16(12) /**< track size of file */
49 #define FILE_USE_DETECT BIT_U16(13) /**< use content_inspected tracker */
50 #define FILE_HAS_GAPS BIT_U16(15)
51 
52 typedef enum FileState_ {
53  FILE_STATE_NONE = 0, /**< no state */
54  FILE_STATE_OPENED, /**< flow file is opened */
55  FILE_STATE_CLOSED, /**< flow file is completed,
56  there will be no more data. */
57  FILE_STATE_TRUNCATED, /**< flow file is not complete, but
58  there will be no more data. */
59  FILE_STATE_ERROR, /**< file is in an error state */
61 } FileState;
62 
63 typedef struct File_ {
64  uint16_t flags;
65  uint16_t name_len;
68  uint64_t txid; /**< tx this file is part of */
69  uint32_t file_track_id; /**< id used by protocol parser */
70  uint32_t file_store_id; /**< id used in store file name file.<id> */
71  int fd; /**< file descriptor for filestore, not
72  open if equal to -1 */
73  uint8_t *name;
74 #ifdef HAVE_MAGIC
75  char *magic;
76 #endif
77  struct File_ *next;
78 #ifdef HAVE_NSS
79  HASHContext *md5_ctx;
80  uint8_t md5[MD5_LENGTH];
81  HASHContext *sha1_ctx;
82  uint8_t sha1[SHA1_LENGTH];
83  HASHContext *sha256_ctx;
84  uint8_t sha256[SHA256_LENGTH];
85 #endif
86  uint64_t content_inspected; /**< used in pruning if FILE_USE_DETECT
87  * flag is set */
88  uint64_t content_stored;
89  uint64_t size;
90  uint32_t inspect_window;
91  uint32_t inspect_min_size;
92  uint64_t start;
93  uint64_t end;
94 
95  uint32_t *sid; /* signature id of a rule that triggered the filestore event */
96  uint32_t sid_cnt;
97  uint32_t sid_max;
98 } File;
99 
100 typedef struct FileContainer_ {
103 } FileContainer;
104 
107 
109 
111 
112 /**
113  * \brief Open a new File
114  *
115  * \param ffc flow container
116  * \param sbcfg buffer config
117  * \param name filename character array
118  * \param name_len filename len
119  * \param data initial data
120  * \param data_len initial data len
121  * \param flags open flags
122  *
123  * \retval ff flowfile object
124  *
125  * \note filename is not a string, so it's not nul terminated.
126  *
127  * If flags contains the FILE_USE_DETECT bit, the pruning code will
128  * consider not just the content_stored tracker, but also content_inspected.
129  * It's the responsibility of the API user to make sure this tracker is
130  * properly updated.
131  */
133  uint32_t track_id, const uint8_t *name, uint16_t name_len,
134  const uint8_t *data, uint32_t data_len, uint16_t flags);
135 
136 /**
137  * \brief Close a File
138  *
139  * \param ffc the container
140  * \param data final data if any
141  * \param data_len data len if any
142  * \param flags flags
143  *
144  * \retval 0 ok
145  * \retval -1 error
146  */
147 int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len,
148  uint16_t flags);
149 int FileCloseFileById(FileContainer *, uint32_t track_id,
150  const uint8_t *data, uint32_t data_len, uint16_t flags);
151 int FileCloseFilePtr(File *ff, const uint8_t *data,
152  uint32_t data_len, uint16_t flags);
153 
154 /**
155  * \brief Store a chunk of file data in the flow. The open "flowfile"
156  * will be used.
157  *
158  * \param ffc the container
159  * \param data data chunk
160  * \param data_len data chunk len
161  *
162  * \retval 0 ok
163  * \retval -1 error
164  */
165 int FileAppendData(FileContainer *, const uint8_t *data, uint32_t data_len);
166 int FileAppendDataById(FileContainer *, uint32_t track_id,
167  const uint8_t *data, uint32_t data_len);
168 int FileAppendGAPById(FileContainer *ffc, uint32_t track_id,
169  const uint8_t *data, uint32_t data_len);
170 
171 void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min);
172 
173 /**
174  * \brief Sets the offset range for a file.
175  *
176  * \param ffc the container
177  * \param start start offset
178  * \param end end offset
179  *
180  * \retval 0 ok
181  * \retval -1 error
182  */
183 int FileSetRange(FileContainer *, uint64_t start, uint64_t end);
184 
185 /**
186  * \brief Tag a file for storing
187  *
188  * \param ff The file to store
189  */
190 int FileStore(File *);
191 
192 /**
193  * \brief Set the TX id for a file
194  *
195  * \param ff The file to store
196  * \param txid the tx id
197  */
198 int FileSetTx(File *, uint64_t txid);
199 void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id);
200 
201 /**
202  * \brief disable file storage for a flow
203  *
204  * \param f *LOCKED* flow
205  */
206 void FileDisableStoring(struct Flow_ *, uint8_t);
207 
208 void FileDisableFilesize(Flow *f, uint8_t direction);
209 
210 /**
211  * \brief disable file storing for a transaction
212  *
213  * \param f flow
214  * \param tx_id transaction id
215  */
216 void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id);
217 
218 void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id);
219 void FilePrune(FileContainer *ffc);
220 
221 void FileForceFilestoreEnable(void);
222 int FileForceFilestore(void);
223 void FileReassemblyDepthEnable(uint32_t size);
224 uint32_t FileReassemblyDepth(void);
225 
226 void FileDisableMagic(Flow *f, uint8_t);
227 void FileForceMagicEnable(void);
228 int FileForceMagic(void);
229 
230 void FileDisableMd5(Flow *f, uint8_t);
231 void FileForceMd5Enable(void);
232 int FileForceMd5(void);
233 
234 void FileDisableSha1(Flow *f, uint8_t);
235 void FileForceSha1Enable(void);
236 int FileForceSha1(void);
237 
238 void FileDisableSha256(Flow *f, uint8_t);
239 void FileForceSha256Enable(void);
240 int FileForceSha256(void);
241 
243 
244 void FileForceTrackingEnable(void);
245 
247 void FileStoreAllFilesForTx(FileContainer *, uint64_t);
248 void FileStoreFileById(FileContainer *fc, uint32_t);
249 
251 
252 uint64_t FileDataSize(const File *file);
253 uint64_t FileTrackedSize(const File *file);
254 
255 uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);
256 
257 #endif /* __UTIL_FILE_H__ */
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:906
int FileForceMagic(void)
Definition: util-file.c:132
void FileContainerAdd(FileContainer *, File *)
Definition: util-file.c:527
void FileForceSha256Enable(void)
Definition: util-file.c:108
void FileForceTrackingEnable(void)
Definition: util-file.c:152
void FileStoreAllFiles(FileContainer *)
Definition: util-file.c:1340
uint64_t size
Definition: util-file.h:89
void FilePrune(FileContainer *ffc)
Definition: util-file.c:376
uint32_t sid_cnt
Definition: util-file.h:96
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:219
int FileForceSha1(void)
Definition: util-file.c:142
int FileForceFilestore(void)
Definition: util-file.c:113
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 &#39;track_id&#39; in the FileContainer...
Definition: util-file.c:750
void FileDisableSha256(Flow *f, uint8_t)
disable file sha256 calc for this flow
Definition: util-file.c:1182
void FileTruncateAllOpenFiles(FileContainer *)
Definition: util-file.c:1353
uint64_t content_inspected
Definition: util-file.h:86
int fd
Definition: util-file.h:71
FileState_
Definition: util-file.h:52
struct File_ * next
Definition: util-file.h:77
void FileContainerFree(FileContainer *)
Free a FileContainer.
Definition: util-file.c:447
void FileForceFilestoreEnable(void)
Definition: util-file.c:88
struct File_ File
int FileSetRange(FileContainer *, uint64_t start, uint64_t end)
Sets the offset range for a file.
Definition: util-file.c:789
struct FileContainer_ FileContainer
StreamingBuffer * sb
Definition: util-file.h:67
uint32_t inspect_min_size
Definition: util-file.h:91
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:411
uint64_t start
Definition: util-file.h:92
uint16_t flags
Definition: util-file.h:64
void FileContainerRecycle(FileContainer *)
Recycle a FileContainer.
Definition: util-file.c:428
uint64_t end
Definition: util-file.h:93
uint32_t * sid
Definition: util-file.h:95
void FileForceMd5Enable(void)
Definition: util-file.c:98
uint32_t sid_max
Definition: util-file.h:97
uint64_t txid
Definition: util-file.h:68
void FileStoreAllFilesForTx(FileContainer *, uint64_t)
Definition: util-file.c:1325
void FileForceHashParseCfg(ConfNode *)
Function to parse forced file hashing configuration.
Definition: util-file.c:160
uint16_t name_len
Definition: util-file.h:65
int FileStore(File *)
Tag a file for storing.
Definition: util-file.c:542
void FileDisableStoring(struct Flow_ *, uint8_t)
disable file storage for a flow
Definition: util-file.c:1041
int FileCloseFilePtr(File *ff, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:918
int FileCloseFileById(FileContainer *, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:1016
void FileDisableFilesize(Flow *f, uint8_t direction)
disable file size tracking for this flow
Definition: util-file.c:1221
uint32_t inspect_window
Definition: util-file.h:90
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition: util-file.c:296
Definition: conf.h:32
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 &#39;track_id&#39; in the FileContainer...
Definition: util-file.c:719
void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id)
void FileDisableSha1(Flow *f, uint8_t)
disable file sha1 calc for this flow
Definition: util-file.c:1143
int FileForceSha256(void)
Definition: util-file.c:147
void FileForceMagicEnable(void)
Definition: util-file.c:93
void FileDisableMd5(Flow *f, uint8_t)
disable file md5 calc for this flow
Definition: util-file.c:1104
uint16_t tx_id
void FileForceSha1Enable(void)
Definition: util-file.c:103
uint32_t FileReassemblyDepth(void)
Definition: util-file.c:124
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:279
FileState state
Definition: util-file.h:66
uint32_t file_store_id
Definition: util-file.h:70
uint64_t content_stored
Definition: util-file.h:88
uint32_t file_track_id
Definition: util-file.h:69
void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id)
disable file storing for a transaction
Definition: util-file.c:1279
void FileStoreFileById(FileContainer *fc, uint32_t)
flag a file with id "file_id" to be stored.
Definition: util-file.c:1310
void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id)
Definition: util-file.c:562
void FileDisableMagic(Flow *f, uint8_t)
disable file magic lookups for this flow
Definition: util-file.c:1073
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:695
void FileReassemblyDepthEnable(uint32_t size)
Definition: util-file.c:118
int FileForceMd5(void)
Definition: util-file.c:137
int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:1000
enum FileState_ FileState
#define SHA1_LENGTH
Definition: util-crypt.h:45
Flow data structure.
Definition: flow.h:325
uint8_t * name
Definition: util-file.h:73
void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min)
Definition: util-file.c:773
int FileSetTx(File *, uint64_t txid)
Set the TX id for a file.
Definition: util-file.c:554