]> git.ipfire.org Git - people/ms/suricata.git/blame - src/app-layer-htp.h
http: add libhtp uri warning event
[people/ms/suricata.git] / src / app-layer-htp.h
CommitLineData
a0ee6ade 1/* Copyright (C) 2007-2011 Open Information Security Foundation
ce019275
WM
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.
0165b3f0 11 *
ce019275
WM
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
60a99915
EL
18/**
19 * \defgroup httplayer HTTP layer support
20 *
21 * @{
22 */
23
ce019275
WM
24/**
25 * \file
0165b3f0 26 *
07f7ba55 27 * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
0165b3f0 28 * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
07f7ba55 29 *
ce019275 30 * This file provides a HTTP protocol support for the engine using HTP library.
07f7ba55
GS
31 */
32
48248687
VJ
33#ifndef __APP_LAYER_HTP_H__
34#define __APP_LAYER_HTP_H__
07f7ba55 35
a9cdd2bb 36#include "util-radix-tree.h"
e1022ee5 37#include "util-file.h"
ced01da8 38#include "app-layer-htp-mem.h"
a9cdd2bb 39
07f7ba55
GS
40#include <htp/htp.h>
41
6ebe7b7c 42/* default request body limit */
2763a612
VJ
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
fb496791
VJ
49#define HTP_CONFIG_DEFAULT_FIELD_LIMIT_SOFT 9000U
50#define HTP_CONFIG_DEFAULT_FIELD_LIMIT_HARD 18000U
a0ee6ade 51
ff784075
EL
52#define HTP_CONFIG_DEFAULT_RANDOMIZE 1
53#define HTP_CONFIG_DEFAULT_RANDOMIZE_RANGE 10
54
a0ee6ade
VJ
55/** a boundary should be smaller in size */
56#define HTP_BOUNDARY_MAX 200U
6ebe7b7c 57
9878eca0 58#define HTP_FLAG_STATE_OPEN 0x0001 /**< Flag to indicate that HTTP
0165b3f0 59 connection is open */
64625675 60#define HTP_FLAG_STATE_CLOSED_TS 0x0002 /**< Flag to indicate that HTTP
0165b3f0 61 connection is closed */
64625675
AS
62#define HTP_FLAG_STATE_CLOSED_TC 0x0004 /**< Flag to indicate that HTTP
63 connection is closed */
64#define HTP_FLAG_STATE_DATA 0x0008 /**< Flag to indicate that HTTP
0165b3f0 65 connection needs more data */
64625675 66#define HTP_FLAG_STATE_ERROR 0x0010 /**< Flag to indicate that an error
0165b3f0
PR
67 has been occured on HTTP
68 connection */
64625675 69#define HTP_FLAG_NEW_BODY_SET 0x0020 /**< Flag to indicate that HTTP
0165b3f0
PR
70 has parsed a new body (for
71 pcre) */
64625675
AS
72#define HTP_FLAG_STORE_FILES_TS 0x0040
73#define HTP_FLAG_STORE_FILES_TC 0x0080
74#define HTP_FLAG_STORE_FILES_TX_TS 0x0100
75#define HTP_FLAG_STORE_FILES_TX_TC 0x0200
c2c53994 76/** flag the state that a new file has been set in this tx */
64625675 77#define HTP_FLAG_NEW_FILE_TX_TS 0x0400
c2c53994 78/** flag the state that a new file has been set in this tx */
64625675 79#define HTP_FLAG_NEW_FILE_TX_TC 0x0800
9878eca0 80
0165b3f0 81enum {
23e01d23 82 HTP_BODY_NONE = 0, /**< Flag to indicate the current
0165b3f0
PR
83 operation */
84 HTP_BODY_REQUEST, /**< Flag to indicate that the
85 current operation is a request */
86 HTP_BODY_RESPONSE /**< Flag to indicate that the current
87 * operation is a response */
88};
89
23e01d23
VJ
90enum {
91 HTP_BODY_REQUEST_NONE = 0,
3702a33a
VJ
92 HTP_BODY_REQUEST_MULTIPART, /* POST, MP */
93 HTP_BODY_REQUEST_POST, /* POST, no MP */
23e01d23
VJ
94 HTP_BODY_REQUEST_PUT,
95};
96
f713b653 97enum {
e21d8cdf 98 /* libhtp errors/warnings */
f713b653
VJ
99 HTTP_DECODER_EVENT_UNKNOWN_ERROR,
100 HTTP_DECODER_EVENT_GZIP_DECOMPRESSION_FAILED,
101 HTTP_DECODER_EVENT_REQUEST_FIELD_MISSING_COLON,
93d121bf 102 HTTP_DECODER_EVENT_RESPONSE_FIELD_MISSING_COLON,
f713b653
VJ
103 HTTP_DECODER_EVENT_INVALID_REQUEST_CHUNK_LEN,
104 HTTP_DECODER_EVENT_INVALID_RESPONSE_CHUNK_LEN,
105 HTTP_DECODER_EVENT_INVALID_TRANSFER_ENCODING_VALUE_IN_REQUEST,
106 HTTP_DECODER_EVENT_INVALID_TRANSFER_ENCODING_VALUE_IN_RESPONSE,
107 HTTP_DECODER_EVENT_INVALID_CONTENT_LENGTH_FIELD_IN_REQUEST,
108 HTTP_DECODER_EVENT_INVALID_CONTENT_LENGTH_FIELD_IN_RESPONSE,
109 HTTP_DECODER_EVENT_100_CONTINUE_ALREADY_SEEN,
110 HTTP_DECODER_EVENT_UNABLE_TO_MATCH_RESPONSE_TO_REQUEST,
111 HTTP_DECODER_EVENT_INVALID_SERVER_PORT_IN_REQUEST,
112 HTTP_DECODER_EVENT_INVALID_AUTHORITY_PORT,
113 HTTP_DECODER_EVENT_REQUEST_HEADER_INVALID,
93d121bf 114 HTTP_DECODER_EVENT_RESPONSE_HEADER_INVALID,
f713b653
VJ
115 HTTP_DECODER_EVENT_MISSING_HOST_HEADER,
116 HTTP_DECODER_EVENT_HOST_HEADER_AMBIGUOUS,
117 HTTP_DECODER_EVENT_INVALID_REQUEST_FIELD_FOLDING,
118 HTTP_DECODER_EVENT_INVALID_RESPONSE_FIELD_FOLDING,
119 HTTP_DECODER_EVENT_REQUEST_FIELD_TOO_LONG,
120 HTTP_DECODER_EVENT_RESPONSE_FIELD_TOO_LONG,
9f519e95 121 HTTP_DECODER_EVENT_REQUEST_SERVER_PORT_TCP_PORT_MISMATCH,
cb150003
VJ
122 HTTP_DECODER_EVENT_URI_HOST_INVALID,
123 HTTP_DECODER_EVENT_HEADER_HOST_INVALID,
5ad7198d 124 HTTP_DECODER_EVENT_URI_DELIM_NON_COMPLIANT,
e21d8cdf
VJ
125
126 /* suricata errors/warnings */
127 HTTP_DECODER_EVENT_MULTIPART_GENERIC_ERROR,
128 HTTP_DECODER_EVENT_MULTIPART_NO_FILEDATA,
129 HTTP_DECODER_EVENT_MULTIPART_INVALID_HEADER,
f713b653
VJ
130};
131
0165b3f0
PR
132#define HTP_PCRE_NONE 0x00 /**< No pcre executed yet */
133#define HTP_PCRE_DONE 0x01 /**< Flag to indicate that pcre has
134 done some inspection in the
135 chunks */
136#define HTP_PCRE_HAS_MATCH 0x02 /**< Flag to indicate that the chunks
137 matched on some rule */
138
2763a612
VJ
139/** Need a linked list in order to keep track of these */
140typedef struct HTPCfgRec_ {
141 htp_cfg_t *cfg;
142 struct HTPCfgRec_ *next;
143
a8b971c7
VJ
144 int uri_include_all; /**< use all info in uri (bool) */
145
2763a612
VJ
146 /** max size of the client body we inspect */
147 uint32_t request_body_limit;
148 uint32_t response_body_limit;
149
150 uint32_t request_inspect_min_size;
151 uint32_t request_inspect_window;
152
153 uint32_t response_inspect_min_size;
154 uint32_t response_inspect_window;
ff784075
EL
155 int randomize;
156 int randomize_range;
2763a612
VJ
157} HTPCfgRec;
158
0165b3f0 159/** Struct used to hold chunks of a body on a request */
d378b76c 160struct HtpBodyChunk_ {
0165b3f0 161 uint8_t *data; /**< Pointer to the data of the chunk */
a0ee6ade
VJ
162 struct HtpBodyChunk_ *next; /**< Pointer to the next chunk */
163 uint64_t stream_offset;
d378b76c 164 uint32_t len; /**< Length of the chunk */
bac6c3ab 165 int logged;
d378b76c
VJ
166} __attribute__((__packed__));
167typedef struct HtpBodyChunk_ HtpBodyChunk;
0165b3f0
PR
168
169/** Struct used to hold all the chunks of a body on a request */
7a8cd61f
VJ
170typedef struct HtpBody_ {
171 HtpBodyChunk *first; /**< Pointer to the first chunk */
172 HtpBodyChunk *last; /**< Pointer to the last chunk */
a0ee6ade 173
b402d971
VJ
174 /* Holds the length of the htp request body seen so far */
175 uint64_t content_len_so_far;
d378b76c 176 /* parser tracker */
b402d971 177 uint64_t body_parsed;
d378b76c
VJ
178 /* inspection tracker */
179 uint64_t body_inspected;
0165b3f0 180} HtpBody;
fc2f7f29 181
94e25276
AS
182#define HTP_CONTENTTYPE_SET 0x01 /**< We have the content type */
183#define HTP_BOUNDARY_SET 0x02 /**< We have a boundary string */
184#define HTP_BOUNDARY_OPEN 0x04 /**< We have a boundary string */
185#define HTP_FILENAME_SET 0x08 /**< filename is registered in the flow */
186#define HTP_DONTSTORE 0x10 /**< not storing this file */
23e01d23
VJ
187
188#define HTP_TX_HAS_FILE 0x01
189#define HTP_TX_HAS_FILENAME 0x02 /**< filename is known at this time */
190#define HTP_TX_HAS_TYPE 0x04
191#define HTP_TX_HAS_FILECONTENT 0x08 /**< file has content so we can do type detect */
192
193#define HTP_RULE_NEED_FILE HTP_TX_HAS_FILE
194#define HTP_RULE_NEED_FILENAME HTP_TX_HAS_FILENAME
195#define HTP_RULE_NEED_TYPE HTP_TX_HAS_TYPE
196#define HTP_RULE_NEED_FILECONTENT HTP_TX_HAS_FILECONTENT
6ebe7b7c 197
06a65cb4
PR
198/** Now the Body Chunks will be stored per transaction, at
199 * the tx user data */
66a3cd96 200typedef struct HtpTxUserData_ {
5c6a65dc 201 /* Body of the request (if any) */
48cf0585
AS
202 uint8_t request_body_init;
203 uint8_t response_body_init;
66a3cd96 204 HtpBody request_body;
b402d971 205 HtpBody response_body;
a0ee6ade 206
48cf0585
AS
207 bstr *request_uri_normalized;
208
209 uint8_t *request_headers_raw;
210 uint8_t *response_headers_raw;
211 uint32_t request_headers_raw_len;
212 uint32_t response_headers_raw_len;
213
3f5acc54
VJ
214 AppLayerDecoderEvents *decoder_events; /**< per tx events */
215
a0ee6ade
VJ
216 /** Holds the boundary identificator string if any (used on
217 * multipart/form-data only)
218 */
6d60b3a7 219 uint8_t *boundary;
a0ee6ade 220 uint8_t boundary_len;
6d60b3a7 221
43c7fd75
VJ
222 uint8_t tsflags;
223 uint8_t tcflags;
b402d971
VJ
224
225 int16_t operation;
d378b76c
VJ
226
227 uint8_t request_body_type;
228 uint8_t response_body_type;
229
66a3cd96 230} HtpTxUserData;
06a65cb4 231
07f7ba55 232typedef struct HtpState_ {
07f7ba55 233
48cf0585
AS
234 /* Connection parser structure for each connection */
235 htp_connp_t *connp;
236 /* Connection structure for each connection */
237 htp_conn_t *conn;
6d60b3a7 238 Flow *f; /**< Needed to retrieve the original flow when usin HTPLib callbacks */
d4d18e31 239 uint64_t transaction_cnt;
d4d18e31 240 uint64_t store_tx_id;
d59ca75e
VJ
241 FileContainer *files_ts;
242 FileContainer *files_tc;
2763a612 243 struct HTPCfgRec_ *cfg;
d4d18e31 244 uint16_t flags;
3f5acc54
VJ
245 uint16_t events;
246 uint16_t htp_messages_offset; /**< offset into conn->messages list */
07f7ba55
GS
247} HtpState;
248
6fa46d75 249/** part of the engine needs the request body (e.g. http_client_body keyword) */
92679442 250#define HTP_REQUIRE_REQUEST_BODY (1 << 0)
6fa46d75
AS
251/** part of the engine needs the request body multipart header (e.g. filename
252 * and / or fileext keywords) */
92679442 253#define HTP_REQUIRE_REQUEST_MULTIPART (1 << 1)
6fa46d75 254/** part of the engine needs the request file (e.g. log-file module) */
92679442 255#define HTP_REQUIRE_REQUEST_FILE (1 << 2)
6fa46d75 256/** part of the engine needs the request body (e.g. file_data keyword) */
92679442
EL
257#define HTP_REQUIRE_RESPONSE_BODY (1 << 3)
258
259SC_ATOMIC_DECLARE(uint32_t, htp_config_flags);
6fa46d75 260
07f7ba55
GS
261void RegisterHTPParsers(void);
262void HTPParserRegisterTests(void);
fc2f7f29
GS
263void HTPAtExitPrintStats(void);
264void HTPFreeConfig(void);
48248687 265
0165b3f0
PR
266htp_tx_t *HTPTransactionMain(const HtpState *);
267
268int HTPCallbackRequestBodyData(htp_tx_data_t *);
4e44073c 269int HtpTransactionGetLoggableId(Flow *);
0165b3f0
PR
270void HtpBodyPrint(HtpBody *);
271void HtpBodyFree(HtpBody *);
25a3a5c6
PR
272/* To free the state from unittests using app-layer-htp */
273void HTPStateFree(void *);
97d49d8f 274void AppLayerHtpEnableRequestBodyCallback(void);
b402d971 275void AppLayerHtpEnableResponseBodyCallback(void);
6d60b3a7 276void AppLayerHtpNeedFileInspection(void);
6fca55e0 277void AppLayerHtpPrintStats(void);
0165b3f0 278
ab4b15c2
AS
279void HTPConfigure(void);
280
281void HtpConfigCreateBackup(void);
282void HtpConfigRestoreBackup(void);
283
48248687 284#endif /* __APP_LAYER_HTP_H__ */
07f7ba55 285
60a99915
EL
286/**
287 * @}
288 */