suricata
app-layer-htp.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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  * \defgroup httplayer HTTP layer support
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
28  * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
29  *
30  * This file provides a HTTP protocol support for the engine using HTP library.
31  */
32 
33 #ifndef SURICATA_APP_LAYER_HTP_H
34 #define SURICATA_APP_LAYER_HTP_H
35 
36 #include "rust.h"
37 #include "app-layer-parser.h"
38 #include "app-layer-frames.h"
39 
40 #include "htp/htp_rs.h"
41 
42 /* default request body limit */
43 #define HTP_CONFIG_DEFAULT_REQUEST_BODY_LIMIT 4096U
44 #define HTP_CONFIG_DEFAULT_RESPONSE_BODY_LIMIT 4096U
45 #define HTP_CONFIG_DEFAULT_REQUEST_INSPECT_MIN_SIZE 32768U
46 #define HTP_CONFIG_DEFAULT_REQUEST_INSPECT_WINDOW 4096U
47 #define HTP_CONFIG_DEFAULT_RESPONSE_INSPECT_MIN_SIZE 32768U
48 #define HTP_CONFIG_DEFAULT_RESPONSE_INSPECT_WINDOW 4096U
49 #define HTP_CONFIG_DEFAULT_FIELD_LIMIT 18000U
50 
51 #define HTP_CONFIG_DEFAULT_LZMA_LAYERS 0U
52 /* default libhtp lzma limit, taken from libhtp. */
53 #define HTP_CONFIG_DEFAULT_LZMA_MEMLIMIT 1048576U
54 #define HTP_CONFIG_DEFAULT_COMPRESSION_BOMB_LIMIT 1048576U
55 // 100000 usec is 0.1 sec
56 #define HTP_CONFIG_DEFAULT_COMPRESSION_TIME_LIMIT 100000
57 
58 #define HTP_CONFIG_DEFAULT_RANDOMIZE 1
59 #define HTP_CONFIG_DEFAULT_RANDOMIZE_RANGE 10
60 
61 // 0x0001 not used
62 #define HTP_FLAG_STATE_CLOSED_TS \
63  0x0002 /**< Flag to indicate that HTTP \
64  connection is closed */
65 #define HTP_FLAG_STATE_CLOSED_TC \
66  0x0004 /**< Flag to indicate that HTTP \
67  connection is closed */
68 
69 enum {
72  HTP_BODY_REQUEST_POST, /* POST, no MP */
74 };
75 
76 enum {
77  /* suricata errors/warnings */
81 
86 };
87 
88 typedef enum HtpSwfCompressType_ {
94 
95 typedef struct HTPCfgDir_ {
96  uint32_t body_limit;
97  uint32_t inspect_min_size;
98  uint32_t inspect_window;
100 
101 /** Need a linked list in order to keep track of these */
102 typedef struct HTPCfgRec_ {
103  htp_cfg_t *cfg;
104  struct HTPCfgRec_ *next;
105 
106  /** max size of the client body we inspect */
110 
115 
118 
119  bool uri_include_all; /**< use all info in uri (bool) */
121 
122 /** Struct used to hold chunks of a body on a request */
124  struct HtpBodyChunk_ *next; /**< Pointer to the next chunk */
125  int logged;
126  StreamingBufferSegment sbseg;
127 } __attribute__((__packed__));
128 typedef struct HtpBodyChunk_ HtpBodyChunk;
129 
130 /** Struct used to hold all the chunks of a body on a request */
131 typedef struct HtpBody_ {
132  HtpBodyChunk *first; /**< Pointer to the first chunk */
133  HtpBodyChunk *last; /**< Pointer to the last chunk */
134 
136 
137  /* Holds the length of the htp request body seen so far */
139  /* parser tracker */
140  uint64_t body_parsed;
141  /* inspection tracker */
142  uint64_t body_inspected;
144 
145 #define HTP_BOUNDARY_SET BIT_U8(1) /**< We have a boundary string */
146 #define HTP_FILENAME_SET BIT_U8(3) /**< filename is registered in the flow */
147 #define HTP_DONTSTORE BIT_U8(4) /**< not storing this file */
148 #define HTP_STREAM_DEPTH_SET BIT_U8(5) /**< stream-depth is set */
149 
150 /** Now the Body Chunks will be stored per transaction, at
151  * the tx user data */
152 typedef struct HtpTxUserData_ {
153  /* Body of the request (if any) */
156 
159 
160  uint8_t tsflags;
161  uint8_t tcflags;
162 
164 
167 
172 
173  MimeStateHTTP *mime_state;
174 
175  HttpRangeContainerBlock *file_range; /**< used to assign track ids to range file */
176 
181 
182 typedef struct HtpState_ {
183  /* Connection parser structure for each connection */
184  htp_connp_t *connp;
185  /* Connection structure for each connection */
186  htp_conn_t *conn;
187  Flow *f; /**< Needed to retrieve the original flow when using HTPLib callbacks */
188  uint64_t transaction_cnt;
189  const struct HTPCfgRec_ *cfg;
190  uint16_t flags;
191  uint16_t events;
192  uint16_t htp_messages_count; /**< Number of already logged messages */
193  uint32_t file_track_id; /**< used to assign file track ids to files */
201 
202 /** part of the engine needs the request body (e.g. http_client_body keyword) */
203 #define HTP_REQUIRE_REQUEST_BODY (1 << 0)
204 /** part of the engine needs the request file (e.g. log-file module) */
205 #define HTP_REQUIRE_REQUEST_FILE (1 << 2)
206 /** part of the engine needs the request body (e.g. file_data keyword) */
207 #define HTP_REQUIRE_RESPONSE_BODY (1 << 3)
208 
209 SC_ATOMIC_EXTERN(uint32_t, htp_config_flags);
210 
211 void RegisterHTPParsers(void);
212 void HTPAtExitPrintStats(void);
213 void HTPFreeConfig(void);
214 
215 /* To free the state from unittests using app-layer-htp */
216 void HTPStateFree(void *);
220 void AppLayerHtpPrintStats(void);
221 
222 void HTPConfigure(void);
223 
224 void HtpConfigCreateBackup(void);
225 void HtpConfigRestoreBackup(void);
226 
227 void *HtpGetTxForH2(void *);
228 
229 #endif /* SURICATA_APP_LAYER_HTP_H */
230 
231 /**
232  * @}
233  */
HtpState
struct HtpState_ HtpState
HtpState_::cfg
const struct HTPCfgRec_ * cfg
Definition: app-layer-htp.h:189
StreamSlice
Definition: app-layer-parser.h:131
FileContainer_
Definition: util-file.h:37
HTTP_DECODER_EVENT_MULTIPART_GENERIC_ERROR
@ HTTP_DECODER_EVENT_MULTIPART_GENERIC_ERROR
Definition: app-layer-htp.h:78
HTP_BODY_REQUEST_POST
@ HTP_BODY_REQUEST_POST
Definition: app-layer-htp.h:72
AppLayerHtpNeedFileInspection
void AppLayerHtpNeedFileInspection(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request file...
Definition: app-layer-htp.c:579
HtpState_::slice
StreamSlice * slice
Definition: app-layer-htp.h:196
HtpTxUserData_::request_headers_raw_len
uint32_t request_headers_raw_len
Definition: app-layer-htp.h:170
HTPCfgDir_
Definition: app-layer-htp.h:95
HtpBody_::sb
StreamingBuffer * sb
Definition: app-layer-htp.h:135
HtpTxUserData_::files_tc
FileContainer files_tc
Definition: app-layer-htp.h:179
HTPCfgRec_::response
HTPCfgDir response
Definition: app-layer-htp.h:117
HtpTxUserData
struct HtpTxUserData_ HtpTxUserData
AppLayerHtpPrintStats
void AppLayerHtpPrintStats(void)
Definition: app-layer-htp.c:2426
HTPCfgDir_::body_limit
uint32_t body_limit
Definition: app-layer-htp.h:96
HtpSwfCompressType_
HtpSwfCompressType_
Definition: app-layer-htp.h:88
HtpTxUserData_::mime_state
MimeStateHTTP * mime_state
Definition: app-layer-htp.h:173
AppLayerStateData
Definition: app-layer-parser.h:160
Flow_
Flow data structure.
Definition: flow.h:347
HtpGetTxForH2
void * HtpGetTxForH2(void *)
Definition: app-layer-htp.c:2515
HtpState_::flags
uint16_t flags
Definition: app-layer-htp.h:190
HtpState_::f
Flow * f
Definition: app-layer-htp.h:187
HtpBody_::last
HtpBodyChunk * last
Definition: app-layer-htp.h:133
HtpTxUserData_::request_body
HtpBody request_body
Definition: app-layer-htp.h:165
HtpSwfCompressType
enum HtpSwfCompressType_ HtpSwfCompressType
rust.h
HTTP_SWF_COMPRESSION_ZLIB
@ HTTP_SWF_COMPRESSION_ZLIB
Definition: app-layer-htp.h:90
HTP_BODY_REQUEST_PUT
@ HTP_BODY_REQUEST_PUT
Definition: app-layer-htp.h:73
HtpState_::transaction_cnt
uint64_t transaction_cnt
Definition: app-layer-htp.h:188
HTPCfgRec_::swf_compress_depth
uint32_t swf_compress_depth
Definition: app-layer-htp.h:114
HTPStateFree
void HTPStateFree(void *)
Function to frees the HTTP state memory and also frees the HTTP connection parser memory which was us...
Definition: app-layer-htp.c:505
HTTP_DECODER_EVENT_FILE_NAME_TOO_LONG
@ HTTP_DECODER_EVENT_FILE_NAME_TOO_LONG
Definition: app-layer-htp.h:84
HTPCfgDir_::inspect_window
uint32_t inspect_window
Definition: app-layer-htp.h:98
HTPConfigure
void HTPConfigure(void)
Definition: app-layer-htp.c:2355
HTTP_DECODER_EVENT_TOO_MANY_WARNINGS
@ HTTP_DECODER_EVENT_TOO_MANY_WARNINGS
Definition: app-layer-htp.h:82
HtpBody_::first
HtpBodyChunk * first
Definition: app-layer-htp.h:132
HtpState_
Definition: app-layer-htp.h:182
HtpBody_::content_len_so_far
uint64_t content_len_so_far
Definition: app-layer-htp.h:138
__attribute__
struct HtpBodyChunk_ __attribute__((__packed__))
DNP3 link header.
Definition: decode-vlan.c:102
HttpRangeContainerBlock
Definition: app-layer-htp-range.h:33
HtpState_::response_frame_id
FrameId response_frame_id
Definition: app-layer-htp.h:198
HtpTxUserData_::file_range
HttpRangeContainerBlock * file_range
Definition: app-layer-htp.h:175
HtpState_::last_request_data_stamp
uint64_t last_request_data_stamp
Definition: app-layer-htp.h:194
HtpBody_::body_parsed
uint64_t body_parsed
Definition: app-layer-htp.h:140
HTPCfgRec_::http_body_inline
int http_body_inline
Definition: app-layer-htp.h:109
HTPCfgRec_::randomize_range
int randomize_range
Definition: app-layer-htp.h:108
AppLayerTxData
Definition: app-layer-parser.h:177
HTTP_SWF_COMPRESSION_NONE
@ HTTP_SWF_COMPRESSION_NONE
Definition: app-layer-htp.h:89
HtpConfigCreateBackup
void HtpConfigCreateBackup(void)
Definition: app-layer-htp.c:2691
HtpState_::file_track_id
uint32_t file_track_id
Definition: app-layer-htp.h:193
HTPCfgRec_::randomize
int randomize
Definition: app-layer-htp.h:107
HTPCfgDir_::inspect_min_size
uint32_t inspect_min_size
Definition: app-layer-htp.h:97
AppLayerHtpEnableRequestBodyCallback
void AppLayerHtpEnableRequestBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
Definition: app-layer-htp.c:552
HtpTxUserData_::response_has_trailers
uint8_t response_has_trailers
Definition: app-layer-htp.h:158
HtpTxUserData_::request_headers_raw
uint8_t * request_headers_raw
Definition: app-layer-htp.h:168
HtpState_::conn
htp_conn_t * conn
Definition: app-layer-htp.h:186
HTPCfgRec_::swf_decompress_depth
uint32_t swf_decompress_depth
Definition: app-layer-htp.h:113
HTPCfgRec_::swf_decompression_enabled
int swf_decompression_enabled
Definition: app-layer-htp.h:111
HtpTxUserData_::tx_data
AppLayerTxData tx_data
Definition: app-layer-htp.h:177
HtpState_::state_data
AppLayerStateData state_data
Definition: app-layer-htp.h:199
FrameId
int64_t FrameId
Definition: app-layer-frames.h:32
HTPAtExitPrintStats
void HTPAtExitPrintStats(void)
Print the stats of the HTTP requests.
Definition: app-layer-htp.c:1582
app-layer-parser.h
HTTP_DECODER_EVENT_RANGE_INVALID
@ HTTP_DECODER_EVENT_RANGE_INVALID
Definition: app-layer-htp.h:83
HtpTxUserData_::request_has_trailers
uint8_t request_has_trailers
Definition: app-layer-htp.h:157
HTPCfgRec
struct HTPCfgRec_ HTPCfgRec
HTPCfgRec_::uri_include_all
bool uri_include_all
Definition: app-layer-htp.h:119
HtpState_::htp_messages_count
uint16_t htp_messages_count
Definition: app-layer-htp.h:192
HtpBody
struct HtpBody_ HtpBody
AppLayerHtpEnableResponseBodyCallback
void AppLayerHtpEnableResponseBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
Definition: app-layer-htp.c:565
HTPCfgRec_::swf_compression_type
HtpSwfCompressType swf_compression_type
Definition: app-layer-htp.h:112
HtpTxUserData_::response_body
HtpBody response_body
Definition: app-layer-htp.h:166
HtpBodyChunk_::logged
int logged
Definition: app-layer-htp.h:125
HTTP_DECODER_EVENT_FAILED_PROTOCOL_CHANGE
@ HTTP_DECODER_EVENT_FAILED_PROTOCOL_CHANGE
Definition: app-layer-htp.h:85
HTPCfgRec_::next
struct HTPCfgRec_ * next
Definition: app-layer-htp.h:104
StreamingBuffer_
Definition: util-streaming-buffer.h:108
HtpTxUserData_::request_body_type
uint8_t request_body_type
Definition: app-layer-htp.h:163
HTPCfgRec_::cfg
htp_cfg_t * cfg
Definition: app-layer-htp.h:103
app-layer-frames.h
HtpBody_
Definition: app-layer-htp.h:131
HTTP_DECODER_EVENT_MULTIPART_NO_FILEDATA
@ HTTP_DECODER_EVENT_MULTIPART_NO_FILEDATA
Definition: app-layer-htp.h:79
HTPCfgRec_::request
HTPCfgDir request
Definition: app-layer-htp.h:116
HtpTxUserData_::response_headers_raw
uint8_t * response_headers_raw
Definition: app-layer-htp.h:169
HtpBodyChunk_
Definition: app-layer-htp.h:123
HtpState_::connp
htp_connp_t * connp
Definition: app-layer-htp.h:184
HtpTxUserData_::tcflags
uint8_t tcflags
Definition: app-layer-htp.h:161
HTTP_DECODER_EVENT_MULTIPART_INVALID_HEADER
@ HTTP_DECODER_EVENT_MULTIPART_INVALID_HEADER
Definition: app-layer-htp.h:80
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint32_t, htp_config_flags)
HtpTxUserData_
Definition: app-layer-htp.h:152
HtpState_::last_response_data_stamp
uint64_t last_response_data_stamp
Definition: app-layer-htp.h:195
HtpConfigRestoreBackup
void HtpConfigRestoreBackup(void)
Definition: app-layer-htp.c:2696
HTP_BODY_REQUEST_NONE
@ HTP_BODY_REQUEST_NONE
Definition: app-layer-htp.h:70
HtpState_::events
uint16_t events
Definition: app-layer-htp.h:191
HtpBodyChunk_::next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:124
HtpTxUserData_::response_body_init
uint8_t response_body_init
Definition: app-layer-htp.h:155
HTTP_SWF_COMPRESSION_BOTH
@ HTTP_SWF_COMPRESSION_BOTH
Definition: app-layer-htp.h:92
HTPCfgRec_
Definition: app-layer-htp.h:102
RegisterHTPParsers
void RegisterHTPParsers(void)
Register the HTTP protocol and state handling functions to APP layer of the engine.
Definition: app-layer-htp.c:2620
HTTP_SWF_COMPRESSION_LZMA
@ HTTP_SWF_COMPRESSION_LZMA
Definition: app-layer-htp.h:91
HtpState_::request_frame_id
FrameId request_frame_id
Definition: app-layer-htp.h:197
HTP_BODY_REQUEST_MULTIPART
@ HTP_BODY_REQUEST_MULTIPART
Definition: app-layer-htp.h:71
HTPCfgDir
struct HTPCfgDir_ HTPCfgDir
HtpTxUserData_::files_ts
FileContainer files_ts
Definition: app-layer-htp.h:178
HTPFreeConfig
void HTPFreeConfig(void)
Clears the HTTP server configuration memory used by HTP library.
Definition: app-layer-htp.c:1595
HtpTxUserData_::response_headers_raw_len
uint32_t response_headers_raw_len
Definition: app-layer-htp.h:171
HtpTxUserData_::request_body_init
uint8_t request_body_init
Definition: app-layer-htp.h:154
HtpBodyChunk_::sbseg
StreamingBufferSegment sbseg
Definition: app-layer-htp.h:126
HtpTxUserData_::tsflags
uint8_t tsflags
Definition: app-layer-htp.h:160
HtpBody_::body_inspected
uint64_t body_inspected
Definition: app-layer-htp.h:142