]>
Commit | Line | Data |
---|---|---|
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" |
a9cdd2bb | 38 | |
07f7ba55 GS |
39 | #include <htp/htp.h> |
40 | ||
6ebe7b7c | 41 | /* default request body limit */ |
a0ee6ade | 42 | #define HTP_CONFIG_DEFAULT_REQUEST_BODY_LIMIT 4096U |
b402d971 | 43 | #define HTP_CONFIG_DEFAULT_RESPONSE_BODY_LIMIT 4096U |
a0ee6ade VJ |
44 | |
45 | /** a boundary should be smaller in size */ | |
46 | #define HTP_BOUNDARY_MAX 200U | |
6ebe7b7c | 47 | |
9878eca0 | 48 | #define HTP_FLAG_STATE_OPEN 0x0001 /**< Flag to indicate that HTTP |
0165b3f0 | 49 | connection is open */ |
9878eca0 | 50 | #define HTP_FLAG_STATE_CLOSED 0x0002 /**< Flag to indicate that HTTP |
0165b3f0 | 51 | connection is closed */ |
9878eca0 | 52 | #define HTP_FLAG_STATE_DATA 0x0004 /**< Flag to indicate that HTTP |
0165b3f0 | 53 | connection needs more data */ |
9878eca0 | 54 | #define HTP_FLAG_STATE_ERROR 0x0008 /**< Flag to indicate that an error |
0165b3f0 PR |
55 | has been occured on HTTP |
56 | connection */ | |
9878eca0 | 57 | #define HTP_FLAG_NEW_BODY_SET 0x0010 /**< Flag to indicate that HTTP |
0165b3f0 PR |
58 | has parsed a new body (for |
59 | pcre) */ | |
9878eca0 VJ |
60 | #define HTP_FLAG_STORE_FILES_TS 0x0020 |
61 | #define HTP_FLAG_STORE_FILES_TC 0x0040 | |
62 | #define HTP_FLAG_STORE_FILES_TX_TS 0x0080 | |
63 | #define HTP_FLAG_STORE_FILES_TX_TC 0x0100 | |
c2c53994 VJ |
64 | /** flag the state that a new file has been set in this tx */ |
65 | #define HTP_FLAG_NEW_FILE_TX_TS 0x0200 | |
66 | /** flag the state that a new file has been set in this tx */ | |
67 | #define HTP_FLAG_NEW_FILE_TX_TC 0x0400 | |
9878eca0 | 68 | |
0165b3f0 | 69 | enum { |
23e01d23 | 70 | HTP_BODY_NONE = 0, /**< Flag to indicate the current |
0165b3f0 PR |
71 | operation */ |
72 | HTP_BODY_REQUEST, /**< Flag to indicate that the | |
73 | current operation is a request */ | |
74 | HTP_BODY_RESPONSE /**< Flag to indicate that the current | |
75 | * operation is a response */ | |
76 | }; | |
77 | ||
23e01d23 VJ |
78 | enum { |
79 | HTP_BODY_REQUEST_NONE = 0, | |
80 | HTP_BODY_REQUEST_MULTIPART, | |
81 | HTP_BODY_REQUEST_PUT, | |
82 | }; | |
83 | ||
f713b653 VJ |
84 | enum { |
85 | HTTP_DECODER_EVENT_UNKNOWN_ERROR, | |
86 | HTTP_DECODER_EVENT_GZIP_DECOMPRESSION_FAILED, | |
87 | HTTP_DECODER_EVENT_REQUEST_FIELD_MISSING_COLON, | |
93d121bf | 88 | HTTP_DECODER_EVENT_RESPONSE_FIELD_MISSING_COLON, |
f713b653 VJ |
89 | HTTP_DECODER_EVENT_INVALID_REQUEST_CHUNK_LEN, |
90 | HTTP_DECODER_EVENT_INVALID_RESPONSE_CHUNK_LEN, | |
91 | HTTP_DECODER_EVENT_INVALID_TRANSFER_ENCODING_VALUE_IN_REQUEST, | |
92 | HTTP_DECODER_EVENT_INVALID_TRANSFER_ENCODING_VALUE_IN_RESPONSE, | |
93 | HTTP_DECODER_EVENT_INVALID_CONTENT_LENGTH_FIELD_IN_REQUEST, | |
94 | HTTP_DECODER_EVENT_INVALID_CONTENT_LENGTH_FIELD_IN_RESPONSE, | |
95 | HTTP_DECODER_EVENT_100_CONTINUE_ALREADY_SEEN, | |
96 | HTTP_DECODER_EVENT_UNABLE_TO_MATCH_RESPONSE_TO_REQUEST, | |
97 | HTTP_DECODER_EVENT_INVALID_SERVER_PORT_IN_REQUEST, | |
98 | HTTP_DECODER_EVENT_INVALID_AUTHORITY_PORT, | |
99 | HTTP_DECODER_EVENT_REQUEST_HEADER_INVALID, | |
93d121bf | 100 | HTTP_DECODER_EVENT_RESPONSE_HEADER_INVALID, |
f713b653 VJ |
101 | HTTP_DECODER_EVENT_MISSING_HOST_HEADER, |
102 | HTTP_DECODER_EVENT_HOST_HEADER_AMBIGUOUS, | |
103 | HTTP_DECODER_EVENT_INVALID_REQUEST_FIELD_FOLDING, | |
104 | HTTP_DECODER_EVENT_INVALID_RESPONSE_FIELD_FOLDING, | |
105 | HTTP_DECODER_EVENT_REQUEST_FIELD_TOO_LONG, | |
106 | HTTP_DECODER_EVENT_RESPONSE_FIELD_TOO_LONG, | |
107 | }; | |
108 | ||
0165b3f0 PR |
109 | #define HTP_PCRE_NONE 0x00 /**< No pcre executed yet */ |
110 | #define HTP_PCRE_DONE 0x01 /**< Flag to indicate that pcre has | |
111 | done some inspection in the | |
112 | chunks */ | |
113 | #define HTP_PCRE_HAS_MATCH 0x02 /**< Flag to indicate that the chunks | |
114 | matched on some rule */ | |
115 | ||
116 | /** Struct used to hold chunks of a body on a request */ | |
7a8cd61f | 117 | typedef struct HtpBodyChunk_ { |
0165b3f0 PR |
118 | uint8_t *data; /**< Pointer to the data of the chunk */ |
119 | uint32_t len; /**< Length of the chunk */ | |
0165b3f0 | 120 | uint32_t id; /**< number of chunk of the current body */ |
a0ee6ade VJ |
121 | struct HtpBodyChunk_ *next; /**< Pointer to the next chunk */ |
122 | uint64_t stream_offset; | |
7a8cd61f | 123 | } HtpBodyChunk; |
0165b3f0 PR |
124 | |
125 | /** Struct used to hold all the chunks of a body on a request */ | |
7a8cd61f VJ |
126 | typedef struct HtpBody_ { |
127 | HtpBodyChunk *first; /**< Pointer to the first chunk */ | |
128 | HtpBodyChunk *last; /**< Pointer to the last chunk */ | |
0165b3f0 | 129 | uint32_t nchunks; /**< Number of chunks in the current operation */ |
23e01d23 | 130 | uint8_t type; |
a0ee6ade | 131 | |
b402d971 VJ |
132 | /* Holds the length of the htp request body */ |
133 | uint64_t content_len; | |
134 | /* Holds the length of the htp request body seen so far */ | |
135 | uint64_t content_len_so_far; | |
136 | ||
137 | uint64_t body_parsed; | |
138 | ||
a0ee6ade | 139 | /* pahole: padding: 3 */ |
0165b3f0 | 140 | } HtpBody; |
fc2f7f29 | 141 | |
a0ee6ade VJ |
142 | #define HTP_BODY_COMPLETE 0x01 /**< body is complete or limit is reached, |
143 | either way, this is it. */ | |
144 | #define HTP_CONTENTTYPE_SET 0x02 /**< We have the content type */ | |
145 | #define HTP_BOUNDARY_SET 0x04 /**< We have a boundary string */ | |
146 | #define HTP_BOUNDARY_OPEN 0x08 /**< We have a boundary string */ | |
147 | #define HTP_FILENAME_SET 0x10 /**< filename is registered in the flow */ | |
23e01d23 VJ |
148 | #define HTP_DONTSTORE 0x20 /**< not storing this file */ |
149 | ||
150 | #define HTP_TX_HAS_FILE 0x01 | |
151 | #define HTP_TX_HAS_FILENAME 0x02 /**< filename is known at this time */ | |
152 | #define HTP_TX_HAS_TYPE 0x04 | |
153 | #define HTP_TX_HAS_FILECONTENT 0x08 /**< file has content so we can do type detect */ | |
154 | ||
155 | #define HTP_RULE_NEED_FILE HTP_TX_HAS_FILE | |
156 | #define HTP_RULE_NEED_FILENAME HTP_TX_HAS_FILENAME | |
157 | #define HTP_RULE_NEED_TYPE HTP_TX_HAS_TYPE | |
158 | #define HTP_RULE_NEED_FILECONTENT HTP_TX_HAS_FILECONTENT | |
6ebe7b7c | 159 | |
06a65cb4 PR |
160 | /** Now the Body Chunks will be stored per transaction, at |
161 | * the tx user data */ | |
66a3cd96 | 162 | typedef struct HtpTxUserData_ { |
5c6a65dc | 163 | /* Body of the request (if any) */ |
66a3cd96 | 164 | HtpBody request_body; |
b402d971 | 165 | HtpBody response_body; |
a0ee6ade VJ |
166 | |
167 | /** Holds the boundary identificator string if any (used on | |
168 | * multipart/form-data only) | |
169 | */ | |
6d60b3a7 | 170 | uint8_t *boundary; |
a0ee6ade | 171 | uint8_t boundary_len; |
6d60b3a7 | 172 | |
6ebe7b7c | 173 | uint8_t flags; |
b402d971 VJ |
174 | |
175 | int16_t operation; | |
66a3cd96 | 176 | } HtpTxUserData; |
06a65cb4 | 177 | |
07f7ba55 | 178 | typedef struct HtpState_ { |
07f7ba55 | 179 | |
7a8cd61f VJ |
180 | htp_connp_t *connp; /**< Connection parser structure for |
181 | each connection */ | |
6d60b3a7 | 182 | Flow *f; /**< Needed to retrieve the original flow when usin HTPLib callbacks */ |
9878eca0 | 183 | uint16_t flags; |
70b32f73 VJ |
184 | uint16_t transaction_cnt; |
185 | uint16_t transaction_done; | |
9878eca0 | 186 | uint16_t store_tx_id; |
6ebe7b7c | 187 | uint32_t request_body_limit; |
b402d971 | 188 | uint32_t response_body_limit; |
d59ca75e VJ |
189 | FileContainer *files_ts; |
190 | FileContainer *files_tc; | |
07f7ba55 GS |
191 | } HtpState; |
192 | ||
07f7ba55 GS |
193 | void RegisterHTPParsers(void); |
194 | void HTPParserRegisterTests(void); | |
fc2f7f29 GS |
195 | void HTPAtExitPrintStats(void); |
196 | void HTPFreeConfig(void); | |
48248687 | 197 | |
0165b3f0 PR |
198 | htp_tx_t *HTPTransactionMain(const HtpState *); |
199 | ||
200 | int HTPCallbackRequestBodyData(htp_tx_data_t *); | |
4e44073c | 201 | int HtpTransactionGetLoggableId(Flow *); |
0165b3f0 PR |
202 | void HtpBodyPrint(HtpBody *); |
203 | void HtpBodyFree(HtpBody *); | |
204 | void AppLayerHtpRegisterExtraCallbacks(void); | |
25a3a5c6 PR |
205 | /* To free the state from unittests using app-layer-htp */ |
206 | void HTPStateFree(void *); | |
97d49d8f | 207 | void AppLayerHtpEnableRequestBodyCallback(void); |
b402d971 | 208 | void AppLayerHtpEnableResponseBodyCallback(void); |
6d60b3a7 | 209 | void AppLayerHtpNeedFileInspection(void); |
6fca55e0 | 210 | void AppLayerHtpPrintStats(void); |
0165b3f0 | 211 | |
48248687 | 212 | #endif /* __APP_LAYER_HTP_H__ */ |
07f7ba55 | 213 | |
60a99915 EL |
214 | /** |
215 | * @} | |
216 | */ |