HTP 0.5
htp_private.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (c) 2009-2010 Open Information Security Foundation
3 * Copyright (c) 2010-2013 Qualys, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16
17 * - Neither the name of the Qualys, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ***************************************************************************/
33
39#ifndef _HTP_PRIVATE_H
40#define _HTP_PRIVATE_H
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS)
47/* C99 requires that inttypes.h only exposes PRI* macros
48 * for C++ implementations if this is defined: */
49#define __STDC_FORMAT_MACROS
50#endif
51
52#include <ctype.h>
53#include <errno.h>
54#include <iconv.h>
55#include <inttypes.h>
56#include <stdarg.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <unistd.h>
60#include <sys/types.h>
61#include <sys/stat.h>
62#include <stdint.h>
63
64#include "htp_config_auto_gen.h"
65#include "htp.h"
66#include "htp_config_private.h"
69#include "htp_list_private.h"
71#include "htp_table_private.h"
72
73#ifndef CR
74#define CR '\r'
75#endif
76
77#ifndef LF
78#define LF '\n'
79#endif
80
81// 1048576 is 1 Mbyte
82#define HTP_LZMA_MEMLIMIT 1048576
83//deflate max ratio is about 1000
84#define HTP_COMPRESSION_BOMB_RATIO 2048
85#define HTP_COMPRESSION_BOMB_LIMIT 1048576
86// 0.1 second
87#define HTP_COMPRESSION_TIME_LIMIT_USEC 100000
88// test time for compression every 256 callbacks
89#define HTP_COMPRESSION_TIME_FREQ_TEST 256
90
91#define HTP_FIELD_LIMIT_HARD 18000
92#define HTP_FIELD_LIMIT_SOFT 9000
93
94#define HTP_VALID_STATUS_MIN 100
95#define HTP_VALID_STATUS_MAX 999
96
97// Parser states, in the order in which they are
98// used as a single transaction is processed.
99
115
126
127// Parsing functions
128
131htp_status_t htp_parse_request_header_generic(htp_connp_t *connp, htp_header_t *h, unsigned char *data, size_t len);
132htp_status_t htp_process_request_header_generic(htp_connp_t *, unsigned char *data, size_t len);
133
135htp_status_t htp_process_request_header_apache_2_2(htp_connp_t *, unsigned char *data, size_t len);
136
138htp_status_t htp_parse_response_header_generic(htp_connp_t *connp, htp_header_t *h, unsigned char *data, size_t len);
139htp_status_t htp_process_response_header_generic(htp_connp_t *connp, unsigned char *data, size_t len);
140
141
142// Private transaction functions
143
145
146
147// Utility functions
148
150int htp_is_lws(int c);
151int htp_is_separator(int c);
152int htp_is_text(int c);
153int htp_is_token(int c);
154int htp_chomp(unsigned char *data, size_t *len);
155int htp_is_space(int c);
156
157int htp_parse_protocol(bstr *protocol);
158
159int htp_is_line_empty(unsigned char *data, size_t len);
160int htp_is_line_whitespace(unsigned char *data, size_t len);
161
162int htp_connp_is_line_folded(unsigned char *data, size_t len);
163int htp_is_folding_char(int c);
164int htp_connp_is_line_terminator(htp_connp_t *connp, unsigned char *data, size_t len, int next_no_lf);
165int htp_connp_is_line_ignorable(htp_connp_t *connp, unsigned char *data, size_t len);
166
167int htp_parse_uri(bstr *input, htp_uri_t **uri);
168htp_status_t htp_parse_hostport(bstr *authority, bstr **hostname, bstr **port, int *port_number, int *invalid);
169htp_status_t htp_parse_header_hostport(bstr *authority, bstr **hostname, bstr **port, int *port_number, uint64_t *flags);
170int htp_validate_hostname(bstr *hostname);
171int htp_parse_uri_hostport(htp_connp_t *connp, bstr *input, htp_uri_t *uri);
172int htp_normalize_parsed_uri(htp_tx_t *tx, htp_uri_t *parsed_uri_incomplete, htp_uri_t *parsed_uri);
174
176
177 int htp_prenormalize_uri_path_inplace(bstr *s, int *flags, int case_insensitive, int backslash, int decode_separators, int remove_consecutive);
179
181void htp_utf8_validate_path(htp_tx_t *tx, bstr *path);
182
183int64_t htp_parse_content_length(bstr *b, htp_connp_t *connp);
184int64_t htp_parse_chunked_length(unsigned char *data, size_t len);
185int64_t htp_parse_positive_integer_whitespace(unsigned char *data, size_t len, int base);
186int htp_parse_status(bstr *status);
189
190void htp_print_log(FILE *stream, htp_log_t *log);
191
192void fprint_bstr(FILE *stream, const char *name, bstr *b);
193void fprint_raw_data(FILE *stream, const char *name, const void *data, size_t len);
194void fprint_raw_data_ex(FILE *stream, const char *name, const void *data, size_t offset, size_t len);
195
200
202
203int htp_treat_response_line_as_body(const uint8_t *data, size_t len);
204
207
213
215
216int htp_transcode_params(htp_connp_t *connp, htp_table_t **params, int destroy_old);
217int htp_transcode_bstr(iconv_t cd, bstr *input, bstr **output);
218
219int htp_parse_single_cookie_v0(htp_connp_t *connp, unsigned char *data, size_t len);
222
223htp_status_t htp_extract_quoted_string_as_bstr(unsigned char *data, size_t len, bstr **out, size_t *endoffset);
224
226
228
231
233
235
237
239
241
242htp_status_t htp_tx_req_process_body_data_ex(htp_tx_t *tx, const void *data, size_t len);
243htp_status_t htp_tx_res_process_body_data_ex(htp_tx_t *tx, const void *data, size_t len);
244
247
249
250#ifndef HAVE_STRLCAT
251size_t strlcat(char *dst, const char *src, size_t size);
252#endif
253
254#ifndef HAVE_STRLCPY
255size_t strlcpy(char *dst, const char *src, size_t size);
256#endif
257
258#ifdef __cplusplus
259}
260#endif
261
262#endif /* _HTP_PRIVATE_H */
263
int htp_status_t
Definition: htp_core.h:46
int htp_is_lws(int c)
Definition: htp_util.c:59
htp_status_t htp_connp_RES_BODY_CHUNKED_DATA_END(htp_connp_t *connp)
Definition: htp_response.c:289
htp_status_t htp_parse_request_line_generic(htp_connp_t *connp)
Definition: htp_request_generic.c:278
size_t strlcat(char *dst, const char *src, size_t size)
Definition: strlcat.c:50
htp_status_t htp_tx_state_request_complete_partial(htp_tx_t *tx)
Definition: htp_transaction.c:1024
int htp_normalize_parsed_uri(htp_tx_t *tx, htp_uri_t *parsed_uri_incomplete, htp_uri_t *parsed_uri)
Definition: htp_util.c:1760
htp_status_t htp_extract_quoted_string_as_bstr(unsigned char *data, size_t len, bstr **out, size_t *endoffset)
Definition: htp_util.c:2352
int htp_parse_authorization_digest(htp_connp_t *connp, htp_header_t *auth_header)
Definition: htp_parsers.c:102
void htp_tx_destroy_incomplete(htp_tx_t *tx)
Definition: htp_transaction.c:121
void fprint_raw_data(FILE *stream, const char *name, const void *data, size_t len)
Definition: htp_util.c:1987
char * htp_tx_request_progress_as_string(htp_tx_t *tx)
Definition: htp_util.c:2113
htp_status_t htp_tx_req_process_body_data_ex(htp_tx_t *tx, const void *data, size_t len)
Definition: htp_transaction.c:603
int htp_parse_cookies_v0(htp_connp_t *connp)
Definition: htp_cookies.c:88
int htp_connp_is_line_ignorable(htp_connp_t *connp, unsigned char *data, size_t len)
Definition: htp_util.c:510
htp_status_t htp_tx_urldecode_uri_inplace(htp_tx_t *tx, bstr *input)
Definition: htp_util.c:1520
htp_status_t htp_parse_header_hostport(bstr *authority, bstr **hostname, bstr **port, int *port_number, uint64_t *flags)
Definition: htp_util.c:679
htp_status_t htp_connp_RES_IDLE(htp_connp_t *connp)
Definition: htp_response.c:1183
void fprint_bstr(FILE *stream, const char *name, bstr *b)
Definition: htp_util.c:1975
htp_status_t htp_tx_res_process_body_data_ex(htp_tx_t *tx, const void *data, size_t len)
Definition: htp_transaction.c:953
int htp_parse_protocol(bstr *protocol)
Definition: htp_parsers.c:51
int htp_is_separator(int c)
Definition: htp_util.c:70
htp_status_t htp_connp_REQ_BODY_CHUNKED_DATA_END(htp_connp_t *connp)
Definition: htp_request.c:412
htp_status_t htp_connp_REQ_FINALIZE(htp_connp_t *connp)
Definition: htp_request.c:845
htp_status_t htp_ch_urlencoded_callback_request_line(htp_tx_t *tx)
Definition: htp_content_handlers.c:136
int htp_parse_status(bstr *status)
Definition: htp_parsers.c:87
htp_status_t htp_connp_RES_BODY_IDENTITY_STREAM_CLOSE(htp_connp_t *connp)
Definition: htp_response.c:514
int htp_transcode_params(htp_connp_t *connp, htp_table_t **params, int destroy_old)
Definition: htp_transcoder.c:50
int htp_validate_hostname(bstr *hostname)
Definition: htp_util.c:2447
htp_status_t htp_tx_urldecode_params_inplace(htp_tx_t *tx, bstr *input)
Definition: htp_util.c:1540
int htp_parse_uri(bstr *input, htp_uri_t **uri)
Definition: htp_util.c:705
htp_status_t htp_connp_RES_BODY_IDENTITY_CL_KNOWN(htp_connp_t *connp)
Definition: htp_response.c:468
int htp_prenormalize_uri_path_inplace(bstr *s, int *flags, int case_insensitive, int backslash, int decode_separators, int remove_consecutive)
htp_status_t htp_parse_ct_header(bstr *header, bstr **ct)
Definition: htp_util.c:2418
htp_status_t htp_connp_REQ_CONNECT_CHECK(htp_connp_t *connp)
Definition: htp_request.c:291
htp_status_t htp_parse_hostport(bstr *authority, bstr **hostname, bstr **port, int *port_number, int *invalid)
Definition: htp_util.c:551
void htp_connp_destroy_decompressors(htp_connp_t *connp)
Definition: htp_transaction.c:846
htp_status_t htp_connp_RES_LINE(htp_connp_t *connp)
Definition: htp_response.c:1000
size_t strlcpy(char *dst, const char *src, size_t size)
Definition: strlcpy.c:48
char * htp_tx_response_progress_as_string(htp_tx_t *tx)
Definition: htp_util.c:2137
htp_status_t htp_process_request_header_generic(htp_connp_t *, unsigned char *data, size_t len)
Definition: htp_request_generic.c:52
htp_status_t htp_ch_urlencoded_callback_request_body_data(htp_tx_data_t *d)
Definition: htp_content_handlers.c:50
htp_status_t htp_parse_request_line_generic_ex(htp_connp_t *connp, int nul_terminates)
Definition: htp_request_generic.c:282
htp_status_t htp_process_request_header_apache_2_2(htp_connp_t *, unsigned char *data, size_t len)
Definition: htp_request_apache_2_2.c:52
htp_status_t htp_ch_multipart_callback_request_headers(htp_tx_t *tx)
Definition: htp_content_handlers.c:244
htp_status_t htp_connp_RES_BODY_DETERMINE(htp_connp_t *connp)
Definition: htp_response.c:546
htp_header_t * htp_connp_header_parse(htp_connp_t *, unsigned char *, size_t)
int htp_connp_is_line_folded(unsigned char *data, size_t len)
Definition: htp_util.c:459
bstr * htp_normalize_hostname_inplace(bstr *input)
Definition: htp_util.c:1859
int htp_treat_response_line_as_body(const uint8_t *data, size_t len)
Definition: htp_util.c:2265
char * htp_connp_out_state_as_string(htp_connp_t *connp)
Definition: htp_util.c:2093
void htp_utf8_validate_path(htp_tx_t *tx, bstr *path)
Definition: htp_util.c:1063
htp_status_t htp_tx_state_response_complete_ex(htp_tx_t *tx, int hybrid_mode)
Definition: htp_transaction.c:1208
htp_status_t htp_connp_RES_FINALIZE(htp_connp_t *connp)
Definition: htp_response.c:1126
htp_status_t htp_connp_REQ_BODY_CHUNKED_LENGTH(htp_connp_t *connp)
Definition: htp_request.c:481
int htp_is_token(int c)
Definition: htp_util.c:120
int64_t htp_parse_content_length(bstr *b, htp_connp_t *connp)
Definition: htp_util.c:266
htp_status_t htp_process_response_header_generic(htp_connp_t *connp, unsigned char *data, size_t len)
Definition: htp_response_generic.c:252
htp_status_t htp_ch_multipart_callback_request_body_data(htp_tx_data_t *d)
Definition: htp_content_handlers.c:193
htp_status_t htp_connp_REQ_PROTOCOL(htp_connp_t *connp)
Definition: htp_request.c:733
int htp_is_space(int c)
Definition: htp_util.c:167
int htp_parse_authorization_basic(htp_connp_t *connp, htp_header_t *auth_header)
Definition: htp_parsers.c:126
htp_status_t htp_parse_request_line_apache_2_2(htp_connp_t *connp)
Definition: htp_request_apache_2_2.c:62
int htp_decode_path_inplace(htp_tx_t *tx, bstr *path)
Definition: htp_util.c:1256
int htp_transcode_bstr(iconv_t cd, bstr *input, bstr **output)
Definition: htp_transcoder.c:143
bstr * htp_unparse_uri_noencode(htp_uri_t *uri)
Definition: htp_util.c:2158
int htp_tx_is_complete(htp_tx_t *tx)
Definition: htp_transaction.c:1550
int htp_parse_authorization(htp_connp_t *connp)
Definition: htp_parsers.c:169
int htp_is_line_whitespace(unsigned char *data, size_t len)
Definition: htp_util.c:247
int htp_parse_uri_hostport(htp_connp_t *connp, bstr *input, htp_uri_t *uri)
Definition: htp_util.c:650
htp_status_t htp_tx_finalize(htp_tx_t *tx)
Definition: htp_transaction.c:1191
htp_status_t htp_connp_RES_HEADERS(htp_connp_t *connp)
Definition: htp_response.c:792
htp_status_t htp_parse_request_header_generic(htp_connp_t *connp, htp_header_t *h, unsigned char *data, size_t len)
Definition: htp_request_generic.c:143
htp_status_t htp_parse_response_line_generic(htp_connp_t *connp)
Definition: htp_response_generic.c:49
htp_status_t htp_connp_REQ_IGNORE_DATA_AFTER_HTTP_0_9(htp_connp_t *connp)
Definition: htp_request.c:917
htp_status_t htp_connp_REQ_CONNECT_PROBE_DATA(htp_connp_t *connp)
Definition: htp_request.c:317
void fprint_raw_data_ex(FILE *stream, const char *name, const void *data, size_t offset, size_t len)
Definition: htp_util.c:1999
int htp_is_folding_char(int c)
Definition: htp_util.c:464
htp_status_t htp_php_parameter_processor(htp_param_t *p)
Definition: htp_php.c:52
int htp_is_line_empty(unsigned char *data, size_t len)
Definition: htp_util.c:231
htp_status_t htp_connp_REQ_BODY_IDENTITY(htp_connp_t *connp)
Definition: htp_request.c:533
int htp_chomp(unsigned char *data, size_t *len)
Definition: htp_util.c:135
htp_status_t htp_connp_res_receiver_finalize_clear(htp_connp_t *connp)
Definition: htp_response.c:117
htp_status_t htp_res_run_hook_body_data(htp_connp_t *connp, htp_tx_data_t *d)
Definition: htp_util.c:2328
int htp_connp_is_line_terminator(htp_connp_t *connp, unsigned char *data, size_t len, int next_no_lf)
Definition: htp_util.c:477
int htp_parse_single_cookie_v0(htp_connp_t *connp, unsigned char *data, size_t len)
Definition: htp_cookies.c:51
htp_status_t htp_connp_RES_BODY_CHUNKED_LENGTH(htp_connp_t *connp)
Definition: htp_response.c:398
char * htp_connp_in_state_as_string(htp_connp_t *connp)
Definition: htp_util.c:2070
int64_t htp_parse_chunked_length(unsigned char *data, size_t len)
Definition: htp_util.c:303
htp_status_t htp_connp_REQ_BODY_CHUNKED_DATA(htp_connp_t *connp)
Definition: htp_request.c:436
void htp_utf8_decode_path_inplace(htp_cfg_t *cfg, htp_tx_t *tx, bstr *path)
Definition: htp_util.c:953
void htp_print_log(FILE *stream, htp_log_t *log)
htp_status_t htp_ch_urlencoded_callback_request_headers(htp_tx_t *tx)
Definition: htp_content_handlers.c:103
htp_status_t htp_connp_REQ_CONNECT_WAIT_RESPONSE(htp_connp_t *connp)
Definition: htp_request.c:383
htp_status_t htp_parse_response_header_generic(htp_connp_t *connp, htp_header_t *h, unsigned char *data, size_t len)
Definition: htp_response_generic.c:124
int htp_is_text(int c)
Definition: htp_util.c:108
htp_status_t htp_connp_REQ_LINE(htp_connp_t *connp)
Definition: htp_request.c:827
htp_status_t htp_connp_REQ_BODY_DETERMINE(htp_connp_t *connp)
Definition: htp_request.c:572
int htp_convert_method_to_number(bstr *)
Definition: htp_util.c:187
htp_status_t htp_connp_RES_BODY_CHUNKED_DATA(htp_connp_t *connp)
Definition: htp_response.c:314
void htp_connp_tx_remove(htp_connp_t *connp, htp_tx_t *tx)
Definition: htp_connection_parser.c:222
void htp_normalize_uri_path_inplace(bstr *s)
Definition: htp_util.c:1876
htp_status_t htp_connp_REQ_IDLE(htp_connp_t *connp)
Definition: htp_request.c:940
int64_t htp_parse_positive_integer_whitespace(unsigned char *data, size_t len, int base)
Definition: htp_util.c:344
htp_status_t htp_req_run_hook_body_data(htp_connp_t *connp, htp_tx_data_t *d)
Definition: htp_util.c:2291
htp_status_t htp_connp_req_receiver_finalize_clear(htp_connp_t *connp)
Definition: htp_request.c:131
htp_status_t htp_connp_REQ_LINE_complete(htp_connp_t *connp)
Definition: htp_request.c:774
htp_status_t htp_connp_REQ_HEADERS(htp_connp_t *connp)
Definition: htp_request.c:615
Definition: bstr.h:57
Definition: htp_config_private.h:123
Definition: htp_connection_parser_private.h:51
Definition: htp.h:171
Definition: htp.h:145
Definition: htp.h:185
Definition: htp_table_private.h:63
Definition: htp.h:551
Definition: htp.h:208
Definition: htp.h:575