suricata
app-layer-http2.c
Go to the documentation of this file.
1 /* Copyright (C) 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  * \file
20  *
21  * \author Philippe Antoine <p.antoine@catenacyber.fr>
22  *
23  * Parser for HTTP2, RFC 7540
24  */
25 
26 #include "suricata-common.h"
27 #include "stream.h"
28 #include "conf.h"
29 
30 #include "util-unittest.h"
31 
32 #include "app-layer-detect-proto.h"
33 #include "app-layer-parser.h"
34 
35 #include "app-layer-htp.h"
36 #include "app-layer-htp-libhtp.h"
37 #include "app-layer-http2.h"
38 #include "rust.h"
39 
40 static int HTTP2RegisterPatternsForProtocolDetection(void)
41 {
42  /* Using the 24 bytes pattern makes AppLayerTest09 fail/leak
43  * The complete pattern is "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
44  */
46  "PRI * HTTP/2.0\r\n",
47  16, 0, STREAM_TOSERVER) < 0)
48  {
49  return -1;
50  }
51  return 0;
52 }
53 
55 static SuricataFileContext sfc = { &sbcfg };
56 
58 {
59  const char *proto_name = "http2";
60 
61  if (AppLayerProtoDetectConfProtoDetectionEnabledDefault("tcp", proto_name, true)) {
63  if (HTTP2RegisterPatternsForProtocolDetection() < 0)
64  return;
65 
66  rs_http2_init(&sfc);
67  rs_http2_register_parser();
68  }
69 
70 #ifdef UNITTESTS
71  //TODOask HTTP2ParserRegisterTests();
72 #endif
73 }
74 
75 void HTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
76 {
77  htp_tx_t *h1tx = HtpGetTxForH2(alstate_orig);
78  if (h2s == NULL || h1tx == NULL) {
79  return;
80  }
81  if (htp_tx_request_method(h1tx) == NULL) {
82  // may happen if we only got the reply, not the HTTP1 request
83  return;
84  }
85  // else
86  rs_http2_tx_set_method(h2s, bstr_ptr(htp_tx_request_method(h1tx)),
87  (uint32_t)bstr_len(htp_tx_request_method(h1tx)));
88  if (htp_tx_request_uri(h1tx) != NULL) {
89  // A request line without spaces gets interpreted as a request_method
90  // and has request_uri=NULL
91  rs_http2_tx_set_uri(h2s, bstr_ptr(htp_tx_request_uri(h1tx)),
92  (uint32_t)bstr_len(htp_tx_request_uri(h1tx)));
93  }
94  size_t nbheaders = htp_table_size(htp_tx_request_headers(h1tx));
95  for (size_t i = 0; i < nbheaders; i++) {
96  htp_header_t *h = htp_table_get_index(htp_tx_request_headers(h1tx), i, NULL);
97  rs_http2_tx_add_header(h2s, bstr_ptr(h->name), (uint32_t)bstr_len(h->name),
98  bstr_ptr(h->value), (uint32_t)bstr_len(h->value));
99  }
100 }
AppLayerProtoDetectPMRegisterPatternCI
int AppLayerProtoDetectPMRegisterPatternCI(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
Registers a case-insensitive pattern for protocol detection.
Definition: app-layer-detect-proto.c:1691
htp_tx_request_uri
#define htp_tx_request_uri(tx)
Definition: app-layer-htp-libhtp.h:112
STREAMING_BUFFER_CONFIG_INITIALIZER
#define STREAMING_BUFFER_CONFIG_INITIALIZER
Definition: util-streaming-buffer.h:74
HtpGetTxForH2
void * HtpGetTxForH2(void *alstate)
Definition: app-layer-htp.c:2717
rust.h
app-layer-http2.h
htp_tx_request_method
#define htp_tx_request_method(tx)
Definition: app-layer-htp-libhtp.h:106
util-unittest.h
htp_tx_request_headers
#define htp_tx_request_headers(tx)
Definition: app-layer-htp-libhtp.h:113
app-layer-detect-proto.h
app-layer-htp.h
app-layer-parser.h
stream.h
conf.h
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1782
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
suricata-common.h
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
SuricataFileContext_
Definition: rust-context.h:66
app-layer-htp-libhtp.h
HTTP2MimicHttp1Request
void HTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
Definition: app-layer-http2.c:75
RegisterHTTP2Parsers
void RegisterHTTP2Parsers(void)
Definition: app-layer-http2.c:57
AppLayerProtoDetectConfProtoDetectionEnabledDefault
int AppLayerProtoDetectConfProtoDetectionEnabledDefault(const char *ipproto, const char *alproto, bool default_enabled)
Given a protocol name, checks if proto detection is enabled in the conf file.
Definition: app-layer-detect-proto.c:1904